blob: ac1321efeae2aa3ab066d6f55f3b373401bd4cf9 [file] [log] [blame]
Reid Kleckner10aa7702015-09-16 20:15:55 +00001// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple=x86_64-pc-windows-msvc \
David Majnemer4ff6f732016-02-20 09:23:41 +00002// RUN: -mconstructor-aliases -fexceptions -fcxx-exceptions \
Reid Kleckner1f88e932015-10-07 21:03:41 +00003// RUN: -O1 -disable-llvm-optzns \
Reid Kleckner10aa7702015-09-16 20:15:55 +00004// RUN: | FileCheck -check-prefix WIN64 %s
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00005
6extern "C" void might_throw();
7
8// Simplify the generated IR with noexcept.
9extern "C" void recover() noexcept(true);
10extern "C" void handle_exception(void *e) noexcept(true);
11
12extern "C" void catch_all() {
13 try {
14 might_throw();
15 } catch (...) {
16 recover();
17 }
18}
19
20// WIN64-LABEL: define void @catch_all()
21// WIN64: invoke void @might_throw()
David Majnemer4e52d6f2015-12-12 05:39:21 +000022// WIN64-NEXT: to label %[[cont:[^ ]*]] unwind label %[[catchswitch_lpad:[^ ]*]]
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000023//
David Majnemer4e52d6f2015-12-12 05:39:21 +000024// WIN64: [[catchswitch_lpad]]
25// WIN64: %[[catchswitch:[^ ]*]] = catchswitch within none [label %[[catchpad_lpad:[^ ]*]]] unwind to caller
Reid Kleckner10aa7702015-09-16 20:15:55 +000026//
David Majnemer4e52d6f2015-12-12 05:39:21 +000027// WIN64: [[catchpad_lpad]]
28// WIN64: catchpad within %[[catchswitch]] [i8* null, i32 64, i8* null]
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000029// WIN64: call void @recover()
David Majnemer4e52d6f2015-12-12 05:39:21 +000030// WIN64: catchret from %{{.*}} to label %[[catchret:[^ ]*]]
Reid Kleckner10aa7702015-09-16 20:15:55 +000031//
32// WIN64: [[catchret]]
33// WIN64-NEXT: br label %[[ret:[^ ]*]]
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000034//
35// WIN64: [[ret]]
36// WIN64: ret void
Reid Kleckner10aa7702015-09-16 20:15:55 +000037//
38// WIN64: [[cont]]
39// WIN64: br label %[[ret]]
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000040
41extern "C" void catch_int() {
42 try {
43 might_throw();
44 } catch (int e) {
45 handle_exception(&e);
46 }
47}
48
49// WIN64-LABEL: define void @catch_int()
David Majnemer4e52d6f2015-12-12 05:39:21 +000050// WIN64: catchpad within %{{[^ ]*}} [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i32* %[[e_addr:[^\]]*]]]
Reid Kleckner1f88e932015-10-07 21:03:41 +000051//
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//
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000057// WIN64: %[[e_i8:[^ ]*]] = bitcast i32* %[[e_addr]] to i8*
Reid Kleckner1f88e932015-10-07 21:03:41 +000058// WIN64-NOT: lifetime.start
59// WIN64: call void @handle_exception
60// WIN64-SAME: (i8* %[[e_i8]])
61// WIN64-NOT: lifetime.end
Reid Kleckner10aa7702015-09-16 20:15:55 +000062// WIN64: catchret
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000063
Reid Kleckner67cf0352015-04-07 00:09:59 +000064extern "C" void catch_int_unnamed() {
65 try {
66 might_throw();
67 } catch (int) {
68 }
69}
70
71// WIN64-LABEL: define void @catch_int_unnamed()
David Majnemer4e52d6f2015-12-12 05:39:21 +000072// WIN64: catchpad within %{{.*}} [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i8* null]
Reid Kleckner10aa7702015-09-16 20:15:55 +000073// WIN64: catchret
Reid Kleckner67cf0352015-04-07 00:09:59 +000074
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000075struct A {
76 A();
77 A(const A &o);
78 ~A();
79 int a;
80};
81
82struct B : A {
83 B();
84 B(const B &o);
85 ~B();
86 int b;
87};
88
89extern "C" void catch_a_byval() {
90 try {
91 might_throw();
92 } catch (A e) {
93 handle_exception(&e);
94 }
95}
96
97// WIN64-LABEL: define void @catch_a_byval()
98// WIN64: %[[e_addr:[^ ]*]] = alloca %struct.A
David Majnemer4e52d6f2015-12-12 05:39:21 +000099// WIN64: catchpad within %{{[^ ]*}} [%rtti.TypeDescriptor7* @"\01??_R0?AUA@@@8", i32 0, %struct.A* %[[e_addr]]]
Reid Klecknerfff8e7f2015-03-03 19:21:04 +0000100// WIN64: %[[e_i8:[^ ]*]] = bitcast %struct.A* %[[e_addr]] to i8*
101// WIN64: call void @handle_exception(i8* %[[e_i8]])
Reid Kleckner10aa7702015-09-16 20:15:55 +0000102// WIN64: catchret
Reid Klecknerfff8e7f2015-03-03 19:21:04 +0000103
104extern "C" void catch_a_ref() {
105 try {
106 might_throw();
107 } catch (A &e) {
108 handle_exception(&e);
109 }
110}
111
112// WIN64-LABEL: define void @catch_a_ref()
113// WIN64: %[[e_addr:[^ ]*]] = alloca %struct.A*
David Majnemer4e52d6f2015-12-12 05:39:21 +0000114// WIN64: catchpad within %{{[^ ]*}} [%rtti.TypeDescriptor7* @"\01??_R0?AUA@@@8", i32 8, %struct.A** %[[e_addr]]]
Reid Klecknerfff8e7f2015-03-03 19:21:04 +0000115// WIN64: %[[eptr:[^ ]*]] = load %struct.A*, %struct.A** %[[e_addr]]
116// WIN64: %[[eptr_i8:[^ ]*]] = bitcast %struct.A* %[[eptr]] to i8*
117// WIN64: call void @handle_exception(i8* %[[eptr_i8]])
Reid Kleckner10aa7702015-09-16 20:15:55 +0000118// WIN64: catchret
David Majnemer1f192e22015-04-01 04:45:52 +0000119
120extern "C" void fn_with_exc_spec() throw(int) {
121 might_throw();
122}
123
124// WIN64-LABEL: define void @fn_with_exc_spec()
125// WIN64: call void @might_throw()
126// WIN64-NEXT: ret void
Reid Kleckner7d4bc9c2015-04-22 23:39:15 +0000127
128extern "C" void catch_nested() {
129 try {
130 might_throw();
131 } catch (int) {
132 try {
133 might_throw();
134 } catch (int) {
135 might_throw();
136 }
137 }
138}
139
140// WIN64-LABEL: define void @catch_nested()
141// WIN64: invoke void @might_throw()
David Majnemer4e52d6f2015-12-12 05:39:21 +0000142// WIN64-NEXT: to label %{{.*}} unwind label %[[catchswitch_outer:[^ ]*]]
Reid Kleckner7d4bc9c2015-04-22 23:39:15 +0000143//
David Majnemer4e52d6f2015-12-12 05:39:21 +0000144// WIN64: [[catchswitch_outer]]
145// WIN64: %[[catchswitch_outer_scope:[^ ]*]] = catchswitch within none [label %[[catch_int_outer:[^ ]*]]] unwind to caller
Reid Kleckner10aa7702015-09-16 20:15:55 +0000146//
David Majnemer4e52d6f2015-12-12 05:39:21 +0000147// WIN64: [[catch_int_outer]]
148// WIN64: %[[catchpad:[^ ]*]] = catchpad within %[[catchswitch_outer_scope]] [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i8* null]
Reid Kleckner7d4bc9c2015-04-22 23:39:15 +0000149// WIN64: invoke void @might_throw()
David Majnemer4e52d6f2015-12-12 05:39:21 +0000150// WIN64-NEXT: to label %[[cont2:[^ ]*]] unwind label %[[catchswitch_inner:[^ ]*]]
Reid Kleckner7d4bc9c2015-04-22 23:39:15 +0000151//
David Majnemer4e52d6f2015-12-12 05:39:21 +0000152// WIN64: [[catchswitch_inner]]
153// WIN64: %[[catchswitch_inner_scope:[^ ]*]] = catchswitch within %[[catchpad]] [label %[[catch_int_inner:[^ ]*]]] unwind to caller
Reid Kleckner7d4bc9c2015-04-22 23:39:15 +0000154//
David Majnemer4e52d6f2015-12-12 05:39:21 +0000155// WIN64: [[catch_int_inner]]
156// WIN64: catchpad within %[[catchswitch_inner_scope]] [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i8* null]
157// WIN64-NEXT: call void @might_throw()
Reid Kleckner10aa7702015-09-16 20:15:55 +0000158// WIN64: catchret {{.*}} to label %[[catchret2:[^ ]*]]
159//
160// WIN64: [[catchret2]]
161// WIN64: catchret {{.*}} to label %[[mainret:[^ ]*]]
162//
163// WIN64: [[mainret]]
164// WIN64: ret void