1 // RUN: %clang_cc1 %s -triple=x86_64-pc-linux -emit-llvm -o %t 2 // RUN: %clang_cc1 %s -triple=x86_64-pc-linux -emit-llvm -std=c++03 -o %t.03 3 // RUN: %clang_cc1 %s -triple=x86_64-pc-linux -emit-llvm -std=c++11 -o %t.11 4 // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -disable-llvm-passes -O3 -emit-llvm -o %t.opt 5 // RUN: FileCheck %s < %t 6 // RUN: FileCheck %s < %t.03 7 // RUN: FileCheck %s < %t.11 8 // RUN: FileCheck --check-prefix=CHECK-OPT %s < %t.opt 9 10 namespace { 11 struct A { 12 virtual void f() { } 13 }; 14 } 15 16 void f() { A b; } 17 18 struct B { 19 B(); 20 virtual void f(); 21 }; 22 23 B::B() { } 24 25 struct C : virtual B { 26 C(); 27 virtual void f() { } 28 }; 29 30 C::C() { } 31 32 struct D { 33 virtual void f(); 34 }; 35 36 void D::f() { } 37 38 static struct : D { } e; 39 40 // Force 'e' to be constructed and therefore have a vtable defined. 41 void use_e() { 42 e.f(); 43 } 44 45 // The destructor is the key function. 46 template<typename T> 47 struct E { 48 virtual ~E(); 49 }; 50 51 template<typename T> E<T>::~E() { } 52 53 // Anchor is the key function 54 template<> 55 struct E<char> { 56 virtual void anchor(); 57 }; 58 59 void E<char>::anchor() { } 60 61 template struct E<short>; 62 extern template struct E<int>; 63 64 void use_E() { 65 E<int> ei; 66 (void)ei; 67 E<long> el; 68 (void)el; 69 } 70 71 // No key function 72 template<typename T> 73 struct F { 74 virtual void foo() { } 75 }; 76 77 // No key function 78 template<> 79 struct F<char> { 80 virtual void foo() { } 81 }; 82 83 template struct F<short>; 84 extern template struct F<int>; 85 86 void use_F() { 87 F<char> fc; 88 fc.foo(); 89 F<int> fi; 90 fi.foo(); 91 F<long> fl; 92 (void)fl; 93 } 94 95 // B has a key function that is not defined in this translation unit so its vtable 96 // has external linkage. 97 // CHECK-DAG: @_ZTV1B = external unnamed_addr constant 98 99 // C has no key function, so its vtable should have weak_odr linkage 100 // and hidden visibility (rdar://problem/7523229). 101 // CHECK-DAG: @_ZTV1C = linkonce_odr unnamed_addr constant {{.*}}, comdat, 102 // CHECK-DAG: @_ZTS1C = linkonce_odr constant {{.*}}, comdat{{$}} 103 // CHECK-DAG: @_ZTI1C = linkonce_odr constant {{.*}}, comdat{{$}} 104 // CHECK-DAG: @_ZTT1C = linkonce_odr unnamed_addr constant {{.*}}, comdat{{$}} 105 106 // D has a key function that is defined in this translation unit so its vtable is 107 // defined in the translation unit. 108 // CHECK-DAG: @_ZTV1D = unnamed_addr constant 109 // CHECK-DAG: @_ZTS1D = constant 110 // CHECK-DAG: @_ZTI1D = constant 111 112 // E<char> is an explicit specialization with a key function defined 113 // in this translation unit, so its vtable should have external 114 // linkage. 115 // CHECK-DAG: @_ZTV1EIcE = unnamed_addr constant 116 // CHECK-DAG: @_ZTS1EIcE = constant 117 // CHECK-DAG: @_ZTI1EIcE = constant 118 119 // E<short> is an explicit template instantiation with a key function 120 // defined in this translation unit, so its vtable should have 121 // weak_odr linkage. 122 // CHECK-DAG: @_ZTV1EIsE = weak_odr unnamed_addr constant {{.*}}, comdat, 123 // CHECK-DAG: @_ZTS1EIsE = weak_odr constant {{.*}}, comdat{{$}} 124 // CHECK-DAG: @_ZTI1EIsE = weak_odr constant {{.*}}, comdat{{$}} 125 126 // F<short> is an explicit template instantiation without a key 127 // function, so its vtable should have weak_odr linkage 128 // CHECK-DAG: @_ZTV1FIsE = weak_odr unnamed_addr constant {{.*}}, comdat, 129 // CHECK-DAG: @_ZTS1FIsE = weak_odr constant {{.*}}, comdat{{$}} 130 // CHECK-DAG: @_ZTI1FIsE = weak_odr constant {{.*}}, comdat{{$}} 131 132 // E<long> is an implicit template instantiation with a key function 133 // defined in this translation unit, so its vtable should have 134 // linkonce_odr linkage. 135 // CHECK-DAG: @_ZTV1EIlE = linkonce_odr unnamed_addr constant {{.*}}, comdat, 136 // CHECK-DAG: @_ZTS1EIlE = linkonce_odr constant {{.*}}, comdat{{$}} 137 // CHECK-DAG: @_ZTI1EIlE = linkonce_odr constant {{.*}}, comdat{{$}} 138 139 // F<long> is an implicit template instantiation with no key function, 140 // so its vtable should have linkonce_odr linkage. 141 // CHECK-DAG: @_ZTV1FIlE = linkonce_odr unnamed_addr constant {{.*}}, comdat, 142 // CHECK-DAG: @_ZTS1FIlE = linkonce_odr constant {{.*}}, comdat{{$}} 143 // CHECK-DAG: @_ZTI1FIlE = linkonce_odr constant {{.*}}, comdat{{$}} 144 145 // F<int> is an explicit template instantiation declaration without a 146 // key function, so its vtable should have external linkage. 147 // CHECK-DAG: @_ZTV1FIiE = external unnamed_addr constant 148 // CHECK-OPT-DAG: @_ZTV1FIiE = external unnamed_addr constant 149 150 // E<int> is an explicit template instantiation declaration. It has a 151 // key function is not instantiated, so we know that vtable definition 152 // will be generated in TU where key function will be defined 153 // so we can mark it as available_externally (only with optimizations) 154 // CHECK-DAG: @_ZTV1EIiE = external unnamed_addr constant 155 // CHECK-OPT-DAG: @_ZTV1EIiE = available_externally unnamed_addr constant 156 157 // The anonymous struct for e has no linkage, so the vtable should have 158 // internal linkage. 159 // CHECK-DAG: @"_ZTV3$_0" = internal unnamed_addr constant 160 // CHECK-DAG: @"_ZTS3$_0" = internal constant 161 // CHECK-DAG: @"_ZTI3$_0" = internal constant 162 163 // The A vtable should have internal linkage since it is inside an anonymous 164 // namespace. 165 // CHECK-DAG: @_ZTVN12_GLOBAL__N_11AE = internal unnamed_addr constant 166 // CHECK-DAG: @_ZTSN12_GLOBAL__N_11AE = internal constant 167 // CHECK-DAG: @_ZTIN12_GLOBAL__N_11AE = internal constant 168 169 // F<char> is an explicit specialization without a key function, so 170 // its vtable should have linkonce_odr linkage. 171 // CHECK-DAG: @_ZTV1FIcE = linkonce_odr unnamed_addr constant {{.*}}, comdat, 172 // CHECK-DAG: @_ZTS1FIcE = linkonce_odr constant {{.*}}, comdat{{$}} 173 // CHECK-DAG: @_ZTI1FIcE = linkonce_odr constant {{.*}}, comdat{{$}} 174 175 // CHECK-DAG: @_ZTV1GIiE = linkonce_odr unnamed_addr constant {{.*}}, comdat, 176 template <typename T> 177 class G { 178 public: 179 G() {} 180 virtual void f0(); 181 virtual void f1(); 182 }; 183 template <> 184 void G<int>::f1() {} 185 template <typename T> 186 void G<T>::f0() {} 187 void G_f0() { new G<int>(); } 188 189 // H<int> has a key function without a body but it's a template instantiation 190 // so its VTable must be emitted. 191 // CHECK-DAG: @_ZTV1HIiE = linkonce_odr unnamed_addr constant {{.*}}, comdat, 192 template <typename T> 193 class H { 194 public: 195 virtual ~H(); 196 }; 197 198 void use_H() { 199 H<int> h; 200 } 201 202 // I<int> has an explicit instantiation declaration and needs a VTT and 203 // construction vtables. 204 205 // CHECK-DAG: @_ZTV1IIiE = external unnamed_addr constant 206 // CHECK-DAG: @_ZTT1IIiE = external unnamed_addr constant 207 // CHECK-NOT: @_ZTC1IIiE 208 // 209 // CHECK-OPT-DAG: @_ZTV1IIiE = available_externally unnamed_addr constant 210 // CHECK-OPT-DAG: @_ZTT1IIiE = available_externally unnamed_addr constant 211 struct VBase1 { virtual void f(); }; struct VBase2 : virtual VBase1 {}; 212 template<typename T> 213 struct I : VBase2 {}; 214 extern template struct I<int>; 215 I<int> i; 216