blob: 03f335f4eccaee8a548e89ca39f511c81705dd16 [file] [log] [blame]
John McCallac418162010-04-22 01:10:34 +00001// RUN: %clang_cc1 -fexceptions -triple x86_64-apple-darwin -std=c++0x -emit-llvm %s -o %t.ll
Mike Stumpa4463432009-11-20 18:51:28 +00002// RUN: FileCheck --input-file=%t.ll %s
3
4struct test1_D {
5 double d;
6} d1;
7
8void test1() {
9 throw d1;
10}
11
John McCallac418162010-04-22 01:10:34 +000012// CHECK: define void @_Z5test1v()
13// CHECK: [[FREEVAR:%.*]] = alloca i1
14// CHECK-NEXT: [[EXNOBJVAR:%.*]] = alloca i8*
15// CHECK-NEXT: store i1 false, i1* [[FREEVAR]]
16// CHECK-NEXT: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 8)
17// CHECK-NEXT: store i8* [[EXNOBJ]], i8** [[EXNOBJVAR]]
18// CHECK-NEXT: store i1 true, i1* [[FREEVAR]]
19// CHECK-NEXT: [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[DSTAR:%[^*]*\*]]
20// CHECK-NEXT: [[EXN2:%.*]] = bitcast [[DSTAR]] [[EXN]] to i8*
21// CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[EXN2]], i8* bitcast ([[DSTAR]] @d1 to i8*), i64 8, i32 8, i1 false)
22// CHECK-NEXT: store i1 false, i1* [[FREEVAR]]
23// CHECK-NEXT: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast (%0* @_ZTI7test1_D to i8*), i8* null) noreturn
Mike Stumpa4463432009-11-20 18:51:28 +000024// CHECK-NEXT: unreachable
25
26
27struct test2_D {
28 test2_D(const test2_D&o);
29 test2_D();
30 virtual void bar() { }
31 int i; int j;
32} d2;
33
34void test2() {
35 throw d2;
36}
37
John McCallac418162010-04-22 01:10:34 +000038// CHECK: define void @_Z5test2v()
39// CHECK: [[FREEVAR:%.*]] = alloca i1
40// CHECK-NEXT: [[EXNOBJVAR:%.*]] = alloca i8*
John McCallf1549f62010-07-06 01:34:17 +000041// CHECK-NEXT: [[EXNSLOTVAR:%.*]] = alloca i8*
John McCallff8e1152010-07-23 21:56:41 +000042// CHECK-NEXT: [[CLEANUPDESTVAR:%.*]] = alloca i32
John McCallac418162010-04-22 01:10:34 +000043// CHECK-NEXT: store i1 false, i1* [[FREEVAR]]
44// CHECK-NEXT: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 16)
45// CHECK-NEXT: store i8* [[EXNOBJ]], i8** [[EXNOBJVAR]]
46// CHECK-NEXT: store i1 true, i1* [[FREEVAR]]
47// CHECK-NEXT: [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[DSTAR:%[^*]*\*]]
48// CHECK-NEXT: invoke void @_ZN7test2_DC1ERKS_([[DSTAR]] [[EXN]], [[DSTAR]] @d2)
49// CHECK-NEXT: to label %[[CONT:.*]] unwind label %{{.*}}
John McCall7e2f1282010-04-22 03:27:09 +000050// : [[CONT]]: (can't check this in Release-Asserts builds)
51// CHECK: store i1 false, i1* [[FREEVAR]]
John McCallac418162010-04-22 01:10:34 +000052// CHECK-NEXT: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast (%{{.*}}* @_ZTI7test2_D to i8*), i8* null) noreturn
Mike Stumpa4463432009-11-20 18:51:28 +000053// CHECK-NEXT: unreachable
54
55
56struct test3_D {
57 test3_D() { }
58 test3_D(volatile test3_D&o);
59 virtual void bar();
60};
61
62void test3() {
63 throw (volatile test3_D *)0;
64}
65
John McCallac418162010-04-22 01:10:34 +000066// CHECK: define void @_Z5test3v()
67// CHECK: [[FREEVAR:%.*]] = alloca i1
68// CHECK-NEXT: [[EXNOBJVAR:%.*]] = alloca i8*
69// CHECK-NEXT: store i1 false, i1* [[FREEVAR]]
70// CHECK-NEXT: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 8)
71// CHECK-NEXT: store i8* [[EXNOBJ]], i8** [[EXNOBJVAR]]
72// CHECK-NEXT: store i1 true, i1* [[FREEVAR]]
73// CHECK-NEXT: [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[DSS:%[^*]*\*]]*
74// CHECK-NEXT: store [[DSS]] null, [[DSS]]* [[EXN]]
75// CHECK-NEXT: store i1 false, i1* [[FREEVAR]]
76// CHECK-NEXT: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast (%1* @_ZTIPV7test3_D to i8*), i8* null) noreturn
77// CHECK-NEXT: unreachable
Mike Stumpa4463432009-11-20 18:51:28 +000078
79
80void test4() {
81 throw;
82}
83
John McCallac418162010-04-22 01:10:34 +000084// CHECK: define void @_Z5test4v()
Mike Stump53b48102010-01-13 21:23:04 +000085// CHECK: call void @__cxa_rethrow() noreturn
Mike Stumpa4463432009-11-20 18:51:28 +000086// CHECK-NEXT: unreachable
John McCallac418162010-04-22 01:10:34 +000087
88
89// rdar://problem/7696549
90namespace test5 {
91 struct A {
92 A();
93 A(const A&);
94 ~A();
95 };
96
97 void test() {
98 try { throw A(); } catch (A &x) {}
99 }
100// CHECK: define void @_ZN5test54testEv()
101// CHECK: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 1)
102// CHECK: [[EXNCAST:%.*]] = bitcast i8* [[EXNOBJ]] to [[A:%[^*]*]]*
103// CHECK-NEXT: invoke void @_ZN5test51AC1Ev([[A]]* [[EXNCAST]])
104// CHECK: invoke void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast ({{%.*}}* @_ZTIN5test51AE to i8*), i8* bitcast (void ([[A]]*)* @_ZN5test51AD1Ev to i8*)) noreturn
105// CHECK-NEXT: to label {{%.*}} unwind label %[[HANDLER:[^ ]*]]
John McCall7e2f1282010-04-22 03:27:09 +0000106// : [[HANDLER]]: (can't check this in Release-Asserts builds)
John McCallac418162010-04-22 01:10:34 +0000107// CHECK: {{%.*}} = call i32 @llvm.eh.typeid.for(i8* bitcast ({{%.*}}* @_ZTIN5test51AE to i8*))
108}
John McCallf1549f62010-07-06 01:34:17 +0000109
110namespace test6 {
111 template <class T> struct allocator {
112 ~allocator() throw() { }
113 };
114
115 void foo() {
116 allocator<int> a;
117 }
118}
119
120// PR7127
121namespace test7 {
122// CHECK: define i32 @_ZN5test73fooEv()
123 int foo() {
124// CHECK: [[FREEEXNOBJ:%.*]] = alloca i1
125// CHECK-NEXT: [[EXNALLOCVAR:%.*]] = alloca i8*
126// CHECK-NEXT: [[CAUGHTEXNVAR:%.*]] = alloca i8*
127// CHECK-NEXT: [[INTCATCHVAR:%.*]] = alloca i32
John McCallff8e1152010-07-23 21:56:41 +0000128// CHECK-NEXT: [[EHCLEANUPDESTVAR:%.*]] = alloca i32
John McCallf1549f62010-07-06 01:34:17 +0000129// CHECK-NEXT: store i1 false, i1* [[FREEEXNOBJ]]
130 try {
131 try {
132// CHECK-NEXT: [[EXNALLOC:%.*]] = call i8* @__cxa_allocate_exception
133// CHECK-NEXT: store i8* [[EXNALLOC]], i8** [[EXNALLOCVAR]]
134// CHECK-NEXT: store i1 true, i1* [[FREEEXNOBJ]]
135// CHECK-NEXT: bitcast i8* [[EXNALLOC]] to i32*
136// CHECK-NEXT: store i32 1, i32*
137// CHECK-NEXT: store i1 false, i1* [[FREEEXNOBJ]]
138// CHECK-NEXT: invoke void @__cxa_throw(i8* [[EXNALLOC]], i8* bitcast (i8** @_ZTIi to i8*), i8* null
139 throw 1;
140 }
John McCallf1549f62010-07-06 01:34:17 +0000141
142// CHECK: [[CAUGHTEXN:%.*]] = call i8* @llvm.eh.exception()
143// CHECK-NEXT: store i8* [[CAUGHTEXN]], i8** [[CAUGHTEXNVAR]]
144// 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)
145// CHECK-NEXT: call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
146// CHECK-NEXT: icmp eq
147// CHECK-NEXT: br i1
148// CHECK: load i8** [[CAUGHTEXNVAR]]
149// CHECK-NEXT: call i8* @__cxa_begin_catch
150// CHECK: invoke void @__cxa_rethrow
151 catch (int) {
152 throw;
153 }
154 }
155// CHECK: [[CAUGHTEXN:%.*]] = call i8* @llvm.eh.exception()
156// CHECK-NEXT: store i8* [[CAUGHTEXN]], i8** [[CAUGHTEXNVAR]]
157// CHECK-NEXT: call i32 (i8*, i8*, ...)* @llvm.eh.selector(i8* [[CAUGHTEXN]], i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*), i8* null)
John McCallff8e1152010-07-23 21:56:41 +0000158// CHECK-NEXT: store i32 1, i32* [[EHCLEANUPDESTVAR]]
John McCallf1549f62010-07-06 01:34:17 +0000159// CHECK-NEXT: call void @__cxa_end_catch()
160// CHECK-NEXT: br label
161// CHECK: load i8** [[CAUGHTEXNVAR]]
162// CHECK-NEXT: call i8* @__cxa_begin_catch
163// CHECK-NEXT: call void @__cxa_end_catch
164 catch (...) {
165 }
166// CHECK: ret i32 0
167 return 0;
168 }
169}
170
171// Ordering of destructors in a catch handler.
172namespace test8 {
173 struct A { A(const A&); ~A(); };
174 void bar();
175
176 // CHECK: define void @_ZN5test83fooEv()
177 void foo() {
178 try {
179 // CHECK: invoke void @_ZN5test83barEv()
180 bar();
181 } catch (A a) {
182 // CHECK: call i8* @__cxa_get_exception_ptr
183 // CHECK-NEXT: bitcast
184 // CHECK-NEXT: invoke void @_ZN5test81AC1ERKS0_(
185 // CHECK: call i8* @__cxa_begin_catch
186 // CHECK-NEXT: invoke void @_ZN5test81AD1Ev(
John McCallf1549f62010-07-06 01:34:17 +0000187 // CHECK: call void @__cxa_end_catch()
John McCallf1549f62010-07-06 01:34:17 +0000188 // CHECK: ret void
189 }
190 }
191}
John McCall59a70002010-07-07 06:56:46 +0000192
193// Constructor function-try-block must rethrow on fallthrough.
194// rdar://problem/7696603
195namespace test9 {
196 void opaque();
197
198 struct A { A(); };
199
Rafael Espindola0691a5c2011-01-25 19:10:24 +0000200 // CHECK: define void @_ZN5test91AC1Ev(%"struct.test10::A"* %this) unnamed_addr
John McCall59a70002010-07-07 06:56:46 +0000201 // CHECK: call void @_ZN5test91AC2Ev
202 // CHECK-NEXT: ret void
203
Rafael Espindola0691a5c2011-01-25 19:10:24 +0000204 // CHECK: define void @_ZN5test91AC2Ev(%"struct.test10::A"* %this) unnamed_addr
John McCall59a70002010-07-07 06:56:46 +0000205 A::A() try {
206 // CHECK: invoke void @_ZN5test96opaqueEv()
207 opaque();
208 } catch (int x) {
209 // CHECK: call i8* @__cxa_begin_catch
210 // CHECK: invoke void @_ZN5test96opaqueEv()
211 // CHECK: invoke void @__cxa_rethrow()
212 opaque();
213 }
214
215 // landing pad from first call to invoke
216 // CHECK: call i8* @llvm.eh.exception
217 // 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)
218}
John McCall8e3f8612010-07-13 22:12:14 +0000219
220// __cxa_end_catch can throw for some kinds of caught exceptions.
221namespace test10 {
222 void opaque();
223
224 struct A { ~A(); };
225 struct B { int x; };
226
227 // CHECK: define void @_ZN6test103fooEv()
228 void foo() {
229 A a; // force a cleanup context
230
231 try {
232 // CHECK: invoke void @_ZN6test106opaqueEv()
233 opaque();
234 } catch (int i) {
235 // CHECK: call i8* @__cxa_begin_catch
236 // CHECK-NEXT: bitcast
237 // CHECK-NEXT: load i32*
238 // CHECK-NEXT: store i32
239 // CHECK-NEXT: call void @__cxa_end_catch() nounwind
240 } catch (B a) {
241 // CHECK: call i8* @__cxa_begin_catch
242 // CHECK-NEXT: bitcast
243 // CHECK-NEXT: bitcast
244 // CHECK-NEXT: bitcast
245 // CHECK-NEXT: call void @llvm.memcpy
246 // CHECK-NEXT: invoke void @__cxa_end_catch()
247 } catch (...) {
248 // CHECK: call i8* @__cxa_begin_catch
249 // CHECK-NEXT: invoke void @__cxa_end_catch()
250 }
251
252 // CHECK: call void @_ZN6test101AD1Ev(
253 }
254}
John McCall204b0752010-07-20 22:17:55 +0000255
256// __cxa_begin_catch returns pointers by value, even when catching by reference
257// <rdar://problem/8212123>
258namespace test11 {
259 void opaque();
260
261 // CHECK: define void @_ZN6test113fooEv()
262 void foo() {
263 try {
264 // CHECK: invoke void @_ZN6test116opaqueEv()
265 opaque();
266 } catch (int**&p) {
267 // CHECK: [[EXN:%.*]] = load i8**
268 // CHECK-NEXT: call i8* @__cxa_begin_catch(i8* [[EXN]]) nounwind
269 // CHECK-NEXT: [[ADJ1:%.*]] = getelementptr i8* [[EXN]], i32 32
270 // CHECK-NEXT: [[ADJ2:%.*]] = bitcast i8* [[ADJ1]] to i32***
271 // CHECK-NEXT: store i32*** [[ADJ2]], i32**** [[P:%.*]]
272 // CHECK-NEXT: call void @__cxa_end_catch() nounwind
273 }
274 }
275
276 struct A {};
277
278 // CHECK: define void @_ZN6test113barEv()
279 void bar() {
280 try {
281 // CHECK: [[EXNSLOT:%.*]] = alloca i8*
282 // CHECK-NEXT: [[P:%.*]] = alloca [[A:%.*]]**,
283 // CHECK-NEXT: [[TMP:%.*]] = alloca [[A]]*
284 // CHECK-NEXT: invoke void @_ZN6test116opaqueEv()
285 opaque();
286 } catch (A*&p) {
287 // CHECK: [[EXN:%.*]] = load i8** [[EXNSLOT]]
288 // CHECK-NEXT: [[ADJ1:%.*]] = call i8* @__cxa_begin_catch(i8* [[EXN]]) nounwind
289 // CHECK-NEXT: [[ADJ2:%.*]] = bitcast i8* [[ADJ1]] to [[A]]*
290 // CHECK-NEXT: store [[A]]* [[ADJ2]], [[A]]** [[TMP]]
291 // CHECK-NEXT: store [[A]]** [[TMP]], [[A]]*** [[P]]
292 // CHECK-NEXT: call void @__cxa_end_catch() nounwind
293 }
294 }
295}
John McCallff8e1152010-07-23 21:56:41 +0000296
297// PR7686
298namespace test12 {
299 struct A { ~A(); };
300 bool opaque(const A&);
301
302 // CHECK: define void @_ZN6test124testEv()
303 void test() {
304 // CHECK: [[X:%.*]] = alloca [[A:%.*]],
305 // CHECK: [[EHCLEANUPDEST:%.*]] = alloca i32
306 // CHECK: [[Y:%.*]] = alloca [[A]]
307 // CHECK: [[Z:%.*]] = alloca [[A]]
308 // CHECK: [[CLEANUPDEST:%.*]] = alloca i32
309
310 A x;
311 // CHECK: invoke zeroext i1 @_ZN6test126opaqueERKNS_1AE(
312 if (opaque(x)) {
313 A y;
314 A z;
315
316 // CHECK: invoke void @_ZN6test121AD1Ev([[A]]* [[Z]])
317 // CHECK: invoke void @_ZN6test121AD1Ev([[A]]* [[Y]])
318
319 // It'd be great if something eliminated this switch.
320 // CHECK: load i32* [[CLEANUPDEST]]
321 // CHECK-NEXT: switch i32
322 goto success;
323 }
324
325 success:
326 bool _ = true;
327
328 // CHECK: call void @_ZN6test121AD1Ev([[A]]* [[X]])
329 // CHECK-NEXT: ret void
330 }
331}
332
333// Reduced from some TableGen code that was causing a self-host crash.
334namespace test13 {
335 struct A { ~A(); };
336
337 void test0(int x) {
338 try {
339 switch (x) {
340 case 0:
341 break;
342 case 1:{
343 A a;
344 break;
345 }
346 default:
347 return;
348 }
349 return;
350 } catch (int x) {
351 }
352 return;
353 }
354
355 void test1(int x) {
356 A y;
357 try {
358 switch (x) {
359 default: break;
360 }
361 } catch (int x) {}
362 }
363}
John McCall7cd4b062010-07-26 22:44:58 +0000364
365// rdar://problem/8231514
366namespace test14 {
367 struct A { ~A(); };
368 struct B { ~B(); };
369
370 B b();
371 void opaque();
372
373 void foo() {
374 A a;
375 try {
376 B str = b();
377 opaque();
378 } catch (int x) {
379 }
380 }
381}
John McCall413e6772010-07-28 01:07:35 +0000382
383// rdar://problem/8231514
384// JumpDests shouldn't get confused by scopes that aren't normal cleanups.
385namespace test15 {
386 struct A { ~A(); };
387
388 bool opaque(int);
389
390 // CHECK: define void @_ZN6test153fooEv()
391 void foo() {
392 A a;
393
394 try {
395 // CHECK: [[X:%.*]] = alloca i32
396 // CHECK: store i32 10, i32* [[X]]
397 // CHECK-NEXT: br label
398 // -> while.cond
399 int x = 10;
400
401 while (true) {
402 // CHECK: load i32* [[X]]
403 // CHECK-NEXT: [[COND:%.*]] = invoke zeroext i1 @_ZN6test156opaqueEi
404 // CHECK: br i1 [[COND]]
405 if (opaque(x))
406 // CHECK: br label
407 break;
408
409 // CHECK: br label
410 }
411 // CHECK: br label
412 } catch (int x) { }
413
414 // CHECK: call void @_ZN6test151AD1Ev
415 }
416}