1 // RUN: %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -std=c++98 -emit-llvm -o %t
2 // RUN: %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -std=c++98 -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 // Can emit D's vtable available_externally.
279 // CHECK-TEST11: @_ZTVN6Test111DE = available_externally unnamed_addr constant
280 struct D : C {
281   virtual void key();
282 };
283 D f();
284 
285 void g(D& a) {
286   C c;
287   c = a;
288   a.key();
289   a.key();
290 }
291 void g() {
292   D d;
293   d = f();
294   g(d);
295 }
296 }  // Test 11
297 
298 namespace Test12 {
299 
300 // CHECK-TEST12: @_ZTVN6Test121AE = external unnamed_addr constant
301 struct A {
302   virtual void foo();
303   virtual ~A() {}
304 };
305 // CHECK-TEST12: @_ZTVN6Test121BE = external unnamed_addr constant
306 struct B : A {
307   void foo();
308 };
309 
310 void g() {
311   A a;
312   a.foo();
313   B b;
314   b.foo();
315 }
316 }
317 
318 namespace Test13 {
319 
320 // CHECK-TEST13-DAG: @_ZTVN6Test131AE = available_externally unnamed_addr constant
321 // CHECK-TEST13-DAG: @_ZTVN6Test131BE = external unnamed_addr constant
322 struct A {
323   virtual ~A();
324 };
325 struct B : A {
326   virtual void f();
327   void operator delete(void *);
328   ~B() {}
329 };
330 
331 void g() {
332   A *b = new B;
333 }
334 }
335 
336 namespace Test14 {
337 
338 // CHECK-TEST14: @_ZTVN6Test141AE = available_externally unnamed_addr constant
339 struct A {
340   virtual void f();
341   void operator delete(void *);
342   ~A();
343 };
344 
345 void g() {
346   A *b = new A;
347   delete b;
348 }
349 }
350 
351 namespace Test15 {
352 // In this test D's vtable has two slots for function f(), but uses only one,
353 // so the second slot is set to null.
354 // CHECK-TEST15: @_ZTVN6Test151DE = available_externally unnamed_addr constant
355 struct A { virtual void f() {} };
356 struct B : virtual A {};
357 struct C : virtual A {};
358 struct D : B, C {
359   virtual void g();
360   void f();
361 };
362 
363 void test() {
364   D * d = new D;
365   d->f();
366 }
367 }
368 
369 namespace Test16 {
370 // S has virtual method that is hidden, because of it we can't
371 // generate available_externally vtable for it.
372 // CHECK-TEST16-DAG: @_ZTVN6Test161SE = external unnamed_addr constant
373 // CHECK-TEST16-DAG: @_ZTVN6Test162S2E = available_externally
374 
375 struct S {
376   __attribute__((visibility("hidden"))) virtual void doStuff();
377 };
378 
379 struct S2 {
380   virtual void doStuff();
381   __attribute__((visibility("hidden"))) void unused();
382 
383 };
384 
385 void test() {
386   S *s = new S;
387   s->doStuff();
388 
389   S2 *s2 = new S2;
390   s2->doStuff();
391 }
392 }
393 
394 namespace Test17 {
395 // This test checks if we emit vtables opportunistically.
396 // CHECK-TEST17-DAG: @_ZTVN6Test171AE = available_externally
397 // CHECK-TEST17-DAG: @_ZTVN6Test171BE = external
398 
399 struct A {
400   virtual void key();
401   virtual void bar() {}
402 };
403 
404 // We won't gonna use deleting destructor for this type, which will disallow
405 // emitting vtable as available_externally
406 struct B {
407   virtual void key();
408   virtual ~B() {}
409 };
410 
411 void testcaseA() {
412   A a;
413   a.bar(); // this forces to emit definition of bar
414 }
415 
416 void testcaseB() {
417   B b; // This only forces emitting of complete object destructor
418 }
419 
420 } // namespace Test17
421