xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/throw-expressions.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -Wno-unreachable-code -Werror -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc int val = 42;
test1()4f4a2713aSLionel Sambuc int& test1() {
5f4a2713aSLionel Sambuc   return throw val, val;
6f4a2713aSLionel Sambuc }
7f4a2713aSLionel Sambuc 
test2()8f4a2713aSLionel Sambuc int test2() {
9f4a2713aSLionel Sambuc   return val ? throw val : val;
10f4a2713aSLionel Sambuc }
11f4a2713aSLionel Sambuc 
12f4a2713aSLionel Sambuc // rdar://problem/8608801
test3()13f4a2713aSLionel Sambuc void test3() {
14f4a2713aSLionel Sambuc   throw false;
15f4a2713aSLionel Sambuc }
16f4a2713aSLionel Sambuc 
17f4a2713aSLionel Sambuc // PR10582
test4()18f4a2713aSLionel Sambuc int test4() {
19f4a2713aSLionel Sambuc   return 1 ? throw val : val;
20f4a2713aSLionel Sambuc }
21f4a2713aSLionel Sambuc 
22f4a2713aSLionel Sambuc // PR15923
test5(bool x,bool y,int z)23f4a2713aSLionel Sambuc int test5(bool x, bool y, int z) {
24f4a2713aSLionel Sambuc   return (x ? throw 1 : y) ? z : throw 2;
25f4a2713aSLionel Sambuc }
26f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @_Z5test5bbi(
27f4a2713aSLionel Sambuc // CHECK: br i1
28f4a2713aSLionel Sambuc //
29f4a2713aSLionel Sambuc // x.true:
30f4a2713aSLionel Sambuc // CHECK: call void @__cxa_throw(
31f4a2713aSLionel Sambuc // CHECK-NEXT: unreachable
32f4a2713aSLionel Sambuc //
33f4a2713aSLionel Sambuc // x.false:
34f4a2713aSLionel Sambuc // CHECK: br i1
35f4a2713aSLionel Sambuc //
36f4a2713aSLionel Sambuc // y.true:
37f4a2713aSLionel Sambuc // CHECK: load i32*
38f4a2713aSLionel Sambuc // CHECK: br label
39f4a2713aSLionel Sambuc //
40f4a2713aSLionel Sambuc // y.false:
41f4a2713aSLionel Sambuc // CHECK: call void @__cxa_throw(
42f4a2713aSLionel Sambuc // CHECK-NEXT: unreachable
43f4a2713aSLionel Sambuc //
44f4a2713aSLionel Sambuc // end:
45f4a2713aSLionel Sambuc // CHECK: ret i32
46f4a2713aSLionel Sambuc 
test6(bool x,bool y,int z)47f4a2713aSLionel Sambuc int test6(bool x, bool y, int z) {
48f4a2713aSLionel Sambuc   return (x ? throw 1 : y) ? z : (throw 2);
49f4a2713aSLionel Sambuc }
50f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @_Z5test6bbi(
51f4a2713aSLionel Sambuc // CHECK: br i1
52f4a2713aSLionel Sambuc //
53f4a2713aSLionel Sambuc // x.true:
54f4a2713aSLionel Sambuc // CHECK: call void @__cxa_throw(
55f4a2713aSLionel Sambuc // CHECK-NEXT: unreachable
56f4a2713aSLionel Sambuc //
57f4a2713aSLionel Sambuc // x.false:
58f4a2713aSLionel Sambuc // CHECK: br i1
59f4a2713aSLionel Sambuc //
60f4a2713aSLionel Sambuc // y.true:
61f4a2713aSLionel Sambuc // CHECK: load i32*
62f4a2713aSLionel Sambuc // CHECK: br label
63f4a2713aSLionel Sambuc //
64f4a2713aSLionel Sambuc // y.false:
65f4a2713aSLionel Sambuc // CHECK: call void @__cxa_throw(
66f4a2713aSLionel Sambuc // CHECK-NEXT: unreachable
67f4a2713aSLionel Sambuc //
68f4a2713aSLionel Sambuc // end:
69f4a2713aSLionel Sambuc // CHECK: ret i32
70*0a6a1f1dSLionel Sambuc 
71*0a6a1f1dSLionel Sambuc namespace DR1560 {
72*0a6a1f1dSLionel Sambuc   struct A {
73*0a6a1f1dSLionel Sambuc     ~A();
74*0a6a1f1dSLionel Sambuc   };
75*0a6a1f1dSLionel Sambuc   extern bool b;
76*0a6a1f1dSLionel Sambuc   A get();
77*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @_ZN6DR15601bE
78*0a6a1f1dSLionel Sambuc   const A &r = b ? get() : throw 0;
79*0a6a1f1dSLionel Sambuc   // CHECK-NOT: call {{.*}}@_ZN6DR15601AD1Ev
80*0a6a1f1dSLionel Sambuc   // CHECK: call {{.*}} @__cxa_atexit({{.*}} @_ZN6DR15601AD1Ev {{.*}} @_ZGRN6DR15601rE
81*0a6a1f1dSLionel Sambuc   // CHECK-NOT: call {{.*}}@_ZN6DR15601AD1Ev
82*0a6a1f1dSLionel Sambuc }
83*0a6a1f1dSLionel Sambuc 
84*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define void @_Z5test7b(
test7(bool cond)85*0a6a1f1dSLionel Sambuc void test7(bool cond) {
86*0a6a1f1dSLionel Sambuc   // CHECK: br i1
87*0a6a1f1dSLionel Sambuc   //
88*0a6a1f1dSLionel Sambuc   // x.true:
89*0a6a1f1dSLionel Sambuc   // CHECK: call void @__cxa_throw(
90*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: unreachable
91*0a6a1f1dSLionel Sambuc   //
92*0a6a1f1dSLionel Sambuc   // x.false:
93*0a6a1f1dSLionel Sambuc   // CHECK: br label
94*0a6a1f1dSLionel Sambuc   //
95*0a6a1f1dSLionel Sambuc   // end:
96*0a6a1f1dSLionel Sambuc   // CHECK: ret void
97*0a6a1f1dSLionel Sambuc   cond ? throw test7 : val;
98*0a6a1f1dSLionel Sambuc }
99*0a6a1f1dSLionel Sambuc 
100*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define dereferenceable(4) i32* @_Z5test8b(
test8(bool cond)101*0a6a1f1dSLionel Sambuc int &test8(bool cond) {
102*0a6a1f1dSLionel Sambuc   // CHECK: br i1
103*0a6a1f1dSLionel Sambuc   //
104*0a6a1f1dSLionel Sambuc   // x.true:
105*0a6a1f1dSLionel Sambuc   // CHECK: br label
106*0a6a1f1dSLionel Sambuc   //
107*0a6a1f1dSLionel Sambuc   // x.false:
108*0a6a1f1dSLionel Sambuc   // CHECK: call void @__cxa_throw(
109*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: unreachable
110*0a6a1f1dSLionel Sambuc   //
111*0a6a1f1dSLionel Sambuc   // end:
112*0a6a1f1dSLionel Sambuc   // CHECK: ret i32* @val
113*0a6a1f1dSLionel Sambuc   return cond ? val : ((throw "foo"));
114*0a6a1f1dSLionel Sambuc }
115