[ELF] Reorder SectionBase/InputSectionBase members
[llvm-project.git] / clang / test / CodeGenCXX / microsoft-abi-structors.cpp
blob07abc3d065e5e1441767fac53829e92f07e3f355
1 // RUN: %clang_cc1 -no-enable-noundef-analysis -emit-llvm -fno-rtti %s -std=c++11 -o - -mconstructor-aliases -triple=i386-pc-win32 -fno-rtti > %t
2 // RUN: FileCheck %s < %t
3 // vftables are emitted very late, so do another pass to try to keep the checks
4 // in source order.
5 // RUN: FileCheck --check-prefix DTORS %s < %t
6 // RUN: FileCheck --check-prefix DTORS2 %s < %t
7 // RUN: FileCheck --check-prefix DTORS3 %s < %t
8 // RUN: FileCheck --check-prefix DTORS4 %s < %t
9 //
10 // RUN: %clang_cc1 -emit-llvm %s -o - -mconstructor-aliases -triple=x86_64-pc-win32 -fno-rtti -std=c++11 | FileCheck --check-prefix DTORS-X64 %s
12 namespace basic {
14 class A {
15 public:
16 A() { }
17 ~A();
20 void no_constructor_destructor_infinite_recursion() {
21 A a;
23 // CHECK: define linkonce_odr dso_local x86_thiscallcc ptr @"??0A@basic@@QAE@XZ"(ptr {{[^,]*}} returned {{[^,]*}} %this) {{.*}} comdat {{.*}} {
24 // CHECK: [[THIS_ADDR:%[.0-9A-Z_a-z]+]] = alloca ptr, align 4
25 // CHECK-NEXT: store ptr %this, ptr [[THIS_ADDR]], align 4
26 // CHECK-NEXT: [[T1:%[.0-9A-Z_a-z]+]] = load ptr, ptr [[THIS_ADDR]]
27 // CHECK-NEXT: ret ptr [[T1]]
28 // CHECK-NEXT: }
31 A::~A() {
32 // Make sure that the destructor doesn't call itself:
33 // CHECK: define {{.*}} @"??1A@basic@@QAE@XZ"
34 // CHECK-NOT: call void @"??1A@basic@@QAE@XZ"
35 // CHECK: ret
38 struct B {
39 B();
42 // Tests that we can define constructors outside the class (PR12784).
43 B::B() {
44 // CHECK: define dso_local x86_thiscallcc ptr @"??0B@basic@@QAE@XZ"(ptr {{[^,]*}} returned {{[^,]*}} %this)
45 // CHECK: ret
48 struct C {
49 virtual ~C() {
50 // DTORS: define linkonce_odr dso_local x86_thiscallcc ptr @"??_GC@basic@@UAEPAXI@Z"(ptr {{[^,]*}} %this, i32 %should_call_delete) {{.*}} comdat {{.*}} {
51 // DTORS: store i32 %should_call_delete, ptr %[[SHOULD_DELETE_VAR:[0-9a-z._]+]], align 4
52 // DTORS: store ptr %{{.*}}, ptr %[[RETVAL:retval]]
53 // DTORS: %[[SHOULD_DELETE_VALUE:[0-9a-z._]+]] = load i32, ptr %[[SHOULD_DELETE_VAR]]
54 // DTORS: call x86_thiscallcc void @"??1C@basic@@UAE@XZ"(ptr {{[^,]*}} %[[THIS:[0-9a-z]+]])
55 // DTORS-NEXT: %[[CONDITION:[0-9]+]] = icmp eq i32 %[[SHOULD_DELETE_VALUE]], 0
56 // DTORS-NEXT: br i1 %[[CONDITION]], label %[[CONTINUE_LABEL:[0-9a-z._]+]], label %[[CALL_DELETE_LABEL:[0-9a-z._]+]]
58 // DTORS: [[CALL_DELETE_LABEL]]
59 // DTORS-NEXT: call void @"??3@YAXPAX@Z"(ptr %[[THIS]])
60 // DTORS-NEXT: br label %[[CONTINUE_LABEL]]
62 // DTORS: [[CONTINUE_LABEL]]
63 // DTORS-NEXT: %[[RET:.*]] = load ptr, ptr %[[RETVAL]]
64 // DTORS-NEXT: ret ptr %[[RET]]
66 // Check that we do the mangling correctly on x64.
67 // DTORS-X64: @"??_GC@basic@@UEAAPEAXI@Z"
69 virtual void foo();
72 // Emits the vftable in the output.
73 void C::foo() {}
75 void check_vftable_offset() {
76 C c;
77 // The vftable pointer should point at the beginning of the vftable.
78 // CHECK: store ptr @"??_7C@basic@@6B@", ptr {{.*}}
81 void call_complete_dtor(C *obj_ptr) {
82 // CHECK: define dso_local void @"?call_complete_dtor@basic@@YAXPAUC@1@@Z"(ptr %obj_ptr)
83 obj_ptr->~C();
84 // CHECK: %[[OBJ_PTR_VALUE:.*]] = load ptr, ptr %{{.*}}, align 4
85 // CHECK-NEXT: %[[VTABLE:.*]] = load ptr, ptr %[[OBJ_PTR_VALUE]]
86 // CHECK-NEXT: %[[PVDTOR:.*]] = getelementptr inbounds ptr, ptr %[[VTABLE]], i64 0
87 // CHECK-NEXT: %[[VDTOR:.*]] = load ptr, ptr %[[PVDTOR]]
88 // CHECK-NEXT: call x86_thiscallcc ptr %[[VDTOR]](ptr {{[^,]*}} %[[OBJ_PTR_VALUE]], i32 0)
89 // CHECK-NEXT: ret void
92 void call_deleting_dtor(C *obj_ptr) {
93 // CHECK: define dso_local void @"?call_deleting_dtor@basic@@YAXPAUC@1@@Z"(ptr %obj_ptr)
94 delete obj_ptr;
95 // CHECK: %[[OBJ_PTR_VALUE:.*]] = load ptr, ptr %{{.*}}, align 4
96 // CHECK: br i1 {{.*}}, label %[[DELETE_NULL:.*]], label %[[DELETE_NOTNULL:.*]]
98 // CHECK: [[DELETE_NOTNULL]]
99 // CHECK-NEXT: %[[VTABLE:.*]] = load ptr, ptr %[[OBJ_PTR_VALUE]]
100 // CHECK-NEXT: %[[PVDTOR:.*]] = getelementptr inbounds ptr, ptr %[[VTABLE]], i64 0
101 // CHECK-NEXT: %[[VDTOR:.*]] = load ptr, ptr %[[PVDTOR]]
102 // CHECK-NEXT: call x86_thiscallcc ptr %[[VDTOR]](ptr {{[^,]*}} %[[OBJ_PTR_VALUE]], i32 1)
103 // CHECK: ret void
106 void call_deleting_dtor_and_global_delete(C *obj_ptr) {
107 // CHECK: define dso_local void @"?call_deleting_dtor_and_global_delete@basic@@YAXPAUC@1@@Z"(ptr %obj_ptr)
108 ::delete obj_ptr;
109 // CHECK: %[[OBJ_PTR_VALUE:.*]] = load ptr, ptr %{{.*}}, align 4
110 // CHECK: br i1 {{.*}}, label %[[DELETE_NULL:.*]], label %[[DELETE_NOTNULL:.*]]
112 // CHECK: [[DELETE_NOTNULL]]
113 // CHECK-NEXT: %[[VTABLE:.*]] = load ptr, ptr %[[OBJ_PTR_VALUE]]
114 // CHECK-NEXT: %[[PVDTOR:.*]] = getelementptr inbounds ptr, ptr %[[VTABLE]], i64 0
115 // CHECK-NEXT: %[[VDTOR:.*]] = load ptr, ptr %[[PVDTOR]]
116 // CHECK-NEXT: %[[CALL:.*]] = call x86_thiscallcc ptr %[[VDTOR]](ptr {{[^,]*}} %[[OBJ_PTR_VALUE]], i32 0)
117 // CHECK-NEXT: call void @"??3@YAXPAX@Z"(ptr %[[CALL]])
118 // CHECK: ret void
121 struct D {
122 static int foo();
124 D() {
125 static int ctor_static = foo();
126 // CHECK that the static in the ctor gets mangled correctly:
127 // CHECK: @"?ctor_static@?1???0D@basic@@QAE@XZ@4HA"
129 ~D() {
130 static int dtor_static = foo();
131 // CHECK that the static in the dtor gets mangled correctly:
132 // CHECK: @"?dtor_static@?1???1D@basic@@QAE@XZ@4HA"
136 void use_D() { D c; }
138 } // end namespace basic
140 namespace dtor_in_second_nvbase {
142 struct A {
143 virtual void f(); // A needs vftable to be primary.
145 struct B {
146 virtual ~B();
148 struct C : A, B {
149 virtual ~C();
152 C::~C() {
153 // CHECK-LABEL: define dso_local x86_thiscallcc void @"??1C@dtor_in_second_nvbase@@UAE@XZ"(ptr{{[^,]*}} %this)
154 // No this adjustment!
155 // CHECK-NOT: getelementptr
156 // CHECK: load ptr, ptr %{{.*}}
157 // Now we this-adjust before calling ~B.
158 // CHECK: getelementptr inbounds i8, ptr %{{.*}}, i32 4
159 // CHECK: call x86_thiscallcc void @"??1B@dtor_in_second_nvbase@@UAE@XZ"(ptr{{[^,]*}} %{{.*}})
160 // CHECK: ret void
163 void foo() {
164 C c;
166 // DTORS2-LABEL: define linkonce_odr dso_local x86_thiscallcc ptr @"??_EC@dtor_in_second_nvbase@@W3AEPAXI@Z"(ptr %this, i32 %should_call_delete)
167 // Do an adjustment from B* to C*.
168 // DTORS2: getelementptr i8, ptr %{{.*}}, i32 -4
169 // DTORS2: %[[CALL:.*]] = tail call x86_thiscallcc ptr @"??_GC@dtor_in_second_nvbase@@UAEPAXI@Z"
170 // DTORS2: ret ptr %[[CALL]]
173 namespace test2 {
174 // Just like dtor_in_second_nvbase, except put that in a vbase of a diamond.
176 // C's dtor is in the non-primary base.
177 struct A { virtual void f(); };
178 struct B { virtual ~B(); };
179 struct C : A, B { virtual ~C(); int c; };
181 // Diamond hierarchy, with C as the shared vbase.
182 struct D : virtual C { int d; };
183 struct E : virtual C { int e; };
184 struct F : D, E { ~F(); int f; };
186 F::~F() {
187 // CHECK-LABEL: define dso_local x86_thiscallcc void @"??1F@test2@@UAE@XZ"(ptr{{[^,]*}})
188 // Do an adjustment from C vbase subobject to F as though F was the
189 // complete type.
190 // CHECK: getelementptr inbounds i8, ptr %{{.*}}, i32 -20
191 // CHECK: store ptr
194 void foo() {
195 F f;
197 // DTORS3-LABEL: define linkonce_odr dso_local x86_thiscallcc void @"??_DF@test2@@QAEXXZ"({{.*}} {{.*}} comdat
198 // Do an adjustment from C* to F*.
199 // DTORS3: getelementptr i8, ptr %{{.*}}, i32 20
200 // DTORS3: call x86_thiscallcc void @"??1F@test2@@UAE@XZ"
201 // DTORS3: ret void
205 namespace constructors {
207 struct A {
208 A() {}
211 struct B : A {
212 B();
213 ~B();
216 B::B() {
217 // CHECK: define dso_local x86_thiscallcc ptr @"??0B@constructors@@QAE@XZ"(ptr {{[^,]*}} returned {{[^,]*}} %this)
218 // CHECK: call x86_thiscallcc ptr @"??0A@constructors@@QAE@XZ"(ptr {{[^,]*}} %{{.*}})
219 // CHECK: ret
222 struct C : virtual A {
223 C();
226 C::C() {
227 // CHECK: define dso_local x86_thiscallcc ptr @"??0C@constructors@@QAE@XZ"(ptr {{[^,]*}} returned {{[^,]*}} %this, i32 %is_most_derived)
228 // TODO: make sure this works in the Release build too;
229 // CHECK: store i32 %is_most_derived, ptr %[[IS_MOST_DERIVED_VAR:.*]], align 4
230 // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32, ptr %[[IS_MOST_DERIVED_VAR]]
231 // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
232 // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
234 // CHECK: [[INIT_VBASES]]
235 // CHECK-NEXT: %[[vbptr_off:.*]] = getelementptr inbounds i8, ptr %{{.*}}, i32 0
236 // CHECK-NEXT: store ptr @"??_8C@constructors@@7B@", ptr %[[vbptr_off]]
237 // CHECK-NEXT: getelementptr inbounds i8, ptr %{{.*}}, i32 4
238 // CHECK-NEXT: call x86_thiscallcc ptr @"??0A@constructors@@QAE@XZ"(ptr {{[^,]*}} %{{.*}})
239 // CHECK-NEXT: br label %[[SKIP_VBASES]]
241 // CHECK: [[SKIP_VBASES]]
242 // Class C does not define or override methods, so shouldn't change the vfptr.
243 // CHECK-NOT: @"??_7C@constructors@@6B@"
244 // CHECK: ret
247 void create_C() {
248 C c;
249 // CHECK: define dso_local void @"?create_C@constructors@@YAXXZ"()
250 // CHECK: call x86_thiscallcc ptr @"??0C@constructors@@QAE@XZ"(ptr {{[^,]*}} %c, i32 1)
251 // CHECK: ret
254 struct D : C {
255 D();
258 D::D() {
259 // CHECK: define dso_local x86_thiscallcc ptr @"??0D@constructors@@QAE@XZ"(ptr {{[^,]*}} returned {{[^,]*}} %this, i32 %is_most_derived) unnamed_addr
260 // CHECK: store i32 %is_most_derived, ptr %[[IS_MOST_DERIVED_VAR:.*]], align 4
261 // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32, ptr %[[IS_MOST_DERIVED_VAR]]
262 // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
263 // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
265 // CHECK: [[INIT_VBASES]]
266 // CHECK-NEXT: %[[vbptr_off:.*]] = getelementptr inbounds i8, ptr %{{.*}}, i32 0
267 // CHECK-NEXT: store ptr @"??_8D@constructors@@7B@", ptr %[[vbptr_off]]
268 // CHECK-NEXT: getelementptr inbounds i8, ptr %{{.*}}, i32 4
269 // CHECK-NEXT: call x86_thiscallcc ptr @"??0A@constructors@@QAE@XZ"(ptr {{[^,]*}} %{{.*}})
270 // CHECK-NEXT: br label %[[SKIP_VBASES]]
272 // CHECK: [[SKIP_VBASES]]
273 // CHECK: call x86_thiscallcc ptr @"??0C@constructors@@QAE@XZ"(ptr {{[^,]*}} %{{.*}}, i32 0)
274 // CHECK: ret
277 struct E : virtual C {
278 E();
281 E::E() {
282 // CHECK: define dso_local x86_thiscallcc ptr @"??0E@constructors@@QAE@XZ"(ptr {{[^,]*}} returned {{[^,]*}} %this, i32 %is_most_derived) unnamed_addr
283 // CHECK: store i32 %is_most_derived, ptr %[[IS_MOST_DERIVED_VAR:.*]], align 4
284 // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32, ptr %[[IS_MOST_DERIVED_VAR]]
285 // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
286 // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
288 // CHECK: [[INIT_VBASES]]
289 // CHECK-NEXT: %[[offs:.*]] = getelementptr inbounds i8, ptr %{{.*}}, i32 0
290 // CHECK-NEXT: store ptr @"??_8E@constructors@@7B01@@", ptr %[[offs]]
291 // CHECK-NEXT: %[[offs:.*]] = getelementptr inbounds i8, ptr %{{.*}}, i32 4
292 // CHECK-NEXT: store ptr @"??_8E@constructors@@7BC@1@@", ptr %[[offs]]
293 // CHECK-NEXT: getelementptr inbounds i8, ptr %{{.*}}, i32 4
294 // CHECK-NEXT: call x86_thiscallcc ptr @"??0A@constructors@@QAE@XZ"(ptr {{[^,]*}} %{{.*}})
295 // CHECK: call x86_thiscallcc ptr @"??0C@constructors@@QAE@XZ"(ptr {{[^,]*}} %{{.*}}, i32 0)
296 // CHECK-NEXT: br label %[[SKIP_VBASES]]
298 // CHECK: [[SKIP_VBASES]]
299 // CHECK: ret
302 // PR16735 - even abstract classes should have a constructor emitted.
303 struct F {
304 F();
305 virtual void f() = 0;
308 F::F() {}
309 // CHECK: define dso_local x86_thiscallcc ptr @"??0F@constructors@@QAE@XZ"
311 } // end namespace constructors
313 namespace dtors {
315 struct A {
316 ~A();
319 void call_nv_complete(A *a) {
320 a->~A();
321 // CHECK: define dso_local void @"?call_nv_complete@dtors@@YAXPAUA@1@@Z"
322 // CHECK: call x86_thiscallcc void @"??1A@dtors@@QAE@XZ"
323 // CHECK: ret
326 // CHECK: declare dso_local x86_thiscallcc void @"??1A@dtors@@QAE@XZ"
328 // Now try some virtual bases, where we need the complete dtor.
330 struct B : virtual A { ~B(); };
331 struct C : virtual A { ~C(); };
332 struct D : B, C { ~D(); };
334 void call_vbase_complete(D *d) {
335 d->~D();
336 // CHECK: define dso_local void @"?call_vbase_complete@dtors@@YAXPAUD@1@@Z"
337 // CHECK: call x86_thiscallcc void @"??_DD@dtors@@QAEXXZ"(ptr {{[^,]*}} %{{[^,]+}})
338 // CHECK: ret
341 // The complete dtor should call the base dtors for D and the vbase A (once).
342 // CHECK: define linkonce_odr dso_local x86_thiscallcc void @"??_DD@dtors@@QAEXXZ"({{.*}}) {{.*}} comdat
343 // CHECK-NOT: call
344 // CHECK: call x86_thiscallcc void @"??1D@dtors@@QAE@XZ"
345 // CHECK-NOT: call
346 // CHECK: call x86_thiscallcc void @"??1A@dtors@@QAE@XZ"
347 // CHECK-NOT: call
348 // CHECK: ret
350 void destroy_d_complete() {
351 D d;
352 // CHECK: define dso_local void @"?destroy_d_complete@dtors@@YAXXZ"
353 // CHECK: call x86_thiscallcc void @"??_DD@dtors@@QAEXXZ"(ptr {{[^,]*}} %{{[^,]+}})
354 // CHECK: ret
357 // FIXME: Clang manually inlines the deletion, so we don't get a call to the
358 // deleting dtor (_G). The only way to call deleting dtors currently is through
359 // a vftable.
360 void call_nv_deleting_dtor(D *d) {
361 delete d;
362 // CHECK: define dso_local void @"?call_nv_deleting_dtor@dtors@@YAXPAUD@1@@Z"
363 // CHECK: call x86_thiscallcc void @"??_DD@dtors@@QAEXXZ"(ptr {{[^,]*}} %{{[^,]+}})
364 // CHECK: call void @"??3@YAXPAX@Z"
365 // CHECK: ret
370 namespace test1 {
371 struct A { };
372 struct B : virtual A {
373 B(int *a);
374 B(const char *a, ...);
375 __cdecl B(short *a);
377 B::B(int *a) {}
378 B::B(const char *a, ...) {}
379 B::B(short *a) {}
380 // CHECK: define dso_local x86_thiscallcc ptr @"??0B@test1@@QAE@PAH@Z"
381 // CHECK: (ptr {{[^,]*}} returned {{[^,]*}} %this, ptr %a, i32 %is_most_derived)
382 // CHECK: define dso_local ptr @"??0B@test1@@QAA@PBDZZ"
383 // CHECK: (ptr {{[^,]*}} returned {{[^,]*}} %this, i32 %is_most_derived, ptr %a, ...)
384 // CHECK: define dso_local x86_thiscallcc ptr @"??0B@test1@@QAE@PAF@Z"
385 // CHECK: (ptr {{[^,]*}} returned {{[^,]*}} %this, ptr %a, i32 %is_most_derived)
387 void construct_b() {
388 int a;
389 B b1(&a);
390 B b2("%d %d", 1, 2);
392 // CHECK-LABEL: define dso_local void @"?construct_b@test1@@YAXXZ"()
393 // CHECK: call x86_thiscallcc ptr @"??0B@test1@@QAE@PAH@Z"
394 // CHECK: (ptr {{.*}}, ptr {{.*}}, i32 1)
395 // CHECK: call ptr (ptr, i32, ptr, ...) @"??0B@test1@@QAA@PBDZZ"
396 // CHECK: (ptr {{.*}}, i32 1, ptr {{.*}}, i32 1, i32 2)
399 namespace implicit_copy_vtable {
400 // This was a crash that only reproduced in ABIs without key functions.
401 struct ImplicitCopy {
402 // implicit copy ctor
403 virtual ~ImplicitCopy();
405 void CreateCopy(ImplicitCopy *a) {
406 new ImplicitCopy(*a);
408 // CHECK: store {{.*}} @"??_7ImplicitCopy@implicit_copy_vtable@@6B@"
410 struct MoveOnly {
411 MoveOnly(MoveOnly &&o) = default;
412 virtual ~MoveOnly();
414 MoveOnly &&f();
415 void g() { new MoveOnly(f()); }
416 // CHECK: store {{.*}} @"??_7MoveOnly@implicit_copy_vtable@@6B@"
419 namespace delegating_ctor {
420 struct Y {};
421 struct X : virtual Y {
422 X(int);
423 X();
425 X::X(int) : X() {}
427 // CHECK: define dso_local x86_thiscallcc ptr @"??0X@delegating_ctor@@QAE@H@Z"(
428 // CHECK: %[[is_most_derived_addr:.*]] = alloca i32, align 4
429 // CHECK: store i32 %is_most_derived, ptr %[[is_most_derived_addr]]
430 // CHECK: %[[is_most_derived:.*]] = load i32, ptr %[[is_most_derived_addr]]
431 // CHECK: call x86_thiscallcc ptr @"??0X@delegating_ctor@@QAE@XZ"({{.*}} i32 %[[is_most_derived]])
433 // Dtor thunks for classes in anonymous namespaces should be internal, not
434 // linkonce_odr.
435 namespace {
436 struct A {
437 virtual ~A() { }
440 void *getA() {
441 return (void*)new A();
443 // CHECK: define internal x86_thiscallcc ptr @"??_GA@?A0x{{[^@]*}}@@UAEPAXI@Z"
444 // CHECK: (ptr {{[^,]*}} %this, i32 %should_call_delete)
445 // CHECK: define internal x86_thiscallcc void @"??1A@?A0x{{[^@]*}}@@UAE@XZ"
446 // CHECK: (ptr {{[^,]*}} %this)
448 // Check that we correctly transform __stdcall to __thiscall for ctors and
449 // dtors.
450 class G {
451 public:
452 __stdcall G() {};
453 // DTORS4: define linkonce_odr dso_local x86_thiscallcc ptr @"??0G@@QAE@XZ"
454 __stdcall ~G() {};
455 // DTORS4: define linkonce_odr dso_local x86_thiscallcc void @"??1G@@QAE@XZ"
458 extern void testG() {
459 G g;