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