microsoft-abi-structors.cpp revision ba8fa0c8e5739884c641f7435b43b073e11d6125
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// DTORS:      define {{.*}} x86_thiscallcc void @"\01??_GB@@UAEPAXI@Z"(%struct.B* %this, i1 zeroext %should_call_delete)
36// DTORS:        %[[FROMBOOL:[0-9a-z]+]] = zext i1 %should_call_delete to i8
37// DTORS-NEXT:   store i8 %[[FROMBOOL]], i8* %[[SHOULD_DELETE_VAR:[0-9a-z._]+]], align 1
38// DTORS:        %[[SHOULD_DELETE_VALUE:[0-9a-z._]+]] = load i8* %[[SHOULD_DELETE_VAR]]
39// DTORS:        call x86_thiscallcc void @"\01??1B@@UAE@XZ"(%struct.B* %[[THIS:[0-9a-z]+]])
40// DTORS-NEXT:   %[[CONDITION:[0-9]+]] = icmp eq i8 %[[SHOULD_DELETE_VALUE]], 0
41// DTORS-NEXT:   br i1 %[[CONDITION]], label %[[CONTINUE_LABEL:[0-9a-z._]+]], label %[[CALL_DELETE_LABEL:[0-9a-z._]+]]
42//
43// DTORS:      [[CALL_DELETE_LABEL]]
44// DTORS-NEXT:   %[[THIS_AS_VOID:[0-9a-z]+]] = bitcast %struct.B* %[[THIS]] to i8*
45// DTORS-NEXT:   call void @"\01??3@YAXPAX@Z"(i8* %[[THIS_AS_VOID]]) nounwind
46// DTORS-NEXT:   br label %[[CONTINUE_LABEL]]
47//
48// DTORS:      [[CONTINUE_LABEL]]
49// DTORS-NEXT:   ret void
50  }
51  virtual void foo();
52};
53
54// Emits the vftable in the output.
55void B::foo() {}
56
57void check_vftable_offset() {
58  B b;
59// The vftable pointer should point at the beginning of the vftable.
60// CHECK: [[THIS_PTR:%[0-9]+]] = bitcast %struct.B* {{.*}} to i8***
61// CHECK: store i8** getelementptr inbounds ([2 x i8*]* @"\01??_7B@@6B@", i64 0, i64 0), i8*** [[THIS_PTR]]
62}
63
64// FIXME: Enable the following block and add expectations when calls
65// to virtual complete dtor are supported.
66#if 0
67void call_complete_dtor(B *obj_ptr) {
68  obj_ptr->~B();
69}
70#endif
71
72void call_deleting_dtor(B *obj_ptr) {
73// FIXME: Add CHECKs when calls to virtual deleting dtor are generated properly.
74  delete obj_ptr;
75}
76
77struct C {
78  static int foo();
79
80  C() {
81    static int ctor_static = foo();
82    // CHECK that the static in the ctor gets mangled correctly:
83    // CHECK: @"\01?ctor_static@?1???0C@@QAE@XZ@4HA"
84  }
85  ~C() {
86    static int dtor_static = foo();
87    // CHECK that the static in the dtor gets mangled correctly:
88    // CHECK: @"\01?dtor_static@?1???1C@@QAE@XZ@4HA"
89  }
90};
91
92void use_C() { C c; }
93