1 // RUN: %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -emit-llvm -o %t
2 // RUN: %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -O2 -disable-llvm-passes -emit-llvm -o %t.opt
3 // RUN: FileCheck --check-prefix=CHECK-TEST1 %s < %t
4 // RUN: FileCheck --check-prefix=CHECK-TEST2 %s < %t
5 // RUN: FileCheck --check-prefix=CHECK-TEST5 %s < %t
6 // RUN: FileCheck --check-prefix=CHECK-TEST8 %s < %t.opt
7 // RUN: FileCheck --check-prefix=CHECK-TEST9 %s < %t.opt
8 // RUN: FileCheck --check-prefix=CHECK-TEST10 %s < %t.opt
9 // RUN: FileCheck --check-prefix=CHECK-TEST11 %s < %t.opt
10 // RUN: FileCheck --check-prefix=CHECK-TEST12 %s < %t.opt
11 // RUN: FileCheck --check-prefix=CHECK-TEST13 %s < %t.opt
12 // RUN: FileCheck --check-prefix=CHECK-TEST14 %s < %t.opt
13 // RUN: FileCheck --check-prefix=CHECK-TEST15 %s < %t.opt
14 // RUN: FileCheck --check-prefix=CHECK-TEST16 %s < %t.opt
15 // RUN: FileCheck --check-prefix=CHECK-TEST17 %s < %t.opt
16 
17 #include <typeinfo>
18 
19 // CHECK-TEST1: @_ZTVN5Test11AE = external unnamed_addr constant
20 namespace Test1 {
21 
22 struct A {
23   A();
24   virtual void f();
25   virtual ~A() { }
26 };
27 
28 A::A() { }
29 
30 void f(A* a) {
31   a->f();
32 };
33 
34 // CHECK-LABEL: define void @_ZN5Test11gEv
35 // CHECK: call void @_ZN5Test11A1fEv
36 void g() {
37   A a;
38   f(&a);
39 }
40 
41 }
42 
43 // Test2::A's key function (f) is defined in this translation unit, but when
44 // we're doing codegen for the typeid(A) call, we don't know that yet.
45 // This tests mainly that the typeinfo and typename constants have their linkage
46 // updated correctly.
47 
48 // CHECK-TEST2: @_ZTSN5Test21AE = constant
49 // CHECK-TEST2: @_ZTIN5Test21AE = constant
50 // CHECK-TEST2: @_ZTVN5Test21AE = unnamed_addr constant
51 namespace Test2 {
52   struct A {
53     virtual void f();
54   };
55 
56   const std::type_info &g() {
57     return typeid(A);
58   };
59 
60   void A::f() { }
61 }
62 
63 // Test that we don't assert on this test.
64 namespace Test3 {
65 
66 struct A {
67   virtual void f();
68   virtual ~A() { }
69 };
70 
71 struct B : A {
72   B();
73   virtual void f();
74 };
75 
76 B::B() { }
77 
78 void g(A* a) {
79   a->f();
80 };
81 
82 }
83 
84 // PR9114, test that we don't try to instantiate RefPtr<Node>.
85 namespace Test4 {
86 
87 template <class T> struct RefPtr {
88   T* p;
89   ~RefPtr() {
90     p->deref();
91   }
92 };
93 
94 struct A {
95   virtual ~A();
96 };
97 
98 struct Node;
99 
100 struct B : A {
101   virtual void deref();
102   RefPtr<Node> m;
103 };
104 
105 void f() {
106   RefPtr<B> b;
107 }
108 
109 }
110 
111 // PR9130, test that we emit a definition of A::f.
112 // CHECK-TEST5-LABEL: define linkonce_odr void @_ZN5Test51A1fEv
113 namespace Test5 {
114 
115 struct A {
116   virtual void f() { }
117 };
118 
119 struct B : A {
120   virtual ~B();
121 };
122 
123 B::~B() { }
124 
125 }
126 
127 // Check that we don't assert on this test.
128 namespace Test6 {
129 
130 struct A {
131   virtual ~A();
132   int a;
133 };
134 
135 struct B {
136   virtual ~B();
137   int b;
138 };
139 
140 struct C : A, B {
141   C();
142 };
143 
144 struct D : C {
145   virtual void f();
146   D();
147 };
148 
149 D::D() { }
150 
151 }
152 
153 namespace Test7 {
154 
155 struct c1 {};
156 struct c10 : c1{
157   virtual void foo ();
158 };
159 struct c11 : c10, c1{
160   virtual void f6 ();
161 };
162 struct c28 : virtual c11{
163   void f6 ();
164 };
165 }
166 
167 namespace Test8 {
168 // CHECK-TEST8: @_ZTVN5Test81YE = available_externally unnamed_addr constant
169 // vtable for X is not generated because there are no stores here
170 struct X {
171   X();
172   virtual void foo();
173 };
174 struct Y : X {
175   void foo();
176 };
177 
178 void g(X* p) { p->foo(); }
179 void f() {
180   Y y;
181   g(&y);
182   X x;
183   g(&x);
184 }
185 
186 }  // Test8
187 
188 namespace Test9 {
189 // All virtual functions are outline, so we can assume that it will
190 // be generated in translation unit where foo is defined.
191 // CHECK-TEST9-DAG: @_ZTVN5Test91AE = available_externally unnamed_addr constant
192 // CHECK-TEST9-DAG: @_ZTVN5Test91BE = available_externally unnamed_addr constant
193 struct A {
194   virtual void foo();
195   virtual void bar();
196 };
197 void A::bar() {}
198 
199 struct B : A {
200   void foo();
201 };
202 
203 void g() {
204   A a;
205   a.foo();
206   B b;
207   b.foo();
208 }
209 
210 }  // Test9
211 
212 namespace Test10 {
213 
214 // because A's key function is defined here, vtable is generated in this TU
215 // CHECK-TEST10-DAG: @_ZTVN6Test101AE = unnamed_addr constant
216 struct A {
217   virtual void foo();
218   virtual void bar();
219 };
220 void A::foo() {}
221 
222 // Because key function is inline we will generate vtable as linkonce_odr.
223 // CHECK-TEST10-DAG: @_ZTVN6Test101DE = linkonce_odr unnamed_addr constant
224 struct D : A {
225   void bar();
226 };
227 inline void D::bar() {}
228 
229 // Because B has outline all virtual functions, we can refer to them.
230 // CHECK-TEST10-DAG: @_ZTVN6Test101BE = available_externally unnamed_addr constant
231 struct B : A {
232   void foo();
233   void bar();
234 };
235 
236 // C's key function (car) is outline, but C has inline virtual function so we
237 // can't guarantee that we will be able to refer to bar from name
238 // so (at the moment) we can't emit vtable available_externally.
239 // CHECK-TEST10-DAG: @_ZTVN6Test101CE = external unnamed_addr constant
240 struct C : A {
241   void bar() {}               // defined in body - not key function
242   virtual inline void gar();  // inline in body - not key function
243   virtual void car();
244 };
245 
246 // no key function, vtable will be generated everywhere it will be used
247 // CHECK-TEST10-DAG: @_ZTVN6Test101EE = linkonce_odr unnamed_addr constant
248 struct E : A {};
249 
250 void g(A& a) {
251   a.foo();
252   a.bar();
253 }
254 
255 void f() {
256   A a;
257   g(a);
258   B b;
259   g(b);
260   C c;
261   g(c);
262   D d;
263   g(d);
264   E e;
265   g(e);
266 }
267 
268 }  // Test10
269 
270 namespace Test11 {
271 struct D;
272 // Can emit C's vtable available_externally.
273 // CHECK-TEST11: @_ZTVN6Test111CE = available_externally unnamed_addr constant
274 struct C {
275   virtual D& operator=(const D&);
276 };
277 
278 // Cannot emit D's vtable available_externally, because we cannot create
279 // a reference to the inline virtual D::operator= function.
280 // CHECK-TEST11: @_ZTVN6Test111DE = external unnamed_addr constant
281 struct D : C {
282   virtual void key();
283 };
284 D f();
285 
286 void g(D& a) {
287   C c;
288   c = a;
289   a.key();
290   a.key();
291 }
292 void g() {
293   D d;
294   d = f();
295   g(d);
296 }
297 }  // Test 11
298 
299 namespace Test12 {
300 
301 // CHECK-TEST12: @_ZTVN6Test121AE = external unnamed_addr constant
302 struct A {
303   virtual void foo();
304   virtual ~A() {}
305 };
306 // CHECK-TEST12: @_ZTVN6Test121BE = external unnamed_addr constant
307 struct B : A {
308   void foo();
309 };
310 
311 void g() {
312   A a;
313   a.foo();
314   B b;
315   b.foo();
316 }
317 }
318 
319 namespace Test13 {
320 
321 // CHECK-TEST13-DAG: @_ZTVN6Test131AE = available_externally unnamed_addr constant
322 // CHECK-TEST13-DAG: @_ZTVN6Test131BE = external unnamed_addr constant
323 struct A {
324   virtual ~A();
325 };
326 struct B : A {
327   virtual void f();
328   void operator delete(void *);
329   ~B() {}
330 };
331 
332 void g() {
333   A *b = new B;
334 }
335 }
336 
337 namespace Test14 {
338 
339 // CHECK-TEST14: @_ZTVN6Test141AE = available_externally unnamed_addr constant
340 struct A {
341   virtual void f();
342   void operator delete(void *);
343   ~A();
344 };
345 
346 void g() {
347   A *b = new A;
348   delete b;
349 }
350 }
351 
352 namespace Test15 {
353 // In this test D's vtable has two slots for function f(), but uses only one,
354 // so the second slot is set to null.
355 // CHECK-TEST15: @_ZTVN6Test151DE = available_externally unnamed_addr constant
356 struct A { virtual void f() {} };
357 struct B : virtual A {};
358 struct C : virtual A {};
359 struct D : B, C {
360   virtual void g();
361   void f();
362 };
363 
364 void test() {
365   D * d = new D;
366   d->f();
367 }
368 }
369 
370 namespace Test16 {
371 // S has virtual method that is hidden, because of it we can't
372 // generate available_externally vtable for it.
373 // CHECK-TEST16-DAG: @_ZTVN6Test161SE = external unnamed_addr constant
374 // CHECK-TEST16-DAG: @_ZTVN6Test162S2E = available_externally
375 
376 struct S {
377   __attribute__((visibility("hidden"))) virtual void doStuff();
378 };
379 
380 struct S2 {
381   virtual void doStuff();
382   __attribute__((visibility("hidden"))) void unused();
383 
384 };
385 
386 void test() {
387   S *s = new S;
388   s->doStuff();
389 
390   S2 *s2 = new S2;
391   s2->doStuff();
392 }
393 }
394 
395 namespace Test17 {
396 // This test checks if we emit vtables opportunistically.
397 // CHECK-TEST17-DAG: @_ZTVN6Test171AE = available_externally
398 // CHECK-TEST17-DAG: @_ZTVN6Test171BE = external
399 
400 struct A {
401   virtual void key();
402   virtual void bar() {}
403 };
404 
405 // We won't gonna use deleting destructor for this type, which will disallow
406 // emitting vtable as available_externally
407 struct B {
408   virtual void key();
409   virtual ~B() {}
410 };
411 
412 void testcaseA() {
413   A a;
414   a.bar(); // this forces to emit definition of bar
415 }
416 
417 void testcaseB() {
418   B b; // This only forces emitting of complete object destructor
419 }
420 
421 } // namespace Test17
422