1 // RUN: %clang_cc1 %s -triple=x86_64-linux-gnu -emit-llvm -std=c++98 -o - -fcxx-exceptions -fexceptions | FileCheck -check-prefix=CHECK -check-prefix=CHECK98 %s
2 // RUN: %clang_cc1 %s -triple=x86_64-linux-gnu -emit-llvm -std=c++11 -o - -fcxx-exceptions -fexceptions | FileCheck -check-prefix=CHECK -check-prefix=CHECK11 %s
3
4 // CHECK: %[[STRUCT_TEST13_A:.*]] = type { i32, i32 }
5
6 typedef __typeof(sizeof(0)) size_t;
7
8 // Declare the reserved global placement new.
9 void *operator new(size_t, void*);
10
11 // This just shouldn't crash.
12 namespace test0 {
13 struct allocator {
14 allocator();
15 allocator(const allocator&);
16 ~allocator();
17 };
18
19 void f();
g(bool b,bool c)20 void g(bool b, bool c) {
21 if (b) {
22 if (!c)
23 throw allocator();
24
25 return;
26 }
27 f();
28 }
29 }
30
31 namespace test1 {
32 struct A { A(int); A(int, int); ~A(); void *p; };
33
a()34 A *a() {
35 // CHECK: define [[A:%.*]]* @_ZN5test11aEv()
36 // CHECK: [[NEW:%.*]] = call noalias nonnull i8* @_Znwm(i64 8)
37 // CHECK-NEXT: [[CAST:%.*]] = bitcast i8* [[NEW]] to [[A]]*
38 // CHECK-NEXT: invoke void @_ZN5test11AC1Ei([[A]]* {{[^,]*}} [[CAST]], i32 5)
39 // CHECK: ret [[A]]* [[CAST]]
40 // CHECK: call void @_ZdlPv(i8* [[NEW]])
41 return new A(5);
42 }
43
b()44 A *b() {
45 // CHECK: define [[A:%.*]]* @_ZN5test11bEv()
46 // CHECK: [[NEW:%.*]] = call noalias nonnull i8* @_Znwm(i64 8)
47 // CHECK-NEXT: [[CAST:%.*]] = bitcast i8* [[NEW]] to [[A]]*
48 // CHECK-NEXT: [[FOO:%.*]] = invoke i32 @_ZN5test13fooEv()
49 // CHECK: invoke void @_ZN5test11AC1Ei([[A]]* {{[^,]*}} [[CAST]], i32 [[FOO]])
50 // CHECK: ret [[A]]* [[CAST]]
51 // CHECK: call void @_ZdlPv(i8* [[NEW]])
52 extern int foo();
53 return new A(foo());
54 }
55
56 struct B { B(); ~B(); operator int(); int x; };
57 B makeB();
58
c()59 A *c() {
60 // CHECK: define [[A:%.*]]* @_ZN5test11cEv()
61 // CHECK: [[ACTIVE:%.*]] = alloca i1
62 // CHECK-NEXT: [[NEW:%.*]] = call noalias nonnull i8* @_Znwm(i64 8)
63 // CHECK-NEXT: store i1 true, i1* [[ACTIVE]]
64 // CHECK-NEXT: [[CAST:%.*]] = bitcast i8* [[NEW]] to [[A]]*
65 // CHECK-NEXT: invoke void @_ZN5test11BC1Ev([[B:%.*]]* {{[^,]*}} [[T0:%.*]])
66 // CHECK: [[T1:%.*]] = getelementptr inbounds [[B]], [[B]]* [[T0]], i32 0, i32 0
67 // CHECK-NEXT: [[T2:%.*]] = load i32, i32* [[T1]], align 4
68 // CHECK-NEXT: invoke void @_ZN5test11AC1Ei([[A]]* {{[^,]*}} [[CAST]], i32 [[T2]])
69 // CHECK: store i1 false, i1* [[ACTIVE]]
70
71 // CHECK98-NEXT: invoke void @_ZN5test11BD1Ev([[B]]* {{[^,]*}} [[T0]])
72 // CHECK11-NEXT: call void @_ZN5test11BD1Ev([[B]]* {{[^,]*}} [[T0]])
73
74 // CHECK: ret [[A]]* [[CAST]]
75 // CHECK: [[ISACTIVE:%.*]] = load i1, i1* [[ACTIVE]]
76 // CHECK-NEXT: br i1 [[ISACTIVE]]
77 // CHECK: call void @_ZdlPv(i8* [[NEW]])
78 return new A(B().x);
79 }
80
81 // rdar://11904428
82 // Terminate landing pads should call __cxa_begin_catch first.
83 // CHECK98: define linkonce_odr hidden void @__clang_call_terminate(i8* %0) [[NI_NR_NUW:#[0-9]+]] comdat
84 // CHECK98-NEXT: [[T0:%.*]] = call i8* @__cxa_begin_catch(i8* %0) [[NUW:#[0-9]+]]
85 // CHECK98-NEXT: call void @_ZSt9terminatev() [[NR_NUW:#[0-9]+]]
86 // CHECK98-NEXT: unreachable
87
d()88 A *d() {
89 // CHECK: define [[A:%.*]]* @_ZN5test11dEv()
90 // CHECK: [[ACTIVE:%.*]] = alloca i1
91 // CHECK-NEXT: [[NEW:%.*]] = call noalias nonnull i8* @_Znwm(i64 8)
92 // CHECK-NEXT: store i1 true, i1* [[ACTIVE]]
93 // CHECK-NEXT: [[CAST:%.*]] = bitcast i8* [[NEW]] to [[A]]*
94 // CHECK-NEXT: invoke void @_ZN5test11BC1Ev([[B:%.*]]* {{[^,]*}} [[T0:%.*]])
95 // CHECK: [[T1:%.*]] = invoke i32 @_ZN5test11BcviEv([[B]]* {{[^,]*}} [[T0]])
96 // CHECK: invoke void @_ZN5test11AC1Ei([[A]]* {{[^,]*}} [[CAST]], i32 [[T1]])
97 // CHECK: store i1 false, i1* [[ACTIVE]]
98
99 // CHECK98-NEXT: invoke void @_ZN5test11BD1Ev([[B]]* {{[^,]*}} [[T0]])
100 // CHECK11-NEXT: call void @_ZN5test11BD1Ev([[B]]* {{[^,]*}} [[T0]])
101
102 // CHECK: ret [[A]]* [[CAST]]
103 // CHECK: [[ISACTIVE:%.*]] = load i1, i1* [[ACTIVE]]
104 // CHECK-NEXT: br i1 [[ISACTIVE]]
105 // CHECK: call void @_ZdlPv(i8* [[NEW]])
106 return new A(B());
107 }
108
e()109 A *e() {
110 // CHECK: define [[A:%.*]]* @_ZN5test11eEv()
111 // CHECK: [[ACTIVE:%.*]] = alloca i1
112 // CHECK-NEXT: [[NEW:%.*]] = call noalias nonnull i8* @_Znwm(i64 8)
113 // CHECK-NEXT: store i1 true, i1* [[ACTIVE]]
114 // CHECK-NEXT: [[CAST:%.*]] = bitcast i8* [[NEW]] to [[A]]*
115 // CHECK-NEXT: invoke void @_ZN5test11BC1Ev([[B:%.*]]* {{[^,]*}} [[T0:%.*]])
116 // CHECK: [[T1:%.*]] = invoke i32 @_ZN5test11BcviEv([[B]]* {{[^,]*}} [[T0]])
117 // CHECK: invoke void @_ZN5test11BC1Ev([[B]]* {{[^,]*}} [[T2:%.*]])
118 // CHECK: [[T3:%.*]] = invoke i32 @_ZN5test11BcviEv([[B]]* {{[^,]*}} [[T2]])
119 // CHECK: invoke void @_ZN5test11AC1Eii([[A]]* {{[^,]*}} [[CAST]], i32 [[T1]], i32 [[T3]])
120 // CHECK: store i1 false, i1* [[ACTIVE]]
121
122 // CHECK98-NEXT: invoke void @_ZN5test11BD1Ev([[B]]* {{[^,]*}} [[T2]])
123 // CHECK11-NEXT: call void @_ZN5test11BD1Ev([[B]]* {{[^,]*}} [[T2]])
124
125 // CHECK98: invoke void @_ZN5test11BD1Ev([[B]]* {{[^,]*}} [[T0]])
126 // CHECK11: call void @_ZN5test11BD1Ev([[B]]* {{[^,]*}} [[T0]])
127
128 // CHECK: ret [[A]]* [[CAST]]
129 // CHECK: [[ISACTIVE:%.*]] = load i1, i1* [[ACTIVE]]
130 // CHECK-NEXT: br i1 [[ISACTIVE]]
131 // CHECK: call void @_ZdlPv(i8* [[NEW]])
132 return new A(B(), B());
133 }
f()134 A *f() {
135 return new A(makeB().x);
136 }
g()137 A *g() {
138 return new A(makeB());
139 }
h()140 A *h() {
141 return new A(makeB(), makeB());
142 }
143
i()144 A *i() {
145 // CHECK: define [[A:%.*]]* @_ZN5test11iEv()
146 // CHECK: [[X:%.*]] = alloca [[A]]*, align 8
147 // CHECK: [[ACTIVE:%.*]] = alloca i1
148 // CHECK: [[NEW:%.*]] = call noalias nonnull i8* @_Znwm(i64 8)
149 // CHECK-NEXT: store i1 true, i1* [[ACTIVE]]
150 // CHECK-NEXT: [[CAST:%.*]] = bitcast i8* [[NEW]] to [[A]]*
151 // CHECK-NEXT: invoke void @_ZN5test15makeBEv([[B:%.*]]* sret([[B]]) align 4 [[T0:%.*]])
152 // CHECK: [[T1:%.*]] = invoke i32 @_ZN5test11BcviEv([[B]]* {{[^,]*}} [[T0]])
153 // CHECK: invoke void @_ZN5test11AC1Ei([[A]]* {{[^,]*}} [[CAST]], i32 [[T1]])
154 // CHECK: store i1 false, i1* [[ACTIVE]]
155 // CHECK-NEXT: store [[A]]* [[CAST]], [[A]]** [[X]], align 8
156 // CHECK: invoke void @_ZN5test15makeBEv([[B:%.*]]* sret([[B]]) align 4 [[T2:%.*]])
157 // CHECK: [[RET:%.*]] = load [[A]]*, [[A]]** [[X]], align 8
158
159 // CHECK98: invoke void @_ZN5test11BD1Ev([[B]]* {{[^,]*}} [[T2]])
160 // CHECK11: call void @_ZN5test11BD1Ev([[B]]* {{[^,]*}} [[T2]])
161
162 // CHECK98: invoke void @_ZN5test11BD1Ev([[B]]* {{[^,]*}} [[T0]])
163 // CHECK11: call void @_ZN5test11BD1Ev([[B]]* {{[^,]*}} [[T0]])
164
165 // CHECK: ret [[A]]* [[RET]]
166 // CHECK: [[ISACTIVE:%.*]] = load i1, i1* [[ACTIVE]]
167 // CHECK-NEXT: br i1 [[ISACTIVE]]
168 // CHECK: call void @_ZdlPv(i8* [[NEW]])
169 A *x;
170 return (x = new A(makeB()), makeB(), x);
171 }
172 }
173
174 namespace test2 {
175 struct A {
176 A(int); A(int, int); ~A();
177 void *p;
178 void *operator new(size_t);
179 void operator delete(void*, size_t);
180 };
181
a()182 A *a() {
183 // CHECK: define [[A:%.*]]* @_ZN5test21aEv()
184 // CHECK: [[NEW:%.*]] = call i8* @_ZN5test21AnwEm(i64 8)
185 // CHECK-NEXT: [[CAST:%.*]] = bitcast i8* [[NEW]] to [[A]]*
186 // CHECK-NEXT: invoke void @_ZN5test21AC1Ei([[A]]* {{[^,]*}} [[CAST]], i32 5)
187 // CHECK: ret [[A]]* [[CAST]]
188
189 // CHECK98: invoke void @_ZN5test21AdlEPvm(i8* [[NEW]], i64 8)
190 // CHECK11: call void @_ZN5test21AdlEPvm(i8* [[NEW]], i64 8)
191
192 // CHECK98: call void @__clang_call_terminate(i8* {{%.*}}) [[NR_NUW]]
193 return new A(5);
194 }
195 }
196
197 namespace test3 {
198 struct A {
199 A(int); A(int, int); A(const A&); ~A();
200 void *p;
201 void *operator new(size_t, void*, double);
202 void operator delete(void*, void*, double);
203 };
204
205 void *foo();
206 double bar();
207 A makeA(), *makeAPtr();
208
a()209 A *a() {
210 // CHECK: define [[A:%.*]]* @_ZN5test31aEv()
211 // CHECK: [[FOO:%.*]] = call i8* @_ZN5test33fooEv()
212 // CHECK: [[BAR:%.*]] = call double @_ZN5test33barEv()
213 // CHECK: [[NEW:%.*]] = call i8* @_ZN5test31AnwEmPvd(i64 8, i8* [[FOO]], double [[BAR]])
214 // CHECK-NEXT: [[CAST:%.*]] = bitcast i8* [[NEW]] to [[A]]*
215 // CHECK-NEXT: invoke void @_ZN5test31AC1Ei([[A]]* {{[^,]*}} [[CAST]], i32 5)
216 // CHECK: ret [[A]]* [[CAST]]
217
218 // CHECK98: invoke void @_ZN5test31AdlEPvS1_d(i8* [[NEW]], i8* [[FOO]], double [[BAR]])
219 // CHECK11: call void @_ZN5test31AdlEPvS1_d(i8* [[NEW]], i8* [[FOO]], double [[BAR]])
220
221 // CHECK98: call void @__clang_call_terminate(i8* {{%.*}}) [[NR_NUW]]
222 return new(foo(),bar()) A(5);
223 }
224
225 // rdar://problem/8439196
b(bool cond)226 A *b(bool cond) {
227
228 // CHECK: define [[A:%.*]]* @_ZN5test31bEb(i1 zeroext
229 // CHECK: [[SAVED0:%.*]] = alloca i8*
230 // CHECK-NEXT: [[SAVED1:%.*]] = alloca i8*
231 // CHECK-NEXT: [[CLEANUPACTIVE:%.*]] = alloca i1
232
233 // CHECK: [[COND:%.*]] = trunc i8 {{.*}} to i1
234 // CHECK-NEXT: store i1 false, i1* [[CLEANUPACTIVE]]
235 // CHECK-NEXT: br i1 [[COND]]
236 return (cond ?
237
238 // CHECK: [[FOO:%.*]] = call i8* @_ZN5test33fooEv()
239 // CHECK-NEXT: [[NEW:%.*]] = call i8* @_ZN5test31AnwEmPvd(i64 8, i8* [[FOO]], double [[CONST:.*]])
240 // CHECK-NEXT: store i8* [[NEW]], i8** [[SAVED0]]
241 // CHECK-NEXT: store i8* [[FOO]], i8** [[SAVED1]]
242 // CHECK-NEXT: store i1 true, i1* [[CLEANUPACTIVE]]
243 // CHECK-NEXT: [[CAST:%.*]] = bitcast i8* [[NEW]] to [[A]]*
244 // CHECK-NEXT: invoke void @_ZN5test35makeAEv([[A]]* sret([[A]]) align 8 [[CAST]])
245 // CHECK: br label
246 // -> cond.end
247 new(foo(),10.0) A(makeA()) :
248
249 // CHECK: [[MAKE:%.*]] = call [[A]]* @_ZN5test38makeAPtrEv()
250 // CHECK: br label
251 // -> cond.end
252 makeAPtr());
253
254 // cond.end:
255 // CHECK: [[RESULT:%.*]] = phi [[A]]* {{.*}}[[CAST]]{{.*}}[[MAKE]]
256 // CHECK: ret [[A]]* [[RESULT]]
257
258 // in the EH path:
259 // CHECK: [[ISACTIVE:%.*]] = load i1, i1* [[CLEANUPACTIVE]]
260 // CHECK-NEXT: br i1 [[ISACTIVE]]
261 // CHECK: [[V0:%.*]] = load i8*, i8** [[SAVED0]]
262 // CHECK-NEXT: [[V1:%.*]] = load i8*, i8** [[SAVED1]]
263
264 // CHECK98-NEXT: invoke void @_ZN5test31AdlEPvS1_d(i8* [[V0]], i8* [[V1]], double [[CONST]])
265 // CHECK11-NEXT: call void @_ZN5test31AdlEPvS1_d(i8* [[V0]], i8* [[V1]], double [[CONST]])
266 }
267 }
268
269 namespace test4 {
270 struct A {
271 A(int); A(int, int); ~A();
272 void *p;
273 void *operator new(size_t, void*, void*);
274 void operator delete(void*, size_t, void*, void*); // not a match
275 };
276
a()277 A *a() {
278 // CHECK: define [[A:%.*]]* @_ZN5test41aEv()
279 // CHECK: [[FOO:%.*]] = call i8* @_ZN5test43fooEv()
280 // CHECK-NEXT: [[BAR:%.*]] = call i8* @_ZN5test43barEv()
281 // CHECK-NEXT: [[NEW:%.*]] = call i8* @_ZN5test41AnwEmPvS1_(i64 8, i8* [[FOO]], i8* [[BAR]])
282 // CHECK-NEXT: [[CAST:%.*]] = bitcast i8* [[NEW]] to [[A]]*
283 // CHECK-NEXT: call void @_ZN5test41AC1Ei([[A]]* {{[^,]*}} [[CAST]], i32 5)
284 // CHECK-NEXT: ret [[A]]* [[CAST]]
285 extern void *foo(), *bar();
286
287 return new(foo(),bar()) A(5);
288 }
289 }
290
291 // PR7908
292 namespace test5 {
293 struct T { T(); ~T(); };
294
295 struct A {
296 A(const A &x, const T &t = T());
297 ~A();
298 };
299
300 void foo();
301
302 // CHECK-LABEL: define void @_ZN5test54testEv()
303 // CHECK: [[EXNSLOT:%.*]] = alloca i8*
304 // CHECK-NEXT: [[SELECTORSLOT:%.*]] = alloca i32
305 // CHECK-NEXT: [[A:%.*]] = alloca [[A_T:%.*]], align 1
306 // CHECK-NEXT: [[T:%.*]] = alloca [[T_T:%.*]], align 1
307 // CHECK-NEXT: invoke void @_ZN5test53fooEv()
308 // CHECK: [[EXN:%.*]] = load i8*, i8** [[EXNSLOT]]
309 // CHECK-NEXT: [[ADJ:%.*]] = call i8* @__cxa_get_exception_ptr(i8* [[EXN]])
310 // CHECK-NEXT: [[SRC:%.*]] = bitcast i8* [[ADJ]] to [[A_T]]*
311 // CHECK-NEXT: invoke void @_ZN5test51TC1Ev([[T_T]]* {{[^,]*}} [[T]])
312 // CHECK: invoke void @_ZN5test51AC1ERKS0_RKNS_1TE([[A_T]]* {{[^,]*}} [[A]], [[A_T]]* nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) [[SRC]], [[T_T]]* nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) [[T]])
313
314 // CHECK98: invoke void @_ZN5test51TD1Ev([[T_T]]* {{[^,]*}} [[T]])
315 // CHECK11: call void @_ZN5test51TD1Ev([[T_T]]* {{[^,]*}} [[T]])
316
317 // CHECK98: call i8* @__cxa_begin_catch(i8* [[EXN]]) [[NUW]]
318 // CHECK98-NEXT: invoke void @_ZN5test51AD1Ev([[A_T]]* {{[^,]*}} [[A]])
319
320 // CHECK: call void @__cxa_end_catch()
test()321 void test() {
322 try {
323 foo();
324 } catch (A a) {
325 }
326 }
327 }
328
329 // PR9303: invalid assert on this
330 namespace test6 {
331 bool cond();
test()332 void test() {
333 try {
334 lbl:
335 if (cond()) goto lbl;
336 } catch (...) {
337 }
338 }
339 }
340
341 // PR9298
342 namespace test7 {
343 struct A { A(); ~A(); };
344 struct B {
345 // The throw() operator means that a bad allocation is signalled
346 // with a null return, which means that the initializer is
347 // evaluated conditionally.
348 static void *operator new(size_t size) throw();
349 B(const A&, B*);
350 ~B();
351 };
352
test()353 B *test() {
354 // CHECK: define [[B:%.*]]* @_ZN5test74testEv()
355 // CHECK: [[OUTER_NEW:%.*]] = alloca i1
356 // CHECK-NEXT: alloca [[A:%.*]],
357 // CHECK-NEXT: alloca i8*
358 // CHECK-NEXT: alloca i32
359 // CHECK-NEXT: [[OUTER_A:%.*]] = alloca i1
360 // CHECK-NEXT: alloca i8*
361 // CHECK-NEXT: [[INNER_NEW:%.*]] = alloca i1
362 // CHECK-NEXT: alloca [[A]]
363 // CHECK-NEXT: [[INNER_A:%.*]] = alloca i1
364
365 // Allocate the outer object.
366 // CHECK-NEXT: [[NEW:%.*]] = call i8* @_ZN5test71BnwEm(
367 // CHECK-NEXT: icmp eq i8* [[NEW]], null
368
369 // These stores, emitted before the outermost conditional branch,
370 // deactivate the temporary cleanups.
371 // CHECK-NEXT: store i1 false, i1* [[OUTER_NEW]]
372 // CHECK-NEXT: store i1 false, i1* [[OUTER_A]]
373 // CHECK-NEXT: store i1 false, i1* [[INNER_NEW]]
374 // CHECK-NEXT: store i1 false, i1* [[INNER_A]]
375 // CHECK-NEXT: br i1
376
377 // We passed the first null check; activate that cleanup and continue.
378 // CHECK: store i1 true, i1* [[OUTER_NEW]]
379 // CHECK-NEXT: bitcast
380
381 // Create the first A temporary and activate that cleanup.
382 // CHECK-NEXT: invoke void @_ZN5test71AC1Ev(
383 // CHECK: store i1 true, i1* [[OUTER_A]]
384
385 // Allocate the inner object.
386 // CHECK-NEXT: [[NEW:%.*]] = call i8* @_ZN5test71BnwEm(
387 // CHECK-NEXT: icmp eq i8* [[NEW]], null
388 // CHECK-NEXT: br i1
389
390 // We passed the second null check; save that pointer, activate
391 // that cleanup, and continue.
392 // CHECK: store i8* [[NEW]]
393 // CHECK-NEXT: store i1 true, i1* [[INNER_NEW]]
394 // CHECK-NEXT: bitcast
395
396 // Build the second A temporary and activate that cleanup.
397 // CHECK-NEXT: invoke void @_ZN5test71AC1Ev(
398 // CHECK: store i1 true, i1* [[INNER_A]]
399
400 // Build the inner B object and deactivate the inner delete cleanup.
401 // CHECK-NEXT: invoke void @_ZN5test71BC1ERKNS_1AEPS0_(
402 // CHECK: store i1 false, i1* [[INNER_NEW]]
403 // CHECK: phi
404
405 // Build the outer B object and deactivate the outer delete cleanup.
406 // CHECK-NEXT: invoke void @_ZN5test71BC1ERKNS_1AEPS0_(
407 // CHECK: store i1 false, i1* [[OUTER_NEW]]
408 // CHECK: phi
409 // CHECK-NEXT: store [[B]]*
410
411 // Destroy the inner A object.
412 // CHECK-NEXT: load i1, i1* [[INNER_A]]
413 // CHECK-NEXT: br i1
414
415 // CHECK98: invoke void @_ZN5test71AD1Ev(
416 // CHECK11: call void @_ZN5test71AD1Ev(
417
418 // Destroy the outer A object.
419 // CHECK: load i1, i1* [[OUTER_A]]
420 // CHECK-NEXT: br i1
421
422 // CHECK98: invoke void @_ZN5test71AD1Ev(
423 // CHECK11: call void @_ZN5test71AD1Ev(
424
425 return new B(A(), new B(A(), 0));
426 }
427 }
428
429 // Just don't crash.
430 namespace test8 {
431 struct A {
432 // Having both of these is required to trigger the assert we're
433 // trying to avoid.
434 A(const A&);
435 A&operator=(const A&);
436
437 ~A();
438 };
439
440 A makeA();
test()441 void test() {
442 throw makeA();
443 }
444 // CHECK-LABEL: define void @_ZN5test84testEv
445 }
446
447 // Make sure we generate the correct code for the delete[] call which
448 // happens if A::A() throws. (We were previously calling delete[] on
449 // a pointer to the first array element, not the pointer returned by new[].)
450 // PR10870
451 namespace test9 {
452 struct A {
453 A();
454 ~A();
455 };
test()456 A* test() {
457 return new A[10];
458 }
459 // CHECK: define {{%.*}}* @_ZN5test94testEv
460 // CHECK: [[TEST9_NEW:%.*]] = call noalias nonnull i8* @_Znam
461 // CHECK: call void @_ZdaPv(i8* [[TEST9_NEW]])
462 }
463
464 // In a destructor with a function-try-block, a return statement in a
465 // catch handler behaves differently from running off the end of the
466 // catch handler. PR13102.
467 namespace test10 {
468 extern void cleanup();
469 extern bool suppress;
470
471 struct A { ~A(); };
~A()472 A::~A() try { cleanup(); } catch (...) { return; }
473 // CHECK-LABEL: define void @_ZN6test101AD1Ev(
474 // CHECK: invoke void @_ZN6test107cleanupEv()
475 // CHECK-NOT: rethrow
476 // CHECK: ret void
477
478 struct B { ~B(); };
~B()479 B::~B() try { cleanup(); } catch (...) {}
480 // CHECK-LABEL: define void @_ZN6test101BD1Ev(
481 // CHECK: invoke void @_ZN6test107cleanupEv()
482 // CHECK: call i8* @__cxa_begin_catch
483 // CHECK-NEXT: invoke void @__cxa_rethrow()
484 // CHECK: unreachable
485
486 struct C { ~C(); };
~C()487 C::~C() try { cleanup(); } catch (...) { if (suppress) return; }
488 // CHECK-LABEL: define void @_ZN6test101CD1Ev(
489 // CHECK: invoke void @_ZN6test107cleanupEv()
490 // CHECK: call i8* @__cxa_begin_catch
491 // CHECK-NEXT: load i8, i8* @_ZN6test108suppressE, align 1
492 // CHECK-NEXT: trunc
493 // CHECK-NEXT: br i1
494
495 // CHECK98: call void @__cxa_end_catch()
496 // CHECK98-NEXT: br label
497 // CHECK11: invoke void @__cxa_end_catch()
498 // CHECK11-NEXT: to label
499
500 // CHECK: invoke void @__cxa_rethrow()
501 // CHECK: unreachable
502 }
503
504 // Ensure that an exception in a constructor destroys
505 // already-constructed array members. PR14514
506 namespace test11 {
507 struct A {
508 A();
~Atest11::A509 ~A() {}
510 };
511
512 struct C {
513 A single;
514 A array[2][3];
515
516 C();
517 };
518
C()519 C::C() {
520 throw 0;
521 }
522 // CHECK-LABEL: define void @_ZN6test111CC2Ev(
523 // CHECK: [[THIS:%.*]] = load [[C:%.*]]*, [[C:%.*]]** {{%.*}}
524 // Construct single.
525 // CHECK-NEXT: [[SINGLE:%.*]] = getelementptr inbounds [[C]], [[C]]* [[THIS]], i32 0, i32 0
526 // CHECK-NEXT: call void @_ZN6test111AC1Ev([[A:%.*]]* {{[^,]*}} [[SINGLE]])
527 // Construct array.
528 // CHECK-NEXT: [[ARRAY:%.*]] = getelementptr inbounds [[C]], [[C]]* [[THIS]], i32 0, i32 1
529 // CHECK-NEXT: [[ARRAYBEGIN:%.*]] = getelementptr inbounds [2 x [3 x [[A]]]], [2 x [3 x [[A]]]]* [[ARRAY]], i32 0, i32 0, i32 0
530 // CHECK-NEXT: [[ARRAYEND:%.*]] = getelementptr inbounds [[A]], [[A]]* [[ARRAYBEGIN]], i64 6
531 // CHECK-NEXT: br label
532 // CHECK: [[CUR:%.*]] = phi [[A]]* [ [[ARRAYBEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
533 // CHECK-NEXT: invoke void @_ZN6test111AC1Ev([[A:%.*]]* {{[^,]*}} [[CUR]])
534 // CHECK: [[NEXT]] = getelementptr inbounds [[A]], [[A]]* [[CUR]], i64 1
535 // CHECK-NEXT: [[DONE:%.*]] = icmp eq [[A]]* [[NEXT]], [[ARRAYEND]]
536 // CHECK-NEXT: br i1 [[DONE]],
537 // throw 0;
538 // CHECK: invoke void @__cxa_throw(
539 // Landing pad 1, from constructor in array-initialization loop:
540 // CHECK: landingpad
541 // - First, destroy already-constructed bits of array.
542 // CHECK: [[EMPTY:%.*]] = icmp eq [[A]]* [[ARRAYBEGIN]], [[CUR]]
543 // CHECK-NEXT: br i1 [[EMPTY]]
544 // CHECK: [[AFTER:%.*]] = phi [[A]]* [ [[CUR]], {{%.*}} ], [ [[ELT:%.*]], {{%.*}} ]
545 // CHECK-NEXT: [[ELT]] = getelementptr inbounds [[A]], [[A]]* [[AFTER]], i64 -1
546
547 // CHECK98-NEXT: invoke void @_ZN6test111AD1Ev([[A]]* {{[^,]*}} [[ELT]])
548 // CHECK11-NEXT: call void @_ZN6test111AD1Ev([[A]]* {{[^,]*}} [[ELT]])
549
550 // CHECK: [[DONE:%.*]] = icmp eq [[A]]* [[ELT]], [[ARRAYBEGIN]]
551 // CHECK-NEXT: br i1 [[DONE]],
552 // - Next, chain to cleanup for single.
553 // CHECK: br label
554 // Landing pad 2, from throw site.
555 // CHECK: landingpad
556 // - First, destroy all of array.
557 // CHECK: [[ARRAYBEGIN:%.*]] = getelementptr inbounds [2 x [3 x [[A]]]], [2 x [3 x [[A]]]]* [[ARRAY]], i32 0, i32 0, i32 0
558 // CHECK-NEXT: [[ARRAYEND:%.*]] = getelementptr inbounds [[A]], [[A]]* [[ARRAYBEGIN]], i64 6
559 // CHECK-NEXT: br label
560 // CHECK: [[AFTER:%.*]] = phi [[A]]* [ [[ARRAYEND]], {{%.*}} ], [ [[ELT:%.*]], {{%.*}} ]
561 // CHECK-NEXT: [[ELT]] = getelementptr inbounds [[A]], [[A]]* [[AFTER]], i64 -1
562
563 // CHECK98-NEXT: invoke void @_ZN6test111AD1Ev([[A]]* {{[^,]*}} [[ELT]])
564 // CHECK11-NEXT: call void @_ZN6test111AD1Ev([[A]]* {{[^,]*}} [[ELT]])
565
566 // CHECK: [[DONE:%.*]] = icmp eq [[A]]* [[ELT]], [[ARRAYBEGIN]]
567 // CHECK-NEXT: br i1 [[DONE]],
568 // - Next, chain to cleanup for single.
569 // CHECK: br label
570 // Finally, the cleanup for single.
571
572 // CHECK98: invoke void @_ZN6test111AD1Ev([[A]]* {{[^,]*}} [[SINGLE]])
573 // CHECK11: call void @_ZN6test111AD1Ev([[A]]* {{[^,]*}} [[SINGLE]])
574
575 // CHECK: br label
576 // CHECK: resume
577 // (After this is a terminate landingpad.)
578 }
579
580 namespace test12 {
581 struct A {
582 void operator delete(void *, void *);
583 A();
584 };
585
test(void * ptr)586 A *test(void *ptr) {
587 return new (ptr) A();
588 }
589 // CHECK-LABEL: define {{.*}} @_ZN6test124testEPv(
590 // CHECK: [[PTR:%.*]] = load i8*, i8*
591 // CHECK-NEXT: [[CAST:%.*]] = bitcast i8* [[PTR]] to [[A:%.*]]*
592 // CHECK-NEXT: invoke void @_ZN6test121AC1Ev([[A]]* {{[^,]*}} [[CAST]])
593 // CHECK: ret [[A]]* [[CAST]]
594
595 // CHECK98: invoke void @_ZN6test121AdlEPvS1_(i8* [[PTR]], i8* [[PTR]])
596 // CHECK11: call void @_ZN6test121AdlEPvS1_(i8* [[PTR]], i8* [[PTR]])
597 }
598
599 namespace test13 {
600
601 struct A {
602 A();
603 ~A();
604 int a, b;
605 };
606
607 // CHECK: define void @_ZN6test134testEi(
608 // CHECK: %[[REF_TMP:.*]] = alloca %[[STRUCT_TEST13_A]], align 4
609 // CHECK: %[[CLEANUP_COND:.*]] = alloca i1, align 1
610 // CHECK: %[[REF_TMP1:.*]] = alloca %[[STRUCT_TEST13_A]], align 4
611 // CHECK: %[[CLEANUP_COND2:.*]] = alloca i1, align 1
612
613 // CHECK: call void @_ZN6test131AC1Ev(%[[STRUCT_TEST13_A]]* {{[^,]*}} %[[REF_TMP]])
614 // CHECK: store i1 true, i1* %[[CLEANUP_COND]], align 1
615 // CHECK: br
616
617 // CHECK: invoke void @_ZN6test131AC1Ev(%[[STRUCT_TEST13_A]]* {{[^,]*}} %[[REF_TMP1]])
618
619 // CHECK: store i1 true, i1* %[[CLEANUP_COND2]], align 1
620 // CHECK: br
621
622 // Check the flag before destructing the temporary.
623
624 // CHECK: landingpad { i8*, i32 }
625 // CHECK: %[[CLEANUP_IS_ACTIVE:.*]] = load i1, i1* %[[CLEANUP_COND]], align 1
626 // CHECK: br i1 %[[CLEANUP_IS_ACTIVE]],
627
628 // CHECK: void @_ZN6test131AD1Ev(%[[STRUCT_TEST13_A]]* {{[^,]*}} %[[REF_TMP]])
629
test(int c)630 void test(int c) {
631 const A &s = c ? static_cast<const A &>(A()) : static_cast<const A &>(A());
632 }
633
634 }
635
636 // CHECK98: attributes [[NI_NR_NUW]] = { noinline noreturn nounwind }
637