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
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
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
20 void no_constructor_destructor_infinite_recursion() {
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]]
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"
42 // Tests that we can define constructors outside the class (PR12784).
44 // CHECK: define dso_local x86_thiscallcc ptr @"??0B@basic@@QAE@XZ"(ptr {{[^,]*}} returned {{[^,]*}} %this)
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"
72 // Emits the vftable in the output.
75 void check_vftable_offset() {
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)
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)
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)
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)
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]])
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"
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
{
143 virtual void f(); // A needs vftable to be primary.
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{{[^,]*}} %{{.*}})
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]]
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
; };
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
190 // CHECK: getelementptr inbounds i8, ptr %{{.*}}, i32 -20
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"
205 namespace constructors
{
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 {{[^,]*}} %{{.*}})
222 struct C
: virtual A
{
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@"
249 // CHECK: define dso_local void @"?create_C@constructors@@YAXXZ"()
250 // CHECK: call x86_thiscallcc ptr @"??0C@constructors@@QAE@XZ"(ptr {{[^,]*}} %c, i32 1)
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)
277 struct E
: virtual C
{
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]]
302 // PR16735 - even abstract classes should have a constructor emitted.
305 virtual void f() = 0;
309 // CHECK: define dso_local x86_thiscallcc ptr @"??0F@constructors@@QAE@XZ"
311 } // end namespace constructors
319 void call_nv_complete(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"
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
) {
336 // CHECK: define dso_local void @"?call_vbase_complete@dtors@@YAXPAUD@1@@Z"
337 // CHECK: call x86_thiscallcc void @"??_DD@dtors@@QAEXXZ"(ptr {{[^,]*}} %{{[^,]+}})
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
344 // CHECK: call x86_thiscallcc void @"??1D@dtors@@QAE@XZ"
346 // CHECK: call x86_thiscallcc void @"??1A@dtors@@QAE@XZ"
350 void destroy_d_complete() {
352 // CHECK: define dso_local void @"?destroy_d_complete@dtors@@YAXXZ"
353 // CHECK: call x86_thiscallcc void @"??_DD@dtors@@QAEXXZ"(ptr {{[^,]*}} %{{[^,]+}})
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
360 void call_nv_deleting_dtor(D
*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"
372 struct B
: virtual A
{
374 B(const char *a
, ...);
378 B::B(const char *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)
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@"
411 MoveOnly(MoveOnly
&&o
) = default;
415 void g() { new MoveOnly(f()); }
416 // CHECK: store {{.*}} @"??_7MoveOnly@implicit_copy_vtable@@6B@"
419 namespace delegating_ctor
{
421 struct X
: virtual Y
{
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
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
453 // DTORS4: define linkonce_odr dso_local x86_thiscallcc ptr @"??0G@@QAE@XZ"
455 // DTORS4: define linkonce_odr dso_local x86_thiscallcc void @"??1G@@QAE@XZ"
458 extern void testG() {