vtable-linkage.cpp revision cd3ac4bd96cb19af5117eded84bfe8320c5d787e
1// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o %t 2// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -fhidden-weak-vtables -emit-llvm -o %t.hidden 3// RUN: FileCheck --check-prefix=CHECK-1 %s < %t 4// RUN: FileCheck --check-prefix=CHECK-2 %s < %t 5// RUN: FileCheck --check-prefix=CHECK-2-HIDDEN %s < %t.hidden 6// 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 9// RUN: FileCheck --check-prefix=CHECK-5-HIDDEN %s < %t.hidden 10// RUN: FileCheck --check-prefix=CHECK-6 %s < %t 11// RUN: FileCheck --check-prefix=CHECK-6-HIDDEN %s < %t.hidden 12// 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 18// RUN: FileCheck --check-prefix=CHECK-13 %s < %t 19 20namespace { 21 struct A { 22 virtual void f() { } 23 }; 24} 25 26void f() { A b; } 27 28struct B { 29 B(); 30 virtual void f(); 31}; 32 33B::B() { } 34 35struct C { 36 C(); 37 virtual void f() { } 38}; 39 40C::C() { } 41 42struct D { 43 virtual void f(); 44}; 45 46void D::f() { } 47 48static struct : D { } e; 49 50// The destructor is the key function. 51template<typename T> 52struct E { 53 virtual ~E(); 54}; 55 56template<typename T> E<T>::~E() { } 57 58// Anchor is the key function 59template<> 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 76// 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 91void use_F() { 92 F<char> fc; 93 fc.foo(); 94 F<int> fi; 95 fi.foo(); 96 F<long> fl; 97 (void)fl; 98} 99 100// B has a key function that is not defined in this translation unit so its vtable 101// has external linkage. 102// CHECK-1: @_ZTV1B = external unnamed_addr constant 103 104// C has no key function, so its vtable should have weak_odr linkage 105// and hidden visibility (rdar://problem/7523229). 106// CHECK-2: @_ZTV1C = weak_odr unnamed_addr constant 107// CHECK-2: @_ZTS1C = weak_odr constant 108// CHECK-2: @_ZTI1C = weak_odr unnamed_addr constant 109// CHECK-2-HIDDEN: @_ZTV1C = weak_odr hidden unnamed_addr constant 110// CHECK-2-HIDDEN: @_ZTS1C = weak_odr constant 111// CHECK-2-HIDDEN: @_ZTI1C = weak_odr hidden unnamed_addr constant 112 113// D has a key function that is defined in this translation unit so its vtable is 114// defined in the translation unit. 115// CHECK-3: @_ZTV1D = unnamed_addr constant 116// CHECK-3: @_ZTS1D = constant 117// CHECK-3: @_ZTI1D = unnamed_addr constant 118 119// E<char> is an explicit specialization with a key function defined 120// in this translation unit, so its vtable should have external 121// linkage. 122// CHECK-4: @_ZTV1EIcE = unnamed_addr constant 123// CHECK-4: @_ZTS1EIcE = constant 124// CHECK-4: @_ZTI1EIcE = unnamed_addr constant 125 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. 129// CHECK-5: @_ZTV1EIsE = weak_odr unnamed_addr constant 130// CHECK-5: @_ZTS1EIsE = weak_odr constant 131// CHECK-5: @_ZTI1EIsE = weak_odr unnamed_addr constant 132// CHECK-5-HIDDEN: @_ZTV1EIsE = weak_odr unnamed_addr constant 133// CHECK-5-HIDDEN: @_ZTS1EIsE = weak_odr constant 134// CHECK-5-HIDDEN: @_ZTI1EIsE = weak_odr unnamed_addr constant 135 136// F<short> is an explicit template instantiation without a key 137// function, so its vtable should have weak_odr linkage 138// CHECK-6: @_ZTV1FIsE = weak_odr unnamed_addr constant 139// CHECK-6: @_ZTS1FIsE = weak_odr constant 140// CHECK-6: @_ZTI1FIsE = weak_odr unnamed_addr constant 141// CHECK-6-HIDDEN: @_ZTV1FIsE = weak_odr unnamed_addr constant 142// CHECK-6-HIDDEN: @_ZTS1FIsE = weak_odr constant 143// CHECK-6-HIDDEN: @_ZTI1FIsE = weak_odr unnamed_addr constant 144 145// 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. 148// CHECK-7: @_ZTV1EIlE = weak_odr unnamed_addr constant 149// CHECK-7: @_ZTS1EIlE = weak_odr constant 150// CHECK-7: @_ZTI1EIlE = weak_odr unnamed_addr constant 151 152// F<long> is an implicit template instantiation with no key function, 153// so its vtable should have weak_odr linkage. 154// CHECK-8: @_ZTV1FIlE = weak_odr unnamed_addr constant 155// CHECK-8: @_ZTS1FIlE = weak_odr constant 156// CHECK-8: @_ZTI1FIlE = weak_odr unnamed_addr constant 157 158// F<int> is an explicit template instantiation declaration without a 159// key function, so its vtable should have external linkage. 160// CHECK-9: @_ZTV1FIiE = external unnamed_addr constant 161 162// E<int> is an explicit template instantiation declaration. It has a 163// key function that is not instantiated, so we should only reference 164// its vtable, not define it. 165// CHECK-10: @_ZTV1EIiE = external unnamed_addr constant 166 167// The anonymous struct for e has no linkage, so the vtable should have 168// internal linkage. 169// CHECK-11: @"_ZTV3$_0" = internal unnamed_addr constant 170// CHECK-11: @"_ZTS3$_0" = internal constant 171// CHECK-11: @"_ZTI3$_0" = internal unnamed_addr constant 172 173// The A vtable should have internal linkage since it is inside an anonymous 174// namespace. 175// CHECK-12: @_ZTVN12_GLOBAL__N_11AE = internal unnamed_addr constant 176// CHECK-12: @_ZTSN12_GLOBAL__N_11AE = internal constant 177// CHECK-12: @_ZTIN12_GLOBAL__N_11AE = internal unnamed_addr constant 178 179// F<char> is an explicit specialization without a key function, so 180// its vtable should have weak_odr linkage. 181// CHECK-13: @_ZTV1FIcE = weak_odr unnamed_addr constant 182// CHECK-13: @_ZTS1FIcE = weak_odr constant 183// CHECK-13: @_ZTI1FIcE = weak_odr unnamed_addr constant 184 185// RUN: FileCheck --check-prefix=CHECK-G %s < %t 186// 187// CHECK-G: @_ZTV1GIiE = weak_odr unnamed_addr 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>(); } 200 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 unnamed_addr constant 206template <typename T> 207class H { 208public: 209 virtual ~H(); 210}; 211 212void use_H() { 213 H<int> h; 214} 215