vtable-linkage.cpp revision b9aefa775158b68c453e4ac0e4e2ada561900d26
1// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o %t
2// RUN: FileCheck --check-prefix=CHECK-1 %s < %t
3// RUN: FileCheck --check-prefix=CHECK-2 %s < %t
4// RUN: FileCheck --check-prefix=CHECK-3 %s < %t
5// RUN: FileCheck --check-prefix=CHECK-4 %s < %t
6// RUN: FileCheck --check-prefix=CHECK-5 %s < %t
7// RUN: FileCheck --check-prefix=CHECK-6 %s < %t
8// RUN: FileCheck --check-prefix=CHECK-7 %s < %t
9// RUN: FileCheck --check-prefix=CHECK-8 %s < %t
10// RUN: FileCheck --check-prefix=CHECK-9 %s < %t
11// RUN: FileCheck --check-prefix=CHECK-10 %s < %t
12// RUN: FileCheck --check-prefix=CHECK-11 %s < %t
13// RUN: FileCheck --check-prefix=CHECK-12 %s < %t
14
15namespace {
16  struct A {
17    virtual void f() { }
18  };
19}
20
21void f() { A b; }
22
23struct B {
24  B();
25  virtual void f();
26};
27
28B::B() { }
29
30struct C {
31  C();
32  virtual void f() { }
33};
34
35C::C() { }
36
37struct D {
38  virtual void f();
39};
40
41void D::f() { }
42
43static struct : D { } e;
44
45// The destructor is the key function.
46template<typename T>
47struct E {
48  virtual ~E();
49};
50
51template<typename T> E<T>::~E() { }
52
53// Anchor is the key function
54template<>
55struct E<char> {
56  virtual void anchor();
57};
58
59void E<char>::anchor() { }
60
61template struct E<short>;
62extern template struct E<int>;
63
64void use_E() {
65  E<int> ei;
66  (void)ei;
67  E<long> el;
68  (void)el;
69}
70
71// No key function
72template<typename T>
73struct F {
74  virtual void foo() { }
75};
76
77// No key function
78template<>
79struct F<char> {
80  virtual void foo() { }
81};
82
83template struct F<short>;
84extern template struct F<int>;
85
86void use_F(F<char> &fc) {
87  F<int> fi;
88  fi.foo();
89  F<long> fl;
90  (void)fl;
91  fc.foo();
92}
93
94// B has a key function that is not defined in this translation unit so its vtable
95// has external linkage.
96// CHECK-1: @_ZTV1B = external constant
97
98// C has no key function, so its vtable should have weak_odr linkage.
99// CHECK-2: @_ZTV1C = weak_odr constant
100// CHECK-2: @_ZTS1C = weak_odr constant
101// CHECK-2: @_ZTI1C = weak_odr constant
102
103// D has a key function that is defined in this translation unit so its vtable is
104// defined in the translation unit.
105// CHECK-3: @_ZTV1D = constant
106// CHECK-3: @_ZTS1D = constant
107// CHECK-3: @_ZTI1D = constant
108
109// E<char> is an explicit specialization with a key function defined
110// in this translation unit, so its vtable should have external
111// linkage.
112// CHECK-4: @_ZTV1EIcE = constant
113// CHECK-4: @_ZTS1EIcE = constant
114// CHECK-4: @_ZTI1EIcE = constant
115
116// E<short> is an explicit template instantiation with a key function
117// defined in this translation unit, so its vtable should have
118// weak_odr linkage.
119// CHECK-5: @_ZTV1EIsE = weak_odr constant
120// CHECK-5: @_ZTS1EIsE = weak_odr constant
121// CHECK-5: @_ZTI1EIsE = weak_odr constant
122
123// F<short> is an explicit template instantiation without a key
124// function, so its vtable should have weak_odr linkage
125// CHECK-6: @_ZTV1FIsE = weak_odr constant
126// CHECK-6: @_ZTS1FIsE = weak_odr constant
127// CHECK-6: @_ZTI1FIsE = weak_odr constant
128
129// E<long> is an implicit template instantiation with a key function
130// defined in this translation unit, so its vtable should have
131// weak_odr linkage.
132// CHECK-7: @_ZTV1EIlE = weak_odr constant
133// CHECK-7: @_ZTS1EIlE = weak_odr constant
134// CHECK-7: @_ZTI1EIlE = weak_odr constant
135
136// F<long> is an implicit template instantiation with no key function,
137// so its vtable should have weak_odr linkage.
138// CHECK-8: @_ZTV1FIlE = weak_odr constant
139// CHECK-8: @_ZTS1FIlE = weak_odr constant
140// CHECK-8: @_ZTI1FIlE = weak_odr constant
141
142// F<int> is an explicit template instantiation declaration without a
143// key function, so its vtable should have external linkage.
144// CHECK-9: @_ZTV1FIiE = external constant
145
146// E<int> is an explicit template instantiation declaration. It has a
147// key function that is not instantiated, so we should only reference
148// its vtable, not define it.
149// CHECK-10: @_ZTV1EIiE = external constant
150
151// The anonymous struct for e has no linkage, so the vtable should have
152// internal linkage.
153// CHECK-11: @"_ZTV3$_0" = internal constant
154// CHECK-11: @"_ZTS3$_0" = internal constant
155// CHECK-11: @"_ZTI3$_0" = internal constant
156
157// The A vtable should have internal linkage since it is inside an anonymous
158// namespace.
159// CHECK-12: @_ZTVN12_GLOBAL__N_11AE = internal constant
160// CHECK-12: @_ZTSN12_GLOBAL__N_11AE = internal constant
161// CHECK-12: @_ZTIN12_GLOBAL__N_11AE = internal constant
162
163// RUN: FileCheck --check-prefix=CHECK-G %s < %t
164//
165// CHECK-G: @_ZTV1GIiE = weak_odr constant
166template <typename T>
167class G {
168public:
169  G() {}
170  virtual void f0();
171  virtual void f1();
172};
173template <>
174void G<int>::f1() {}
175template <typename T>
176void G<T>::f0() {}
177void G_f0()  { new G<int>(); }
178