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