[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / CodeGenCXX / thunks.cpp
blobf1ceebf24ba1892d2614b4eca82849d33b601adc
1 // Sparc64 doesn't support musttail (yet), so it uses method cloning for
2 // variadic thunks. Use it for testing.
3 // RUN: %clang_cc1 %s -triple=sparc64-pc-linux-gnu -funwind-tables=2 -emit-llvm -o - \
4 // RUN: | FileCheck --check-prefixes=CHECK,CHECK-CLONE,CHECK-NONOPT %s
5 // RUN: %clang_cc1 %s -triple=sparc64-pc-linux-gnu -debug-info-kind=standalone -dwarf-version=5 -funwind-tables=2 -emit-llvm -o - \
6 // RUN: | FileCheck --check-prefixes=CHECK,CHECK-CLONE,CHECK-NONOPT,CHECK-DBG %s
7 // RUN: %clang_cc1 %s -triple=sparc64-pc-linux-gnu -funwind-tables=2 -emit-llvm -o - -O1 -disable-llvm-passes \
8 // RUN: | FileCheck --check-prefixes=CHECK,CHECK-CLONE,CHECK-OPT %s
10 // Test x86_64, which uses musttail for variadic thunks.
11 // RUN: %clang_cc1 %s -triple=x86_64-pc-linux-gnu -funwind-tables=2 -emit-llvm -o - -O1 -disable-llvm-passes \
12 // RUN: | FileCheck --check-prefixes=CHECK,CHECK-TAIL,CHECK-OPT %s
14 // Finally, reuse these tests for the MS ABI.
15 // RUN: %clang_cc1 %s -triple=x86_64-windows-msvc -funwind-tables=2 -emit-llvm -o - -O1 -disable-llvm-passes \
16 // RUN: | FileCheck --check-prefixes=WIN64 %s
19 namespace Test1 {
21 // Check that we emit a non-virtual thunk for C::f.
23 struct A {
24 virtual void f();
27 struct B {
28 virtual void f();
31 struct C : A, B {
32 virtual void c();
34 virtual void f();
37 // CHECK-LABEL: define{{.*}} void @_ZThn8_N5Test11C1fEv(
38 // CHECK-DBG-NOT: dbg.declare
39 // CHECK: ret void
41 // WIN64-LABEL: define dso_local void @"?f@C@Test1@@UEAAXXZ"(
42 // WIN64-LABEL: define linkonce_odr dso_local void @"?f@C@Test1@@W7EAAXXZ"(
43 // WIN64: getelementptr i8, ptr {{.*}}, i32 -8
44 // WIN64: ret void
45 void C::f() { }
49 namespace Test2 {
51 // Check that we emit a thunk for B::f since it's overriding a virtual base.
53 struct A {
54 virtual void f();
57 struct B : virtual A {
58 virtual void b();
59 virtual void f();
62 // CHECK-LABEL: define{{.*}} void @_ZTv0_n24_N5Test21B1fEv(
63 // CHECK-DBG-NOT: dbg.declare
64 // CHECK: ret void
65 void B::f() { }
67 // No thunk is used for this case in the MS ABI.
68 // WIN64-LABEL: define dso_local void @"?f@B@Test2@@UEAAXXZ"(
69 // WIN64-NOT: define {{.*}} void @"?f@B@Test2
73 namespace Test3 {
75 // Check that we emit a covariant thunk for B::f.
77 struct V1 { };
78 struct V2 : virtual V1 { };
80 struct A {
81 virtual V1 *f();
84 struct B : A {
85 virtual void b();
87 virtual V2 *f();
90 // CHECK: define{{.*}} ptr @_ZTch0_v0_n24_N5Test31B1fEv(
91 // WIN64: define weak_odr dso_local noundef ptr @"?f@B@Test3@@QEAAPEAUV1@2@XZ"(
92 V2 *B::f() { return 0; }
96 namespace Test4 {
98 // Check that the thunk for 'C::f' has the same visibility as the function itself.
100 struct A {
101 virtual void f();
104 struct B {
105 virtual void f();
108 struct __attribute__((visibility("protected"))) C : A, B {
109 virtual void c();
111 virtual void f();
114 // CHECK-LABEL: define protected void @_ZThn8_N5Test41C1fEv(
115 // CHECK-DBG-NOT: dbg.declare
116 // CHECK: ret void
117 void C::f() { }
119 // Visibility doesn't matter on COFF, but whatever. We could add an ELF test
120 // mode later.
121 // WIN64-LABEL: define protected void @"?f@C@Test4@@UEAAXXZ"(
122 // WIN64-LABEL: define linkonce_odr protected void @"?f@C@Test4@@W7EAAXXZ"(
125 // Check that the thunk gets internal linkage.
126 namespace Test4B {
127 struct A {
128 virtual void f();
131 struct B {
132 virtual void f();
135 namespace {
136 struct C : A, B {
137 virtual void c();
138 virtual void f();
141 void C::c() {}
142 void C::f() {}
144 // Force C::f to be used.
145 void f() {
146 C c;
147 c.f();
150 // Not sure why this isn't delayed like in Itanium.
151 // WIN64-LABEL: define internal void @"?f@C@?A{{.*}}@Test4B@@UEAAXXZ"(
153 namespace Test5 {
155 // Check that the thunk for 'B::f' gets the same linkage as the function itself.
156 struct A {
157 virtual void f();
160 struct B : virtual A {
161 virtual void f() { }
164 void f(B b) {
165 b.f();
167 // No thunk in MS ABI in this case.
170 namespace Test6 {
171 struct X {
172 X();
173 X(const X&);
174 X &operator=(const X&);
175 ~X();
178 struct P {
179 P();
180 P(const P&);
181 ~P();
182 X first;
183 X second;
186 P getP();
188 struct Base1 {
189 int i;
191 virtual X f() { return X(); }
194 struct Base2 {
195 float real;
197 virtual X f() { return X(); }
200 struct Thunks : Base1, Base2 {
201 long l;
203 virtual X f();
206 // CHECK-LABEL: define{{.*}} void @_ZThn16_N5Test66Thunks1fEv
207 // CHECK-DBG-NOT: dbg.declare
208 // CHECK-NOT: memcpy
209 // CHECK: {{call void @_ZN5Test66Thunks1fEv.*sret(.+) align 1}}
210 // CHECK: ret void
211 X Thunks::f() { return X(); }
213 // WIN64-LABEL: define linkonce_odr dso_local void @"?f@Thunks@Test6@@WBA@EAA?AUX@2@XZ"({{.*}} sret({{.*}}) align 1 %{{.*}})
214 // WIN64-NOT: memcpy
215 // WIN64: tail call void @"?f@Thunks@Test6@@UEAA?AUX@2@XZ"({{.*}} sret({{.*}}) align 1 %{{.*}})
218 namespace Test7 {
219 // PR7188
220 struct X {
221 X();
222 X(const X&);
223 X &operator=(const X&);
224 ~X();
227 struct Small { short s; };
228 struct Large {
229 char array[1024];
232 class A {
233 protected:
234 virtual void foo() = 0;
237 class B : public A {
238 protected:
239 virtual void bar() = 0;
242 class C : public A {
243 protected:
244 virtual void baz(X, X&, _Complex float, Small, Small&, Large) = 0;
247 class D : public B,
248 public C {
250 void foo() {}
251 void bar() {}
252 void baz(X, X&, _Complex float, Small, Small&, Large);
255 void D::baz(X, X&, _Complex float, Small, Small&, Large) { }
257 // CHECK-LABEL: define{{.*}} void @_ZThn8_N5Test71D3bazENS_1XERS1_CfNS_5SmallERS4_NS_5LargeE(
258 // CHECK-DBG-NOT: dbg.declare
259 // CHECK-NOT: memcpy
260 // CHECK: ret void
261 void testD() { D d; }
263 // MS C++ ABI doesn't use a thunk, so this case isn't interesting.
266 namespace Test8 {
267 struct NonPOD { ~NonPOD(); int x, y, z; };
268 struct A { virtual void foo(); };
269 struct B { virtual void bar(NonPOD); };
270 struct C : A, B { virtual void bar(NonPOD); static void helper(NonPOD); };
272 // CHECK: define{{.*}} void @_ZN5Test81C6helperENS_6NonPODE(ptr
273 void C::helper(NonPOD var) {}
275 // CHECK-LABEL: define{{.*}} void @_ZThn8_N5Test81C3barENS_6NonPODE(
276 // CHECK-DBG-NOT: dbg.declare
277 // CHECK-NOT: load [[NONPODTYPE:%.*]], ptr
278 // CHECK-NOT: memcpy
279 // CHECK: ret void
280 void C::bar(NonPOD var) {}
282 // MS C++ ABI doesn't use a thunk, so this case isn't interesting.
285 // PR7241: Emitting thunks for a method shouldn't require the vtable for
286 // that class to be emitted.
287 namespace Test9 {
288 struct A { virtual ~A() { } };
289 struct B : A { virtual void test() const {} };
290 struct C : B { C(); ~C(); };
291 struct D : C { D() {} };
292 void test() {
293 D d;
297 namespace Test10 {
298 struct A { virtual void foo(); };
299 struct B { virtual void foo(); };
300 struct C : A, B { void foo() {} };
302 // Test later.
303 void test() {
304 C c;
308 // PR7611
309 namespace Test11 {
310 struct A { virtual A* f(); };
311 struct B : virtual A { virtual A* f(); };
312 struct C : B { virtual C* f(); };
313 C* C::f() { return 0; }
315 // C::f itself.
316 // CHECK: define {{.*}} @_ZN6Test111C1fEv(
318 // The this-adjustment and return-adjustment thunk required when
319 // C::f appears in a vtable where A is at a nonzero offset from C.
320 // CHECK: define {{.*}} @_ZTcv0_n24_v0_n32_N6Test111C1fEv(
321 // CHECK-DBG-NOT: dbg.declare
322 // CHECK: ret
324 // The return-adjustment thunk required when C::f appears in a vtable
325 // where A is at a zero offset from C.
326 // CHECK: define {{.*}} @_ZTch0_v0_n32_N6Test111C1fEv(
327 // CHECK-DBG-NOT: dbg.declare
328 // CHECK: ret
330 // WIN64-LABEL: define dso_local noundef ptr @"?f@C@Test11@@UEAAPEAU12@XZ"(ptr
332 // WIN64-LABEL: define weak_odr dso_local noundef ptr @"?f@C@Test11@@QEAAPEAUA@2@XZ"(ptr
333 // WIN64: call noundef ptr @"?f@C@Test11@@UEAAPEAU12@XZ"(ptr noundef %{{.*}})
335 // Match the vbtable return adjustment.
336 // WIN64: load ptr, ptr %{{[^,]*}}, align 8
337 // WIN64: getelementptr inbounds i32, ptr %{{[^,]*}}, i32 1
338 // WIN64: load i32, ptr %{{[^,]*}}, align 4
341 // Varargs thunk test.
342 namespace Test12 {
343 struct A {
344 virtual A* f(int x, ...);
346 struct B {
347 virtual B* f(int x, ...);
349 struct C : A, B {
350 virtual void c();
351 virtual C* f(int x, ...);
353 C* makeC();
354 C* C::f(int x, ...) { return makeC(); }
356 // C::f
357 // CHECK: define {{.*}} @_ZN6Test121C1fEiz
359 // Varargs thunk; check that both the this and covariant adjustments
360 // are generated.
361 // CHECK: define {{.*}} @_ZTchn8_h8_N6Test121C1fEiz
362 // CHECK-DBG-NOT: dbg.declare
363 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 -8
364 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 8
366 // The vtable layout goes:
367 // C vtable in A:
368 // - f impl, no adjustment
369 // C vtable in B:
370 // - f thunk 2, covariant, clone
371 // - f thunk 2, musttail this adjust to impl
372 // FIXME: The weak_odr linkage is probably not necessary and just an artifact
373 // of Itanium ABI details.
374 // WIN64-LABEL: define dso_local {{.*}} @"?f@C@Test12@@UEAAPEAU12@HZZ"(
375 // WIN64: call noundef ptr @"?makeC@Test12@@YAPEAUC@1@XZ"()
377 // This thunk needs return adjustment, clone.
378 // WIN64-LABEL: define weak_odr dso_local {{.*}} @"?f@C@Test12@@W7EAAPEAUB@2@HZZ"(
379 // WIN64: call noundef ptr @"?makeC@Test12@@YAPEAUC@1@XZ"()
380 // WIN64: getelementptr inbounds i8, ptr %{{.*}}, i32 8
382 // Musttail call back to the A implementation after this adjustment from B to A.
383 // WIN64-LABEL: define linkonce_odr dso_local noundef ptr @"?f@C@Test12@@W7EAAPEAU12@HZZ"(
384 // WIN64: getelementptr i8, ptr %{{[^,]*}}, i32 -8
385 // WIN64: musttail call {{.*}} @"?f@C@Test12@@UEAAPEAU12@HZZ"(
386 C c;
389 // PR13832
390 namespace Test13 {
391 struct B1 {
392 virtual B1 &foo1();
394 struct Pad1 {
395 virtual ~Pad1();
397 struct Proxy1 : Pad1, B1 {
398 virtual ~Proxy1();
400 struct D : virtual Proxy1 {
401 virtual ~D();
402 virtual D &foo1();
404 D& D::foo1() {
405 return *this;
407 // CHECK: define {{.*}} @_ZTcvn8_n32_v8_n24_N6Test131D4foo1Ev
408 // CHECK-DBG-NOT: dbg.declare
409 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 -8
410 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 -32
411 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 -24
412 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 8
413 // CHECK: ret ptr
415 // WIN64-LABEL: define weak_odr dso_local noundef ptr @"?foo1@D@Test13@@$4PPPPPPPE@A@EAAAEAUB1@2@XZ"(
416 // This adjustment.
417 // WIN64: getelementptr inbounds i8, ptr {{.*}}, i64 -12
418 // Call implementation.
419 // WIN64: call {{.*}} @"?foo1@D@Test13@@UEAAAEAU12@XZ"(ptr {{.*}})
420 // Virtual + nonvirtual return adjustment.
421 // WIN64: load ptr, ptr %{{[^,]*}}, align 8
422 // WIN64: getelementptr inbounds i32, ptr %{{[^,]*}}, i32 1
423 // WIN64: load i32, ptr %{{[^,]*}}, align 4
424 // WIN64: getelementptr inbounds i8, ptr %{{[^,]*}}, i32 %{{[^,]*}}
427 namespace Test14 {
428 class A {
429 virtual void f();
431 class B {
432 virtual void f();
434 class C : public A, public B {
435 virtual void f();
437 void C::f() {
439 // CHECK: define{{.*}} void @_ZThn8_N6Test141C1fEv({{.*}}) unnamed_addr [[NUW:#[0-9]+]]
440 // CHECK-DBG-NOT: dbg.declare
441 // CHECK: ret void
444 // Varargs non-covariant thunk test.
445 // PR18098
446 namespace Test15 {
447 struct A {
448 virtual ~A();
450 struct B {
451 virtual void f(int x, ...);
453 struct C : A, B {
454 virtual void c();
455 virtual void f(int x, ...);
457 void C::c() {}
459 // C::c
460 // CHECK-CLONE: declare void @_ZN6Test151C1fEiz
461 // non-virtual thunk to C::f
462 // CHECK-CLONE: declare void @_ZThn8_N6Test151C1fEiz
464 // If we have musttail, then we emit the thunk as available_externally.
465 // CHECK-TAIL: declare void @_ZN6Test151C1fEiz
466 // CHECK-TAIL: define available_externally void @_ZThn8_N6Test151C1fEiz({{.*}})
467 // CHECK-TAIL: musttail call void (ptr, i32, ...) @_ZN6Test151C1fEiz({{.*}}, ...)
469 // MS C++ ABI doesn't use a thunk, so this case isn't interesting.
472 namespace Test16 {
473 struct A {
474 virtual ~A();
476 struct B {
477 virtual void foo();
479 struct C : public A, public B {
480 void foo() {}
482 struct D : public C {
483 ~D();
485 D::~D() {}
486 // CHECK: define linkonce_odr void @_ZThn8_N6Test161C3fooEv({{.*}}) {{.*}} comdat
487 // CHECK-DBG-NOT: dbg.declare
488 // CHECK: ret void
491 namespace Test17 {
492 class A {
493 virtual void f(const char *, ...);
495 class B {
496 virtual void f(const char *, ...);
498 class C : A, B {
499 virtual void anchor();
500 void f(const char *, ...) override;
502 // Key method and object anchor vtable for Itanium and MSVC.
503 void C::anchor() {}
504 C c;
506 // CHECK-CLONE-LABEL: declare void @_ZThn8_N6Test171C1fEPKcz(
508 // CHECK-TAIL-LABEL: define available_externally void @_ZThn8_N6Test171C1fEPKcz(
509 // CHECK-TAIL: getelementptr inbounds i8, ptr %{{.*}}, i64 -8
510 // CHECK-TAIL: musttail call {{.*}} @_ZN6Test171C1fEPKcz({{.*}}, ...)
512 // MSVC-LABEL: define linkonce_odr dso_local void @"?f@C@Test17@@G7EAAXPEBDZZ"
513 // MSVC-SAME: (ptr %this, ptr %[[ARG:[^,]+]], ...)
514 // MSVC: getelementptr i8, ptr %{{.*}}, i32 -8
515 // MSVC: musttail call void (ptr, ptr, ...) @"?f@C@Test17@@EEAAXPEBDZZ"(ptr %{{.*}}, ptr noundef %[[ARG]], ...)
518 /**** The following has to go at the end of the file ****/
520 // checking without opt
521 // CHECK-NONOPT-LABEL: define internal void @_ZThn8_N6Test4B12_GLOBAL__N_11C1fEv(
522 // CHECK-NONOPT-NOT: comdat
524 // This is from Test5:
525 // CHECK-NONOPT-LABEL: define linkonce_odr void @_ZTv0_n24_N5Test51B1fEv
527 // This is from Test10:
528 // CHECK-NONOPT-LABEL: define linkonce_odr void @_ZN6Test101C3fooEv
529 // CHECK-NONOPT-LABEL: define linkonce_odr void @_ZThn8_N6Test101C3fooEv
531 // Checking with opt
532 // CHECK-OPT-LABEL: define internal void @_ZThn8_N6Test4B12_GLOBAL__N_11C1fEv(ptr noundef %this) unnamed_addr #1 align 2
534 // This is from Test5:
535 // CHECK-OPT-LABEL: define linkonce_odr void @_ZTv0_n24_N5Test51B1fEv
537 // This is from Test10:
538 // CHECK-OPT-LABEL: define linkonce_odr void @_ZN6Test101C3fooEv
539 // CHECK-OPT-LABEL: define linkonce_odr void @_ZThn8_N6Test101C3fooEv
541 // This is from Test10:
542 // WIN64-LABEL: define linkonce_odr dso_local void @"?foo@C@Test10@@UEAAXXZ"(
543 // WIN64-LABEL: define linkonce_odr dso_local void @"?foo@C@Test10@@W7EAAXXZ"(
545 // CHECK-NONOPT: attributes [[NUW]] = { noinline nounwind optnone uwtable{{.*}} }
546 // CHECK-OPT: attributes [[NUW]] = { nounwind uwtable{{.*}} }