StmtPrinter: factor out arg printing code to PrintCallArgs
[clang.git] / test / CodeGenCXX / eh.cpp
blob5ae65ccfaab6ee71b2f6cf7658709cda16aac7b6
1 // RUN: %clang_cc1 -fexceptions -triple x86_64-apple-darwin -std=c++0x -emit-llvm %s -o %t.ll
2 // RUN: FileCheck --input-file=%t.ll %s
4 struct test1_D {
5 double d;
6 } d1;
8 void test1() {
9 throw d1;
12 // CHECK: define void @_Z5test1v()
13 // CHECK: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 8)
14 // CHECK-NEXT: [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[DSTAR:%[^*]*\*]]
15 // CHECK-NEXT: [[EXN2:%.*]] = bitcast [[DSTAR]] [[EXN]] to i8*
16 // CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[EXN2]], i8* bitcast ([[DSTAR]] @d1 to i8*), i64 8, i32 8, i1 false)
17 // CHECK-NEXT: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast (%0* @_ZTI7test1_D to i8*), i8* null) noreturn
18 // CHECK-NEXT: unreachable
21 struct test2_D {
22 test2_D(const test2_D&o);
23 test2_D();
24 virtual void bar() { }
25 int i; int j;
26 } d2;
28 void test2() {
29 throw d2;
32 // CHECK: define void @_Z5test2v()
33 // CHECK: [[EXNSLOTVAR:%.*]] = alloca i8*
34 // CHECK-NEXT: [[CLEANUPDESTVAR:%.*]] = alloca i32
35 // CHECK-NEXT: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 16)
36 // CHECK-NEXT: [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[DSTAR:%[^*]*\*]]
37 // CHECK-NEXT: invoke void @_ZN7test2_DC1ERKS_([[DSTAR]] [[EXN]], [[DSTAR]] @d2)
38 // CHECK-NEXT: to label %[[CONT:.*]] unwind label %{{.*}}
39 // : [[CONT]]: (can't check this in Release-Asserts builds)
40 // CHECK: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast (%{{.*}}* @_ZTI7test2_D to i8*), i8* null) noreturn
41 // CHECK-NEXT: unreachable
44 struct test3_D {
45 test3_D() { }
46 test3_D(volatile test3_D&o);
47 virtual void bar();
50 void test3() {
51 throw (volatile test3_D *)0;
54 // CHECK: define void @_Z5test3v()
55 // CHECK: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 8)
56 // CHECK-NEXT: [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[D:%[^*]+]]**
57 // CHECK-NEXT: store [[D]]* null, [[D]]** [[EXN]]
58 // CHECK-NEXT: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast (%1* @_ZTIPV7test3_D to i8*), i8* null) noreturn
59 // CHECK-NEXT: unreachable
62 void test4() {
63 throw;
66 // CHECK: define void @_Z5test4v()
67 // CHECK: call void @__cxa_rethrow() noreturn
68 // CHECK-NEXT: unreachable
71 // rdar://problem/7696549
72 namespace test5 {
73 struct A {
74 A();
75 A(const A&);
76 ~A();
79 void test() {
80 try { throw A(); } catch (A &x) {}
82 // CHECK: define void @_ZN5test54testEv()
83 // CHECK: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 1)
84 // CHECK: [[EXNCAST:%.*]] = bitcast i8* [[EXNOBJ]] to [[A:%[^*]*]]*
85 // CHECK-NEXT: invoke void @_ZN5test51AC1Ev([[A]]* [[EXNCAST]])
86 // CHECK: invoke void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast ({{%.*}}* @_ZTIN5test51AE to i8*), i8* bitcast (void ([[A]]*)* @_ZN5test51AD1Ev to i8*)) noreturn
87 // CHECK-NEXT: to label {{%.*}} unwind label %[[HANDLER:[^ ]*]]
88 // : [[HANDLER]]: (can't check this in Release-Asserts builds)
89 // CHECK: {{%.*}} = call i32 @llvm.eh.typeid.for(i8* bitcast ({{%.*}}* @_ZTIN5test51AE to i8*))
92 namespace test6 {
93 template <class T> struct allocator {
94 ~allocator() throw() { }
97 void foo() {
98 allocator<int> a;
102 // PR7127
103 namespace test7 {
104 // CHECK: define i32 @_ZN5test73fooEv()
105 int foo() {
106 // CHECK: [[CAUGHTEXNVAR:%.*]] = alloca i8*
107 // CHECK-NEXT: [[INTCATCHVAR:%.*]] = alloca i32
108 // CHECK-NEXT: [[EHCLEANUPDESTVAR:%.*]] = alloca i32
109 try {
110 try {
111 // CHECK-NEXT: [[EXNALLOC:%.*]] = call i8* @__cxa_allocate_exception
112 // CHECK-NEXT: bitcast i8* [[EXNALLOC]] to i32*
113 // CHECK-NEXT: store i32 1, i32*
114 // CHECK-NEXT: invoke void @__cxa_throw(i8* [[EXNALLOC]], i8* bitcast (i8** @_ZTIi to i8*), i8* null
115 throw 1;
118 // CHECK: [[CAUGHTEXN:%.*]] = call i8* @llvm.eh.exception()
119 // CHECK-NEXT: store i8* [[CAUGHTEXN]], i8** [[CAUGHTEXNVAR]]
120 // CHECK-NEXT: call i32 (i8*, i8*, ...)* @llvm.eh.selector(i8* [[CAUGHTEXN]], i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*), i8* bitcast (i8** @_ZTIi to i8*), i8* null)
121 // CHECK-NEXT: call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
122 // CHECK-NEXT: icmp eq
123 // CHECK-NEXT: br i1
124 // CHECK: load i8** [[CAUGHTEXNVAR]]
125 // CHECK-NEXT: call i8* @__cxa_begin_catch
126 // CHECK: invoke void @__cxa_rethrow
127 catch (int) {
128 throw;
131 // CHECK: [[CAUGHTEXN:%.*]] = call i8* @llvm.eh.exception()
132 // CHECK-NEXT: store i8* [[CAUGHTEXN]], i8** [[CAUGHTEXNVAR]]
133 // CHECK-NEXT: call i32 (i8*, i8*, ...)* @llvm.eh.selector(i8* [[CAUGHTEXN]], i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*), i8* null)
134 // CHECK-NEXT: store i32 1, i32* [[EHCLEANUPDESTVAR]]
135 // CHECK-NEXT: call void @__cxa_end_catch()
136 // CHECK-NEXT: br label
137 // CHECK: load i8** [[CAUGHTEXNVAR]]
138 // CHECK-NEXT: call i8* @__cxa_begin_catch
139 // CHECK-NEXT: call void @__cxa_end_catch
140 catch (...) {
142 // CHECK: ret i32 0
143 return 0;
147 // Ordering of destructors in a catch handler.
148 namespace test8 {
149 struct A { A(const A&); ~A(); };
150 void bar();
152 // CHECK: define void @_ZN5test83fooEv()
153 void foo() {
154 try {
155 // CHECK: invoke void @_ZN5test83barEv()
156 bar();
157 } catch (A a) {
158 // CHECK: call i8* @__cxa_get_exception_ptr
159 // CHECK-NEXT: bitcast
160 // CHECK-NEXT: invoke void @_ZN5test81AC1ERKS0_(
161 // CHECK: call i8* @__cxa_begin_catch
162 // CHECK-NEXT: invoke void @_ZN5test81AD1Ev(
163 // CHECK: call void @__cxa_end_catch()
164 // CHECK: ret void
169 // Constructor function-try-block must rethrow on fallthrough.
170 // rdar://problem/7696603
171 namespace test9 {
172 void opaque();
174 struct A { A(); };
176 // CHECK: define void @_ZN5test91AC1Ev(%"struct.test10::A"* %this) unnamed_addr
177 // CHECK: call void @_ZN5test91AC2Ev
178 // CHECK-NEXT: ret void
180 // CHECK: define void @_ZN5test91AC2Ev(%"struct.test10::A"* %this) unnamed_addr
181 A::A() try {
182 // CHECK: invoke void @_ZN5test96opaqueEv()
183 opaque();
184 } catch (int x) {
185 // CHECK: call i8* @__cxa_begin_catch
186 // CHECK: invoke void @_ZN5test96opaqueEv()
187 // CHECK: invoke void @__cxa_rethrow()
188 opaque();
191 // landing pad from first call to invoke
192 // CHECK: call i8* @llvm.eh.exception
193 // CHECK: call i32 (i8*, i8*, ...)* @llvm.eh.selector(i8* {{.*}}, i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*), i8* bitcast (i8** @_ZTIi to i8*), i8* null)
196 // __cxa_end_catch can throw for some kinds of caught exceptions.
197 namespace test10 {
198 void opaque();
200 struct A { ~A(); };
201 struct B { int x; };
203 // CHECK: define void @_ZN6test103fooEv()
204 void foo() {
205 A a; // force a cleanup context
207 try {
208 // CHECK: invoke void @_ZN6test106opaqueEv()
209 opaque();
210 } catch (int i) {
211 // CHECK: call i8* @__cxa_begin_catch
212 // CHECK-NEXT: bitcast
213 // CHECK-NEXT: load i32*
214 // CHECK-NEXT: store i32
215 // CHECK-NEXT: call void @__cxa_end_catch() nounwind
216 } catch (B a) {
217 // CHECK: call i8* @__cxa_begin_catch
218 // CHECK-NEXT: bitcast
219 // CHECK-NEXT: bitcast
220 // CHECK-NEXT: bitcast
221 // CHECK-NEXT: call void @llvm.memcpy
222 // CHECK-NEXT: invoke void @__cxa_end_catch()
223 } catch (...) {
224 // CHECK: call i8* @__cxa_begin_catch
225 // CHECK-NEXT: invoke void @__cxa_end_catch()
228 // CHECK: call void @_ZN6test101AD1Ev(
232 // __cxa_begin_catch returns pointers by value, even when catching by reference
233 // <rdar://problem/8212123>
234 namespace test11 {
235 void opaque();
237 // CHECK: define void @_ZN6test113fooEv()
238 void foo() {
239 try {
240 // CHECK: invoke void @_ZN6test116opaqueEv()
241 opaque();
242 } catch (int**&p) {
243 // CHECK: [[EXN:%.*]] = load i8**
244 // CHECK-NEXT: call i8* @__cxa_begin_catch(i8* [[EXN]]) nounwind
245 // CHECK-NEXT: [[ADJ1:%.*]] = getelementptr i8* [[EXN]], i32 32
246 // CHECK-NEXT: [[ADJ2:%.*]] = bitcast i8* [[ADJ1]] to i32***
247 // CHECK-NEXT: store i32*** [[ADJ2]], i32**** [[P:%.*]]
248 // CHECK-NEXT: call void @__cxa_end_catch() nounwind
252 struct A {};
254 // CHECK: define void @_ZN6test113barEv()
255 void bar() {
256 try {
257 // CHECK: [[EXNSLOT:%.*]] = alloca i8*
258 // CHECK-NEXT: [[P:%.*]] = alloca [[A:%.*]]**,
259 // CHECK-NEXT: [[TMP:%.*]] = alloca [[A]]*
260 // CHECK-NEXT: invoke void @_ZN6test116opaqueEv()
261 opaque();
262 } catch (A*&p) {
263 // CHECK: [[EXN:%.*]] = load i8** [[EXNSLOT]]
264 // CHECK-NEXT: [[ADJ1:%.*]] = call i8* @__cxa_begin_catch(i8* [[EXN]]) nounwind
265 // CHECK-NEXT: [[ADJ2:%.*]] = bitcast i8* [[ADJ1]] to [[A]]*
266 // CHECK-NEXT: store [[A]]* [[ADJ2]], [[A]]** [[TMP]]
267 // CHECK-NEXT: store [[A]]** [[TMP]], [[A]]*** [[P]]
268 // CHECK-NEXT: call void @__cxa_end_catch() nounwind
273 // PR7686
274 namespace test12 {
275 struct A { ~A(); };
276 bool opaque(const A&);
278 // CHECK: define void @_ZN6test124testEv()
279 void test() {
280 // CHECK: [[X:%.*]] = alloca [[A:%.*]],
281 // CHECK: [[EHCLEANUPDEST:%.*]] = alloca i32
282 // CHECK: [[Y:%.*]] = alloca [[A]]
283 // CHECK: [[Z:%.*]] = alloca [[A]]
284 // CHECK: [[CLEANUPDEST:%.*]] = alloca i32
286 A x;
287 // CHECK: invoke zeroext i1 @_ZN6test126opaqueERKNS_1AE(
288 if (opaque(x)) {
289 A y;
290 A z;
292 // CHECK: invoke void @_ZN6test121AD1Ev([[A]]* [[Z]])
293 // CHECK: invoke void @_ZN6test121AD1Ev([[A]]* [[Y]])
295 // It'd be great if something eliminated this switch.
296 // CHECK: load i32* [[CLEANUPDEST]]
297 // CHECK-NEXT: switch i32
298 goto success;
301 success:
302 bool _ = true;
304 // CHECK: call void @_ZN6test121AD1Ev([[A]]* [[X]])
305 // CHECK-NEXT: ret void
309 // Reduced from some TableGen code that was causing a self-host crash.
310 namespace test13 {
311 struct A { ~A(); };
313 void test0(int x) {
314 try {
315 switch (x) {
316 case 0:
317 break;
318 case 1:{
319 A a;
320 break;
322 default:
323 return;
325 return;
326 } catch (int x) {
328 return;
331 void test1(int x) {
332 A y;
333 try {
334 switch (x) {
335 default: break;
337 } catch (int x) {}
341 // rdar://problem/8231514
342 namespace test14 {
343 struct A { ~A(); };
344 struct B { ~B(); };
346 B b();
347 void opaque();
349 void foo() {
350 A a;
351 try {
352 B str = b();
353 opaque();
354 } catch (int x) {
359 // rdar://problem/8231514
360 // JumpDests shouldn't get confused by scopes that aren't normal cleanups.
361 namespace test15 {
362 struct A { ~A(); };
364 bool opaque(int);
366 // CHECK: define void @_ZN6test153fooEv()
367 void foo() {
368 A a;
370 try {
371 // CHECK: [[X:%.*]] = alloca i32
372 // CHECK: store i32 10, i32* [[X]]
373 // CHECK-NEXT: br label
374 // -> while.cond
375 int x = 10;
377 while (true) {
378 // CHECK: load i32* [[X]]
379 // CHECK-NEXT: [[COND:%.*]] = invoke zeroext i1 @_ZN6test156opaqueEi
380 // CHECK: br i1 [[COND]]
381 if (opaque(x))
382 // CHECK: br label
383 break;
385 // CHECK: br label
387 // CHECK: br label
388 } catch (int x) { }
390 // CHECK: call void @_ZN6test151AD1Ev
394 namespace test16 {
395 struct A { A(); ~A(); };
396 struct B { int x; B(const A &); ~B(); };
397 void foo();
398 bool cond();
400 // CHECK: define void @_ZN6test163barEv()
401 void bar() {
402 // CHECK: [[EXN_SAVE:%.*]] = alloca i8*
403 // CHECK-NEXT: [[EXN_ACTIVE:%.*]] = alloca i1
404 // CHECK-NEXT: [[TEMP:%.*]] = alloca [[A:%.*]],
405 // CHECK-NEXT: [[EXNSLOT:%.*]] = alloca i8*
406 // CHECK-NEXT: [[EHDEST:%.*]] = alloca i32
407 // CHECK-NEXT: [[TEMP_ACTIVE:%.*]] = alloca i1
409 cond() ? throw B(A()) : foo();
411 // CHECK-NEXT: [[COND:%.*]] = call zeroext i1 @_ZN6test164condEv()
412 // CHECK-NEXT: store i1 false, i1* [[EXN_ACTIVE]]
413 // CHECK-NEXT: store i1 false, i1* [[TEMP_ACTIVE]]
414 // CHECK-NEXT: br i1 [[COND]],
416 // CHECK: [[EXN:%.*]] = call i8* @__cxa_allocate_exception(i64 4)
417 // CHECK-NEXT: store i8* [[EXN]], i8** [[EXN_SAVE]]
418 // CHECK-NEXT: store i1 true, i1* [[EXN_ACTIVE]]
419 // CHECK-NEXT: [[T0:%.*]] = bitcast i8* [[EXN]] to [[B:%.*]]*
420 // CHECK-NEXT: invoke void @_ZN6test161AC1Ev([[A]]* [[TEMP]])
421 // CHECK: store i1 true, i1* [[TEMP_ACTIVE]]
422 // CHECK-NEXT: invoke void @_ZN6test161BC1ERKNS_1AE([[B]]* [[T0]], [[A]]* [[TEMP]])
423 // CHECK: store i1 false, i1* [[EXN_ACTIVE]]
424 // CHECK-NEXT: invoke void @__cxa_throw(i8* [[EXN]],
426 // CHECK: invoke void @_ZN6test163fooEv()
427 // CHECK: br label
429 // CHECK: invoke void @_ZN6test161AD1Ev([[A]]* [[TEMP]])
430 // CHECK: ret void
432 // CHECK: [[T0:%.*]] = load i1* [[EXN_ACTIVE]]
433 // CHECK-NEXT: br i1 [[T0]]
434 // CHECK: [[T1:%.*]] = load i8** [[EXN_SAVE]]
435 // CHECK-NEXT: call void @__cxa_free_exception(i8* [[T1]])
436 // CHECK-NEXT: br label