xref: /llvm-project/clang/test/CodeGenCXX/microsoft-abi-eh-catch.cpp (revision c2985a330cd5c418488cbd87bd7a2922d6a8f7da)
1 // RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple=x86_64-pc-windows-msvc \
2 // RUN:     -mconstructor-aliases -fexceptions -fcxx-exceptions \
3 // RUN:     -O1 -disable-llvm-passes \
4 // RUN:     | FileCheck -check-prefix WIN64 %s
5 
6 extern "C" void might_throw();
7 
8 // Simplify the generated IR with noexcept.
9 extern "C" void recover() noexcept(true);
10 extern "C" void handle_exception(void *e) noexcept(true);
11 
catch_all()12 extern "C" void catch_all() {
13   try {
14     might_throw();
15   } catch (...) {
16     recover();
17   }
18 }
19 
20 // WIN64-LABEL: define dso_local void @catch_all()
21 // WIN64: invoke void @might_throw()
22 // WIN64-NEXT: to label %[[cont:[^ ]*]] unwind label %[[catchswitch_lpad:[^ ]*]]
23 //
24 // WIN64: [[catchswitch_lpad]]
25 // WIN64: %[[catchswitch:[^ ]*]] = catchswitch within none [label %[[catchpad_lpad:[^ ]*]]] unwind to caller
26 //
27 // WIN64: [[catchpad_lpad]]
28 // WIN64: catchpad within %[[catchswitch]] [ptr null, i32 64, ptr null]
29 // WIN64: call void @recover()
30 // WIN64: catchret from %{{.*}} to label %[[catchret:[^ ]*]]
31 //
32 // WIN64: [[catchret]]
33 // WIN64-NEXT: br label %[[ret:[^ ]*]]
34 //
35 // WIN64: [[ret]]
36 // WIN64: ret void
37 //
38 // WIN64: [[cont]]
39 // WIN64: br label %[[ret]]
40 
catch_int()41 extern "C" void catch_int() {
42   try {
43     might_throw();
44   } catch (int e) {
45     handle_exception(&e);
46   }
47 }
48 
49 // WIN64-LABEL: define dso_local void @catch_int()
50 // WIN64: catchpad within %{{[^ ]*}} [ptr @"??_R0H@8", i32 0, ptr %[[e_addr:.*]]]
51 //
52 // The catchpad instruction starts the lifetime of 'e'. Unfortunately, that
53 // leaves us with nowhere to put lifetime.start, so we don't emit lifetime
54 // markers for now.
55 // WIN64-NOT: lifetime.start
56 //
57 // WIN64-NOT: lifetime.start
58 // WIN64: call void @handle_exception
59 // WIN64-SAME: (ptr noundef %[[e_addr]])
60 // WIN64-NOT: lifetime.end
61 // WIN64: catchret
62 
catch_int_unnamed()63 extern "C" void catch_int_unnamed() {
64   try {
65     might_throw();
66   } catch (int) {
67   }
68 }
69 
70 // WIN64-LABEL: define dso_local void @catch_int_unnamed()
71 // WIN64: catchpad within %{{.*}} [ptr @"??_R0H@8", i32 0, ptr null]
72 // WIN64: catchret
73 
74 struct A {
75   A();
76   A(const A &o);
77   ~A();
78   int a;
79 };
80 
81 struct B : A {
82   B();
83   B(const B &o);
84   ~B();
85   int b;
86 };
87 
catch_a_byval()88 extern "C" void catch_a_byval() {
89   try {
90     might_throw();
91   } catch (A e) {
92     handle_exception(&e);
93   }
94 }
95 
96 // WIN64-LABEL: define dso_local void @catch_a_byval()
97 // WIN64: %[[e_addr:[^ ]*]] = alloca %struct.A
98 // WIN64: catchpad within %{{[^ ]*}} [ptr @"??_R0?AUA@@@8", i32 0, ptr %[[e_addr]]]
99 // WIN64: call void @handle_exception(ptr noundef %[[e_addr]])
100 // WIN64: catchret
101 
catch_a_ref()102 extern "C" void catch_a_ref() {
103   try {
104     might_throw();
105   } catch (A &e) {
106     handle_exception(&e);
107   }
108 }
109 
110 // WIN64-LABEL: define dso_local void @catch_a_ref()
111 // WIN64: %[[e_addr:[^ ]*]] = alloca ptr
112 // WIN64: catchpad within %{{[^ ]*}} [ptr @"??_R0?AUA@@@8", i32 8, ptr %[[e_addr]]]
113 // WIN64: %[[eptr:[^ ]*]] = load ptr, ptr %[[e_addr]]
114 // WIN64: call void @handle_exception(ptr noundef %[[eptr]])
115 // WIN64: catchret
116 
fn_with_exc_spec()117 extern "C" void fn_with_exc_spec() throw(int) {
118   might_throw();
119 }
120 
121 // WIN64-LABEL: define dso_local void @fn_with_exc_spec()
122 // WIN64: call void @might_throw()
123 // WIN64-NEXT: ret void
124 
catch_nested()125 extern "C" void catch_nested() {
126   try {
127     might_throw();
128   } catch (int) {
129     try {
130       might_throw();
131     } catch (int) {
132       might_throw();
133     }
134   }
135 }
136 
137 // WIN64-LABEL: define dso_local void @catch_nested()
138 // WIN64: invoke void @might_throw()
139 // WIN64-NEXT: to label %{{.*}} unwind label %[[catchswitch_outer:[^ ]*]]
140 //
141 // WIN64: [[catchswitch_outer]]
142 // WIN64: %[[catchswitch_outer_scope:[^ ]*]] = catchswitch within none [label %[[catch_int_outer:[^ ]*]]] unwind to caller
143 //
144 // WIN64: [[catch_int_outer]]
145 // WIN64: %[[catchpad:[^ ]*]] = catchpad within %[[catchswitch_outer_scope]] [ptr @"??_R0H@8", i32 0, ptr null]
146 // WIN64: invoke void @might_throw()
147 // WIN64-NEXT: to label %[[cont2:[^ ]*]] unwind label %[[catchswitch_inner:[^ ]*]]
148 //
149 // WIN64: [[catchswitch_inner]]
150 // WIN64: %[[catchswitch_inner_scope:[^ ]*]] = catchswitch within %[[catchpad]] [label %[[catch_int_inner:[^ ]*]]] unwind to caller
151 //
152 // WIN64: [[catch_int_inner]]
153 // WIN64: catchpad within %[[catchswitch_inner_scope]] [ptr @"??_R0H@8", i32 0, ptr null]
154 // WIN64-NEXT: call void @might_throw()
155 // WIN64: catchret {{.*}} to label %[[catchret2:[^ ]*]]
156 //
157 // WIN64: [[catchret2]]
158 // WIN64: catchret {{.*}} to label %[[mainret:[^ ]*]]
159 //
160 // WIN64: [[mainret]]
161 // WIN64: ret void
162