1 // RUN: %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -fstrict-vtable-pointers -disable-llvm-optzns -O2 -emit-llvm -o %t.ll
2 // RUN: FileCheck --check-prefix=CHECK-CTORS %s < %t.ll
3 // RUN: FileCheck --check-prefix=CHECK-NEW %s < %t.ll
4 // RUN: FileCheck --check-prefix=CHECK-DTORS %s < %t.ll
5 // RUN: FileCheck --check-prefix=CHECK-LINK-REQ %s < %t.ll
6 
7 typedef __typeof__(sizeof(0)) size_t;
8 void *operator new(size_t, void*) throw();
9 
10 struct NotTrivialDtor {
11   ~NotTrivialDtor();
12 };
13 
14 struct DynamicBase1 {
15   NotTrivialDtor obj;
16   virtual void foo();
17 };
18 
19 struct DynamicDerived : DynamicBase1 {
20   void foo();
21 };
22 
23 struct DynamicBase2 {
24   virtual void bar();
25   ~DynamicBase2() {
26     bar();
27   }
28 };
29 
30 struct DynamicDerivedMultiple : DynamicBase1, DynamicBase2 {
31   virtual void foo();
32   virtual void bar();
33 };
34 
35 struct StaticBase {
36   NotTrivialDtor obj;
37   void bar();
38 };
39 
40 struct DynamicFromStatic : StaticBase {
41   virtual void bar();
42 };
43 
44 struct DynamicFromVirtualStatic1 : virtual StaticBase {
45 };
46 
47 struct DynamicFromVirtualStatic2 : virtual StaticBase {
48 };
49 
50 struct DynamicFrom2Virtuals :
51             DynamicFromVirtualStatic1,
52             DynamicFromVirtualStatic2 {
53 };
54 
55 // CHECK-NEW-LABEL: define void @_Z12LocalObjectsv()
56 // CHECK-NEW-NOT: @llvm.invariant.group.barrier(
57 // CHECK-NEW-LABEL: }
58 void LocalObjects() {
59   DynamicBase1 DB;
60   DB.foo();
61   DynamicDerived DD;
62   DD.foo();
63 
64   DynamicBase2 DB2;
65   DB2.bar();
66 
67   StaticBase SB;
68   SB.bar();
69 
70   DynamicDerivedMultiple DDM;
71   DDM.foo();
72   DDM.bar();
73 
74   DynamicFromStatic DFS;
75   DFS.bar();
76   DynamicFromVirtualStatic1 DFVS1;
77   DFVS1.bar();
78   DynamicFrom2Virtuals DF2V;
79   DF2V.bar();
80 }
81 
82 struct DynamicFromVirtualStatic1;
83 // CHECK-CTORS-LABEL: define linkonce_odr void @_ZN25DynamicFromVirtualStatic1C1Ev
84 // CHECK-CTORS-NOT: @llvm.invariant.group.barrier(
85 // CHECK-CTORS-LABEL: }
86 
87 struct DynamicFrom2Virtuals;
88 // CHECK-CTORS-LABEL: define linkonce_odr void @_ZN20DynamicFrom2VirtualsC1Ev
89 // CHECK-CTORS: call i8* @llvm.invariant.group.barrier(
90 // CHECK-CTORS-LABEL: }
91 
92 
93 // CHECK-NEW-LABEL: define void @_Z9Pointers1v()
94 // CHECK-NEW-NOT: @llvm.invariant.group.barrier(
95 // CHECK-NEW-LABEL: call void @_ZN12DynamicBase1C1Ev(
96 
97 // CHECK-NEW: %[[THIS3:.*]] = call i8* @llvm.invariant.group.barrier(i8* %[[THIS2:.*]])
98 // CHECK-NEW: %[[THIS4:.*]] = bitcast i8* %[[THIS3]] to %[[DynamicDerived:.*]]*
99 // CHECK-NEW: call void @_ZN14DynamicDerivedC1Ev(%[[DynamicDerived:.*]]* %[[THIS4]])
100 // CHECK-NEW-LABEL: }
101 void Pointers1() {
102   DynamicBase1 *DB = new DynamicBase1;
103   DB->foo();
104 
105   DynamicDerived *DD = new (DB) DynamicDerived;
106   DD->foo();
107   DD->~DynamicDerived();
108 }
109 
110 // CHECK-NEW-LABEL: define void @_Z14HackingObjectsv()
111 // CHECK-NEW:  call void @_ZN12DynamicBase1C1Ev
112 // CHECK-NEW:  call i8* @llvm.invariant.group.barrier(
113 // CHECK-NEW:  call void @_ZN14DynamicDerivedC1Ev(
114 // CHECK-NEW:  call i8* @llvm.invariant.group.barrier(
115 // CHECK-NEW: call void @_ZN12DynamicBase1C1Ev(
116 // CHECK-NEW-LABEL: }
117 void HackingObjects() {
118   DynamicBase1 DB;
119   DB.foo();
120 
121   DynamicDerived *DB2 = new (&DB) DynamicDerived;
122   // Using DB now is prohibited.
123   DB2->foo();
124   DB2->~DynamicDerived();
125 
126   // We have to get back to the previous type to avoid calling wrong destructor
127   new (&DB) DynamicBase1;
128   DB.foo();
129 }
130 
131 /*** Testing Constructors ***/
132 struct DynamicBase1;
133 // CHECK-CTORS-LABEL: define linkonce_odr void @_ZN12DynamicBase1C2Ev(
134 // CHECK-CTORS-NOT: call i8* @llvm.invariant.group.barrier(
135 // CHECK-CTORS-LABEL: }
136 
137 
138 struct DynamicDerived;
139 // CHECK-CTORS-LABEL: define linkonce_odr void @_ZN14DynamicDerivedC2Ev(
140 // CHECK-CTORS: call void @_ZN12DynamicBase1C2Ev(
141 // CHECK-CTORS: %[[THIS1:.*]] = bitcast %[[DynamicDerived:.*]]* %[[THIS0:.*]] to i8*
142 // CHECK-CTORS: %[[THIS2:.*]] = call i8* @llvm.invariant.group.barrier(i8* %[[THIS1:.*]])
143 // CHECK-CTORS: %[[THIS3:.*]] = bitcast i8* %[[THIS2:.*]] to %[[DynamicDerived]]*
144 // CHECK-CTORS: %[[THIS4:.*]] = bitcast %struct.DynamicDerived* %[[THIS3:.*]] to i32 (...)***
145 // CHECK-CTORS: store {{.*}} %[[THIS4:.*]]
146 // CHECK-CTORS-LABEL: }
147 
148 struct DynamicDerivedMultiple;
149 // CHECK-CTORS-LABEL: define linkonce_odr void @_ZN22DynamicDerivedMultipleC2Ev
150 // CHECK-CTORS: call void @_ZN12DynamicBase1C2Ev(
151 // CHECK-CTORS-NOT: @llvm.invariant.group.barrier
152 // CHECK-CTORS-LABEL: call void @_ZN12DynamicBase2C2Ev(
153 // CHECK-CTORS: %[[THIS1:.*]] = bitcast %[[CLASS:.*]]* %[[THIS0:.*]] to i8*
154 // CHECK-CTORS: %[[THIS2:.*]] = call i8* @llvm.invariant.group.barrier(i8* %[[THIS1:.*]])
155 // CHECK-CTORS: %[[THIS3:.*]] = bitcast i8* %[[THIS2:.*]] to %[[CLASS]]*
156 // CHECK-CTORS-NOT: invariant.group.barrier
157 // CHECK-CTORS: store {{.*}} @_ZTV22DynamicDerivedMultiple, i64 0, i64 2)
158 // CHECK-CTORS: store {{.*}} @_ZTV22DynamicDerivedMultiple, i64 0, i64 6)
159 // CHECK-CTORS-LABEL: }
160 
161 struct DynamicFromStatic;
162 // CHECK-CTORS-LABEL: define linkonce_odr void @_ZN17DynamicFromStaticC2Ev(
163 // CHECK-CTORS-NOT: @llvm.invariant.group.barrier(
164 // CHECK-CTORS-LABEL: }
165 
166 
167 /** DTORS **/
168 // CHECK-DTORS-LABEL: define linkonce_odr void @_ZN10StaticBaseD2Ev(
169 // CHECK-DTORS-NOT: call i8* @llvm.invariant.group.barrier(
170 // CHECK-DTORS-LABEL: }
171 
172 
173 // CHECK-DTORS-LABEL: define linkonce_odr void @_ZN25DynamicFromVirtualStatic2D2Ev(
174 // CHECK-DTORS-NOT: invariant.barrier
175 // CHECK-DTORS-LABEL: }
176 
177 // CHECK-DTORS-LABEL: define linkonce_odr void @_ZN17DynamicFromStaticD2Ev
178 // CHECK-DTORS-NOT: call i8* @llvm.invariant.group.barrier(
179 // CHECK-DTORS-LABEL: }
180 
181 
182 // CHECK-DTORS-LABEL: define linkonce_odr void @_ZN22DynamicDerivedMultipleD2Ev(
183 
184 // CHECK-DTORS-LABEL: define linkonce_odr void @_ZN12DynamicBase2D2Ev(
185 // CHECK-DTORS: call i8* @llvm.invariant.group.barrier(
186 // CHECK-DTORS-LABEL: }
187 
188 // CHECK-DTORS-LABEL: define linkonce_odr void @_ZN12DynamicBase1D2Ev
189 // CHECK-DTORS: call i8* @llvm.invariant.group.barrier(
190 // CHECK-DTORS-LABEL: }
191 
192 // CHECK-DTORS-LABEL: define linkonce_odr void @_ZN14DynamicDerivedD2Ev
193 // CHECK-DTORS-NOT: call i8* @llvm.invariant.group.barrier(
194 // CHECK-DTORS-LABEL: }
195 
196 
197 // CHECK-LINK-REQ: !llvm.module.flags = !{![[FIRST:.*]], ![[SEC:.*]]{{.*}}}
198 
199 // CHECK-LINK-REQ: ![[FIRST]] = !{i32 1, !"StrictVTablePointers", i32 1}
200 // CHECK-LINK-REQ: ![[SEC]] = !{i32 3, !"StrictVTablePointersRequirement", ![[META:.*]]}
201 // CHECK-LINK-REQ: ![[META]] = !{!"StrictVTablePointers", i32 1}
202 
203