xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/microsoft-abi-exceptions.cpp (revision 5ba302fdeaa9e153d58b5dcaef42d660aedee92e)
1 // RUN: %clang_cc1 -emit-llvm %s -o - -triple=i386-pc-win32 -mconstructor-aliases -cxx-abi microsoft -fexceptions -fno-rtti | FileCheck -check-prefix WIN32 %s
2 
3 struct A {
4   A();
5   ~A();
6   int a;
7 };
8 
9 A getA();
10 
11 int TakesTwo(A a, A b);
12 void HasEHCleanup() {
13   TakesTwo(getA(), getA());
14 }
15 
16 // With exceptions, we need to clean up at least one of these temporaries.
17 // WIN32: define void @"\01?HasEHCleanup@@YAXXZ"() {{.*}} {
18 //    First one doesn't have any cleanups, no need for invoke.
19 // WIN32:   call void @"\01?getA@@YA?AUA@@XZ"(%struct.A* sret %{{.*}})
20 //    If this call throws, we have to cleanup the first temporary.
21 // WIN32:   invoke void @"\01?getA@@YA?AUA@@XZ"(%struct.A* sret %{{.*}})
22 //    If this call throws, we already popped our cleanups
23 // WIN32:   call i32 @"\01?TakesTwo@@YAHUA@@0@Z"
24 // WIN32:   ret void
25 //
26 //    There should be one dtor call for unwinding from the second getA.
27 // WIN32:   invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"
28 // WIN32: }
29 
30 void TakeRef(const A &a);
31 int HasDeactivatedCleanups() {
32   return TakesTwo((TakeRef(A()), A()), (TakeRef(A()), A()));
33 }
34 
35 // WIN32: define i32 @"\01?HasDeactivatedCleanups@@YAHXZ"() {{.*}} {
36 // WIN32:   %[[isactive:.*]] = alloca i1
37 // WIN32:   call x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"
38 // WIN32:   invoke void @"\01?TakeRef@@YAXABUA@@@Z"
39 // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"(%struct.A* %[[arg1:.*]])
40 // WIN32:   store i1 true, i1* %[[isactive]]
41 // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"
42 // WIN32:   invoke void @"\01?TakeRef@@YAXABUA@@@Z"
43 // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"
44 // WIN32:   store i1 false, i1* %[[isactive]]
45 // WIN32:   invoke i32 @"\01?TakesTwo@@YAHUA@@0@Z"
46 //        Destroy the two const ref temporaries.
47 // WIN32:   invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"
48 // WIN32:   call x86_thiscallcc void @"\01??1A@@QAE@XZ"
49 // WIN32:   ret i32
50 //
51 //        Conditionally destroy arg1.
52 // WIN32:   %[[cond:.*]] = load i1* %[[isactive]]
53 // WIN32:   br i1 %[[cond]]
54 // WIN32:   invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %[[arg1]])
55 // WIN32: }
56 
57 // Test putting the cleanups inside a conditional.
58 int CouldThrow();
59 int HasConditionalCleanup(bool cond) {
60   return (cond ? TakesTwo(A(), A()) : CouldThrow());
61 }
62 
63 // WIN32: define i32 @"\01?HasConditionalCleanup@@YAH_N@Z"(i1 zeroext %{{.*}}) {{.*}} {
64 // WIN32:   store i1 false
65 // WIN32:   br i1
66 //        No cleanups, so we call and then activate a cleanup if it succeeds.
67 // WIN32:   call x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"(%struct.A* %[[arg1:.*]])
68 // WIN32:   store i1 true
69 //        Now we have a cleanup for the first aggregate, so we invoke.
70 // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"(%struct.A* %{{.*}})
71 //        Now we have no cleanups because TakeTwo will destruct both args.
72 // WIN32:   call i32 @"\01?TakesTwo@@YAHUA@@0@Z"
73 //        Still no cleanups, so call.
74 // WIN32:   call i32 @"\01?CouldThrow@@YAHXZ"()
75 //        Somewhere in the landing pad for our single invoke, call the dtor.
76 // WIN32:   invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %[[arg1]])
77 // WIN32: }
78 
79 // Now test both.
80 int HasConditionalDeactivatedCleanups(bool cond) {
81   return (cond ? TakesTwo((TakeRef(A()), A()), (TakeRef(A()), A())) : CouldThrow());
82 }
83 
84 // WIN32: define i32 @"\01?HasConditionalDeactivatedCleanups@@YAH_N@Z"{{.*}} {
85 // WIN32:   %[[arg1:.*]] = alloca %struct.A, align 4
86 // WIN32:   alloca i1
87 // WIN32:   %[[arg1_cond:.*]] = alloca i1
88 //        Start all four cleanups as deactivated.
89 // WIN32:   store i1 false
90 // WIN32:   store i1 false
91 // WIN32:   store i1 false
92 // WIN32:   store i1 false
93 // WIN32:   br i1
94 //        True condition.
95 // WIN32:   call x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"
96 // WIN32:   store i1 true
97 // WIN32:   invoke void @"\01?TakeRef@@YAXABUA@@@Z"
98 // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"(%struct.A* %[[arg1]])
99 // WIN32:   store i1 true, i1* %[[arg1_cond]]
100 // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"
101 // WIN32:   store i1 true
102 // WIN32:   invoke void @"\01?TakeRef@@YAXABUA@@@Z"
103 // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"
104 // WIN32:   store i1 true
105 // WIN32:   store i1 false, i1* %[[arg1_cond]]
106 // WIN32:   invoke i32 @"\01?TakesTwo@@YAHUA@@0@Z"
107 //        False condition.
108 // WIN32:   invoke i32 @"\01?CouldThrow@@YAHXZ"()
109 //        Two normal cleanups for TakeRef args.
110 // WIN32:   invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"
111 // WIN32:   call x86_thiscallcc void @"\01??1A@@QAE@XZ"
112 // WIN32:   ret i32
113 //
114 //        Somewhere in the landing pad soup, we conditionally destroy arg1.
115 // WIN32:   %[[isactive:.*]] = load i1* %[[arg1_cond]]
116 // WIN32:   br i1 %[[isactive]]
117 // WIN32:   invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %[[arg1]])
118 // WIN32: }
119 
120 namespace crash_on_partial_destroy {
121 struct A {
122   virtual ~A();
123 };
124 
125 struct B : virtual A {
126   // Has an implicit destructor.
127 };
128 
129 struct C : B {
130   C();
131 };
132 
133 void foo();
134 // We used to crash when emitting this.
135 C::C() { foo(); }
136 
137 // Verify that we don't bother with a vbtable lookup when adjusting the this
138 // pointer to call a base destructor from a constructor while unwinding.
139 // WIN32-LABEL: define {{.*}} @"\01??0C@crash_on_partial_destroy@@QAE@XZ"{{.*}} {
140 // WIN32:      landingpad
141 //
142 //        We shouldn't do any vbptr loads, just constant GEPs.
143 // WIN32-NOT:  load
144 // WIN32:      getelementptr i8* %{{.*}}, i32 4
145 // WIN32-NOT:  load
146 // WIN32:      bitcast i8* %{{.*}} to %"struct.crash_on_partial_destroy::B"*
147 // WIN32:      invoke x86_thiscallcc void @"\01??1B@crash_on_partial_destroy@@UAE@XZ"
148 //
149 // WIN32-NOT:  load
150 // WIN32:      bitcast %"struct.crash_on_partial_destroy::C"* %{{.*}} to i8*
151 // WIN32-NOT:  load
152 // WIN32:      getelementptr inbounds i8* %{{.*}}, i64 4
153 // WIN32-NOT:  load
154 // WIN32:      bitcast i8* %{{.*}} to %"struct.crash_on_partial_destroy::A"*
155 // WIN32:      invoke x86_thiscallcc void @"\01??1A@crash_on_partial_destroy@@UAE@XZ"
156 // WIN32: }
157 }
158