Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o %t |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame^] | 2 | // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -fhidden-weak-vtables -emit-llvm -o %t.hidden |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3 | // RUN: FileCheck --check-prefix=CHECK-1 %s < %t |
| 4 | // RUN: FileCheck --check-prefix=CHECK-2 %s < %t |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame^] | 5 | // RUN: FileCheck --check-prefix=CHECK-2-HIDDEN %s < %t.hidden |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 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 |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame^] | 9 | // RUN: FileCheck --check-prefix=CHECK-5-HIDDEN %s < %t.hidden |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10 | // RUN: FileCheck --check-prefix=CHECK-6 %s < %t |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame^] | 11 | // RUN: FileCheck --check-prefix=CHECK-6-HIDDEN %s < %t.hidden |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 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 |
John McCall | 0c7d32b | 2010-08-04 06:38:15 +0000 | [diff] [blame] | 18 | // RUN: FileCheck --check-prefix=CHECK-13 %s < %t |
Anders Carlsson | 7ca4643 | 2009-12-05 17:04:47 +0000 | [diff] [blame] | 19 | |
| 20 | namespace { |
Anders Carlsson | 7ca4643 | 2009-12-05 17:04:47 +0000 | [diff] [blame] | 21 | struct A { |
| 22 | virtual void f() { } |
| 23 | }; |
Anders Carlsson | 7ca4643 | 2009-12-05 17:04:47 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Anders Carlsson | 152d4dc | 2009-12-05 22:19:10 +0000 | [diff] [blame] | 26 | void f() { A b; } |
| 27 | |
| 28 | struct B { |
| 29 | B(); |
| 30 | virtual void f(); |
| 31 | }; |
| 32 | |
| 33 | B::B() { } |
| 34 | |
Anders Carlsson | 891c8b7 | 2009-12-05 22:24:38 +0000 | [diff] [blame] | 35 | struct C { |
| 36 | C(); |
| 37 | virtual void f() { } |
| 38 | }; |
| 39 | |
| 40 | C::C() { } |
| 41 | |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 42 | struct D { |
| 43 | virtual void f(); |
| 44 | }; |
| 45 | |
| 46 | void D::f() { } |
| 47 | |
Eli Friedman | 470fb73 | 2009-12-11 20:48:18 +0000 | [diff] [blame] | 48 | static struct : D { } e; |
| 49 | |
Douglas Gregor | 074a2cf | 2010-01-05 21:40:05 +0000 | [diff] [blame] | 50 | // The destructor is the key function. |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 51 | template<typename T> |
| 52 | struct E { |
| 53 | virtual ~E(); |
| 54 | }; |
| 55 | |
| 56 | template<typename T> E<T>::~E() { } |
| 57 | |
Douglas Gregor | 074a2cf | 2010-01-05 21:40:05 +0000 | [diff] [blame] | 58 | // Anchor is the key function |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 59 | template<> |
| 60 | struct E<char> { |
| 61 | virtual void anchor(); |
| 62 | }; |
| 63 | |
| 64 | void E<char>::anchor() { } |
| 65 | |
| 66 | template struct E<short>; |
| 67 | extern template struct E<int>; |
| 68 | |
| 69 | void use_E() { |
| 70 | E<int> ei; |
| 71 | (void)ei; |
| 72 | E<long> el; |
| 73 | (void)el; |
| 74 | } |
| 75 | |
Douglas Gregor | 074a2cf | 2010-01-05 21:40:05 +0000 | [diff] [blame] | 76 | // No key function |
| 77 | template<typename T> |
| 78 | struct F { |
| 79 | virtual void foo() { } |
| 80 | }; |
| 81 | |
| 82 | // No key function |
| 83 | template<> |
| 84 | struct F<char> { |
| 85 | virtual void foo() { } |
| 86 | }; |
| 87 | |
| 88 | template struct F<short>; |
| 89 | extern template struct F<int>; |
| 90 | |
John McCall | 0c7d32b | 2010-08-04 06:38:15 +0000 | [diff] [blame] | 91 | void use_F() { |
| 92 | F<char> fc; |
| 93 | fc.foo(); |
Douglas Gregor | 074a2cf | 2010-01-05 21:40:05 +0000 | [diff] [blame] | 94 | F<int> fi; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 95 | fi.foo(); |
Douglas Gregor | 074a2cf | 2010-01-05 21:40:05 +0000 | [diff] [blame] | 96 | F<long> fl; |
| 97 | (void)fl; |
Douglas Gregor | 074a2cf | 2010-01-05 21:40:05 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Anders Carlsson | 152d4dc | 2009-12-05 22:19:10 +0000 | [diff] [blame] | 100 | // B has a key function that is not defined in this translation unit so its vtable |
| 101 | // has external linkage. |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 102 | // CHECK-1: @_ZTV1B = external constant |
Anders Carlsson | 152d4dc | 2009-12-05 22:19:10 +0000 | [diff] [blame] | 103 | |
John McCall | 3d640e6 | 2010-08-03 07:24:12 +0000 | [diff] [blame] | 104 | // C has no key function, so its vtable should have weak_odr linkage |
| 105 | // and hidden visibility (rdar://problem/7523229). |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame^] | 106 | // CHECK-2: @_ZTV1C = weak_odr constant |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 107 | // CHECK-2: @_ZTS1C = weak_odr constant |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame^] | 108 | // CHECK-2: @_ZTI1C = weak_odr constant |
| 109 | // CHECK-2-HIDDEN: @_ZTV1C = weak_odr hidden constant |
| 110 | // CHECK-2-HIDDEN: @_ZTS1C = weak_odr constant |
| 111 | // CHECK-2-HIDDEN: @_ZTI1C = weak_odr hidden constant |
Anders Carlsson | 891c8b7 | 2009-12-05 22:24:38 +0000 | [diff] [blame] | 112 | |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 113 | // D has a key function that is defined in this translation unit so its vtable is |
| 114 | // defined in the translation unit. |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 115 | // CHECK-3: @_ZTV1D = constant |
| 116 | // CHECK-3: @_ZTS1D = constant |
| 117 | // CHECK-3: @_ZTI1D = constant |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 118 | |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 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. |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 122 | // CHECK-4: @_ZTV1EIcE = constant |
| 123 | // CHECK-4: @_ZTS1EIcE = constant |
| 124 | // CHECK-4: @_ZTI1EIcE = constant |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 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. |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 129 | // CHECK-5: @_ZTV1EIsE = weak_odr constant |
| 130 | // CHECK-5: @_ZTS1EIsE = weak_odr constant |
| 131 | // CHECK-5: @_ZTI1EIsE = weak_odr constant |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame^] | 132 | // CHECK-5-HIDDEN: @_ZTV1EIsE = weak_odr constant |
| 133 | // CHECK-5-HIDDEN: @_ZTS1EIsE = weak_odr constant |
| 134 | // CHECK-5-HIDDEN: @_ZTI1EIsE = weak_odr constant |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 135 | |
Douglas Gregor | 074a2cf | 2010-01-05 21:40:05 +0000 | [diff] [blame] | 136 | // F<short> is an explicit template instantiation without a key |
| 137 | // function, so its vtable should have weak_odr linkage |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 138 | // CHECK-6: @_ZTV1FIsE = weak_odr constant |
| 139 | // CHECK-6: @_ZTS1FIsE = weak_odr constant |
| 140 | // CHECK-6: @_ZTI1FIsE = weak_odr constant |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame^] | 141 | // CHECK-6-HIDDEN: @_ZTV1FIsE = weak_odr constant |
| 142 | // CHECK-6-HIDDEN: @_ZTS1FIsE = weak_odr constant |
| 143 | // CHECK-6-HIDDEN: @_ZTI1FIsE = weak_odr constant |
Douglas Gregor | 074a2cf | 2010-01-05 21:40:05 +0000 | [diff] [blame] | 144 | |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 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. |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 148 | // CHECK-7: @_ZTV1EIlE = weak_odr constant |
| 149 | // CHECK-7: @_ZTS1EIlE = weak_odr constant |
| 150 | // CHECK-7: @_ZTI1EIlE = weak_odr constant |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 151 | |
Douglas Gregor | 074a2cf | 2010-01-05 21:40:05 +0000 | [diff] [blame] | 152 | // F<long> is an implicit template instantiation with no key function, |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 153 | // so its vtable should have weak_odr linkage. |
| 154 | // CHECK-8: @_ZTV1FIlE = weak_odr constant |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 155 | // CHECK-8: @_ZTS1FIlE = weak_odr constant |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 156 | // CHECK-8: @_ZTI1FIlE = weak_odr constant |
Douglas Gregor | 074a2cf | 2010-01-05 21:40:05 +0000 | [diff] [blame] | 157 | |
| 158 | // F<int> is an explicit template instantiation declaration without a |
Rafael Espindola | 35d6461 | 2010-04-03 04:26:42 +0000 | [diff] [blame] | 159 | // key function, so its vtable should have external linkage. |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 160 | // CHECK-9: @_ZTV1FIiE = external constant |
Douglas Gregor | 074a2cf | 2010-01-05 21:40:05 +0000 | [diff] [blame] | 161 | |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 162 | // E<int> is an explicit template instantiation declaration. It has a |
Douglas Gregor | c84622a | 2010-01-07 04:09:30 +0000 | [diff] [blame] | 163 | // key function that is not instantiated, so we should only reference |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 164 | // its vtable, not define it. |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 165 | // CHECK-10: @_ZTV1EIiE = external constant |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 166 | |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 167 | // The anonymous struct for e has no linkage, so the vtable should have |
| 168 | // internal linkage. |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 169 | // CHECK-11: @"_ZTV3$_0" = internal constant |
| 170 | // CHECK-11: @"_ZTS3$_0" = internal constant |
| 171 | // CHECK-11: @"_ZTI3$_0" = internal constant |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 172 | |
Anders Carlsson | 152d4dc | 2009-12-05 22:19:10 +0000 | [diff] [blame] | 173 | // The A vtable should have internal linkage since it is inside an anonymous |
| 174 | // namespace. |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 175 | // CHECK-12: @_ZTVN12_GLOBAL__N_11AE = internal constant |
| 176 | // CHECK-12: @_ZTSN12_GLOBAL__N_11AE = internal constant |
| 177 | // CHECK-12: @_ZTIN12_GLOBAL__N_11AE = internal constant |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 178 | |
John McCall | 0c7d32b | 2010-08-04 06:38:15 +0000 | [diff] [blame] | 179 | // F<char> is an explicit specialization without a key function, so |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 180 | // its vtable should have weak_odr linkage. |
| 181 | // CHECK-13: @_ZTV1FIcE = weak_odr constant |
John McCall | 0c7d32b | 2010-08-04 06:38:15 +0000 | [diff] [blame] | 182 | // CHECK-13: @_ZTS1FIcE = weak_odr constant |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 183 | // CHECK-13: @_ZTI1FIcE = weak_odr constant |
John McCall | 0c7d32b | 2010-08-04 06:38:15 +0000 | [diff] [blame] | 184 | |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 185 | // RUN: FileCheck --check-prefix=CHECK-G %s < %t |
| 186 | // |
| 187 | // CHECK-G: @_ZTV1GIiE = weak_odr constant |
| 188 | template <typename T> |
| 189 | class G { |
| 190 | public: |
| 191 | G() {} |
| 192 | virtual void f0(); |
| 193 | virtual void f1(); |
| 194 | }; |
| 195 | template <> |
| 196 | void G<int>::f1() {} |
| 197 | template <typename T> |
| 198 | void G<T>::f0() {} |
| 199 | void G_f0() { new G<int>(); } |