1 // RUN: %clang_cc1 %s -triple x86_64-apple-darwin10 -emit-llvm -o - -mconstructor-aliases -fcxx-exceptions -fexceptions -O1 -disable-llvm-optzns > %t
2 // RUN: FileCheck --check-prefix=CHECK1 --input-file=%t %s
3 // RUN: FileCheck --check-prefix=CHECK2 --input-file=%t %s
4 // RUN: FileCheck --check-prefix=CHECK3 --input-file=%t %s
5 // RUN: FileCheck --check-prefix=CHECK4 --input-file=%t %s
6 // RUN: FileCheck --check-prefix=CHECK5 --input-file=%t %s
7 // RUN: %clang_cc1 %s -triple x86_64-apple-darwin10 -emit-llvm -o - -fcxx-exceptions -fexceptions -O1 -disable-llvm-optzns -std=c++11 > %t2
8 // RUN: FileCheck --check-prefix=CHECK6 --input-file=%t2 %s
9 // REQUIRES: asserts
10 
11 struct A {
12   int a;
13 
14   ~A();
15 };
16 
17 // Base with non-trivial destructor
18 struct B : A {
19   ~B();
20 };
21 
22 B::~B() { }
23 
24 // Field with non-trivial destructor
25 struct C {
26   A a;
27 
28   ~C();
29 };
30 
31 C::~C() { }
32 
33 namespace PR7526 {
34   extern void foo();
35   struct allocator {
36     ~allocator() throw();
37   };
38 
39   struct allocator_derived : allocator { };
40 
41   // CHECK1-LABEL: define void @_ZN6PR75263fooEv()
42   // CHECK1: call void {{.*}} @_ZN6PR75269allocatorD2Ev
43 
44   // CHECK1-LABEL: define void @_ZN6PR75269allocatorD2Ev(%"struct.PR7526::allocator"* %this) unnamed_addr
45   // CHECK1: call void @__cxa_call_unexpected
46   allocator::~allocator() throw() { foo(); }
47 
48   void foo() {
49     allocator_derived ad;
50   }
51 }
52 
53 // PR5084
54 template<typename T>
55 class A1 {
56   ~A1();
57 };
58 
59 template<> A1<char>::~A1();
60 
61 // PR5529
62 namespace PR5529 {
63   struct A {
64     ~A();
65   };
66 
67   A::~A() { }
68   struct B : A {
69     virtual ~B();
70   };
71 
72   B::~B()  {}
73 }
74 
75 // FIXME: there's a known problem in the codegen here where, if one
76 // destructor throws, the remaining destructors aren't run.  Fix it,
77 // then make this code check for it.
78 namespace test0 {
79   void foo();
80   struct VBase { ~VBase(); };
81   struct Base { ~Base(); };
82   struct Member { ~Member(); };
83 
84   struct A : Base {
85     Member M;
86     ~A();
87   };
88 
89   // The function-try-block won't suppress -mconstructor-aliases here.
90   A::~A() try { } catch (int i) {}
91 
92 // complete destructor alias tested above
93 
94 // CHECK2-LABEL: @_ZN5test01AD1Ev = alias {{.*}} @_ZN5test01AD2Ev
95 // CHECK2-LABEL: define void @_ZN5test01AD2Ev(%"struct.test0::A"* %this) unnamed_addr
96 // CHECK2: invoke void @_ZN5test06MemberD1Ev
97 // CHECK2:   unwind label [[MEM_UNWIND:%[a-zA-Z0-9.]+]]
98 // CHECK2: invoke void @_ZN5test04BaseD2Ev
99 // CHECK2:   unwind label [[BASE_UNWIND:%[a-zA-Z0-9.]+]]
100 
101   struct B : Base, virtual VBase {
102     Member M;
103     ~B();
104   };
105   B::~B() try { } catch (int i) {}
106   // It will suppress the delegation optimization here, though.
107 
108 // CHECK2-LABEL: define void @_ZN5test01BD2Ev(%"struct.test0::B"* %this, i8** %vtt) unnamed_addr
109 // CHECK2: invoke void @_ZN5test06MemberD1Ev
110 // CHECK2:   unwind label [[MEM_UNWIND:%[a-zA-Z0-9.]+]]
111 // CHECK2: invoke void @_ZN5test04BaseD2Ev
112 // CHECK2:   unwind label [[BASE_UNWIND:%[a-zA-Z0-9.]+]]
113 
114 // CHECK2-LABEL: define void @_ZN5test01BD1Ev(%"struct.test0::B"* %this) unnamed_addr
115 // CHECK2: invoke void @_ZN5test06MemberD1Ev
116 // CHECK2:   unwind label [[MEM_UNWIND:%[a-zA-Z0-9.]+]]
117 // CHECK2: invoke void @_ZN5test04BaseD2Ev
118 // CHECK2:   unwind label [[BASE_UNWIND:%[a-zA-Z0-9.]+]]
119 // CHECK2: invoke void @_ZN5test05VBaseD2Ev
120 // CHECK2:   unwind label [[VBASE_UNWIND:%[a-zA-Z0-9.]+]]
121 }
122 
123 // Test base-class aliasing.
124 namespace test1 {
125   struct A { ~A(); char ***m; }; // non-trivial destructor
126   struct B { ~B(); }; // non-trivial destructor
127   struct Empty { }; // trivial destructor, empty
128   struct NonEmpty { int x; }; // trivial destructor, non-empty
129 
130   // There must be a definition in this translation unit for the alias
131   // optimization to apply.
132   A::~A() { delete m; }
133 
134   struct M : A { ~M(); };
135   M::~M() {}
136   // CHECK3: @_ZN5test11MD2Ev = alias {{.*}} @_ZN5test11AD2Ev
137 
138   struct N : A, Empty { ~N(); };
139   N::~N() {}
140   // CHECK3: @_ZN5test11ND2Ev = alias {{.*}} @_ZN5test11AD2Ev
141 
142   struct O : Empty, A { ~O(); };
143   O::~O() {}
144   // CHECK3: @_ZN5test11OD2Ev = alias {{.*}} @_ZN5test11AD2Ev
145 
146   struct P : NonEmpty, A { ~P(); };
147   P::~P() {} // CHECK3-LABEL: define void @_ZN5test11PD2Ev(%"struct.test1::P"* %this) unnamed_addr
148 
149   struct Q : A, B { ~Q(); };
150   Q::~Q() {} // CHECK3-LABEL: define void @_ZN5test11QD2Ev(%"struct.test1::Q"* %this) unnamed_addr
151 
152   struct R : A { ~R(); };
153   R::~R() { A a; } // CHECK3-LABEL: define void @_ZN5test11RD2Ev(%"struct.test1::R"* %this) unnamed_addr
154 
155   struct S : A { ~S(); int x; };
156   S::~S() {}
157   // CHECK4: @_ZN5test11SD2Ev = alias {{.*}}, bitcast {{.*}} @_ZN5test11AD2Ev
158 
159   struct T : A { ~T(); B x; };
160   T::~T() {} // CHECK4-LABEL: define void @_ZN5test11TD2Ev(%"struct.test1::T"* %this) unnamed_addr
161 
162   // The VTT parameter prevents this.  We could still make this work
163   // for calling conventions that are safe against extra parameters.
164   struct U : A, virtual B { ~U(); };
165   U::~U() {} // CHECK4-LABEL: define void @_ZN5test11UD2Ev(%"struct.test1::U"* %this, i8** %vtt) unnamed_addr
166 }
167 
168 // PR6471
169 namespace test2 {
170   struct A { ~A(); char ***m; };
171   struct B : A { ~B(); };
172 
173   B::~B() {}
174   // CHECK4-LABEL: define void @_ZN5test21BD2Ev(%"struct.test2::B"* %this) unnamed_addr
175   // CHECK4: call void @_ZN5test21AD2Ev
176 }
177 
178 // PR7142
179 namespace test3 {
180   struct A { virtual ~A(); };
181   struct B { virtual ~B(); };
182   namespace { // internal linkage => deferred
183     struct C : A, B {}; // ~B() in D requires a this-adjustment thunk
184     struct D : C {};    // D::~D() is an alias to C::~C()
185   }
186 
187   void test() {
188     new D; // Force emission of D's vtable
189   }
190 
191   // CHECK4-LABEL: define internal void @_ZN5test312_GLOBAL__N_11CD2Ev(%"struct.test3::(anonymous namespace)::C"* %this) unnamed_addr
192   // CHECK4: invoke void @_ZN5test31BD2Ev(
193   // CHECK4: call void @_ZN5test31AD2Ev(
194   // CHECK4: ret void
195 
196   // CHECK4-LABEL: define internal void @_ZN5test312_GLOBAL__N_11DD0Ev(%"struct.test3::(anonymous namespace)::D"* %this) unnamed_addr
197   // CHECK4-SAME:  personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
198   // CHECK4: invoke void {{.*}} @_ZN5test312_GLOBAL__N_11CD2Ev
199   // CHECK4: call void @_ZdlPv({{.*}}) [[NUW:#[0-9]+]]
200   // CHECK4: ret void
201   // CHECK4: landingpad { i8*, i32 }
202   // CHECK4-NEXT: cleanup
203   // CHECK4: call void @_ZdlPv({{.*}}) [[NUW]]
204   // CHECK4: resume { i8*, i32 }
205 
206   // CHECK4-LABEL: define internal void @_ZThn8_N5test312_GLOBAL__N_11DD1Ev(
207   // CHECK4: getelementptr inbounds i8, i8* {{.*}}, i64 -8
208   // CHECK4: call void {{.*}} @_ZN5test312_GLOBAL__N_11CD2Ev
209   // CHECK4: ret void
210 
211   // CHECK4-LABEL: define internal void @_ZThn8_N5test312_GLOBAL__N_11DD0Ev(
212   // CHECK4: getelementptr inbounds i8, i8* {{.*}}, i64 -8
213   // CHECK4: call void @_ZN5test312_GLOBAL__N_11DD0Ev(
214   // CHECK4: ret void
215 
216   // CHECK4-LABEL: define internal void @_ZN5test312_GLOBAL__N_11CD0Ev(%"struct.test3::(anonymous namespace)::C"* %this) unnamed_addr
217   // CHECK4-SAME:  personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
218   // CHECK4: invoke void @_ZN5test312_GLOBAL__N_11CD2Ev(
219   // CHECK4: call void @_ZdlPv({{.*}}) [[NUW]]
220   // CHECK4: ret void
221   // CHECK4: landingpad { i8*, i32 }
222   // CHECK4-NEXT: cleanup
223   // CHECK4: call void @_ZdlPv({{.*}}) [[NUW]]
224   // CHECK4: resume { i8*, i32 }
225 
226   // CHECK4-LABEL: define internal void @_ZThn8_N5test312_GLOBAL__N_11CD1Ev(
227   // CHECK4: getelementptr inbounds i8, i8* {{.*}}, i64 -8
228   // CHECK4: call void @_ZN5test312_GLOBAL__N_11CD2Ev(
229   // CHECK4: ret void
230 
231   // CHECK4-LABEL: define internal void @_ZThn8_N5test312_GLOBAL__N_11CD0Ev(
232   // CHECK4: getelementptr inbounds i8, i8* {{.*}}, i64 -8
233   // CHECK4: call void @_ZN5test312_GLOBAL__N_11CD0Ev(
234   // CHECK4: ret void
235 
236   // CHECK4-LABEL: declare void @_ZN5test31BD2Ev(
237   // CHECK4-LABEL: declare void @_ZN5test31AD2Ev(
238 
239   // CHECK4: attributes [[NUW]] = {{[{].*}} nounwind {{.*[}]}}
240 }
241 
242 namespace test4 {
243   struct A { ~A(); };
244 
245   // CHECK5-LABEL: define void @_ZN5test43fooEv()
246   // CHECK5: call void @_ZN5test41AD1Ev
247   // CHECK5: ret void
248   void foo() {
249     {
250       A a;
251       goto failure;
252     }
253 
254   failure:
255     return;
256   }
257 
258   // CHECK5-LABEL: define void @_ZN5test43barEi(
259   // CHECK5:      [[X:%.*]] = alloca i32
260   // CHECK5-NEXT: [[A:%.*]] = alloca
261   // CHECK5:      br label
262   // CHECK5:      [[TMP:%.*]] = load i32, i32* [[X]]
263   // CHECK5-NEXT: [[CMP:%.*]] = icmp ne i32 [[TMP]], 0
264   // CHECK5-NEXT: br i1
265   // CHECK5:      call void @_ZN5test41AD1Ev(
266   // CHECK5:      br label
267   // CHECK5:      [[TMP:%.*]] = load i32, i32* [[X]]
268   // CHECK5:      [[TMP2:%.*]] = add nsw i32 [[TMP]], -1
269   // CHECK5:      store i32 [[TMP2]], i32* [[X]]
270   // CHECK5:      br label
271   // CHECK5:      ret void
272   void bar(int x) {
273     for (A a; x; ) {
274       x--;
275     }
276   }
277 }
278 
279 // PR7575
280 namespace test5 {
281   struct A { ~A(); };
282 
283   // CHECK5-LABEL: define void @_ZN5test53fooEv()
284   // CHECK5:      [[ELEMS:%.*]] = alloca [5 x [[A:%.*]]], align
285   // CHECK5-NEXT: [[EXN:%.*]] = alloca i8*
286   // CHECK5-NEXT: [[SEL:%.*]] = alloca i32
287   // CHECK5-NEXT: [[PELEMS:%.*]] = bitcast [5 x [[A]]]* [[ELEMS]] to i8*
288   // CHECK5-NEXT: call void @llvm.lifetime.start(i64 5, i8* [[PELEMS]])
289   // CHECK5-NEXT: [[BEGIN:%.*]] = getelementptr inbounds [5 x [[A]]], [5 x [[A]]]* [[ELEMS]], i32 0, i32 0
290   // CHECK5-NEXT: [[END:%.*]] = getelementptr inbounds [[A]], [[A]]* [[BEGIN]], i64 5
291   // CHECK5-NEXT: br label
292   // CHECK5:      [[POST:%.*]] = phi [[A]]* [ [[END]], {{%.*}} ], [ [[ELT:%.*]], {{%.*}} ]
293   // CHECK5-NEXT: [[ELT]] = getelementptr inbounds [[A]], [[A]]* [[POST]], i64 -1
294   // CHECK5-NEXT: invoke void @_ZN5test51AD1Ev([[A]]* [[ELT]])
295   // CHECK5:      [[T0:%.*]] = icmp eq [[A]]* [[ELT]], [[BEGIN]]
296   // CHECK5-NEXT: br i1 [[T0]],
297   // CHECK5:      call void @llvm.lifetime.end
298   // CHECK5-NEXT: ret void
299   // lpad
300   // CHECK5:      [[EMPTY:%.*]] = icmp eq [[A]]* [[BEGIN]], [[ELT]]
301   // CHECK5-NEXT: br i1 [[EMPTY]]
302   // CHECK5:      [[AFTER:%.*]] = phi [[A]]* [ [[ELT]], {{%.*}} ], [ [[CUR:%.*]], {{%.*}} ]
303   // CHECK5-NEXT: [[CUR:%.*]] = getelementptr inbounds [[A]], [[A]]* [[AFTER]], i64 -1
304   // CHECK5-NEXT: invoke void @_ZN5test51AD1Ev([[A]]* [[CUR]])
305   // CHECK5:      [[DONE:%.*]] = icmp eq [[A]]* [[CUR]], [[BEGIN]]
306   // CHECK5-NEXT: br i1 [[DONE]],
307   void foo() {
308     A elems[5];
309   }
310 }
311 
312 namespace test6 {
313   void opaque();
314 
315   struct A { ~A(); };
316   template <unsigned> struct B { B(); ~B(); int _; };
317   struct C : B<0>, B<1>, virtual B<2>, virtual B<3> {
318     A x, y, z;
319 
320     C();
321     ~C();
322   };
323 
324   C::C() { opaque(); }
325   // CHECK5-LABEL: define void @_ZN5test61CC1Ev(%"struct.test6::C"* %this) unnamed_addr
326   // CHECK5:   call void @_ZN5test61BILj2EEC2Ev
327   // CHECK5:   invoke void @_ZN5test61BILj3EEC2Ev
328   // CHECK5:   invoke void @_ZN5test61BILj0EEC2Ev
329   // CHECK5:   invoke void @_ZN5test61BILj1EEC2Ev
330   // CHECK5:   invoke void @_ZN5test66opaqueEv
331   // CHECK5:   ret void
332   // FIXME: way too much EH cleanup code follows
333 
334   C::~C() { opaque(); }
335   // CHECK5-LABEL: define void @_ZN5test61CD2Ev(%"struct.test6::C"* %this, i8** %vtt) unnamed_addr
336   // CHECK5:   invoke void @_ZN5test66opaqueEv
337   // CHECK5:   invoke void @_ZN5test61AD1Ev
338   // CHECK5:   invoke void @_ZN5test61AD1Ev
339   // CHECK5:   invoke void @_ZN5test61AD1Ev
340   // CHECK5:   invoke void @_ZN5test61BILj1EED2Ev
341   // CHECK5:   call void @_ZN5test61BILj0EED2Ev
342   // CHECK5:   ret void
343   // CHECK5:   invoke void @_ZN5test61AD1Ev
344   // CHECK5:   invoke void @_ZN5test61AD1Ev
345   // CHECK5:   invoke void @_ZN5test61AD1Ev
346   // CHECK5:   invoke void @_ZN5test61BILj1EED2Ev
347   // CHECK5:   invoke void @_ZN5test61BILj0EED2Ev
348 
349   // CHECK5-LABEL: define void @_ZN5test61CD1Ev(%"struct.test6::C"* %this) unnamed_addr
350   // CHECK5:   invoke void @_ZN5test61CD2Ev
351   // CHECK5:   invoke void @_ZN5test61BILj3EED2Ev
352   // CHECK5:   call void @_ZN5test61BILj2EED2Ev
353   // CHECK5:   ret void
354   // CHECK5:   invoke void @_ZN5test61BILj3EED2Ev
355   // CHECK5:   invoke void @_ZN5test61BILj2EED2Ev
356 }
357 
358 // PR 9197
359 namespace test7 {
360   struct D { ~D(); };
361 
362   struct A { ~A(); };
363   A::~A() { }
364 
365   struct B : public A {
366     ~B();
367     D arr[1];
368   };
369 
370   // Verify that this doesn't get emitted as an alias
371   // CHECK5-LABEL: define void @_ZN5test71BD2Ev(
372   // CHECK5:   invoke void @_ZN5test71DD1Ev(
373   // CHECK5:   call void @_ZN5test71AD2Ev(
374   B::~B() {}
375 }
376 
377 // PR10467
378 namespace test8 {
379   struct A { A(); ~A(); };
380 
381   void die() __attribute__((noreturn));
382   void test() {
383     A x;
384     while (1) {
385       A y;
386       goto l;
387     }
388   l: die();
389   }
390 
391   // CHECK5-LABEL:    define void @_ZN5test84testEv()
392   // CHECK5:      [[X:%.*]] = alloca [[A:%.*]], align 1
393   // CHECK5-NEXT: [[Y:%.*]] = alloca [[A:%.*]], align 1
394   // CHECK5:      call void @_ZN5test81AC1Ev([[A]]* [[X]])
395   // CHECK5-NEXT: br label
396   // CHECK5:      invoke void @_ZN5test81AC1Ev([[A]]* [[Y]])
397   // CHECK5:      invoke void @_ZN5test81AD1Ev([[A]]* [[Y]])
398   // CHECK5-NOT:  switch
399   // CHECK5:      invoke void @_ZN5test83dieEv()
400   // CHECK5:      unreachable
401 }
402 
403 // PR12710
404 namespace test9 {
405   struct ArgType {
406     ~ArgType();
407   };
408   template<typename T>
409   void f1(const ArgType& = ArgType());
410   void f2();
411   void bar() {
412     f1<int>();
413     f2();
414   }
415   // CHECK5: call void @_ZN5test97ArgTypeD1Ev(%"struct.test9::ArgType"* %
416   // CHECK5: call void @_ZN5test92f2Ev()
417 }
418 
419 namespace test10 {
420   // We used to crash trying to replace _ZN6test106OptionD1Ev with
421   // _ZN6test106OptionD2Ev twice.
422   struct Option {
423     virtual ~Option() {}
424   };
425   template <class DataType> class opt : public Option {};
426   template class opt<int>;
427   // CHECK5-LABEL: define zeroext i1 @_ZN6test1016handleOccurrenceEv(
428   bool handleOccurrence() {
429     // CHECK5: call void @_ZN6test106OptionD2Ev(
430     Option x;
431     return true;
432   }
433 }
434 
435 #if __cplusplus >= 201103L
436 namespace test11 {
437 
438 // Check that lifetime.end is emitted in the landing pad.
439 
440 // CHECK6-LABEL: define void @_ZN6test1115testLifetimeEndEi(
441 // CHECK6: entry:
442 // CHECK6: [[T1:%[a-z0-9]+]] = alloca %"struct.test11::S1"
443 // CHECK6: [[T2:%[a-z0-9]+]] = alloca %"struct.test11::S1"
444 // CHECK6: [[T3:%[a-z0-9]+]] = alloca %"struct.test11::S1"
445 
446 // CHECK6: {{^}}invoke.cont
447 // CHECK6: call void @_ZN6test112S1D1Ev(%"struct.test11::S1"* [[T1]])
448 // CHECK6: [[BC1:%[a-z0-9]+]] = bitcast %"struct.test11::S1"* [[T1]] to i8*
449 // CHECK6: call void @llvm.lifetime.end(i64 32, i8* [[BC1]])
450 // CHECK6: {{^}}lpad
451 // CHECK6: call void @_ZN6test112S1D1Ev(%"struct.test11::S1"* [[T1]])
452 // CHECK6: [[BC2:%[a-z0-9]+]] = bitcast %"struct.test11::S1"* [[T1]] to i8*
453 // CHECK6: call void @llvm.lifetime.end(i64 32, i8* [[BC2]])
454 
455 // CHECK6: {{^}}invoke.cont
456 // CHECK6: call void @_ZN6test112S1D1Ev(%"struct.test11::S1"* [[T2]])
457 // CHECK6: [[BC3:%[a-z0-9]+]] = bitcast %"struct.test11::S1"* [[T2]] to i8*
458 // CHECK6: call void @llvm.lifetime.end(i64 32, i8* [[BC3]])
459 // CHECK6: {{^}}lpad
460 // CHECK6: call void @_ZN6test112S1D1Ev(%"struct.test11::S1"* [[T2]])
461 // CHECK6: [[BC4:%[a-z0-9]+]] = bitcast %"struct.test11::S1"* [[T2]] to i8*
462 // CHECK6: call void @llvm.lifetime.end(i64 32, i8* [[BC4]])
463 
464 // CHECK6: {{^}}invoke.cont
465 // CHECK6: call void @_ZN6test112S1D1Ev(%"struct.test11::S1"* [[T3]])
466 // CHECK6: [[BC5:%[a-z0-9]+]] = bitcast %"struct.test11::S1"* [[T3]] to i8*
467 // CHECK6: call void @llvm.lifetime.end(i64 32, i8* [[BC5]])
468 // CHECK6: {{^}}lpad
469 // CHECK6: call void @_ZN6test112S1D1Ev(%"struct.test11::S1"* [[T3]])
470 // CHECK6: [[BC6:%[a-z0-9]+]] = bitcast %"struct.test11::S1"* [[T3]] to i8*
471 // CHECK6: call void @llvm.lifetime.end(i64 32, i8* [[BC6]])
472 
473   struct S1 {
474     ~S1();
475     int a[8];
476   };
477 
478   void func1(S1 &) noexcept(false);
479 
480   void testLifetimeEnd(int n) {
481     if (n < 10) {
482       S1 t1;
483       func1(t1);
484     } else if (n < 100) {
485       S1 t2;
486       func1(t2);
487     } else if (n < 1000) {
488       S1 t3;
489       func1(t3);
490     }
491   }
492 
493 }
494 #endif
495