1 // RUN: %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -emit-llvm -o %t 2 // RUN: %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -O2 -disable-llvm-optzns -emit-llvm -o %t.opt 3 // RUN: FileCheck --check-prefix=CHECK-TEST1 %s < %t 4 // RUN: FileCheck --check-prefix=CHECK-TEST2 %s < %t 5 // RUN: FileCheck --check-prefix=CHECK-TEST5 %s < %t 6 // RUN: FileCheck --check-prefix=CHECK-TEST8 %s < %t.opt 7 // RUN: FileCheck --check-prefix=CHECK-TEST9 %s < %t.opt 8 // RUN: FileCheck --check-prefix=CHECK-TEST10 %s < %t.opt 9 // RUN: FileCheck --check-prefix=CHECK-TEST11 %s < %t.opt 10 // RUN: FileCheck --check-prefix=CHECK-TEST12 %s < %t.opt 11 // RUN: FileCheck --check-prefix=CHECK-TEST13 %s < %t.opt 12 // RUN: FileCheck --check-prefix=CHECK-TEST14 %s < %t.opt 13 // RUN: FileCheck --check-prefix=CHECK-TEST15 %s < %t.opt 14 15 #include <typeinfo> 16 17 // CHECK-TEST1: @_ZTVN5Test11AE = external unnamed_addr constant 18 namespace Test1 { 19 20 struct A { 21 A(); 22 virtual void f(); 23 virtual ~A() { } 24 }; 25 26 A::A() { } 27 28 void f(A* a) { 29 a->f(); 30 }; 31 32 // CHECK-LABEL: define void @_ZN5Test11gEv 33 // CHECK: call void @_ZN5Test11A1fEv 34 void g() { 35 A a; 36 f(&a); 37 } 38 39 } 40 41 // Test2::A's key function (f) is defined in this translation unit, but when 42 // we're doing codegen for the typeid(A) call, we don't know that yet. 43 // This tests mainly that the typeinfo and typename constants have their linkage 44 // updated correctly. 45 46 // CHECK-TEST2: @_ZTSN5Test21AE = constant 47 // CHECK-TEST2: @_ZTIN5Test21AE = constant 48 // CHECK-TEST2: @_ZTVN5Test21AE = unnamed_addr constant 49 namespace Test2 { 50 struct A { 51 virtual void f(); 52 }; 53 54 const std::type_info &g() { 55 return typeid(A); 56 }; 57 58 void A::f() { } 59 } 60 61 // Test that we don't assert on this test. 62 namespace Test3 { 63 64 struct A { 65 virtual void f(); 66 virtual ~A() { } 67 }; 68 69 struct B : A { 70 B(); 71 virtual void f(); 72 }; 73 74 B::B() { } 75 76 void g(A* a) { 77 a->f(); 78 }; 79 80 } 81 82 // PR9114, test that we don't try to instantiate RefPtr<Node>. 83 namespace Test4 { 84 85 template <class T> struct RefPtr { 86 T* p; 87 ~RefPtr() { 88 p->deref(); 89 } 90 }; 91 92 struct A { 93 virtual ~A(); 94 }; 95 96 struct Node; 97 98 struct B : A { 99 virtual void deref(); 100 RefPtr<Node> m; 101 }; 102 103 void f() { 104 RefPtr<B> b; 105 } 106 107 } 108 109 // PR9130, test that we emit a definition of A::f. 110 // CHECK-TEST5-LABEL: define linkonce_odr void @_ZN5Test51A1fEv 111 namespace Test5 { 112 113 struct A { 114 virtual void f() { } 115 }; 116 117 struct B : A { 118 virtual ~B(); 119 }; 120 121 B::~B() { } 122 123 } 124 125 // Check that we don't assert on this test. 126 namespace Test6 { 127 128 struct A { 129 virtual ~A(); 130 int a; 131 }; 132 133 struct B { 134 virtual ~B(); 135 int b; 136 }; 137 138 struct C : A, B { 139 C(); 140 }; 141 142 struct D : C { 143 virtual void f(); 144 D(); 145 }; 146 147 D::D() { } 148 149 } 150 151 namespace Test7 { 152 153 struct c1 {}; 154 struct c10 : c1{ 155 virtual void foo (); 156 }; 157 struct c11 : c10, c1{ 158 virtual void f6 (); 159 }; 160 struct c28 : virtual c11{ 161 void f6 (); 162 }; 163 } 164 165 namespace Test8 { 166 // CHECK-TEST8: @_ZTVN5Test81YE = available_externally unnamed_addr constant 167 // vtable for X is not generated because there are no stores here 168 struct X { 169 X(); 170 virtual void foo(); 171 }; 172 struct Y : X { 173 void foo(); 174 }; 175 176 void g(X* p) { p->foo(); } 177 void f() { 178 Y y; 179 g(&y); 180 X x; 181 g(&x); 182 } 183 184 } // Test8 185 186 namespace Test9 { 187 // all virtual functions are outline, so we can assume that it will 188 // be generated in translation unit where foo is defined 189 // CHECK-TEST9: @_ZTVN5Test91AE = available_externally unnamed_addr constant 190 // CHECK-TEST9: @_ZTVN5Test91BE = available_externally unnamed_addr constant 191 struct A { 192 virtual void foo(); 193 virtual void bar(); 194 }; 195 void A::bar() {} 196 197 struct B : A { 198 void foo(); 199 }; 200 201 void g() { 202 A a; 203 a.foo(); 204 B b; 205 b.foo(); 206 } 207 208 } // Test9 209 210 namespace Test10 { 211 212 // because A's key function is defined here, vtable is generated in this TU 213 // CHECK-TEST10: @_ZTVN6Test101AE = unnamed_addr constant 214 struct A { 215 virtual void foo(); 216 virtual void bar(); 217 }; 218 void A::foo() {} 219 220 // Because key function is inline we will generate vtable as linkonce_odr 221 // CHECK-TEST10: @_ZTVN6Test101DE = linkonce_odr unnamed_addr constant 222 struct D : A { 223 void bar(); 224 }; 225 inline void D::bar() {} 226 227 // because B has outline key function then we can refer to 228 // CHECK-TEST10: @_ZTVN6Test101BE = available_externally unnamed_addr constant 229 struct B : A { 230 void foo(); 231 void bar(); 232 }; 233 234 // C's key function (car) is outline, but C has inline virtual function so we 235 // can't guarantee that we will be able to refer to bar from name 236 // so (at the moment) we can't emit vtable available_externally 237 // CHECK-TEST10: @_ZTVN6Test101CE = external unnamed_addr constant 238 struct C : A { 239 void bar() {} // defined in body - not key function 240 virtual inline void gar(); // inline in body - not key function 241 virtual void car(); 242 }; 243 244 // no key function, vtable will be generated everywhere it will be used 245 // CHECK-TEST10: @_ZTVN6Test101EE = linkonce_odr unnamed_addr constant 246 struct E : A {}; 247 248 void g(A& a) { 249 a.foo(); 250 a.bar(); 251 } 252 253 void f() { 254 A a; 255 g(a); 256 B b; 257 g(b); 258 C c; 259 g(c); 260 D d; 261 g(d); 262 E e; 263 g(e); 264 } 265 266 } // Test10 267 268 namespace Test11 { 269 struct D; 270 // Can emit C's vtable available_externally. 271 // CHECK-TEST11: @_ZTVN6Test111CE = available_externally unnamed_addr constant 272 struct C { 273 virtual D& operator=(const D&); 274 }; 275 276 // Cannot emit B's vtable available_externally, because we cannot create 277 // a reference to the inline virtual B::operator= function. 278 // CHECK-TEST11: @_ZTVN6Test111DE = external unnamed_addr constant 279 struct D : C { 280 virtual void key(); 281 }; 282 D f(); 283 284 void g(D& a) { 285 C c; 286 c = a; 287 a.key(); 288 a.key(); 289 } 290 void g() { 291 D d; 292 d = f(); 293 g(d); 294 } 295 } // Test 11 296 297 namespace Test12 { 298 299 // CHECK-TEST12: @_ZTVN6Test121AE = external unnamed_addr constant 300 struct A { 301 virtual void foo(); 302 virtual ~A() {} 303 }; 304 // CHECK-TEST12: @_ZTVN6Test121BE = external unnamed_addr constant 305 struct B : A { 306 void foo(); 307 }; 308 309 void g() { 310 A a; 311 a.foo(); 312 B b; 313 b.foo(); 314 } 315 } 316 317 namespace Test13 { 318 319 // CHECK-TEST13-DAG: @_ZTVN6Test131AE = available_externally unnamed_addr constant 320 // CHECK-TEST13-DAG: @_ZTVN6Test131BE = external unnamed_addr constant 321 struct A { 322 virtual ~A(); 323 }; 324 struct B : A { 325 virtual void f(); 326 void operator delete(void *); 327 ~B() {} 328 }; 329 330 void g() { 331 A *b = new B; 332 } 333 } 334 335 namespace Test14 { 336 337 // CHECK-TEST14: @_ZTVN6Test141AE = available_externally unnamed_addr constant 338 struct A { 339 virtual void f(); 340 void operator delete(void *); 341 ~A(); 342 }; 343 344 void g() { 345 A *b = new A; 346 delete b; 347 } 348 } 349 350 namespace Test15 { 351 // In this test D's vtable has two slots for function f(), but uses only one, 352 // so the second slot is set to null. 353 // CHECK-TEST15: @_ZTVN6Test151DE = available_externally unnamed_addr constant 354 struct A { virtual void f() {} }; 355 struct B : virtual A {}; 356 struct C : virtual A {}; 357 struct D : B, C { 358 virtual void g(); 359 void f(); 360 }; 361 362 void test() { 363 D * d = new D; 364 d->f(); 365 } 366 } 367 368 369