blob: 4633a3fe9569f68739589f741a7aa721c13273f5 [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 Carlsson691222d2011-01-29 19:34:19 +000035struct C : virtual B {
Anders Carlsson891c8b72009-12-05 22:24:38 +000036 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.
Rafael Espindolacd3ac4b2011-01-15 08:23:14 +0000102// CHECK-1: @_ZTV1B = external unnamed_addr 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).
Anders Carlssonf502d932011-01-24 00:46:19 +0000106// CHECK-2: @_ZTV1C = linkonce_odr unnamed_addr constant
107// CHECK-2: @_ZTS1C = linkonce_odr constant
108// CHECK-2: @_ZTI1C = linkonce_odr unnamed_addr constant
Anders Carlsson691222d2011-01-29 19:34:19 +0000109// CHECK-2: @_ZTT1C = linkonce_odr unnamed_addr constant
Anders Carlssonf502d932011-01-24 00:46:19 +0000110// CHECK-2-HIDDEN: @_ZTV1C = linkonce_odr hidden unnamed_addr constant
111// CHECK-2-HIDDEN: @_ZTS1C = linkonce_odr constant
112// CHECK-2-HIDDEN: @_ZTI1C = linkonce_odr hidden unnamed_addr constant
Anders Carlsson691222d2011-01-29 19:34:19 +0000113// CHECK-2-HIDDEN: @_ZTT1C = linkonce_odr hidden unnamed_addr constant
Anders Carlsson891c8b72009-12-05 22:24:38 +0000114
Anders Carlsson5794c972009-12-06 00:53:22 +0000115// D has a key function that is defined in this translation unit so its vtable is
116// defined in the translation unit.
Rafael Espindola9f959db2011-01-11 21:10:26 +0000117// CHECK-3: @_ZTV1D = unnamed_addr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000118// CHECK-3: @_ZTS1D = constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000119// CHECK-3: @_ZTI1D = unnamed_addr constant
Anders Carlsson5794c972009-12-06 00:53:22 +0000120
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000121// E<char> is an explicit specialization with a key function defined
122// in this translation unit, so its vtable should have external
123// linkage.
Rafael Espindola9f959db2011-01-11 21:10:26 +0000124// CHECK-4: @_ZTV1EIcE = unnamed_addr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000125// CHECK-4: @_ZTS1EIcE = constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000126// CHECK-4: @_ZTI1EIcE = unnamed_addr constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000127
128// E<short> is an explicit template instantiation with a key function
129// defined in this translation unit, so its vtable should have
130// weak_odr linkage.
Rafael Espindola9f959db2011-01-11 21:10:26 +0000131// CHECK-5: @_ZTV1EIsE = weak_odr unnamed_addr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000132// CHECK-5: @_ZTS1EIsE = weak_odr constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000133// CHECK-5: @_ZTI1EIsE = weak_odr unnamed_addr constant
Rafael Espindola9f959db2011-01-11 21:10:26 +0000134// CHECK-5-HIDDEN: @_ZTV1EIsE = weak_odr unnamed_addr constant
John McCall279b5eb2010-08-12 23:36:15 +0000135// CHECK-5-HIDDEN: @_ZTS1EIsE = weak_odr constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000136// CHECK-5-HIDDEN: @_ZTI1EIsE = weak_odr unnamed_addr constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000137
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000138// F<short> is an explicit template instantiation without a key
139// function, so its vtable should have weak_odr linkage
Rafael Espindola9f959db2011-01-11 21:10:26 +0000140// CHECK-6: @_ZTV1FIsE = weak_odr unnamed_addr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000141// CHECK-6: @_ZTS1FIsE = weak_odr constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000142// CHECK-6: @_ZTI1FIsE = weak_odr unnamed_addr constant
Rafael Espindola9f959db2011-01-11 21:10:26 +0000143// CHECK-6-HIDDEN: @_ZTV1FIsE = weak_odr unnamed_addr constant
John McCall279b5eb2010-08-12 23:36:15 +0000144// CHECK-6-HIDDEN: @_ZTS1FIsE = weak_odr constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000145// CHECK-6-HIDDEN: @_ZTI1FIsE = weak_odr unnamed_addr constant
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000146
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000147// E<long> is an implicit template instantiation with a key function
148// defined in this translation unit, so its vtable should have
Anders Carlssonf502d932011-01-24 00:46:19 +0000149// linkonce_odr linkage.
150// CHECK-7: @_ZTV1EIlE = linkonce_odr unnamed_addr constant
151// CHECK-7: @_ZTS1EIlE = linkonce_odr constant
152// CHECK-7: @_ZTI1EIlE = linkonce_odr unnamed_addr constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000153
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000154// F<long> is an implicit template instantiation with no key function,
Anders Carlssonf502d932011-01-24 00:46:19 +0000155// so its vtable should have linkonce_odr linkage.
156// CHECK-8: @_ZTV1FIlE = linkonce_odr unnamed_addr constant
157// CHECK-8: @_ZTS1FIlE = linkonce_odr constant
158// CHECK-8: @_ZTI1FIlE = linkonce_odr unnamed_addr constant
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000159
160// F<int> is an explicit template instantiation declaration without a
Rafael Espindola35d64612010-04-03 04:26:42 +0000161// key function, so its vtable should have external linkage.
Rafael Espindolacd3ac4b2011-01-15 08:23:14 +0000162// CHECK-9: @_ZTV1FIiE = external unnamed_addr constant
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000163
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000164// E<int> is an explicit template instantiation declaration. It has a
Douglas Gregorc84622a2010-01-07 04:09:30 +0000165// key function that is not instantiated, so we should only reference
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000166// its vtable, not define it.
Rafael Espindolacd3ac4b2011-01-15 08:23:14 +0000167// CHECK-10: @_ZTV1EIiE = external unnamed_addr constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000168
Eli Friedman6c6bda32010-01-08 00:50:11 +0000169// The anonymous struct for e has no linkage, so the vtable should have
170// internal linkage.
Rafael Espindola9f959db2011-01-11 21:10:26 +0000171// CHECK-11: @"_ZTV3$_0" = internal unnamed_addr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000172// CHECK-11: @"_ZTS3$_0" = internal constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000173// CHECK-11: @"_ZTI3$_0" = internal unnamed_addr constant
Eli Friedman6c6bda32010-01-08 00:50:11 +0000174
Anders Carlsson152d4dc2009-12-05 22:19:10 +0000175// The A vtable should have internal linkage since it is inside an anonymous
176// namespace.
Rafael Espindola9f959db2011-01-11 21:10:26 +0000177// CHECK-12: @_ZTVN12_GLOBAL__N_11AE = internal unnamed_addr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000178// CHECK-12: @_ZTSN12_GLOBAL__N_11AE = internal constant
Rafael Espindola57244f62011-01-11 23:55:05 +0000179// CHECK-12: @_ZTIN12_GLOBAL__N_11AE = internal unnamed_addr constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000180
John McCall0c7d32b2010-08-04 06:38:15 +0000181// F<char> is an explicit specialization without a key function, so
Anders Carlssonf502d932011-01-24 00:46:19 +0000182// its vtable should have linkonce_odr linkage.
183// CHECK-13: @_ZTV1FIcE = linkonce_odr unnamed_addr constant
184// CHECK-13: @_ZTS1FIcE = linkonce_odr constant
185// CHECK-13: @_ZTI1FIcE = linkonce_odr unnamed_addr constant
John McCall0c7d32b2010-08-04 06:38:15 +0000186
Daniel Dunbarb9aefa72010-05-25 00:33:13 +0000187// RUN: FileCheck --check-prefix=CHECK-G %s < %t
188//
Anders Carlssonf502d932011-01-24 00:46:19 +0000189// CHECK-G: @_ZTV1GIiE = linkonce_odr unnamed_addr constant
Daniel Dunbarb9aefa72010-05-25 00:33:13 +0000190template <typename T>
191class G {
192public:
193 G() {}
194 virtual void f0();
195 virtual void f1();
196};
197template <>
198void G<int>::f1() {}
199template <typename T>
200void G<T>::f0() {}
201void G_f0() { new G<int>(); }
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000202
203// RUN: FileCheck --check-prefix=CHECK-H %s < %t
204
205// H<int> has a key function without a body but it's a template instantiation
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000206// so its VTable must be emitted.
Anders Carlssonf502d932011-01-24 00:46:19 +0000207// CHECK-H: @_ZTV1HIiE = linkonce_odr unnamed_addr constant
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000208template <typename T>
209class H {
210public:
211 virtual ~H();
212};
213
214void use_H() {
215 H<int> h;
216}