blob: b945e569afb6b4d818912697e4f45e520852ce0c [file] [log] [blame]
Douglas Gregor6fb745b2010-05-13 16:44:06 +00001// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o %t
John McCall279b5eb2010-08-12 23:36:15 +00002// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -fhidden-weak-vtables -emit-llvm -o %t.hidden
Richard Smithb4127a22013-02-16 00:51:21 +00003// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -disable-llvm-optzns -O3 -emit-llvm -o %t.opt
Douglas Gregor6fb745b2010-05-13 16:44:06 +00004// RUN: FileCheck --check-prefix=CHECK-1 %s < %t
5// RUN: FileCheck --check-prefix=CHECK-2 %s < %t
John McCall279b5eb2010-08-12 23:36:15 +00006// RUN: FileCheck --check-prefix=CHECK-2-HIDDEN %s < %t.hidden
Douglas Gregor6fb745b2010-05-13 16:44:06 +00007// RUN: FileCheck --check-prefix=CHECK-3 %s < %t
8// RUN: FileCheck --check-prefix=CHECK-4 %s < %t
9// RUN: FileCheck --check-prefix=CHECK-5 %s < %t
John McCall279b5eb2010-08-12 23:36:15 +000010// RUN: FileCheck --check-prefix=CHECK-5-HIDDEN %s < %t.hidden
Douglas Gregor6fb745b2010-05-13 16:44:06 +000011// RUN: FileCheck --check-prefix=CHECK-6 %s < %t
John McCall279b5eb2010-08-12 23:36:15 +000012// RUN: FileCheck --check-prefix=CHECK-6-HIDDEN %s < %t.hidden
Douglas Gregor6fb745b2010-05-13 16:44:06 +000013// RUN: FileCheck --check-prefix=CHECK-7 %s < %t
14// RUN: FileCheck --check-prefix=CHECK-8 %s < %t
15// RUN: FileCheck --check-prefix=CHECK-9 %s < %t
Richard Smithb4127a22013-02-16 00:51:21 +000016// RUN: FileCheck --check-prefix=CHECK-9-OPT %s < %t.opt
Douglas Gregor6fb745b2010-05-13 16:44:06 +000017// RUN: FileCheck --check-prefix=CHECK-10 %s < %t
Richard Smithb4127a22013-02-16 00:51:21 +000018// RUN: FileCheck --check-prefix=CHECK-10-OPT %s < %t.opt
Douglas Gregor6fb745b2010-05-13 16:44:06 +000019// RUN: FileCheck --check-prefix=CHECK-11 %s < %t
20// RUN: FileCheck --check-prefix=CHECK-12 %s < %t
John McCall0c7d32b2010-08-04 06:38:15 +000021// RUN: FileCheck --check-prefix=CHECK-13 %s < %t
Anders Carlsson7ca46432009-12-05 17:04:47 +000022
23namespace {
Anders Carlsson7ca46432009-12-05 17:04:47 +000024 struct A {
25 virtual void f() { }
26 };
Anders Carlsson7ca46432009-12-05 17:04:47 +000027}
28
Anders Carlsson152d4dc2009-12-05 22:19:10 +000029void f() { A b; }
30
31struct B {
32 B();
33 virtual void f();
34};
35
36B::B() { }
37
Anders Carlsson691222d2011-01-29 19:34:19 +000038struct C : virtual B {
Anders Carlsson891c8b72009-12-05 22:24:38 +000039 C();
40 virtual void f() { }
41};
42
43C::C() { }
44
Anders Carlsson5794c972009-12-06 00:53:22 +000045struct D {
46 virtual void f();
47};
48
49void D::f() { }
50
Eli Friedman470fb732009-12-11 20:48:18 +000051static struct : D { } e;
52
Douglas Gregor074a2cf2010-01-05 21:40:05 +000053// The destructor is the key function.
Douglas Gregorbd6d6192010-01-05 19:06:31 +000054template<typename T>
55struct E {
56 virtual ~E();
57};
58
59template<typename T> E<T>::~E() { }
60
Douglas Gregor074a2cf2010-01-05 21:40:05 +000061// Anchor is the key function
Douglas Gregorbd6d6192010-01-05 19:06:31 +000062template<>
63struct E<char> {
64 virtual void anchor();
65};
66
67void E<char>::anchor() { }
68
69template struct E<short>;
70extern template struct E<int>;
71
72void use_E() {
73 E<int> ei;
74 (void)ei;
75 E<long> el;
76 (void)el;
77}
78
Douglas Gregor074a2cf2010-01-05 21:40:05 +000079// No key function
80template<typename T>
81struct F {
82 virtual void foo() { }
83};
84
85// No key function
86template<>
87struct F<char> {
88 virtual void foo() { }
89};
90
91template struct F<short>;
92extern template struct F<int>;
93
John McCall0c7d32b2010-08-04 06:38:15 +000094void use_F() {
95 F<char> fc;
96 fc.foo();
Douglas Gregor074a2cf2010-01-05 21:40:05 +000097 F<int> fi;
Douglas Gregor6fb745b2010-05-13 16:44:06 +000098 fi.foo();
Douglas Gregor074a2cf2010-01-05 21:40:05 +000099 F<long> fl;
100 (void)fl;
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000101}
102
Anders Carlsson152d4dc2009-12-05 22:19:10 +0000103// B has a key function that is not defined in this translation unit so its vtable
104// has external linkage.
Rafael Espindolacd3ac4b2011-01-15 08:23:14 +0000105// CHECK-1: @_ZTV1B = external unnamed_addr constant
Anders Carlsson152d4dc2009-12-05 22:19:10 +0000106
John McCall3d640e62010-08-03 07:24:12 +0000107// C has no key function, so its vtable should have weak_odr linkage
108// and hidden visibility (rdar://problem/7523229).
Anders Carlssonf502d932011-01-24 00:46:19 +0000109// CHECK-2: @_ZTV1C = linkonce_odr unnamed_addr constant
110// CHECK-2: @_ZTS1C = linkonce_odr constant
111// CHECK-2: @_ZTI1C = linkonce_odr unnamed_addr constant
Anders Carlsson691222d2011-01-29 19:34:19 +0000112// CHECK-2: @_ZTT1C = linkonce_odr unnamed_addr constant
Anders Carlssonf502d932011-01-24 00:46:19 +0000113// CHECK-2-HIDDEN: @_ZTV1C = linkonce_odr hidden unnamed_addr constant
114// CHECK-2-HIDDEN: @_ZTS1C = linkonce_odr constant
115// CHECK-2-HIDDEN: @_ZTI1C = linkonce_odr hidden unnamed_addr constant
Anders Carlsson691222d2011-01-29 19:34:19 +0000116// CHECK-2-HIDDEN: @_ZTT1C = linkonce_odr hidden unnamed_addr constant
Anders Carlsson891c8b72009-12-05 22:24:38 +0000117
Anders Carlsson5794c972009-12-06 00:53:22 +0000118// D has a key function that is defined in this translation unit so its vtable is
119// defined in the translation unit.
Rafael Espindola9f959db2011-01-11 21:10:26 +0000120// CHECK-3: @_ZTV1D = unnamed_addr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000121// CHECK-3: @_ZTS1D = constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000122// CHECK-3: @_ZTI1D = unnamed_addr constant
Anders Carlsson5794c972009-12-06 00:53:22 +0000123
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000124// E<char> is an explicit specialization with a key function defined
125// in this translation unit, so its vtable should have external
126// linkage.
Rafael Espindola9f959db2011-01-11 21:10:26 +0000127// CHECK-4: @_ZTV1EIcE = unnamed_addr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000128// CHECK-4: @_ZTS1EIcE = constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000129// CHECK-4: @_ZTI1EIcE = unnamed_addr constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000130
131// E<short> is an explicit template instantiation with a key function
132// defined in this translation unit, so its vtable should have
133// weak_odr linkage.
Rafael Espindola9f959db2011-01-11 21:10:26 +0000134// CHECK-5: @_ZTV1EIsE = weak_odr unnamed_addr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000135// CHECK-5: @_ZTS1EIsE = weak_odr constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000136// CHECK-5: @_ZTI1EIsE = weak_odr unnamed_addr constant
Rafael Espindola9f959db2011-01-11 21:10:26 +0000137// CHECK-5-HIDDEN: @_ZTV1EIsE = weak_odr unnamed_addr constant
John McCall279b5eb2010-08-12 23:36:15 +0000138// CHECK-5-HIDDEN: @_ZTS1EIsE = weak_odr constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000139// CHECK-5-HIDDEN: @_ZTI1EIsE = weak_odr unnamed_addr constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000140
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000141// F<short> is an explicit template instantiation without a key
142// function, so its vtable should have weak_odr linkage
Rafael Espindola9f959db2011-01-11 21:10:26 +0000143// CHECK-6: @_ZTV1FIsE = weak_odr unnamed_addr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000144// CHECK-6: @_ZTS1FIsE = weak_odr constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000145// CHECK-6: @_ZTI1FIsE = weak_odr unnamed_addr constant
Rafael Espindola9f959db2011-01-11 21:10:26 +0000146// CHECK-6-HIDDEN: @_ZTV1FIsE = weak_odr unnamed_addr constant
John McCall279b5eb2010-08-12 23:36:15 +0000147// CHECK-6-HIDDEN: @_ZTS1FIsE = weak_odr constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000148// CHECK-6-HIDDEN: @_ZTI1FIsE = weak_odr unnamed_addr constant
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000149
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000150// E<long> is an implicit template instantiation with a key function
151// defined in this translation unit, so its vtable should have
Anders Carlssonf502d932011-01-24 00:46:19 +0000152// linkonce_odr linkage.
153// CHECK-7: @_ZTV1EIlE = linkonce_odr unnamed_addr constant
154// CHECK-7: @_ZTS1EIlE = linkonce_odr constant
155// CHECK-7: @_ZTI1EIlE = linkonce_odr unnamed_addr constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000156
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000157// F<long> is an implicit template instantiation with no key function,
Anders Carlssonf502d932011-01-24 00:46:19 +0000158// so its vtable should have linkonce_odr linkage.
159// CHECK-8: @_ZTV1FIlE = linkonce_odr unnamed_addr constant
160// CHECK-8: @_ZTS1FIlE = linkonce_odr constant
161// CHECK-8: @_ZTI1FIlE = linkonce_odr unnamed_addr constant
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000162
163// F<int> is an explicit template instantiation declaration without a
Rafael Espindola35d64612010-04-03 04:26:42 +0000164// key function, so its vtable should have external linkage.
Rafael Espindolacd3ac4b2011-01-15 08:23:14 +0000165// CHECK-9: @_ZTV1FIiE = external unnamed_addr constant
Richard Smithb4127a22013-02-16 00:51:21 +0000166// CHECK-9-OPT: @_ZTV1FIiE = available_externally unnamed_addr constant
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000167
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000168// E<int> is an explicit template instantiation declaration. It has a
Douglas Gregorc84622a2010-01-07 04:09:30 +0000169// key function that is not instantiated, so we should only reference
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000170// its vtable, not define it.
Rafael Espindolacd3ac4b2011-01-15 08:23:14 +0000171// CHECK-10: @_ZTV1EIiE = external unnamed_addr constant
Richard Smithb4127a22013-02-16 00:51:21 +0000172// CHECK-10-OPT: @_ZTV1EIiE = available_externally unnamed_addr constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000173
Eli Friedman6c6bda32010-01-08 00:50:11 +0000174// The anonymous struct for e has no linkage, so the vtable should have
175// internal linkage.
Rafael Espindola9f959db2011-01-11 21:10:26 +0000176// CHECK-11: @"_ZTV3$_0" = internal unnamed_addr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000177// CHECK-11: @"_ZTS3$_0" = internal constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000178// CHECK-11: @"_ZTI3$_0" = internal unnamed_addr constant
Eli Friedman6c6bda32010-01-08 00:50:11 +0000179
Anders Carlsson152d4dc2009-12-05 22:19:10 +0000180// The A vtable should have internal linkage since it is inside an anonymous
181// namespace.
Rafael Espindola9f959db2011-01-11 21:10:26 +0000182// CHECK-12: @_ZTVN12_GLOBAL__N_11AE = internal unnamed_addr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000183// CHECK-12: @_ZTSN12_GLOBAL__N_11AE = internal constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000184// CHECK-12: @_ZTIN12_GLOBAL__N_11AE = internal unnamed_addr constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000185
John McCall0c7d32b2010-08-04 06:38:15 +0000186// F<char> is an explicit specialization without a key function, so
Anders Carlssonf502d932011-01-24 00:46:19 +0000187// its vtable should have linkonce_odr linkage.
188// CHECK-13: @_ZTV1FIcE = linkonce_odr unnamed_addr constant
189// CHECK-13: @_ZTS1FIcE = linkonce_odr constant
190// CHECK-13: @_ZTI1FIcE = linkonce_odr unnamed_addr constant
John McCall0c7d32b2010-08-04 06:38:15 +0000191
Daniel Dunbarb9aefa72010-05-25 00:33:13 +0000192// RUN: FileCheck --check-prefix=CHECK-G %s < %t
193//
Anders Carlssonf502d932011-01-24 00:46:19 +0000194// CHECK-G: @_ZTV1GIiE = linkonce_odr unnamed_addr constant
Daniel Dunbarb9aefa72010-05-25 00:33:13 +0000195template <typename T>
196class G {
197public:
198 G() {}
199 virtual void f0();
200 virtual void f1();
201};
202template <>
203void G<int>::f1() {}
204template <typename T>
205void G<T>::f0() {}
206void G_f0() { new G<int>(); }
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000207
208// RUN: FileCheck --check-prefix=CHECK-H %s < %t
209
210// H<int> has a key function without a body but it's a template instantiation
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000211// so its VTable must be emitted.
Anders Carlssonf502d932011-01-24 00:46:19 +0000212// CHECK-H: @_ZTV1HIiE = linkonce_odr unnamed_addr constant
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000213template <typename T>
214class H {
215public:
216 virtual ~H();
217};
218
219void use_H() {
220 H<int> h;
221}
Richard Smithb4127a22013-02-16 00:51:21 +0000222
223// RUN: FileCheck --check-prefix=CHECK-I %s < %t
224// RUN: FileCheck --check-prefix=CHECK-I-OPT %s < %t.opt
225
226// I<int> has an explicit instantiation declaration and needs a VTT and
227// construction vtables. We emit the VTT available_externally, but point it at
228// internal construction vtables because there is no way to form a reference to
229// the real construction vtables.
230
231// CHECK-I: @_ZTV1IIiE = external unnamed_addr constant
232// CHECK-I: @_ZTT1IIiE = external unnamed_addr constant
233// CHECK-I-NOT: @_ZTC1IIiE
234//
235// CHECK-I-OPT: @_ZTV1IIiE = available_externally unnamed_addr constant
236// CHECK-I-OPT: @_ZTT1IIiE = available_externally unnamed_addr constant {{.*}} @_ZTC1IIiE0_6VBase2
237// CHECK-I-OPT: @_ZTC1IIiE0_6VBase2 = internal unnamed_addr constant
238struct VBase1 { virtual void f(); }; struct VBase2 : virtual VBase1 {};
239template<typename T>
240struct I : VBase2 {};
241extern template struct I<int>;
242I<int> i;