Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm-only -fdump-vtable-layouts 2>&1 | FileCheck %s |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2 | |
Anders Carlsson | 49bac9a | 2010-02-13 23:40:17 +0000 | [diff] [blame] | 3 | // For now, just verify this doesn't crash. |
| 4 | namespace test0 { |
| 5 | struct Obj {}; |
| 6 | |
| 7 | struct Base { virtual const Obj *foo() = 0; }; |
| 8 | struct Derived : Base { virtual Obj *foo() { return new Obj(); } }; |
| 9 | |
| 10 | void test(Derived *D) { D->foo(); } |
| 11 | } |
| 12 | |
| 13 | namespace Test1 { |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 14 | // CHECK: Vtable for 'Test1::A' (3 entries). |
| 15 | // CHECK-NEXT: 0 | offset_to_top (0) |
| 16 | // CHECK-NEXT: 1 | Test1::A RTTI |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 17 | // CHECK-NEXT: -- (Test1::A, 0) vtable address -- |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 18 | // CHECK-NEXT: 2 | void Test1::A::f() |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 19 | struct A { |
| 20 | virtual void f(); |
| 21 | }; |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 22 | void A::f() { } |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 23 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 26 | namespace Test2 { |
| 27 | |
| 28 | // This is a smoke test of the vtable dumper. |
Anders Carlsson | bac7271 | 2010-02-11 18:21:49 +0000 | [diff] [blame] | 29 | // CHECK: Vtable for 'Test2::A' (9 entries). |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 30 | // CHECK-NEXT: 0 | offset_to_top (0) |
| 31 | // CHECK-NEXT: 1 | Test2::A RTTI |
| 32 | // CHECK-NEXT: -- (Test2::A, 0) vtable address -- |
| 33 | // CHECK-NEXT: 2 | void Test2::A::f() |
| 34 | // CHECK-NEXT: 3 | void Test2::A::f() const |
| 35 | // CHECK-NEXT: 4 | Test2::A *Test2::A::g(int) |
| 36 | // CHECK-NEXT: 5 | Test2::A::~A() [complete] |
| 37 | // CHECK-NEXT: 6 | Test2::A::~A() [deleting] |
| 38 | // CHECK-NEXT: 7 | void Test2::A::h() |
Anders Carlsson | bac7271 | 2010-02-11 18:21:49 +0000 | [diff] [blame] | 39 | // CHECK-NEXT: 8 | Test2::A &Test2::A::operator=(Test2::A const &) |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 40 | struct A { |
| 41 | virtual void f(); |
| 42 | virtual void f() const; |
| 43 | |
| 44 | virtual A* g(int a); |
| 45 | virtual ~A(); |
| 46 | virtual void h(); |
Anders Carlsson | bac7271 | 2010-02-11 18:21:49 +0000 | [diff] [blame] | 47 | virtual A& operator=(const A&); |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 48 | }; |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 49 | void A::f() { } |
| 50 | |
Anders Carlsson | 9824142 | 2010-02-12 02:38:13 +0000 | [diff] [blame] | 51 | // Another simple vtable dumper test. |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 52 | |
Anders Carlsson | 9824142 | 2010-02-12 02:38:13 +0000 | [diff] [blame] | 53 | // CHECK: Vtable for 'Test2::B' (6 entries). |
| 54 | // CHECK-NEXT: 0 | offset_to_top (0) |
| 55 | // CHECK-NEXT: 1 | Test2::B RTTI |
| 56 | // CHECK-NEXT: -- (Test2::B, 0) vtable address -- |
| 57 | // CHECK-NEXT: 2 | void Test2::B::f() |
| 58 | // CHECK-NEXT: 3 | void Test2::B::g() [pure] |
| 59 | // CHECK-NEXT: 4 | Test2::B::~B() [complete] [pure] |
| 60 | // CHECK-NEXT: 5 | Test2::B::~B() [deleting] [pure] |
Anders Carlsson | 9824142 | 2010-02-12 02:38:13 +0000 | [diff] [blame] | 61 | struct B { |
| 62 | virtual void f(); |
| 63 | virtual void g() = 0; |
| 64 | virtual ~B() = 0; |
| 65 | }; |
Anders Carlsson | 9824142 | 2010-02-12 02:38:13 +0000 | [diff] [blame] | 66 | void B::f() { } |
| 67 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 68 | } |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 69 | |
| 70 | namespace Test3 { |
| 71 | |
| 72 | // If a function in a derived class overrides a function in a primary base, |
| 73 | // then the function should not have an entry in the derived class (unless the return |
| 74 | // value requires adjusting). |
| 75 | |
| 76 | // CHECK: Vtable for 'Test3::A' (3 entries). |
| 77 | // CHECK-NEXT: 0 | offset_to_top (0) |
| 78 | // CHECK-NEXT: 1 | Test3::A RTTI |
| 79 | // CHECK-NEXT: -- (Test3::A, 0) vtable address -- |
| 80 | // CHECK-NEXT: 2 | void Test3::A::f() |
| 81 | struct A { |
| 82 | virtual void f(); |
| 83 | }; |
| 84 | void A::f() { } |
| 85 | |
| 86 | // CHECK: Vtable for 'Test3::B' (4 entries). |
| 87 | // CHECK-NEXT: 0 | offset_to_top (0) |
| 88 | // CHECK-NEXT: 1 | Test3::B RTTI |
Anders Carlsson | 28cbc8b | 2010-02-12 07:43:48 +0000 | [diff] [blame] | 89 | // CHECK-NEXT: -- (Test3::A, 0) vtable address -- |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 90 | // CHECK-NEXT: -- (Test3::B, 0) vtable address -- |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 91 | // CHECK-NEXT: 2 | void Test3::B::f() |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 92 | // CHECK-NEXT: 3 | void Test3::B::g() |
| 93 | struct B : A { |
| 94 | virtual void f(); |
| 95 | virtual void g(); |
| 96 | }; |
| 97 | void B::f() { } |
| 98 | |
| 99 | // CHECK: Vtable for 'Test3::C' (5 entries). |
| 100 | // CHECK-NEXT: 0 | offset_to_top (0) |
| 101 | // CHECK-NEXT: 1 | Test3::C RTTI |
Anders Carlsson | 28cbc8b | 2010-02-12 07:43:48 +0000 | [diff] [blame] | 102 | // CHECK-NEXT: -- (Test3::A, 0) vtable address -- |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 103 | // CHECK-NEXT: -- (Test3::C, 0) vtable address -- |
| 104 | // CHECK-NEXT: 2 | void Test3::A::f() |
| 105 | // CHECK-NEXT: 3 | void Test3::C::g() |
| 106 | // CHECK-NEXT: 4 | void Test3::C::h() |
| 107 | struct C : A { |
| 108 | virtual void g(); |
| 109 | virtual void h(); |
| 110 | }; |
| 111 | void C::g() { } |
| 112 | |
| 113 | // CHECK: Vtable for 'Test3::D' (5 entries). |
| 114 | // CHECK-NEXT: 0 | offset_to_top (0) |
| 115 | // CHECK-NEXT: 1 | Test3::D RTTI |
Anders Carlsson | 28cbc8b | 2010-02-12 07:43:48 +0000 | [diff] [blame] | 116 | // CHECK-NEXT: -- (Test3::A, 0) vtable address -- |
| 117 | // CHECK-NEXT: -- (Test3::B, 0) vtable address -- |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 118 | // CHECK-NEXT: -- (Test3::D, 0) vtable address -- |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 119 | // CHECK-NEXT: 2 | void Test3::D::f() |
| 120 | // CHECK-NEXT: 3 | void Test3::D::g() |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 121 | // CHECK-NEXT: 4 | void Test3::D::h() |
| 122 | struct D : B { |
| 123 | virtual void f(); |
| 124 | virtual void g(); |
| 125 | virtual void h(); |
| 126 | }; |
| 127 | |
| 128 | void D::f() { } |
| 129 | } |
John McCall | 9605895 | 2010-02-12 06:15:07 +0000 | [diff] [blame] | 130 | |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 131 | namespace Test4 { |
| 132 | |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 133 | // Test non-virtual result adjustments. |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 134 | |
| 135 | struct R1 { int r1; }; |
| 136 | struct R2 { int r2; }; |
| 137 | struct R3 : R1, R2 { int r3; }; |
| 138 | |
| 139 | struct A { |
| 140 | virtual R2 *f(); |
| 141 | }; |
| 142 | |
| 143 | // CHECK: Vtable for 'Test4::B' (4 entries). |
| 144 | // CHECK-NEXT: 0 | offset_to_top (0) |
| 145 | // CHECK-NEXT: 1 | Test4::B RTTI |
| 146 | // CHECK-NEXT: -- (Test4::A, 0) vtable address -- |
| 147 | // CHECK-NEXT: -- (Test4::B, 0) vtable address -- |
| 148 | // CHECK-NEXT: 2 | Test4::R3 *Test4::B::f() |
| 149 | // CHECK-NEXT: [return adjustment: 4 non-virtual] |
| 150 | // CHECK-NEXT: 3 | Test4::R3 *Test4::B::f() |
| 151 | |
| 152 | struct B : A { |
| 153 | virtual R3 *f(); |
| 154 | }; |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 155 | R3 *B::f() { return 0; } |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 156 | |
| 157 | // Test virtual result adjustments. |
| 158 | struct V1 { int v1; }; |
| 159 | struct V2 : virtual V1 { int v1; }; |
| 160 | |
| 161 | struct C { |
| 162 | virtual V1 *f(); |
| 163 | }; |
| 164 | |
| 165 | // CHECK: Vtable for 'Test4::D' (4 entries). |
| 166 | // CHECK-NEXT: 0 | offset_to_top (0) |
| 167 | // CHECK-NEXT: 1 | Test4::D RTTI |
| 168 | // CHECK-NEXT: -- (Test4::C, 0) vtable address -- |
| 169 | // CHECK-NEXT: -- (Test4::D, 0) vtable address -- |
| 170 | // CHECK-NEXT: 2 | Test4::V2 *Test4::D::f() |
| 171 | // CHECK-NEXT: [return adjustment: 0 non-virtual, -24 vbase offset offset] |
| 172 | // CHECK-NEXT: 3 | Test4::V2 *Test4::D::f() |
| 173 | struct D : C { |
| 174 | virtual V2 *f(); |
| 175 | }; |
| 176 | V2 *D::f() { return 0; }; |
| 177 | |
| 178 | // Virtual result adjustments with an additional non-virtual adjustment. |
| 179 | struct V3 : virtual R3 { int r3; }; |
| 180 | |
| 181 | // CHECK: Vtable for 'Test4::E' (4 entries). |
| 182 | // CHECK-NEXT: 0 | offset_to_top (0) |
| 183 | // CHECK-NEXT: 1 | Test4::E RTTI |
| 184 | // CHECK-NEXT: -- (Test4::A, 0) vtable address -- |
| 185 | // CHECK-NEXT: -- (Test4::E, 0) vtable address -- |
| 186 | // CHECK-NEXT: 2 | Test4::V3 *Test4::E::f() |
| 187 | // CHECK-NEXT: [return adjustment: 4 non-virtual, -24 vbase offset offset] |
| 188 | // CHECK-NEXT: 3 | Test4::V3 *Test4::E::f() |
| 189 | |
| 190 | struct E : A { |
| 191 | virtual V3 *f(); |
| 192 | }; |
| 193 | V3 *E::f() { return 0;} |
| 194 | |
Anders Carlsson | 1d05be5 | 2010-02-13 21:16:54 +0000 | [diff] [blame] | 195 | // Test that a pure virtual member doesn't get a thunk. |
| 196 | |
| 197 | // CHECK: Vtable for 'Test4::F' (5 entries). |
| 198 | // CHECK-NEXT: 0 | offset_to_top (0) |
| 199 | // CHECK-NEXT: 1 | Test4::F RTTI |
| 200 | // CHECK-NEXT: -- (Test4::A, 0) vtable address -- |
| 201 | // CHECK-NEXT: -- (Test4::F, 0) vtable address -- |
| 202 | // CHECK-NEXT: 2 | Test4::R3 *Test4::F::f() [pure] |
| 203 | // CHECK-NEXT: 3 | void Test4::F::g() |
| 204 | // CHECK-NEXT: 4 | Test4::R3 *Test4::F::f() [pure] |
| 205 | struct F : A { |
| 206 | virtual void g(); |
| 207 | virtual R3 *f() = 0; |
| 208 | }; |
Anders Carlsson | 1d05be5 | 2010-02-13 21:16:54 +0000 | [diff] [blame] | 209 | void F::g() { } |
| 210 | |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Anders Carlsson | 76f1aa7 | 2010-02-13 22:05:23 +0000 | [diff] [blame] | 213 | namespace Test5 { |
| 214 | |
Anders Carlsson | 49bac9a | 2010-02-13 23:40:17 +0000 | [diff] [blame] | 215 | // Simple secondary vtables without 'this' pointer adjustments. |
Anders Carlsson | 76f1aa7 | 2010-02-13 22:05:23 +0000 | [diff] [blame] | 216 | struct A { |
| 217 | virtual void f(); |
| 218 | virtual void g(); |
| 219 | int a; |
| 220 | }; |
| 221 | |
| 222 | struct B1 : A { |
| 223 | virtual void f(); |
| 224 | int b1; |
| 225 | }; |
| 226 | |
| 227 | struct B2 : A { |
| 228 | virtual void g(); |
| 229 | int b2; |
| 230 | }; |
| 231 | |
| 232 | // CHECK: Vtable for 'Test5::C' (9 entries). |
| 233 | // CHECK-NEXT: 0 | offset_to_top (0) |
| 234 | // CHECK-NEXT: 1 | Test5::C RTTI |
| 235 | // CHECK-NEXT: -- (Test5::A, 0) vtable address -- |
| 236 | // CHECK-NEXT: -- (Test5::B1, 0) vtable address -- |
| 237 | // CHECK-NEXT: -- (Test5::C, 0) vtable address -- |
| 238 | // CHECK-NEXT: 2 | void Test5::B1::f() |
| 239 | // CHECK-NEXT: 3 | void Test5::A::g() |
| 240 | // CHECK-NEXT: 4 | void Test5::C::h() |
| 241 | // CHECK-NEXT: 5 | offset_to_top (-16) |
| 242 | // CHECK-NEXT: 6 | Test5::C RTTI |
| 243 | // CHECK-NEXT: -- (Test5::A, 16) vtable address -- |
| 244 | // CHECK-NEXT: -- (Test5::B2, 16) vtable address -- |
| 245 | // CHECK-NEXT: 7 | void Test5::A::f() |
| 246 | // CHECK-NEXT: 8 | void Test5::B2::g() |
| 247 | struct C : B1, B2 { |
| 248 | virtual void h(); |
| 249 | }; |
| 250 | void C::h() { } |
| 251 | } |
| 252 | |
Anders Carlsson | 49bac9a | 2010-02-13 23:40:17 +0000 | [diff] [blame] | 253 | namespace Test6 { |
John McCall | 9605895 | 2010-02-12 06:15:07 +0000 | [diff] [blame] | 254 | |
Anders Carlsson | 49bac9a | 2010-02-13 23:40:17 +0000 | [diff] [blame] | 255 | // Simple non-virtual 'this' pointer adjustments. |
| 256 | struct A1 { |
| 257 | virtual void f(); |
| 258 | int a; |
| 259 | }; |
John McCall | 9605895 | 2010-02-12 06:15:07 +0000 | [diff] [blame] | 260 | |
Anders Carlsson | 49bac9a | 2010-02-13 23:40:17 +0000 | [diff] [blame] | 261 | struct A2 { |
| 262 | virtual void f(); |
| 263 | int a; |
| 264 | }; |
| 265 | |
| 266 | // CHECK: Vtable for 'Test6::C' (6 entries). |
| 267 | // CHECK-NEXT: 0 | offset_to_top (0) |
| 268 | // CHECK-NEXT: 1 | Test6::C RTTI |
| 269 | // CHECK-NEXT: -- (Test6::A1, 0) vtable address -- |
| 270 | // CHECK-NEXT: -- (Test6::C, 0) vtable address -- |
| 271 | // CHECK-NEXT: 2 | void Test6::C::f() |
| 272 | // CHECK-NEXT: 3 | offset_to_top (-16) |
| 273 | // CHECK-NEXT: 4 | Test6::C RTTI |
| 274 | // CHECK-NEXT: -- (Test6::A2, 16) vtable address -- |
| 275 | // CHECK-NEXT: 5 | void Test6::C::f() |
| 276 | // CHECK-NEXT: [this adjustment: -16 non-virtual] |
| 277 | struct C : A1, A2 { |
| 278 | virtual void f(); |
| 279 | }; |
| 280 | void C::f() { } |
| 281 | |
Anders Carlsson | e67dc30 | 2010-02-14 00:16:19 +0000 | [diff] [blame^] | 282 | } |
| 283 | |
| 284 | namespace Test7 { |
| 285 | |
| 286 | // Test that the D::f overrider for A::f have different 'this' pointer |
| 287 | // adjustments in the two A base subobjects. |
| 288 | |
| 289 | struct A { |
| 290 | virtual void f(); |
| 291 | int a; |
| 292 | }; |
| 293 | |
| 294 | struct B1 : A { }; |
| 295 | struct B2 : A { }; |
| 296 | |
| 297 | struct C { virtual void c(); }; |
| 298 | |
| 299 | // CHECK: Vtable for 'Test7::D' (10 entries). |
| 300 | // CHECK-NEXT: 0 | offset_to_top (0) |
| 301 | // CHECK-NEXT: 1 | Test7::D RTTI |
| 302 | // CHECK-NEXT: -- (Test7::C, 0) vtable address -- |
| 303 | // CHECK-NEXT: -- (Test7::D, 0) vtable address -- |
| 304 | // CHECK-NEXT: 2 | void Test7::C::c() |
| 305 | // CHECK-NEXT: 3 | void Test7::D::f() |
| 306 | // CHECK-NEXT: 4 | offset_to_top (-8) |
| 307 | // CHECK-NEXT: 5 | Test7::D RTTI |
| 308 | // CHECK-NEXT: -- (Test7::A, 8) vtable address -- |
| 309 | // CHECK-NEXT: -- (Test7::B1, 8) vtable address -- |
| 310 | // CHECK-NEXT: 6 | void Test7::D::f() |
| 311 | // CHECK-NEXT: [this adjustment: -8 non-virtual] |
| 312 | // CHECK-NEXT: 7 | offset_to_top (-24) |
| 313 | // CHECK-NEXT: 8 | Test7::D RTTI |
| 314 | // CHECK-NEXT: -- (Test7::A, 24) vtable address -- |
| 315 | // CHECK-NEXT: -- (Test7::B2, 24) vtable address -- |
| 316 | // CHECK-NEXT: 9 | void Test7::D::f() |
| 317 | // CHECK-NEXT: [this adjustment: -24 non-virtual] |
| 318 | struct D : C, B1, B2 { |
| 319 | virtual void f(); |
| 320 | }; |
| 321 | void D::f() { } |
| 322 | |
| 323 | } |