microsoft-abi-structors.cpp revision 4e1125f630e75a52209b928e9d43b638abf39987
1// 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
6
7class A {
8 public:
9  A() { }
10  ~A() { }
11};
12
13void no_constructor_destructor_infinite_recursion() {
14  A a;
15
16// CHECK:      define linkonce_odr x86_thiscallcc %class.A* @"\01??0A@@QAE@XZ"(%class.A* %this)
17// CHECK:        [[THIS_ADDR:%[.0-9A-Z_a-z]+]] = alloca %class.A*, align 4
18// CHECK-NEXT:   store %class.A* %this, %class.A** [[THIS_ADDR]], align 4
19// CHECK-NEXT:   [[T1:%[.0-9A-Z_a-z]+]] = load %class.A** [[THIS_ADDR]]
20// CHECK-NEXT:   ret %class.A* [[T1]]
21// CHECK-NEXT: }
22
23// Make sure that the destructor doesn't call itself:
24// CHECK: define {{.*}} @"\01??1A@@QAE@XZ"
25// CHECK-NOT: call void @"\01??1A@@QAE@XZ"
26// CHECK: ret
27}
28
29struct B {
30  virtual ~B() {
31// Complete destructor first:
32// DTORS: define {{.*}} x86_thiscallcc void @"\01??1B@@UAE@XZ"(%struct.B* %this)
33
34// Then, the scalar deleting destructor (used in the vtable):
35// FIXME: add a test that verifies that the out-of-line scalar deleting
36// destructor is linkonce_odr too.
37// DTORS:      define linkonce_odr x86_thiscallcc void @"\01??_GB@@UAEPAXI@Z"(%struct.B* %this, i1 zeroext %should_call_delete)
38// DTORS:        %[[FROMBOOL:[0-9a-z]+]] = zext i1 %should_call_delete to i8
39// DTORS-NEXT:   store i8 %[[FROMBOOL]], i8* %[[SHOULD_DELETE_VAR:[0-9a-z._]+]], align 1
40// DTORS:        %[[SHOULD_DELETE_VALUE:[0-9a-z._]+]] = load i8* %[[SHOULD_DELETE_VAR]]
41// DTORS:        call x86_thiscallcc void @"\01??1B@@UAE@XZ"(%struct.B* %[[THIS:[0-9a-z]+]])
42// DTORS-NEXT:   %[[CONDITION:[0-9]+]] = icmp eq i8 %[[SHOULD_DELETE_VALUE]], 0
43// DTORS-NEXT:   br i1 %[[CONDITION]], label %[[CONTINUE_LABEL:[0-9a-z._]+]], label %[[CALL_DELETE_LABEL:[0-9a-z._]+]]
44//
45// DTORS:      [[CALL_DELETE_LABEL]]
46// DTORS-NEXT:   %[[THIS_AS_VOID:[0-9a-z]+]] = bitcast %struct.B* %[[THIS]] to i8*
47// DTORS-NEXT:   call void @"\01??3@YAXPAX@Z"(i8* %[[THIS_AS_VOID]]) [[NUW:#[0-9]+]]
48// DTORS-NEXT:   br label %[[CONTINUE_LABEL]]
49//
50// DTORS:      [[CONTINUE_LABEL]]
51// DTORS-NEXT:   ret void
52  }
53  virtual void foo();
54};
55
56// Emits the vftable in the output.
57void B::foo() {}
58
59void check_vftable_offset() {
60  B b;
61// The vftable pointer should point at the beginning of the vftable.
62// CHECK: [[THIS_PTR:%[0-9]+]] = bitcast %struct.B* {{.*}} to i8***
63// CHECK: store i8** getelementptr inbounds ([2 x i8*]* @"\01??_7B@@6B@", i64 0, i64 0), i8*** [[THIS_PTR]]
64}
65
66void call_complete_dtor(B *obj_ptr) {
67// CHECK: define void @"\01?call_complete_dtor@@YAXPAUB@@@Z"(%struct.B* %obj_ptr)
68  obj_ptr->~B();
69// CHECK: %[[OBJ_PTR_VALUE:.*]] = load %struct.B** %{{.*}}, align 4
70// CHECK-NEXT: %[[PVTABLE:.*]] = bitcast %struct.B* %[[OBJ_PTR_VALUE]] to void (%struct.B*, i1)***
71// CHECK-NEXT: %[[VTABLE:.*]] = load void (%struct.B*, i1)*** %[[PVTABLE]]
72// CHECK-NEXT: %[[PVDTOR:.*]] = getelementptr inbounds void (%struct.B*, i1)** %[[VTABLE]], i64 0
73// CHECK-NEXT: %[[VDTOR:.*]] = load void (%struct.B*, i1)** %[[PVDTOR]]
74// CHECK-NEXT: call x86_thiscallcc void %[[VDTOR]](%struct.B* %[[OBJ_PTR_VALUE]], i1 zeroext false)
75// CHECK-NEXT: ret void
76}
77
78void call_deleting_dtor(B *obj_ptr) {
79// CHECK: define void @"\01?call_deleting_dtor@@YAXPAUB@@@Z"(%struct.B* %obj_ptr)
80  delete obj_ptr;
81// CHECK:      %[[OBJ_PTR_VALUE:.*]] = load %struct.B** %{{.*}}, align 4
82// CHECK:      {{.*}}:{{%{0,1}[0-9]*}}
83// CHECK-NEXT:   %[[PVTABLE:.*]] = bitcast %struct.B* %[[OBJ_PTR_VALUE]] to void (%struct.B*, i1)***
84// CHECK-NEXT:   %[[VTABLE:.*]] = load void (%struct.B*, i1)*** %[[PVTABLE]]
85// CHECK-NEXT:   %[[PVDTOR:.*]] = getelementptr inbounds void (%struct.B*, i1)** %[[VTABLE]], i64 0
86// CHECK-NEXT:   %[[VDTOR:.*]] = load void (%struct.B*, i1)** %[[PVDTOR]]
87// CHECK-NEXT:   call x86_thiscallcc void %[[VDTOR]](%struct.B* %[[OBJ_PTR_VALUE]], i1 zeroext true)
88// CHECK:      ret void
89}
90
91struct C {
92  static int foo();
93
94  C() {
95    static int ctor_static = foo();
96    // CHECK that the static in the ctor gets mangled correctly:
97    // CHECK: @"\01?ctor_static@?1???0C@@QAE@XZ@4HA"
98  }
99  ~C() {
100    static int dtor_static = foo();
101    // CHECK that the static in the dtor gets mangled correctly:
102    // CHECK: @"\01?dtor_static@?1???1C@@QAE@XZ@4HA"
103  }
104};
105
106void use_C() { C c; }
107
108// DTORS: attributes [[NUW]] = { nounwind{{.*}} }
109