1// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -triple x86_64-apple-darwin -std=c++11 -emit-llvm %s -o %t.ll
2// RUN: FileCheck --input-file=%t.ll %s
3
4struct test1_D {
5  double d;
6} d1;
7
8void test1() {
9  throw d1;
10}
11
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 ({ i8*, i8* }* @_ZTI7test1_D to i8*), i8* null) [[NR:#[0-9]+]]
18// CHECK-NEXT:  unreachable
19
20
21struct test2_D {
22  test2_D(const test2_D&o);
23  test2_D();
24  virtual void bar() { }
25  int i; int j;
26} d2;
27
28void test2() {
29  throw d2;
30}
31
32// CHECK:     define void @_Z5test2v()
33// CHECK:       [[EXNVAR:%.*]] = alloca i8*
34// CHECK-NEXT:  [[SELECTORVAR:%.*]] = 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) [[NR]]
41// CHECK-NEXT:  unreachable
42
43
44struct test3_D {
45  test3_D() { }
46  test3_D(volatile test3_D&o);
47  virtual void bar();
48};
49
50void test3() {
51  throw (volatile test3_D *)0;
52}
53
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 ({ i8*, i8*, i32, i8* }* @_ZTIPV7test3_D to i8*), i8* null) [[NR]]
59// CHECK-NEXT:  unreachable
60
61
62void test4() {
63  throw;
64}
65
66// CHECK:     define void @_Z5test4v()
67// CHECK:        call void @__cxa_rethrow() [[NR]]
68// CHECK-NEXT:   unreachable
69
70
71// rdar://problem/7696549
72namespace test5 {
73  struct A {
74    A();
75    A(const A&);
76    ~A();
77  };
78
79  void test() {
80    try { throw A(); } catch (A &x) {}
81  }
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*)) [[NR]]
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*))
90}
91
92namespace test6 {
93  template <class T> struct allocator {
94    ~allocator() throw() { }
95  };
96
97  void foo() {
98    allocator<int> a;
99  }
100}
101
102// PR7127
103namespace test7 {
104// CHECK:      define i32 @_ZN5test73fooEv()
105  int foo() {
106// CHECK:      [[CAUGHTEXNVAR:%.*]] = alloca i8*
107// CHECK-NEXT: [[SELECTORVAR:%.*]] = alloca i32
108// CHECK-NEXT: [[INTCATCHVAR:%.*]] = 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;
116      }
117
118// CHECK:      [[CAUGHTVAL:%.*]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
119// CHECK-NEXT:   catch i8* bitcast (i8** @_ZTIi to i8*)
120// CHECK-NEXT:   catch i8* null
121// CHECK-NEXT: [[CAUGHTEXN:%.*]] = extractvalue { i8*, i32 } [[CAUGHTVAL]], 0
122// CHECK-NEXT: store i8* [[CAUGHTEXN]], i8** [[CAUGHTEXNVAR]]
123// CHECK-NEXT: [[SELECTOR:%.*]] = extractvalue { i8*, i32 } [[CAUGHTVAL]], 1
124// CHECK-NEXT: store i32 [[SELECTOR]], i32* [[SELECTORVAR]]
125// CHECK-NEXT: br label
126// CHECK:      [[SELECTOR:%.*]] = load i32* [[SELECTORVAR]]
127// CHECK-NEXT: [[T0:%.*]] = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
128// CHECK-NEXT: icmp eq i32 [[SELECTOR]], [[T0]]
129// CHECK-NEXT: br i1
130// CHECK:      [[T0:%.*]] = load i8** [[CAUGHTEXNVAR]]
131// CHECK-NEXT: [[T1:%.*]] = call i8* @__cxa_begin_catch(i8* [[T0]])
132// CHECK-NEXT: [[T2:%.*]] = bitcast i8* [[T1]] to i32*
133// CHECK-NEXT: [[T3:%.*]] = load i32* [[T2]]
134// CHECK-NEXT: store i32 [[T3]], i32* {{%.*}}, align 4
135// CHECK-NEXT: invoke void @__cxa_rethrow
136      catch (int) {
137        throw;
138      }
139    }
140// CHECK:      [[CAUGHTVAL:%.*]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
141// CHECK-NEXT:   catch i8* null
142// CHECK-NEXT: [[CAUGHTEXN:%.*]] = extractvalue { i8*, i32 } [[CAUGHTVAL]], 0
143// CHECK-NEXT: store i8* [[CAUGHTEXN]], i8** [[CAUGHTEXNVAR]]
144// CHECK-NEXT: [[SELECTOR:%.*]] = extractvalue { i8*, i32 } [[CAUGHTVAL]], 1
145// CHECK-NEXT: store i32 [[SELECTOR]], i32* [[SELECTORVAR]]
146// CHECK-NEXT: call void @__cxa_end_catch()
147// CHECK-NEXT: br label
148// CHECK:      load i8** [[CAUGHTEXNVAR]]
149// CHECK-NEXT: call i8* @__cxa_begin_catch
150// CHECK-NEXT: call void @__cxa_end_catch
151    catch (...) {
152    }
153// CHECK:      ret i32 0
154    return 0;
155  }
156}
157
158// Ordering of destructors in a catch handler.
159namespace test8 {
160  struct A { A(const A&); ~A(); };
161  void bar();
162
163  // CHECK: define void @_ZN5test83fooEv()
164  void foo() {
165    try {
166      // CHECK:      invoke void @_ZN5test83barEv()
167      bar();
168    } catch (A a) {
169      // CHECK:      call i8* @__cxa_get_exception_ptr
170      // CHECK-NEXT: bitcast
171      // CHECK-NEXT: invoke void @_ZN5test81AC1ERKS0_(
172      // CHECK:      call i8* @__cxa_begin_catch
173      // CHECK-NEXT: call void @_ZN5test81AD1Ev(
174      // CHECK:      call void @__cxa_end_catch()
175      // CHECK:      ret void
176    }
177  }
178}
179
180// Constructor function-try-block must rethrow on fallthrough.
181// rdar://problem/7696603
182namespace test9 {
183  void opaque();
184
185  struct A { A(); };
186
187  // CHECK:      define void @_ZN5test91AC1Ev(%"struct.test9::A"* %this) unnamed_addr
188  // CHECK:      call void @_ZN5test91AC2Ev
189  // CHECK-NEXT: ret void
190
191  // CHECK: define void @_ZN5test91AC2Ev(%"struct.test9::A"* %this) unnamed_addr
192  A::A() try {
193  // CHECK:      invoke void @_ZN5test96opaqueEv()
194    opaque();
195  } catch (int x) {
196  // CHECK:      landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
197  // CHECK-NEXT:   catch i8* bitcast (i8** @_ZTIi to i8*)
198
199  // CHECK:      call i8* @__cxa_begin_catch
200  // CHECK:      invoke void @_ZN5test96opaqueEv()
201  // CHECK:      invoke void @__cxa_rethrow()
202    opaque();
203  }
204}
205
206// __cxa_end_catch can throw for some kinds of caught exceptions.
207namespace test10 {
208  void opaque();
209
210  struct A { ~A(); };
211  struct B { int x; };
212
213  // CHECK: define void @_ZN6test103fooEv()
214  void foo() {
215    A a; // force a cleanup context
216
217    try {
218    // CHECK:      invoke void @_ZN6test106opaqueEv()
219      opaque();
220    } catch (int i) {
221    // CHECK:      call i8* @__cxa_begin_catch
222    // CHECK-NEXT: bitcast
223    // CHECK-NEXT: load i32*
224    // CHECK-NEXT: store i32
225    // CHECK-NEXT: call void @__cxa_end_catch() [[NUW:#[0-9]+]]
226    } catch (B a) {
227    // CHECK:      call i8* @__cxa_begin_catch
228    // CHECK-NEXT: bitcast
229    // CHECK-NEXT: bitcast
230    // CHECK-NEXT: bitcast
231    // CHECK-NEXT: call void @llvm.memcpy
232    // CHECK-NEXT: invoke void @__cxa_end_catch()
233    } catch (...) {
234    // CHECK:      call i8* @__cxa_begin_catch
235    // CHECK-NEXT: invoke void @__cxa_end_catch()
236    }
237
238    // CHECK: call void @_ZN6test101AD1Ev(
239  }
240}
241
242// __cxa_begin_catch returns pointers by value, even when catching by reference
243// <rdar://problem/8212123>
244namespace test11 {
245  void opaque();
246
247  // CHECK: define void @_ZN6test113fooEv()
248  void foo() {
249    try {
250      // CHECK:      invoke void @_ZN6test116opaqueEv()
251      opaque();
252    } catch (int**&p) {
253      // CHECK:      [[EXN:%.*]] = load i8**
254      // CHECK-NEXT: call i8* @__cxa_begin_catch(i8* [[EXN]]) [[NUW]]
255      // CHECK-NEXT: [[ADJ1:%.*]] = getelementptr i8* [[EXN]], i32 32
256      // CHECK-NEXT: [[ADJ2:%.*]] = bitcast i8* [[ADJ1]] to i32***
257      // CHECK-NEXT: store i32*** [[ADJ2]], i32**** [[P:%.*]]
258      // CHECK-NEXT: call void @__cxa_end_catch() [[NUW]]
259    }
260  }
261
262  struct A {};
263
264  // CHECK: define void @_ZN6test113barEv()
265  void bar() {
266    try {
267      // CHECK:      [[EXNSLOT:%.*]] = alloca i8*
268      // CHECK-NEXT: [[SELECTORSLOT:%.*]] = alloca i32
269      // CHECK-NEXT: [[P:%.*]] = alloca [[A:%.*]]**,
270      // CHECK-NEXT: [[TMP:%.*]] = alloca [[A]]*
271      // CHECK-NEXT: invoke void @_ZN6test116opaqueEv()
272      opaque();
273    } catch (A*&p) {
274      // CHECK:      [[EXN:%.*]] = load i8** [[EXNSLOT]]
275      // CHECK-NEXT: [[ADJ1:%.*]] = call i8* @__cxa_begin_catch(i8* [[EXN]]) [[NUW]]
276      // CHECK-NEXT: [[ADJ2:%.*]] = bitcast i8* [[ADJ1]] to [[A]]*
277      // CHECK-NEXT: store [[A]]* [[ADJ2]], [[A]]** [[TMP]]
278      // CHECK-NEXT: store [[A]]** [[TMP]], [[A]]*** [[P]]
279      // CHECK-NEXT: call void @__cxa_end_catch() [[NUW]]
280    }
281  }
282}
283
284// PR7686
285namespace test12 {
286  struct A { ~A() noexcept(false); };
287  bool opaque(const A&);
288
289  // CHECK: define void @_ZN6test124testEv()
290  void test() {
291    // CHECK: [[X:%.*]] = alloca [[A:%.*]],
292    // CHECK: [[EHCLEANUPDEST:%.*]] = alloca i32
293    // CHECK: [[Y:%.*]] = alloca [[A]]
294    // CHECK: [[Z:%.*]] = alloca [[A]]
295    // CHECK: [[CLEANUPDEST:%.*]] = alloca i32
296
297    A x;
298    // CHECK: invoke zeroext i1 @_ZN6test126opaqueERKNS_1AE(
299    if (opaque(x)) {
300      A y;
301      A z;
302
303      // CHECK: invoke void @_ZN6test121AD1Ev([[A]]* [[Z]])
304      // CHECK: invoke void @_ZN6test121AD1Ev([[A]]* [[Y]])
305      // CHECK-NOT: switch
306      goto success;
307    }
308
309  success:
310    bool _ = true;
311
312    // CHECK: call void @_ZN6test121AD1Ev([[A]]* [[X]])
313    // CHECK-NEXT: ret void
314  }
315}
316
317// Reduced from some TableGen code that was causing a self-host crash.
318namespace test13 {
319  struct A { ~A(); };
320
321  void test0(int x) {
322    try {
323      switch (x) {
324      case 0:
325        break;
326      case 1:{
327        A a;
328        break;
329      }
330      default:
331        return;
332      }
333      return;
334    } catch (int x) {
335    }
336    return;
337  }
338
339  void test1(int x) {
340    A y;
341    try {
342      switch (x) {
343      default: break;
344      }
345    } catch (int x) {}
346  }
347}
348
349// rdar://problem/8231514
350namespace test14 {
351  struct A { ~A(); };
352  struct B { ~B(); };
353
354  B b();
355  void opaque();
356
357  void foo() {
358    A a;
359    try {
360      B str = b();
361      opaque();
362    } catch (int x) {
363    }
364  }
365}
366
367// rdar://problem/8231514
368// JumpDests shouldn't get confused by scopes that aren't normal cleanups.
369namespace test15 {
370  struct A { ~A(); };
371
372  bool opaque(int);
373
374  // CHECK: define void @_ZN6test153fooEv()
375  void foo() {
376    A a;
377
378    try {
379      // CHECK:      [[X:%.*]] = alloca i32
380      // CHECK:      store i32 10, i32* [[X]]
381      // CHECK-NEXT: br label
382      //   -> while.cond
383      int x = 10;
384
385      while (true) {
386        // CHECK:      load i32* [[X]]
387        // CHECK-NEXT: [[COND:%.*]] = invoke zeroext i1 @_ZN6test156opaqueEi
388        // CHECK:      br i1 [[COND]]
389        if (opaque(x))
390        // CHECK:      br label
391          break;
392
393        // CHECK:      br label
394      }
395      // CHECK:      br label
396    } catch (int x) { }
397
398    // CHECK: call void @_ZN6test151AD1Ev
399  }
400}
401
402namespace test16 {
403  struct A { A(); ~A() noexcept(false); };
404  struct B { int x; B(const A &); ~B() noexcept(false); };
405  void foo();
406  bool cond();
407
408  // CHECK: define void @_ZN6test163barEv()
409  void bar() {
410    // CHECK:      [[EXN_SAVE:%.*]] = alloca i8*
411    // CHECK-NEXT: [[EXN_ACTIVE:%.*]] = alloca i1
412    // CHECK-NEXT: [[TEMP:%.*]] = alloca [[A:%.*]],
413    // CHECK-NEXT: [[EXNSLOT:%.*]] = alloca i8*
414    // CHECK-NEXT: [[SELECTORSLOT:%.*]] = alloca i32
415    // CHECK-NEXT: [[TEMP_ACTIVE:%.*]] = alloca i1
416
417    cond() ? throw B(A()) : foo();
418
419    // CHECK-NEXT: [[COND:%.*]] = call zeroext i1 @_ZN6test164condEv()
420    // CHECK-NEXT: store i1 false, i1* [[EXN_ACTIVE]]
421    // CHECK-NEXT: store i1 false, i1* [[TEMP_ACTIVE]]
422    // CHECK-NEXT: br i1 [[COND]],
423
424    // CHECK:      [[EXN:%.*]] = call i8* @__cxa_allocate_exception(i64 4)
425    // CHECK-NEXT: store i8* [[EXN]], i8** [[EXN_SAVE]]
426    // CHECK-NEXT: store i1 true, i1* [[EXN_ACTIVE]]
427    // CHECK-NEXT: [[T0:%.*]] = bitcast i8* [[EXN]] to [[B:%.*]]*
428    // CHECK-NEXT: invoke void @_ZN6test161AC1Ev([[A]]* [[TEMP]])
429    // CHECK:      store i1 true, i1* [[TEMP_ACTIVE]]
430    // CHECK-NEXT: invoke void @_ZN6test161BC1ERKNS_1AE([[B]]* [[T0]], [[A]]* [[TEMP]])
431    // CHECK:      store i1 false, i1* [[EXN_ACTIVE]]
432    // CHECK-NEXT: invoke void @__cxa_throw(i8* [[EXN]],
433
434    // CHECK:      invoke void @_ZN6test163fooEv()
435    // CHECK:      br label
436
437    // CHECK:      invoke void @_ZN6test161AD1Ev([[A]]* [[TEMP]])
438    // CHECK:      ret void
439
440    // CHECK:      [[T0:%.*]] = load i1* [[EXN_ACTIVE]]
441    // CHECK-NEXT: br i1 [[T0]]
442    // CHECK:      [[T1:%.*]] = load i8** [[EXN_SAVE]]
443    // CHECK-NEXT: call void @__cxa_free_exception(i8* [[T1]])
444    // CHECK-NEXT: br label
445  }
446}
447
448// CHECK: attributes [[NUW]] = { nounwind }
449// CHECK: attributes [[NR]] = { noreturn }
450