[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / class-layout.cpp
blob22fb34b8419c5834fe1af059204cd0c48cf4a367
1 // RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++98 -Wno-inaccessible-base -Wno-invalid-offsetof -Wno-c++11-extensions
2 // RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof
3 // RUN: %clang_cc1 -triple x86_64-apple-darwin %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof -DCLANG_ABI_COMPAT=15
4 // RUN: %clang_cc1 -triple x86_64-scei-ps4 %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof -DCLANG_ABI_COMPAT=6
5 // RUN: %clang_cc1 -triple x86_64-sie-ps5 %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof -DCLANG_ABI_COMPAT=6
6 // RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof -fclang-abi-compat=6 -DCLANG_ABI_COMPAT=6
7 // RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof -fclang-abi-compat=14 -DCLANG_ABI_COMPAT=14
8 // RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof -fclang-abi-compat=15 -DCLANG_ABI_COMPAT=15
9 // RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof -fclang-abi-compat=16 -DCLANG_ABI_COMPAT=16
10 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.3.0.0 %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof -DCLANG_ABI_COMPAT=15
11 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.3.0.0 %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof -fclang-abi-compat=15 -DCLANG_ABI_COMPAT=15
12 // RUN: %clang_cc1 -triple powerpc64-ibm-aix7.3.0.0 %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof -DCLANG_ABI_COMPAT=15
13 // RUN: %clang_cc1 -triple powerpc64-ibm-aix7.3.0.0 %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof -fclang-abi-compat=15 -DCLANG_ABI_COMPAT=15
14 // RUN: %clang_cc1 -triple s390x-none-zos %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof
15 // RUN: %clang_cc1 -triple s390x-none-zos %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof -fclang-abi-compat=15 -DCLANG_ABI_COMPAT=15
17 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++98 -Wno-inaccessible-base -Wno-c++11-extensions -Wno-invalid-offsetof
18 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof
19 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple x86_64-apple-darwin %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -DCLANG_ABI_COMPAT=15 -Wno-invalid-offsetof
20 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple x86_64-scei-ps4 %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -DCLANG_ABI_COMPAT=6 -Wno-invalid-offsetof
21 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple x86_64-sie-ps5 %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -DCLANG_ABI_COMPAT=6 -Wno-invalid-offsetof
22 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -fclang-abi-compat=6 -DCLANG_ABI_COMPAT=6 -Wno-invalid-offsetof
23 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -fclang-abi-compat=14 -DCLANG_ABI_COMPAT=14 -Wno-invalid-offsetof
24 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -fclang-abi-compat=15 -DCLANG_ABI_COMPAT=15 -Wno-invalid-offsetof
25 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -fclang-abi-compat=16 -DCLANG_ABI_COMPAT=16 -Wno-invalid-offsetof
26 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple powerpc-ibm-aix7.3.0.0 %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -DCLANG_ABI_COMPAT=15 -Wno-invalid-offsetof
27 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple powerpc-ibm-aix7.3.0.0 %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -fclang-abi-compat=15 -DCLANG_ABI_COMPAT=15 -Wno-invalid-offsetof
28 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple powerpc64-ibm-aix7.3.0.0 %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -DCLANG_ABI_COMPAT=15 -Wno-invalid-offsetof
29 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple powerpc64-ibm-aix7.3.0.0 %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -fclang-abi-compat=15 -DCLANG_ABI_COMPAT=15 -Wno-invalid-offsetof
30 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple s390x-none-zos %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -Wno-invalid-offsetof
31 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple s390x-none-zos %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -fclang-abi-compat=15 -DCLANG_ABI_COMPAT=15 -Wno-invalid-offsetof
36 // expected-no-diagnostics
38 #if !defined(__MVS__) && !defined(_AIX)
40 #define SA(n, p) int a##n[(p) ? 1 : -1]
42 struct A {
43 int a;
44 char b;
47 SA(0, sizeof(A) == 8);
49 struct B : A {
50 char c;
53 SA(1, sizeof(B) == 12);
55 struct C {
56 // Make fields private so C won't be a POD type.
57 private:
58 int a;
59 char b;
62 SA(2, sizeof(C) == 8);
64 struct D : C {
65 char c;
68 SA(3, sizeof(D) == 8);
70 struct __attribute__((packed)) E {
71 char b;
72 int a;
75 SA(4, sizeof(E) == 5);
77 struct __attribute__((packed)) F : E {
78 char d;
81 SA(5, sizeof(F) == 6);
83 struct G { G(); };
84 struct H : G { };
86 SA(6, sizeof(H) == 1);
88 struct I {
89 char b;
90 int a;
91 } __attribute__((packed));
93 SA(6_1, sizeof(I) == 5);
95 // PR5580
96 namespace PR5580 {
98 class A { bool iv0 : 1; };
99 SA(7, sizeof(A) == 1);
101 class B : A { bool iv0 : 1; };
102 SA(8, sizeof(B) == 2);
104 struct C { bool iv0 : 1; };
105 SA(9, sizeof(C) == 1);
107 struct D : C { bool iv0 : 1; };
108 SA(10, sizeof(D) == 2);
112 namespace Test1 {
114 // Test that we don't assert on this hierarchy.
115 struct A { };
116 struct B : A { virtual void b(); };
117 class C : virtual A { int c; };
118 struct D : virtual B { };
119 struct E : C, virtual D { };
120 class F : virtual E { };
121 struct G : virtual E, F { };
123 SA(0, sizeof(G) == 24);
127 namespace Test2 {
129 // Test that this somewhat complex class structure is laid out correctly.
130 struct A { };
131 struct B : A { virtual void b(); };
132 struct C : virtual B { };
133 struct D : virtual A { };
134 struct E : virtual B, D { };
135 struct F : E, virtual C { };
136 struct G : virtual F, A { };
137 struct H { G g; };
139 SA(0, sizeof(H) == 24);
143 namespace PR16537 {
144 namespace test1 {
145 struct pod_in_11_only {
146 private:
147 long long x;
150 struct tail_padded_pod_in_11_only {
151 pod_in_11_only pod11;
152 char tail_padding;
155 struct might_use_tail_padding : public tail_padded_pod_in_11_only {
156 char may_go_into_tail_padding;
159 SA(0, sizeof(might_use_tail_padding) == 16);
162 namespace test2 {
163 struct pod_in_11_only {
164 private:
165 long long x;
168 struct tail_padded_pod_in_11_only {
169 pod_in_11_only pod11 __attribute__((aligned(16)));
172 struct might_use_tail_padding : public tail_padded_pod_in_11_only {
173 char may_go_into_tail_padding;
176 SA(0, sizeof(might_use_tail_padding) == 16);
179 namespace test3 {
180 struct pod_in_11_only {
181 private:
182 long long x;
185 struct tail_padded_pod_in_11_only {
186 pod_in_11_only pod11;
187 char tail_padding;
190 struct second_base {
191 char foo;
194 struct might_use_tail_padding : public tail_padded_pod_in_11_only, public second_base {
197 SA(0, sizeof(might_use_tail_padding) == 16);
200 namespace test4 {
201 struct pod_in_11_only {
202 private:
203 long long x;
206 struct tail_padded_pod_in_11_only {
207 pod_in_11_only pod11;
208 char tail_padding;
211 struct second_base {
212 char foo;
215 struct might_use_tail_padding : public tail_padded_pod_in_11_only, public second_base {
216 char may_go_into_tail_padding;
218 SA(0, sizeof(might_use_tail_padding) == 16);
221 namespace test5 {
222 struct pod_in_11_only {
223 private:
224 long long x;
227 struct pod_in_11_only2 {
228 private:
229 long long x;
232 struct tail_padded_pod_in_11_only {
233 pod_in_11_only pod11;
234 char tail_padding;
237 struct second_base {
238 pod_in_11_only2 two;
239 char foo;
242 struct might_use_tail_padding : public tail_padded_pod_in_11_only, public second_base {
243 char may_go_into_tail_padding;
245 SA(0, sizeof(might_use_tail_padding) == 32);
248 namespace test6 {
249 struct pod_in_11_only {
250 private:
251 long long x;
254 struct pod_in_11_only2 {
255 private:
256 long long x;
259 struct tail_padded_pod_in_11_only {
260 pod_in_11_only pod11;
261 char tail_padding;
264 struct second_base {
265 pod_in_11_only2 two;
266 char foo;
269 struct might_use_tail_padding : public tail_padded_pod_in_11_only, public second_base {
270 char may_go_into_tail_padding;
272 SA(0, sizeof(might_use_tail_padding) == 32);
275 namespace test7 {
276 struct pod_in_11_only {
277 private:
278 long long x;
281 struct tail_padded_pod_in_11_only {
282 pod_in_11_only pod11;
283 pod_in_11_only pod12;
284 char tail_padding;
287 struct might_use_tail_padding : public tail_padded_pod_in_11_only {
288 char may_go_into_tail_padding;
291 SA(0, sizeof(might_use_tail_padding) == 24);
294 namespace test8 {
295 struct pod_in_11_only {
296 private:
297 long long x;
300 struct tail_padded_pod_in_11_only {
301 pod_in_11_only pod11;
302 char tail_padding;
305 struct another_layer {
306 tail_padded_pod_in_11_only pod;
307 char padding;
310 struct might_use_tail_padding : public another_layer {
311 char may_go_into_tail_padding;
314 SA(0, sizeof(might_use_tail_padding) == 24);
317 namespace test9 {
318 struct pod_in_11_only {
319 private:
320 long long x;
323 struct tail_padded_pod_in_11_only {
324 pod_in_11_only pod11;
325 char tail_padding;
328 struct another_layer : tail_padded_pod_in_11_only {
331 struct might_use_tail_padding : public another_layer {
332 char may_go_into_tail_padding;
335 SA(0, sizeof(might_use_tail_padding) == 16);
338 namespace test10 {
339 struct pod_in_11_only {
340 private:
341 long long x;
344 struct A {
345 pod_in_11_only a;
346 char apad;
349 struct B {
350 char b;
353 struct C {
354 pod_in_11_only c;
355 char cpad;
358 struct D {
359 char d;
362 struct might_use_tail_padding : public A, public B, public C, public D {
365 SA(0, sizeof(might_use_tail_padding) == 32);
368 namespace test11 {
369 struct pod_in_11_only {
370 private:
371 long long x;
374 struct A {
375 pod_in_11_only a;
376 char apad;
379 struct B {
380 char b_pre;
381 pod_in_11_only b;
382 char bpad;
385 struct C {
386 char c_pre;
387 pod_in_11_only c;
388 char cpad;
391 struct D {
392 char d_pre;
393 pod_in_11_only d;
394 char dpad;
397 struct might_use_tail_padding : public A, public B, public C, public D {
398 char m;
401 SA(0, sizeof(might_use_tail_padding) == 88);
404 namespace test12 {
405 struct pod_in_11_only {
406 private:
407 long long x;
410 struct A {
411 pod_in_11_only a __attribute__((aligned(128)));
414 struct B {
415 char bpad;
418 struct C {
419 char cpad;
422 struct D {
423 char dpad;
426 struct might_use_tail_padding : public A, public B, public C, public D {
427 char m;
429 SA(0, sizeof(might_use_tail_padding) == 128);
432 namespace test13 {
433 struct pod_in_11_only {
434 private:
435 long long x;
438 struct A {
439 pod_in_11_only a;
440 char apad;
443 struct B {
446 struct C {
447 char c_pre;
448 pod_in_11_only c;
449 char cpad;
452 struct D {
455 struct might_use_tail_padding : public A, public B, public C, public D {
456 char m;
458 SA(0, sizeof(might_use_tail_padding) == 40);
461 namespace test14 {
462 struct pod_in_11_only {
463 private:
464 long long x;
467 struct A {
468 pod_in_11_only a;
469 char apad;
472 struct might_use_tail_padding : public A {
473 struct {
474 int : 0;
475 } x;
477 SA(0, sizeof(might_use_tail_padding) == 16);
480 namespace test15 {
481 struct pod_in_11_only {
482 private:
483 long long x;
486 struct A {
487 pod_in_11_only a;
488 char apad;
491 struct might_use_tail_padding : public A {
492 struct {
493 char a:1;
494 char b:2;
495 char c:2;
496 char d:2;
497 char e:1;
498 } x;
500 SA(0, sizeof(might_use_tail_padding) == 16);
503 namespace test16 {
504 struct pod_in_11_only {
505 private:
506 long long x;
509 struct A {
510 pod_in_11_only a;
511 char apad;
514 struct B {
515 char bpod;
516 pod_in_11_only b;
517 char bpad;
520 struct C : public A, public B {
523 struct D : public C {
526 struct might_use_tail_padding : public D {
527 char m;
529 SA(0, sizeof(might_use_tail_padding) == 40);
532 namespace test17 {
533 struct pod_in_11_only {
534 private:
535 long long x;
538 struct A {
539 pod_in_11_only a __attribute__((aligned(512)));
542 struct B {
543 char bpad;
544 pod_in_11_only foo;
545 char btail;
548 struct C {
549 char cpad;
552 struct D {
553 char dpad;
556 struct might_use_tail_padding : public A, public B, public C, public D {
557 char a;
559 SA(0, sizeof(might_use_tail_padding) == 512);
562 namespace test18 {
563 struct pod_in_11_only {
564 private:
565 long long x;
568 struct A {
569 pod_in_11_only a;
570 char apad;
573 struct B {
574 char bpod;
575 pod_in_11_only b;
576 char bpad;
579 struct A1 {
580 pod_in_11_only a;
581 char apad;
584 struct B1 {
585 char bpod;
586 pod_in_11_only b;
587 char bpad;
590 struct C : public A, public B {
593 struct D : public A1, public B1 {
596 struct E : public D, public C {
599 struct F : public E {
602 struct might_use_tail_padding : public F {
603 char m;
605 SA(0, sizeof(might_use_tail_padding) == 80);
607 } // namespace PR16537
609 namespace PR37275 {
610 struct X { char c; };
612 struct A { int n; };
613 _Static_assert(_Alignof(A) == _Alignof(int), "");
615 // __attribute__((packed)) does not apply to base classes.
616 struct __attribute__((packed)) B : X, A {};
617 #if defined(CLANG_ABI_COMPAT) && CLANG_ABI_COMPAT <= 6
618 _Static_assert(_Alignof(B) == 1, "");
619 _Static_assert(__builtin_offsetof(B, n) == 1, "");
620 #else
621 _Static_assert(_Alignof(B) == _Alignof(int), "");
622 _Static_assert(__builtin_offsetof(B, n) == 4, "");
623 #endif
625 // #pragma pack does, though.
626 #pragma pack(push, 2)
627 struct C : X, A {};
628 _Static_assert(_Alignof(C) == 2, "");
629 _Static_assert(__builtin_offsetof(C, n) == 2, "");
631 struct __attribute__((packed)) D : X, A {};
632 #if defined(CLANG_ABI_COMPAT) && CLANG_ABI_COMPAT <= 6
633 _Static_assert(_Alignof(D) == 1, "");
634 _Static_assert(__builtin_offsetof(D, n) == 1, "");
635 #else
636 _Static_assert(_Alignof(D) == 2, "");
637 _Static_assert(__builtin_offsetof(D, n) == 2, "");
638 #endif
639 #pragma pack(pop)
642 #endif // !defined(__MVS__) && !defined(__AIX__)
644 namespace non_pod {
645 struct t1 {
646 protected:
647 int a;
649 // GCC prints warning: ignoring packed attribute because of unpacked non-POD field 't1 t2::v1'`
650 struct t2 {
651 char c1;
652 short s1;
653 char c2;
654 t1 v1;
655 } __attribute__((packed));
656 #if defined(CLANG_ABI_COMPAT) && CLANG_ABI_COMPAT <= 15
657 _Static_assert(_Alignof(t1) == 4, "");
658 _Static_assert(_Alignof(t2) == 1, "");
659 #else
660 _Static_assert(_Alignof(t1) == 4, "");
661 _Static_assert(_Alignof(t2) == 4, "");
662 #endif
663 _Static_assert(sizeof(t2) == 8, ""); // it's still packing the rest of the struct
664 } // namespace non_pod
666 namespace non_pod_packed {
667 struct t1 {
668 protected:
669 int a;
670 } __attribute__((packed));
671 struct t2 {
672 t1 v1;
673 } __attribute__((packed));
674 _Static_assert(_Alignof(t1) == 1, "");
675 _Static_assert(_Alignof(t2) == 1, "");
676 } // namespace non_pod_packed
678 namespace non_pod_packed_packed {
679 struct B {
680 int b;
682 struct FromB : B {
683 } __attribute__((packed));
684 struct C {
685 char a[3];
686 FromB b;
687 } __attribute__((packed));
688 _Static_assert(__builtin_offsetof(C, b) == 3, "");
691 namespace cxx11_pod {
692 struct t1 {
693 t1() = default;
694 t1(const t1&) = delete;
695 ~t1() = delete;
696 t1(t1&&) = default;
697 int a;
698 char c;
700 struct t2 {
701 t1 v1;
702 } __attribute__((packed));
703 #if (defined(CLANG_ABI_COMPAT) && CLANG_ABI_COMPAT <= 15) || !defined(__MVS__)
704 _Static_assert(_Alignof(t2) == 1, "");
705 #else
706 _Static_assert(_Alignof(t2) == 4, "");
707 #endif
708 struct t3 : t1 {
709 char c;
711 #if (defined(CLANG_ABI_COMPAT) && CLANG_ABI_COMPAT <= 15) || defined(__MVS__)
712 _Static_assert(sizeof(t3) == 8, "");
713 #else
714 _Static_assert(sizeof(t3) == 12, "");
715 #endif