1 // RUN: %clang_cc1 %s -triple x86_64-apple-darwin10 -emit-llvm -o - -mconstructor-aliases -fcxx-exceptions -fexceptions -O1 -disable-llvm-passes -std=c++03 > %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-prefixes=CHECK4,CHECK4v03 --input-file=%t %s
6 // RUN: FileCheck --check-prefixes=CHECK5,CHECK5v03 --input-file=%t %s
7 // RUN: %clang_cc1 %s -triple x86_64-apple-darwin10 -emit-llvm -o - -mconstructor-aliases -fcxx-exceptions -fexceptions -O1 -disable-llvm-passes -std=c++11 > %t2
8 // RUN: FileCheck --check-prefix=CHECK1 --input-file=%t2 %s
9 // RUN: FileCheck --check-prefix=CHECK2v11 --input-file=%t2 %s
10 // RUN: FileCheck --check-prefix=CHECK3 --input-file=%t2 %s
11 // RUN: FileCheck --check-prefixes=CHECK4,CHECK4v11 --input-file=%t2 %s
12 // RUN: FileCheck --check-prefixes=CHECK5,CHECK5v11 --input-file=%t2 %s
13 // RUN: FileCheck --check-prefix=CHECK6 --input-file=%t2 %s
22 // Base with non-trivial destructor
29 // Field with non-trivial destructor
44 struct allocator_derived
: allocator
{ };
46 // CHECK1-LABEL: define{{.*}} void @_ZN6PR75263fooEv()
47 // CHECK1: call void @_ZN6PR75269allocatorD2Ev
49 // CHECK1-LABEL: define{{.*}} void @_ZN6PR75269allocatorD2Ev(ptr {{[^,]*}} %this) unnamed_addr
50 // CHECK1: call void @__cxa_call_unexpected
51 allocator::~allocator() throw() { foo(); }
64 template<> A1
<char>::~A1();
80 // FIXME: there's a known problem in the codegen here where, if one
81 // destructor throws, the remaining destructors aren't run. Fix it,
82 // then make this code check for it.
85 struct VBase
{ ~VBase(); };
86 struct Base
{ ~Base(); };
87 struct Member
{ ~Member(); };
94 // The function-try-block won't suppress -mconstructor-aliases here.
95 A::~A() try { } catch (int i
) {}
97 // complete destructor alias tested above
99 // CHECK2-LABEL: @_ZN5test01AD1Ev ={{.*}} unnamed_addr alias {{.*}} @_ZN5test01AD2Ev
100 // CHECK2-LABEL: define{{.*}} void @_ZN5test01AD2Ev(ptr {{[^,]*}} %this) unnamed_addr
101 // CHECK2: invoke void @_ZN5test06MemberD1Ev
102 // CHECK2: unwind label [[MEM_UNWIND:%[a-zA-Z0-9.]+]]
103 // CHECK2: invoke void @_ZN5test04BaseD2Ev
104 // CHECK2: unwind label [[BASE_UNWIND:%[a-zA-Z0-9.]+]]
106 // In C++11, the destructors are often known not to throw.
107 // CHECK2v11-LABEL: @_ZN5test01AD1Ev ={{.*}} unnamed_addr alias {{.*}} @_ZN5test01AD2Ev
108 // CHECK2v11-LABEL: define{{.*}} void @_ZN5test01AD2Ev(ptr {{[^,]*}} %this) unnamed_addr
109 // CHECK2v11: call void @_ZN5test06MemberD1Ev
110 // CHECK2v11: call void @_ZN5test04BaseD2Ev
112 struct B
: Base
, virtual VBase
{
116 B::~B() try { } catch (int i
) {}
117 // It will suppress the delegation optimization here, though.
119 // CHECK2-LABEL: define{{.*}} void @_ZN5test01BD2Ev(ptr {{[^,]*}} %this, ptr noundef %vtt) unnamed_addr
120 // CHECK2: invoke void @_ZN5test06MemberD1Ev
121 // CHECK2: unwind label [[MEM_UNWIND:%[a-zA-Z0-9.]+]]
122 // CHECK2: invoke void @_ZN5test04BaseD2Ev
123 // CHECK2: unwind label [[BASE_UNWIND:%[a-zA-Z0-9.]+]]
125 // CHECK2v11-LABEL: define{{.*}} void @_ZN5test01BD2Ev(ptr {{[^,]*}} %this, ptr noundef %vtt) unnamed_addr
126 // CHECK2v11: call void @_ZN5test06MemberD1Ev
127 // CHECK2v11: call void @_ZN5test04BaseD2Ev
129 // CHECK2-LABEL: define{{.*}} void @_ZN5test01BD1Ev(ptr {{[^,]*}} %this) unnamed_addr
130 // CHECK2: invoke void @_ZN5test06MemberD1Ev
131 // CHECK2: unwind label [[MEM_UNWIND:%[a-zA-Z0-9.]+]]
132 // CHECK2: invoke void @_ZN5test04BaseD2Ev
133 // CHECK2: unwind label [[BASE_UNWIND:%[a-zA-Z0-9.]+]]
134 // CHECK2: invoke void @_ZN5test05VBaseD2Ev
135 // CHECK2: unwind label [[VBASE_UNWIND:%[a-zA-Z0-9.]+]]
137 // CHECK2v11-LABEL: define{{.*}} void @_ZN5test01BD1Ev(ptr {{[^,]*}} %this) unnamed_addr
138 // CHECK2v11: call void @_ZN5test06MemberD1Ev
139 // CHECK2v11: call void @_ZN5test04BaseD2Ev
140 // CHECK2v11: call void @_ZN5test05VBaseD2Ev
143 // Test base-class aliasing.
145 struct A
{ ~A(); char ***m
; }; // non-trivial destructor
146 struct B
{ ~B(); }; // non-trivial destructor
147 struct Empty
{ }; // trivial destructor, empty
148 struct NonEmpty
{ int x
; }; // trivial destructor, non-empty
150 // There must be a definition in this translation unit for the alias
151 // optimization to apply.
152 A::~A() { delete m
; }
154 struct M
: A
{ ~M(); };
156 // CHECK3: @_ZN5test11MD2Ev ={{.*}} unnamed_addr alias {{.*}} @_ZN5test11AD2Ev
158 struct N
: A
, Empty
{ ~N(); };
160 // CHECK3: @_ZN5test11ND2Ev ={{.*}} unnamed_addr alias {{.*}} @_ZN5test11AD2Ev
162 struct O
: Empty
, A
{ ~O(); };
164 // CHECK3: @_ZN5test11OD2Ev ={{.*}} unnamed_addr alias {{.*}} @_ZN5test11AD2Ev
166 struct P
: NonEmpty
, A
{ ~P(); };
167 P::~P() {} // CHECK3-LABEL: define{{.*}} void @_ZN5test11PD2Ev(ptr {{[^,]*}} %this) unnamed_addr
169 struct Q
: A
, B
{ ~Q(); };
170 Q::~Q() {} // CHECK3-LABEL: define{{.*}} void @_ZN5test11QD2Ev(ptr {{[^,]*}} %this) unnamed_addr
172 struct R
: A
{ ~R(); };
173 R::~R() { A a
; } // CHECK3-LABEL: define{{.*}} void @_ZN5test11RD2Ev(ptr {{[^,]*}} %this) unnamed_addr
175 struct S
: A
{ ~S(); int x
; };
177 // CHECK4: @_ZN5test11SD2Ev ={{.*}} unnamed_addr alias {{.*}}, ptr @_ZN5test11AD2Ev
179 struct T
: A
{ ~T(); B x
; };
180 T::~T() {} // CHECK4-LABEL: define{{.*}} void @_ZN5test11TD2Ev(ptr {{[^,]*}} %this) unnamed_addr
182 // The VTT parameter prevents this. We could still make this work
183 // for calling conventions that are safe against extra parameters.
184 struct U
: A
, virtual B
{ ~U(); };
185 U::~U() {} // CHECK4-LABEL: define{{.*}} void @_ZN5test11UD2Ev(ptr {{[^,]*}} %this, ptr noundef %vtt) unnamed_addr
190 struct A
{ ~A(); char ***m
; };
191 struct B
: A
{ ~B(); };
194 // CHECK4-LABEL: define{{.*}} void @_ZN5test21BD2Ev(ptr {{[^,]*}} %this) unnamed_addr
195 // CHECK4: call void @_ZN5test21AD2Ev
200 struct A
{ virtual ~A(); };
201 struct B
{ virtual ~B(); };
202 namespace { // internal linkage => deferred
203 struct C
: A
, B
{}; // ~B() in D requires a this-adjustment thunk
204 struct D
: C
{}; // D::~D() is an alias to C::~C()
208 new D
; // Force emission of D's vtable
211 // CHECK4-LABEL: define internal void @_ZN5test312_GLOBAL__N_11CD2Ev(ptr {{[^,]*}} %this) unnamed_addr
212 // CHECK4v03: invoke void @_ZN5test31BD2Ev(
213 // CHECK4v11: call void @_ZN5test31BD2Ev(
214 // CHECK4: call void @_ZN5test31AD2Ev(
217 // CHECK4-LABEL: define internal void @_ZN5test312_GLOBAL__N_11DD0Ev(ptr {{[^,]*}} %this) unnamed_addr
218 // CHECK4v03-SAME: personality ptr @__gxx_personality_v0
219 // CHECK4v03: invoke void @_ZN5test312_GLOBAL__N_11CD2Ev
220 // CHECK4v11: call void @_ZN5test312_GLOBAL__N_11CD2Ev
221 // CHECK4: call void @_ZdlPv({{.*}}) [[NUW:#[0-9]+]]
223 // CHECK4v03: landingpad { ptr, i32 }
224 // CHECK4v03-NEXT: cleanup
225 // CHECK4v03: call void @_ZdlPv({{.*}}) [[NUW]]
226 // CHECK4v03: resume { ptr, i32 }
227 // CHECK4v11-NOT: landingpad
229 // CHECK4-LABEL: define internal void @_ZThn8_N5test312_GLOBAL__N_11DD1Ev(
230 // CHECK4: getelementptr inbounds i8, ptr {{.*}}, i64 -8
231 // CHECK4: call void @_ZN5test312_GLOBAL__N_11CD2Ev
234 // CHECK4-LABEL: define internal void @_ZThn8_N5test312_GLOBAL__N_11DD0Ev(
235 // CHECK4: getelementptr inbounds i8, ptr {{.*}}, i64 -8
236 // CHECK4: call void @_ZN5test312_GLOBAL__N_11DD0Ev(
239 // CHECK4-LABEL: define internal void @_ZN5test312_GLOBAL__N_11CD0Ev(ptr {{[^,]*}} %this) unnamed_addr
240 // CHECK4v03-SAME: personality ptr @__gxx_personality_v0
241 // CHECK4v03: invoke void @_ZN5test312_GLOBAL__N_11CD2Ev(
242 // CHECK4v11: call void @_ZN5test312_GLOBAL__N_11CD2Ev(
243 // CHECK4: call void @_ZdlPv({{.*}}) [[NUW]]
245 // CHECK4v03: landingpad { ptr, i32 }
246 // CHECK4v03-NEXT: cleanup
247 // CHECK4v03: call void @_ZdlPv({{.*}}) [[NUW]]
248 // CHECK4v03: resume { ptr, i32 }
250 // CHECK4-LABEL: define internal void @_ZThn8_N5test312_GLOBAL__N_11CD1Ev(
251 // CHECK4: getelementptr inbounds i8, ptr {{.*}}, i64 -8
252 // CHECK4: call void @_ZN5test312_GLOBAL__N_11CD2Ev(
255 // CHECK4-LABEL: define internal void @_ZThn8_N5test312_GLOBAL__N_11CD0Ev(
256 // CHECK4: getelementptr inbounds i8, ptr {{.*}}, i64 -8
257 // CHECK4: call void @_ZN5test312_GLOBAL__N_11CD0Ev(
260 // CHECK4-LABEL: declare void @_ZN5test31BD2Ev(
261 // CHECK4-LABEL: declare void @_ZN5test31AD2Ev(
263 // CHECK4: attributes [[NUW]] = {{[{].*}} nounwind {{.*[}]}}
269 // CHECK5-LABEL: define{{.*}} void @_ZN5test43fooEv()
270 // CHECK5: call void @_ZN5test41AD1Ev
282 // CHECK5-LABEL: define{{.*}} void @_ZN5test43barEi(
283 // CHECK5: [[X:%.*]] = alloca i32
284 // CHECK5-NEXT: [[A:%.*]] = alloca
286 // CHECK5: [[TMP:%.*]] = load i32, ptr [[X]]
287 // CHECK5-NEXT: [[CMP:%.*]] = icmp ne i32 [[TMP]], 0
288 // CHECK5-NEXT: br i1
289 // CHECK5: call void @_ZN5test41AD1Ev(
291 // CHECK5: [[TMP:%.*]] = load i32, ptr [[X]]
292 // CHECK5: [[TMP2:%.*]] = add nsw i32 [[TMP]], -1
293 // CHECK5: store i32 [[TMP2]], ptr [[X]]
307 // CHECK5-LABEL: define{{.*}} void @_ZN5test53fooEv()
308 // CHECK5: [[ELEMS:%.*]] = alloca [5 x [[A:%.*]]], align
309 // CHECK5v03-NEXT: [[EXN:%.*]] = alloca ptr
310 // CHECK5v03-NEXT: [[SEL:%.*]] = alloca i32
311 // CHECK5-NEXT: call void @llvm.lifetime.start.p0(i64 5, ptr [[ELEMS]])
312 // CHECK5-NEXT: [[BEGIN:%.*]] = getelementptr inbounds [5 x [[A]]], ptr [[ELEMS]], i32 0, i32 0
313 // CHECK5-NEXT: [[END:%.*]] = getelementptr inbounds [[A]], ptr [[BEGIN]], i64 5
314 // CHECK5-NEXT: br label
315 // CHECK5: [[POST:%.*]] = phi ptr [ [[END]], {{%.*}} ], [ [[ELT:%.*]], {{%.*}} ]
316 // CHECK5-NEXT: [[ELT]] = getelementptr inbounds [[A]], ptr [[POST]], i64 -1
317 // CHECK5v03-NEXT: invoke void @_ZN5test51AD1Ev(ptr {{[^,]*}} [[ELT]])
318 // CHECK5v11-NEXT: call void @_ZN5test51AD1Ev(ptr {{[^,]*}} [[ELT]])
319 // CHECK5: [[T0:%.*]] = icmp eq ptr [[ELT]], [[BEGIN]]
320 // CHECK5-NEXT: br i1 [[T0]],
321 // CHECK5: call void @llvm.lifetime.end
322 // CHECK5-NEXT: ret void
324 // CHECK5v03: [[EMPTY:%.*]] = icmp eq ptr [[BEGIN]], [[ELT]]
325 // CHECK5v03-NEXT: br i1 [[EMPTY]]
326 // CHECK5v03: [[AFTER:%.*]] = phi ptr [ [[ELT]], {{%.*}} ], [ [[CUR:%.*]], {{%.*}} ]
327 // CHECK5v03-NEXT: [[CUR:%.*]] = getelementptr inbounds [[A]], ptr [[AFTER]], i64 -1
328 // CHECK5v03-NEXT: invoke void @_ZN5test51AD1Ev(ptr {{[^,]*}} [[CUR]])
329 // CHECK5v03: [[DONE:%.*]] = icmp eq ptr [[CUR]], [[BEGIN]]
330 // CHECK5v03-NEXT: br i1 [[DONE]],
331 // CHECK5v11-NOT: landingpad
342 template <unsigned> struct B
{ B(); ~B(); int _
; };
343 struct C
: B
<0>, B
<1>, virtual B
<2>, virtual B
<3> {
351 // CHECK5-LABEL: define{{.*}} void @_ZN5test61CC1Ev(ptr {{[^,]*}} %this) unnamed_addr
352 // CHECK5: call void @_ZN5test61BILj2EEC2Ev
353 // CHECK5: invoke void @_ZN5test61BILj3EEC2Ev
354 // CHECK5: invoke void @_ZN5test61BILj0EEC2Ev
355 // CHECK5: invoke void @_ZN5test61BILj1EEC2Ev
356 // CHECK5: invoke void @_ZN5test66opaqueEv
358 // FIXME: way too much EH cleanup code follows
360 C::~C() { opaque(); }
361 // CHECK5-LABEL: define{{.*}} void @_ZN5test61CD2Ev(ptr {{[^,]*}} %this, ptr noundef %vtt) unnamed_addr
362 // CHECK5: invoke void @_ZN5test66opaqueEv
363 // CHECK5v03: invoke void @_ZN5test61AD1Ev
364 // CHECK5v03: invoke void @_ZN5test61AD1Ev
365 // CHECK5v03: invoke void @_ZN5test61AD1Ev
366 // CHECK5v03: invoke void @_ZN5test61BILj1EED2Ev
367 // CHECK5v11: call void @_ZN5test61AD1Ev
368 // CHECK5v11: call void @_ZN5test61AD1Ev
369 // CHECK5v11: call void @_ZN5test61AD1Ev
370 // CHECK5v11: call void @_ZN5test61BILj1EED2Ev
371 // CHECK5: call void @_ZN5test61BILj0EED2Ev
373 // CHECK5v03: invoke void @_ZN5test61AD1Ev
374 // CHECK5v03: invoke void @_ZN5test61AD1Ev
375 // CHECK5v03: invoke void @_ZN5test61AD1Ev
376 // CHECK5v03: invoke void @_ZN5test61BILj1EED2Ev
377 // CHECK5v03: invoke void @_ZN5test61BILj0EED2Ev
379 // CHECK5-LABEL: define{{.*}} void @_ZN5test61CD1Ev(ptr {{[^,]*}} %this) unnamed_addr
380 // CHECK5v03: invoke void @_ZN5test61CD2Ev
381 // CHECK5v03: invoke void @_ZN5test61BILj3EED2Ev
382 // CHECK5v03: call void @_ZN5test61BILj2EED2Ev
383 // CHECK5v03: ret void
384 // CHECK5v03: invoke void @_ZN5test61BILj3EED2Ev
385 // CHECK5v03: invoke void @_ZN5test61BILj2EED2Ev
387 // CHECK5v11: call void @_ZN5test61CD2Ev
388 // CHECK5v11: call void @_ZN5test61BILj3EED2Ev
389 // CHECK5v11: call void @_ZN5test61BILj2EED2Ev
390 // CHECK5v11: ret void
400 struct B
: public A
{
405 // Verify that this doesn't get emitted as an alias
406 // CHECK5-LABEL: define{{.*}} void @_ZN5test71BD2Ev(
407 // CHECK5v03: invoke void @_ZN5test71DD1Ev(
408 // CHECK5v11: call void @_ZN5test71DD1Ev(
409 // CHECK5: call void @_ZN5test71AD2Ev(
415 struct A
{ A(); ~A(); };
417 void die() __attribute__((noreturn
));
427 // CHECK5-LABEL: define{{.*}} void @_ZN5test84testEv()
428 // CHECK5: [[X:%.*]] = alloca [[A:%.*]], align 1
429 // CHECK5-NEXT: [[Y:%.*]] = alloca [[A:%.*]], align 1
430 // CHECK5: call void @_ZN5test81AC1Ev(ptr {{[^,]*}} [[X]])
431 // CHECK5-NEXT: br label
432 // CHECK5: invoke void @_ZN5test81AC1Ev(ptr {{[^,]*}} [[Y]])
433 // CHECK5v03: invoke void @_ZN5test81AD1Ev(ptr {{[^,]*}} [[Y]])
434 // CHECK5v11: call void @_ZN5test81AD1Ev(ptr {{[^,]*}} [[Y]])
435 // CHECK5-NOT: switch
436 // CHECK5: invoke void @_ZN5test83dieEv()
437 // CHECK5: unreachable
446 void f1(const ArgType
& = ArgType());
452 // CHECK5: call void @_ZN5test97ArgTypeD1Ev(ptr {{[^,]*}} %
453 // CHECK5: call void @_ZN5test92f2Ev()
457 // We used to crash trying to replace _ZN6test106OptionD1Ev with
458 // _ZN6test106OptionD2Ev twice.
462 template <class DataType
> class opt
: public Option
{};
463 template class opt
<int>;
464 // CHECK5-LABEL: define{{.*}} zeroext i1 @_ZN6test1016handleOccurrenceEv(
465 bool handleOccurrence() {
466 // CHECK5: call void @_ZN6test106OptionD2Ev(
472 #if __cplusplus >= 201103L
475 // Check that lifetime.end is emitted in the landing pad.
477 // CHECK6-LABEL: define{{.*}} void @_ZN6test1115testLifetimeEndEi(
479 // CHECK6: [[T1:%[a-z0-9]+]] = alloca %"struct.test11::S1"
480 // CHECK6: [[T2:%[a-z0-9]+]] = alloca %"struct.test11::S1"
481 // CHECK6: [[T3:%[a-z0-9]+]] = alloca %"struct.test11::S1"
483 // CHECK6: {{^}}invoke.cont
484 // CHECK6: call void @_ZN6test112S1D1Ev(ptr {{[^,]*}} [[T1]])
485 // CHECK6: call void @llvm.lifetime.end.p0(i64 32, ptr [[T1]])
487 // CHECK6: call void @_ZN6test112S1D1Ev(ptr {{[^,]*}} [[T1]])
488 // CHECK6: call void @llvm.lifetime.end.p0(i64 32, ptr [[T1]])
490 // CHECK6: {{^}}invoke.cont
491 // CHECK6: call void @_ZN6test112S1D1Ev(ptr {{[^,]*}} [[T2]])
492 // CHECK6: call void @llvm.lifetime.end.p0(i64 32, ptr [[T2]])
494 // CHECK6: call void @_ZN6test112S1D1Ev(ptr {{[^,]*}} [[T2]])
495 // CHECK6: call void @llvm.lifetime.end.p0(i64 32, ptr [[T2]])
497 // CHECK6: {{^}}invoke.cont
498 // CHECK6: call void @_ZN6test112S1D1Ev(ptr {{[^,]*}} [[T3]])
499 // CHECK6: call void @llvm.lifetime.end.p0(i64 32, ptr [[T3]])
501 // CHECK6: call void @_ZN6test112S1D1Ev(ptr {{[^,]*}} [[T3]])
502 // CHECK6: call void @llvm.lifetime.end.p0(i64 32, ptr [[T3]])
509 void func1(S1
&) noexcept(false);
511 void testLifetimeEnd(int n
) {
515 } else if (n
< 100) {
518 } else if (n
< 1000) {
526 namespace final_dtor
{
529 // CHECK6-LABEL: define {{.*}} @_ZN10final_dtor1AD2Ev(
530 // CHECK6: store {{.*}} @_ZTV
531 // CHECK6-LABEL: {{^}}}
532 virtual ~A() { f(); }
535 // CHECK6-LABEL: define {{.*}} @_ZN10final_dtor1BD2Ev(
536 // CHECK6: store {{.*}} @_ZTV
537 // CHECK6-LABEL: {{^}}}
538 virtual ~B() { f(); }
541 // CHECK6-LABEL: define {{.*}} @_ZN10final_dtor1CD2Ev(
542 // CHECK6-NOT: store {{.*}} @_ZTV
543 // CHECK6-LABEL: {{^}}}
544 virtual ~C() { f(); }
547 // CHECK6-LABEL: define {{.*}} @_ZN10final_dtor1DD2Ev(
548 // CHECK6-NOT: store {{.*}} @_ZTV
549 // CHECK6-LABEL: {{^}}}
550 virtual ~D() final
{ f(); }