blob: 9d4a1c5e9086ec4a673e5caadcf10f5e74c8f11e [file] [log] [blame]
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001// RUN: %clang_cc1 -emit-llvm %s -o - -cxx-abi microsoft -triple=i386-pc-win32 -fno-rtti > %t 2>&1
2// RUN: FileCheck %s < %t
3// Using a different check prefix as the inline destructors might be placed
4// anywhere in the output.
5// RUN: FileCheck --check-prefix=DTORS %s < %t
Timur Iskhodzhanov85607912012-04-20 08:05:00 +00006
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00007namespace basic {
8
Timur Iskhodzhanov85607912012-04-20 08:05:00 +00009class A {
10 public:
11 A() { }
12 ~A() { }
13};
14
Timur Iskhodzhanov6c9dccd2013-02-12 13:22:47 +000015void no_constructor_destructor_infinite_recursion() {
Timur Iskhodzhanov85607912012-04-20 08:05:00 +000016 A a;
17
Stephen Lin3258abc2013-06-19 23:23:19 +000018// CHECK: define linkonce_odr x86_thiscallcc %"class.basic::A"* @"\01??0A@basic@@QAE@XZ"(%"class.basic::A"* %this)
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000019// CHECK: [[THIS_ADDR:%[.0-9A-Z_a-z]+]] = alloca %"class.basic::A"*, align 4
20// CHECK-NEXT: store %"class.basic::A"* %this, %"class.basic::A"** [[THIS_ADDR]], align 4
21// CHECK-NEXT: [[T1:%[.0-9A-Z_a-z]+]] = load %"class.basic::A"** [[THIS_ADDR]]
22// CHECK-NEXT: ret %"class.basic::A"* [[T1]]
John McCallbd315742012-09-25 08:00:39 +000023// CHECK-NEXT: }
Timur Iskhodzhanov85607912012-04-20 08:05:00 +000024
25// Make sure that the destructor doesn't call itself:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000026// CHECK: define {{.*}} @"\01??1A@basic@@QAE@XZ"
27// CHECK-NOT: call void @"\01??1A@basic@@QAE@XZ"
Timur Iskhodzhanov85607912012-04-20 08:05:00 +000028// CHECK: ret
29}
John McCallbd315742012-09-25 08:00:39 +000030
Timur Iskhodzhanov649c7312013-01-21 13:02:41 +000031struct B {
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000032 B();
33};
34
35// Tests that we can define constructors outside the class (PR12784).
36B::B() {
Stephen Lin3258abc2013-06-19 23:23:19 +000037 // CHECK: define x86_thiscallcc %"struct.basic::B"* @"\01??0B@basic@@QAE@XZ"(%"struct.basic::B"* %this)
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000038 // CHECK: ret
39}
40
41struct C {
42 virtual ~C() {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000043// Complete destructor first:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000044// DTORS: define {{.*}} x86_thiscallcc void @"\01??1C@basic@@UAE@XZ"(%"struct.basic::C"* %this)
Timur Iskhodzhanov47453452013-02-13 12:14:25 +000045
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000046// Then, the scalar deleting destructor (used in the vtable):
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +000047// FIXME: add a test that verifies that the out-of-line scalar deleting
48// destructor is linkonce_odr too.
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000049// DTORS: define linkonce_odr x86_thiscallcc void @"\01??_GC@basic@@UAEPAXI@Z"(%"struct.basic::C"* %this, i1 zeroext %should_call_delete)
Timur Iskhodzhanov47453452013-02-13 12:14:25 +000050// DTORS: %[[FROMBOOL:[0-9a-z]+]] = zext i1 %should_call_delete to i8
Timur Iskhodzhanovba8fa0c2013-02-13 12:24:19 +000051// DTORS-NEXT: store i8 %[[FROMBOOL]], i8* %[[SHOULD_DELETE_VAR:[0-9a-z._]+]], align 1
52// DTORS: %[[SHOULD_DELETE_VALUE:[0-9a-z._]+]] = load i8* %[[SHOULD_DELETE_VAR]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000053// DTORS: call x86_thiscallcc void @"\01??1C@basic@@UAE@XZ"(%"struct.basic::C"* %[[THIS:[0-9a-z]+]])
Timur Iskhodzhanov47453452013-02-13 12:14:25 +000054// DTORS-NEXT: %[[CONDITION:[0-9]+]] = icmp eq i8 %[[SHOULD_DELETE_VALUE]], 0
55// DTORS-NEXT: br i1 %[[CONDITION]], label %[[CONTINUE_LABEL:[0-9a-z._]+]], label %[[CALL_DELETE_LABEL:[0-9a-z._]+]]
56//
57// DTORS: [[CALL_DELETE_LABEL]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000058// DTORS-NEXT: %[[THIS_AS_VOID:[0-9a-z]+]] = bitcast %"struct.basic::C"* %[[THIS]] to i8*
Bill Wendling4e1125f2013-02-22 09:10:20 +000059// DTORS-NEXT: call void @"\01??3@YAXPAX@Z"(i8* %[[THIS_AS_VOID]]) [[NUW:#[0-9]+]]
Timur Iskhodzhanov47453452013-02-13 12:14:25 +000060// DTORS-NEXT: br label %[[CONTINUE_LABEL]]
61//
62// DTORS: [[CONTINUE_LABEL]]
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000063// DTORS-NEXT: ret void
64 }
Timur Iskhodzhanov649c7312013-01-21 13:02:41 +000065 virtual void foo();
66};
67
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000068// Emits the vftable in the output.
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000069void C::foo() {}
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000070
Timur Iskhodzhanov649c7312013-01-21 13:02:41 +000071void check_vftable_offset() {
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000072 C c;
Timur Iskhodzhanov649c7312013-01-21 13:02:41 +000073// The vftable pointer should point at the beginning of the vftable.
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000074// CHECK: [[THIS_PTR:%[0-9]+]] = bitcast %"struct.basic::C"* {{.*}} to i8***
75// CHECK: store i8** getelementptr inbounds ([2 x i8*]* @"\01??_7C@basic@@6B@", i64 0, i64 0), i8*** [[THIS_PTR]]
Timur Iskhodzhanov649c7312013-01-21 13:02:41 +000076}
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000077
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000078void call_complete_dtor(C *obj_ptr) {
79// CHECK: define void @"\01?call_complete_dtor@basic@@YAXPAUC@1@@Z"(%"struct.basic::C"* %obj_ptr)
80 obj_ptr->~C();
81// CHECK: %[[OBJ_PTR_VALUE:.*]] = load %"struct.basic::C"** %{{.*}}, align 4
82// CHECK-NEXT: %[[PVTABLE:.*]] = bitcast %"struct.basic::C"* %[[OBJ_PTR_VALUE]] to void (%"struct.basic::C"*, i1)***
83// CHECK-NEXT: %[[VTABLE:.*]] = load void (%"struct.basic::C"*, i1)*** %[[PVTABLE]]
84// CHECK-NEXT: %[[PVDTOR:.*]] = getelementptr inbounds void (%"struct.basic::C"*, i1)** %[[VTABLE]], i64 0
85// CHECK-NEXT: %[[VDTOR:.*]] = load void (%"struct.basic::C"*, i1)** %[[PVDTOR]]
86// CHECK-NEXT: call x86_thiscallcc void %[[VDTOR]](%"struct.basic::C"* %[[OBJ_PTR_VALUE]], i1 zeroext false)
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +000087// CHECK-NEXT: ret void
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000088}
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000089
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000090void call_deleting_dtor(C *obj_ptr) {
91// CHECK: define void @"\01?call_deleting_dtor@basic@@YAXPAUC@1@@Z"(%"struct.basic::C"* %obj_ptr)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000092 delete obj_ptr;
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000093// CHECK: %[[OBJ_PTR_VALUE:.*]] = load %"struct.basic::C"** %{{.*}}, align 4
94// CHECK: br i1 {{.*}}, label %[[DELETE_NULL:.*]], label %[[DELETE_NOTNULL:.*]]
95
96// CHECK: [[DELETE_NOTNULL]]
97// CHECK-NEXT: %[[PVTABLE:.*]] = bitcast %"struct.basic::C"* %[[OBJ_PTR_VALUE]] to void (%"struct.basic::C"*, i1)***
98// CHECK-NEXT: %[[VTABLE:.*]] = load void (%"struct.basic::C"*, i1)*** %[[PVTABLE]]
99// CHECK-NEXT: %[[PVDTOR:.*]] = getelementptr inbounds void (%"struct.basic::C"*, i1)** %[[VTABLE]], i64 0
100// CHECK-NEXT: %[[VDTOR:.*]] = load void (%"struct.basic::C"*, i1)** %[[PVDTOR]]
101// CHECK-NEXT: call x86_thiscallcc void %[[VDTOR]](%"struct.basic::C"* %[[OBJ_PTR_VALUE]], i1 zeroext true)
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +0000102// CHECK: ret void
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000103}
104
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000105struct D {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000106 static int foo();
107
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000108 D() {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000109 static int ctor_static = foo();
110 // CHECK that the static in the ctor gets mangled correctly:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000111 // CHECK: @"\01?ctor_static@?1???0D@basic@@QAE@XZ@4HA"
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000112 }
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000113 ~D() {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000114 static int dtor_static = foo();
115 // CHECK that the static in the dtor gets mangled correctly:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000116 // CHECK: @"\01?dtor_static@?1???1D@basic@@QAE@XZ@4HA"
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000117 }
118};
119
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000120void use_D() { D c; }
Bill Wendling4e1125f2013-02-22 09:10:20 +0000121
122// DTORS: attributes [[NUW]] = { nounwind{{.*}} }
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000123
124} // end namespace basic
125
126
127namespace constructors {
128
129struct A {
130 A() {}
131};
132
133struct B : A {
134 B();
135 ~B();
136};
137
138B::B() {
Stephen Lin3258abc2013-06-19 23:23:19 +0000139 // CHECK: define x86_thiscallcc %"struct.constructors::B"* @"\01??0B@constructors@@QAE@XZ"(%"struct.constructors::B"* %this)
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000140 // CHECK: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
141 // CHECK: ret
142}
143
144struct C : virtual A {
145 C();
146};
147
148C::C() {
Stephen Lin3258abc2013-06-19 23:23:19 +0000149 // CHECK: define x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %this, i32 %is_most_derived)
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000150 // TODO: make sure this works in the Release build too;
151 // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
152 // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
153 // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
154 // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
155 //
156 // CHECK: [[INIT_VBASES]]
Reid Kleckner90633022013-06-19 15:20:38 +0000157 // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::C"* %{{.*}} to i8*
158 // CHECK-NEXT: %[[vbptr_off:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 0
159 // CHECK-NEXT: %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to [2 x i32]**
160 // CHECK-NEXT: store [2 x i32]* @"\01??_8C@constructors@@7B@", [2 x i32]** %[[vbptr]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000161 // CHECK-NEXT: bitcast %"struct.constructors::C"* %{{.*}} to %"struct.constructors::A"*
162 // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
163 // CHECK-NEXT: br label %[[SKIP_VBASES]]
164 //
165 // CHECK: [[SKIP_VBASES]]
166 // CHECK: @"\01??_7C@constructors@@6B@"
167 // CHECK: ret
168}
169
170void create_C() {
171 C c;
172 // CHECK: define void @"\01?create_C@constructors@@YAXXZ"()
173 // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %c, i32 1)
174 // CHECK: ret
175}
176
177struct D : C {
178 D();
179};
180
181D::D() {
Stephen Lin3258abc2013-06-19 23:23:19 +0000182 // CHECK: define x86_thiscallcc %"struct.constructors::D"* @"\01??0D@constructors@@QAE@XZ"(%"struct.constructors::D"* %this, i32 %is_most_derived) unnamed_addr
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000183 // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
184 // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
185 // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
186 // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
187 //
188 // CHECK: [[INIT_VBASES]]
Reid Kleckner90633022013-06-19 15:20:38 +0000189 // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::D"* %{{.*}} to i8*
190 // CHECK-NEXT: %[[vbptr_off:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 0
191 // CHECK-NEXT: %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to [2 x i32]**
192 // CHECK-NEXT: store [2 x i32]* @"\01??_8D@constructors@@7B@", [2 x i32]** %[[vbptr]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000193 // CHECK-NEXT: bitcast %"struct.constructors::D"* %{{.*}} to %"struct.constructors::A"*
194 // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
195 // CHECK-NEXT: br label %[[SKIP_VBASES]]
196 //
197 // CHECK: [[SKIP_VBASES]]
198 // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %{{.*}}, i32 0)
199 // CHECK: ret
200}
201
202struct E : virtual C {
203 E();
204};
205
206E::E() {
Stephen Lin3258abc2013-06-19 23:23:19 +0000207 // CHECK: define x86_thiscallcc %"struct.constructors::E"* @"\01??0E@constructors@@QAE@XZ"(%"struct.constructors::E"* %this, i32 %is_most_derived) unnamed_addr
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000208 // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
209 // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
210 // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
211 // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
212 //
213 // CHECK: [[INIT_VBASES]]
Reid Kleckner90633022013-06-19 15:20:38 +0000214 // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::E"* %{{.*}} to i8*
215 // CHECK-NEXT: %[[offs:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 0
216 // CHECK-NEXT: %[[vbptr_E:.*]] = bitcast i8* %[[offs]] to [3 x i32]**
217 // CHECK-NEXT: store [3 x i32]* @"\01??_8E@constructors@@7B01@@", [3 x i32]** %[[vbptr_E]]
218 // CHECK-NEXT: %[[offs:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 4
219 // CHECK-NEXT: %[[vbptr_C:.*]] = bitcast i8* %[[offs]] to [2 x i32]**
220 // CHECK-NEXT: store [2 x i32]* @"\01??_8E@constructors@@7BC@1@@", [2 x i32]** %[[vbptr_C]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000221 // CHECK-NEXT: bitcast %"struct.constructors::E"* %{{.*}} to %"struct.constructors::A"*
222 // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
223 // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %{{.*}}, i32 0)
224 // CHECK-NEXT: br label %[[SKIP_VBASES]]
225 //
226 // CHECK: [[SKIP_VBASES]]
227 // CHECK: ret
228}
229
230} // end namespace constructors