blob: 265126882a1f6fab86724be487a2ce80ca871508 [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
Douglas Gregor6fb745b2010-05-13 16:44:06 +00003// RUN: FileCheck --check-prefix=CHECK-1 %s < %t
4// RUN: FileCheck --check-prefix=CHECK-2 %s < %t
John McCall279b5eb2010-08-12 23:36:15 +00005// RUN: FileCheck --check-prefix=CHECK-2-HIDDEN %s < %t.hidden
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006// RUN: FileCheck --check-prefix=CHECK-3 %s < %t
7// RUN: FileCheck --check-prefix=CHECK-4 %s < %t
8// RUN: FileCheck --check-prefix=CHECK-5 %s < %t
John McCall279b5eb2010-08-12 23:36:15 +00009// RUN: FileCheck --check-prefix=CHECK-5-HIDDEN %s < %t.hidden
Douglas Gregor6fb745b2010-05-13 16:44:06 +000010// RUN: FileCheck --check-prefix=CHECK-6 %s < %t
John McCall279b5eb2010-08-12 23:36:15 +000011// RUN: FileCheck --check-prefix=CHECK-6-HIDDEN %s < %t.hidden
Douglas Gregor6fb745b2010-05-13 16:44:06 +000012// RUN: FileCheck --check-prefix=CHECK-7 %s < %t
13// RUN: FileCheck --check-prefix=CHECK-8 %s < %t
14// RUN: FileCheck --check-prefix=CHECK-9 %s < %t
15// RUN: FileCheck --check-prefix=CHECK-10 %s < %t
16// RUN: FileCheck --check-prefix=CHECK-11 %s < %t
17// RUN: FileCheck --check-prefix=CHECK-12 %s < %t
John McCall0c7d32b2010-08-04 06:38:15 +000018// RUN: FileCheck --check-prefix=CHECK-13 %s < %t
Anders Carlsson7ca46432009-12-05 17:04:47 +000019
20namespace {
Anders Carlsson7ca46432009-12-05 17:04:47 +000021 struct A {
22 virtual void f() { }
23 };
Anders Carlsson7ca46432009-12-05 17:04:47 +000024}
25
Anders Carlsson152d4dc2009-12-05 22:19:10 +000026void f() { A b; }
27
28struct B {
29 B();
30 virtual void f();
31};
32
33B::B() { }
34
Anders Carlsson891c8b72009-12-05 22:24:38 +000035struct C {
36 C();
37 virtual void f() { }
38};
39
40C::C() { }
41
Anders Carlsson5794c972009-12-06 00:53:22 +000042struct D {
43 virtual void f();
44};
45
46void D::f() { }
47
Eli Friedman470fb732009-12-11 20:48:18 +000048static struct : D { } e;
49
Douglas Gregor074a2cf2010-01-05 21:40:05 +000050// The destructor is the key function.
Douglas Gregorbd6d6192010-01-05 19:06:31 +000051template<typename T>
52struct E {
53 virtual ~E();
54};
55
56template<typename T> E<T>::~E() { }
57
Douglas Gregor074a2cf2010-01-05 21:40:05 +000058// Anchor is the key function
Douglas Gregorbd6d6192010-01-05 19:06:31 +000059template<>
60struct E<char> {
61 virtual void anchor();
62};
63
64void E<char>::anchor() { }
65
66template struct E<short>;
67extern template struct E<int>;
68
69void use_E() {
70 E<int> ei;
71 (void)ei;
72 E<long> el;
73 (void)el;
74}
75
Douglas Gregor074a2cf2010-01-05 21:40:05 +000076// No key function
77template<typename T>
78struct F {
79 virtual void foo() { }
80};
81
82// No key function
83template<>
84struct F<char> {
85 virtual void foo() { }
86};
87
88template struct F<short>;
89extern template struct F<int>;
90
John McCall0c7d32b2010-08-04 06:38:15 +000091void use_F() {
92 F<char> fc;
93 fc.foo();
Douglas Gregor074a2cf2010-01-05 21:40:05 +000094 F<int> fi;
Douglas Gregor6fb745b2010-05-13 16:44:06 +000095 fi.foo();
Douglas Gregor074a2cf2010-01-05 21:40:05 +000096 F<long> fl;
97 (void)fl;
Douglas Gregor074a2cf2010-01-05 21:40:05 +000098}
99
Anders Carlsson152d4dc2009-12-05 22:19:10 +0000100// B has a key function that is not defined in this translation unit so its vtable
101// has external linkage.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000102// CHECK-1: @_ZTV1B = external constant
Anders Carlsson152d4dc2009-12-05 22:19:10 +0000103
John McCall3d640e62010-08-03 07:24:12 +0000104// C has no key function, so its vtable should have weak_odr linkage
105// and hidden visibility (rdar://problem/7523229).
John McCall279b5eb2010-08-12 23:36:15 +0000106// CHECK-2: @_ZTV1C = weak_odr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000107// CHECK-2: @_ZTS1C = weak_odr constant
John McCall279b5eb2010-08-12 23:36:15 +0000108// CHECK-2: @_ZTI1C = weak_odr constant
109// CHECK-2-HIDDEN: @_ZTV1C = weak_odr hidden constant
110// CHECK-2-HIDDEN: @_ZTS1C = weak_odr constant
111// CHECK-2-HIDDEN: @_ZTI1C = weak_odr hidden constant
Anders Carlsson891c8b72009-12-05 22:24:38 +0000112
Anders Carlsson5794c972009-12-06 00:53:22 +0000113// D has a key function that is defined in this translation unit so its vtable is
114// defined in the translation unit.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000115// CHECK-3: @_ZTV1D = constant
116// CHECK-3: @_ZTS1D = constant
117// CHECK-3: @_ZTI1D = constant
Anders Carlsson5794c972009-12-06 00:53:22 +0000118
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000119// E<char> is an explicit specialization with a key function defined
120// in this translation unit, so its vtable should have external
121// linkage.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000122// CHECK-4: @_ZTV1EIcE = constant
123// CHECK-4: @_ZTS1EIcE = constant
124// CHECK-4: @_ZTI1EIcE = constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000125
126// E<short> is an explicit template instantiation with a key function
127// defined in this translation unit, so its vtable should have
128// weak_odr linkage.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000129// CHECK-5: @_ZTV1EIsE = weak_odr constant
130// CHECK-5: @_ZTS1EIsE = weak_odr constant
131// CHECK-5: @_ZTI1EIsE = weak_odr constant
John McCall279b5eb2010-08-12 23:36:15 +0000132// CHECK-5-HIDDEN: @_ZTV1EIsE = weak_odr constant
133// CHECK-5-HIDDEN: @_ZTS1EIsE = weak_odr constant
134// CHECK-5-HIDDEN: @_ZTI1EIsE = weak_odr constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000135
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000136// F<short> is an explicit template instantiation without a key
137// function, so its vtable should have weak_odr linkage
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000138// CHECK-6: @_ZTV1FIsE = weak_odr constant
139// CHECK-6: @_ZTS1FIsE = weak_odr constant
140// CHECK-6: @_ZTI1FIsE = weak_odr constant
John McCall279b5eb2010-08-12 23:36:15 +0000141// CHECK-6-HIDDEN: @_ZTV1FIsE = weak_odr constant
142// CHECK-6-HIDDEN: @_ZTS1FIsE = weak_odr constant
143// CHECK-6-HIDDEN: @_ZTI1FIsE = weak_odr constant
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000144
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000145// E<long> is an implicit template instantiation with a key function
146// defined in this translation unit, so its vtable should have
147// weak_odr linkage.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000148// CHECK-7: @_ZTV1EIlE = weak_odr constant
149// CHECK-7: @_ZTS1EIlE = weak_odr constant
150// CHECK-7: @_ZTI1EIlE = weak_odr constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000151
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000152// F<long> is an implicit template instantiation with no key function,
John McCall7a536902010-08-05 20:39:18 +0000153// so its vtable should have weak_odr linkage.
154// CHECK-8: @_ZTV1FIlE = weak_odr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000155// CHECK-8: @_ZTS1FIlE = weak_odr constant
John McCall7a536902010-08-05 20:39:18 +0000156// CHECK-8: @_ZTI1FIlE = weak_odr constant
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000157
158// F<int> is an explicit template instantiation declaration without a
Rafael Espindola35d64612010-04-03 04:26:42 +0000159// key function, so its vtable should have external linkage.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000160// CHECK-9: @_ZTV1FIiE = external constant
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000161
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000162// E<int> is an explicit template instantiation declaration. It has a
Douglas Gregorc84622a2010-01-07 04:09:30 +0000163// key function that is not instantiated, so we should only reference
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000164// its vtable, not define it.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000165// CHECK-10: @_ZTV1EIiE = external constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000166
Eli Friedman6c6bda32010-01-08 00:50:11 +0000167// The anonymous struct for e has no linkage, so the vtable should have
168// internal linkage.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000169// CHECK-11: @"_ZTV3$_0" = internal constant
170// CHECK-11: @"_ZTS3$_0" = internal constant
171// CHECK-11: @"_ZTI3$_0" = internal constant
Eli Friedman6c6bda32010-01-08 00:50:11 +0000172
Anders Carlsson152d4dc2009-12-05 22:19:10 +0000173// The A vtable should have internal linkage since it is inside an anonymous
174// namespace.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000175// CHECK-12: @_ZTVN12_GLOBAL__N_11AE = internal constant
176// CHECK-12: @_ZTSN12_GLOBAL__N_11AE = internal constant
177// CHECK-12: @_ZTIN12_GLOBAL__N_11AE = internal constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000178
John McCall0c7d32b2010-08-04 06:38:15 +0000179// F<char> is an explicit specialization without a key function, so
John McCall7a536902010-08-05 20:39:18 +0000180// its vtable should have weak_odr linkage.
181// CHECK-13: @_ZTV1FIcE = weak_odr constant
John McCall0c7d32b2010-08-04 06:38:15 +0000182// CHECK-13: @_ZTS1FIcE = weak_odr constant
John McCall7a536902010-08-05 20:39:18 +0000183// CHECK-13: @_ZTI1FIcE = weak_odr constant
John McCall0c7d32b2010-08-04 06:38:15 +0000184
Daniel Dunbarb9aefa72010-05-25 00:33:13 +0000185// RUN: FileCheck --check-prefix=CHECK-G %s < %t
186//
187// CHECK-G: @_ZTV1GIiE = weak_odr constant
188template <typename T>
189class G {
190public:
191 G() {}
192 virtual void f0();
193 virtual void f1();
194};
195template <>
196void G<int>::f1() {}
197template <typename T>
198void G<T>::f0() {}
199void G_f0() { new G<int>(); }
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000200
201// RUN: FileCheck --check-prefix=CHECK-H %s < %t
202
203// H<int> has a key function without a body but it's a template instantiation
204// so its VTable must be emmitted.
205// CHECK-H: @_ZTV1HIiE = weak_odr constant
206template <typename T>
207class H {
208public:
209 virtual ~H();
210};
211
212void use_H() {
213 H<int> h;
214}