1 // RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++98 -Wno-inaccessible-base
2 // RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base
3 // RUN: %clang_cc1 -triple x86_64-apple-darwin %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -DCLANG_ABI_COMPAT=14
4 // RUN: %clang_cc1 -triple x86_64-scei-ps4 %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -DCLANG_ABI_COMPAT=6
5 // RUN: %clang_cc1 -triple x86_64-sie-ps5 %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -DCLANG_ABI_COMPAT=6
6 // RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -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 -fclang-abi-compat=14 -DCLANG_ABI_COMPAT=14
8 // expected-no-diagnostics
10 #define SA(n, p) int a##n[(p) ? 1 : -1]
17 SA(0, sizeof(A
) == 8);
23 SA(1, sizeof(B
) == 12);
26 // Make fields private so C won't be a POD type.
32 SA(2, sizeof(C
) == 8);
38 SA(3, sizeof(D
) == 8);
40 struct __attribute__((packed
)) E
{
45 SA(4, sizeof(E
) == 5);
47 struct __attribute__((packed
)) F
: E
{
51 SA(5, sizeof(F
) == 6);
56 SA(6, sizeof(H
) == 1);
61 } __attribute__((packed
));
63 SA(6_1
, sizeof(I
) == 5);
68 class A
{ bool iv0
: 1; };
69 SA(7, sizeof(A
) == 1);
71 class B
: A
{ bool iv0
: 1; };
72 SA(8, sizeof(B
) == 2);
74 struct C
{ bool iv0
: 1; };
75 SA(9, sizeof(C
) == 1);
77 struct D
: C
{ bool iv0
: 1; };
78 SA(10, sizeof(D
) == 2);
84 // Test that we don't assert on this hierarchy.
86 struct B
: A
{ virtual void b(); };
87 class C
: virtual A
{ int c
; };
88 struct D
: virtual B
{ };
89 struct E
: C
, virtual D
{ };
90 class F
: virtual E
{ };
91 struct G
: virtual E
, F
{ };
93 SA(0, sizeof(G
) == 24);
99 // Test that this somewhat complex class structure is laid out correctly.
101 struct B
: A
{ virtual void b(); };
102 struct C
: virtual B
{ };
103 struct D
: virtual A
{ };
104 struct E
: virtual B
, D
{ };
105 struct F
: E
, virtual C
{ };
106 struct G
: virtual F
, A
{ };
109 SA(0, sizeof(H
) == 24);
115 struct pod_in_11_only
{
120 struct tail_padded_pod_in_11_only
{
121 pod_in_11_only pod11
;
125 struct might_use_tail_padding
: public tail_padded_pod_in_11_only
{
126 char may_go_into_tail_padding
;
129 SA(0, sizeof(might_use_tail_padding
) == 16);
133 struct pod_in_11_only
{
138 struct tail_padded_pod_in_11_only
{
139 pod_in_11_only pod11
__attribute__((aligned(16)));
142 struct might_use_tail_padding
: public tail_padded_pod_in_11_only
{
143 char may_go_into_tail_padding
;
146 SA(0, sizeof(might_use_tail_padding
) == 16);
150 struct pod_in_11_only
{
155 struct tail_padded_pod_in_11_only
{
156 pod_in_11_only pod11
;
164 struct might_use_tail_padding
: public tail_padded_pod_in_11_only
, public second_base
{
167 SA(0, sizeof(might_use_tail_padding
) == 16);
171 struct pod_in_11_only
{
176 struct tail_padded_pod_in_11_only
{
177 pod_in_11_only pod11
;
185 struct might_use_tail_padding
: public tail_padded_pod_in_11_only
, public second_base
{
186 char may_go_into_tail_padding
;
188 SA(0, sizeof(might_use_tail_padding
) == 16);
192 struct pod_in_11_only
{
197 struct pod_in_11_only2
{
202 struct tail_padded_pod_in_11_only
{
203 pod_in_11_only pod11
;
212 struct might_use_tail_padding
: public tail_padded_pod_in_11_only
, public second_base
{
213 char may_go_into_tail_padding
;
215 SA(0, sizeof(might_use_tail_padding
) == 32);
219 struct pod_in_11_only
{
224 struct pod_in_11_only2
{
229 struct tail_padded_pod_in_11_only
{
230 pod_in_11_only pod11
;
239 struct might_use_tail_padding
: public tail_padded_pod_in_11_only
, public second_base
{
240 char may_go_into_tail_padding
;
242 SA(0, sizeof(might_use_tail_padding
) == 32);
246 struct pod_in_11_only
{
251 struct tail_padded_pod_in_11_only
{
252 pod_in_11_only pod11
;
253 pod_in_11_only pod12
;
257 struct might_use_tail_padding
: public tail_padded_pod_in_11_only
{
258 char may_go_into_tail_padding
;
261 SA(0, sizeof(might_use_tail_padding
) == 24);
265 struct pod_in_11_only
{
270 struct tail_padded_pod_in_11_only
{
271 pod_in_11_only pod11
;
275 struct another_layer
{
276 tail_padded_pod_in_11_only pod
;
280 struct might_use_tail_padding
: public another_layer
{
281 char may_go_into_tail_padding
;
284 SA(0, sizeof(might_use_tail_padding
) == 24);
288 struct pod_in_11_only
{
293 struct tail_padded_pod_in_11_only
{
294 pod_in_11_only pod11
;
298 struct another_layer
: tail_padded_pod_in_11_only
{
301 struct might_use_tail_padding
: public another_layer
{
302 char may_go_into_tail_padding
;
305 SA(0, sizeof(might_use_tail_padding
) == 16);
309 struct pod_in_11_only
{
332 struct might_use_tail_padding
: public A
, public B
, public C
, public D
{
335 SA(0, sizeof(might_use_tail_padding
) == 32);
339 struct pod_in_11_only
{
367 struct might_use_tail_padding
: public A
, public B
, public C
, public D
{
371 SA(0, sizeof(might_use_tail_padding
) == 88);
375 struct pod_in_11_only
{
381 pod_in_11_only a
__attribute__((aligned(128)));
396 struct might_use_tail_padding
: public A
, public B
, public C
, public D
{
399 SA(0, sizeof(might_use_tail_padding
) == 128);
403 struct pod_in_11_only
{
425 struct might_use_tail_padding
: public A
, public B
, public C
, public D
{
428 SA(0, sizeof(might_use_tail_padding
) == 40);
432 struct pod_in_11_only
{
442 struct might_use_tail_padding
: public A
{
447 SA(0, sizeof(might_use_tail_padding
) == 16);
451 struct pod_in_11_only
{
461 struct might_use_tail_padding
: public A
{
470 SA(0, sizeof(might_use_tail_padding
) == 16);
474 struct pod_in_11_only
{
490 struct C
: public A
, public B
{
493 struct D
: public C
{
496 struct might_use_tail_padding
: public D
{
499 SA(0, sizeof(might_use_tail_padding
) == 40);
503 struct pod_in_11_only
{
509 pod_in_11_only a
__attribute__((aligned(512)));
526 struct might_use_tail_padding
: public A
, public B
, public C
, public D
{
529 SA(0, sizeof(might_use_tail_padding
) == 512);
533 struct pod_in_11_only
{
560 struct C
: public A
, public B
{
563 struct D
: public A1
, public B1
{
566 struct E
: public D
, public C
{
569 struct F
: public E
{
572 struct might_use_tail_padding
: public F
{
575 SA(0, sizeof(might_use_tail_padding
) == 80);
577 } // namespace PR16537
580 struct X
{ char c
; };
583 _Static_assert(_Alignof(A
) == _Alignof(int), "");
585 // __attribute__((packed)) does not apply to base classes.
586 struct __attribute__((packed
)) B
: X
, A
{};
587 #if defined(CLANG_ABI_COMPAT) && CLANG_ABI_COMPAT <= 6
588 _Static_assert(_Alignof(B
) == 1, "");
589 _Static_assert(__builtin_offsetof(B
, n
) == 1, "");
591 _Static_assert(_Alignof(B
) == _Alignof(int), "");
592 _Static_assert(__builtin_offsetof(B
, n
) == 4, "");
595 // #pragma pack does, though.
596 #pragma pack(push, 2)
598 _Static_assert(_Alignof(C
) == 2, "");
599 _Static_assert(__builtin_offsetof(C
, n
) == 2, "");
601 struct __attribute__((packed
)) D
: X
, A
{};
602 #if defined(CLANG_ABI_COMPAT) && CLANG_ABI_COMPAT <= 6
603 _Static_assert(_Alignof(D
) == 1, "");
604 _Static_assert(__builtin_offsetof(D
, n
) == 1, "");
606 _Static_assert(_Alignof(D
) == 2, "");
607 _Static_assert(__builtin_offsetof(D
, n
) == 2, "");
617 // GCC prints warning: ignoring packed attribute because of unpacked non-POD field 't1 t2::v1'`
623 } __attribute__((packed
));
624 #if defined(CLANG_ABI_COMPAT) && CLANG_ABI_COMPAT <= 14
625 _Static_assert(_Alignof(t1
) == 4, "");
626 _Static_assert(_Alignof(t2
) == 1, "");
628 _Static_assert(_Alignof(t1
) == 4, "");
629 _Static_assert(_Alignof(t2
) == 4, "");
631 _Static_assert(sizeof(t2
) == 8, ""); // it's still packing the rest of the struct
632 } // namespace non_pod
634 namespace non_pod_packed
{
638 } __attribute__((packed
));
641 } __attribute__((packed
));
642 _Static_assert(_Alignof(t1
) == 1, "");
643 _Static_assert(_Alignof(t2
) == 1, "");
644 } // namespace non_pod_packed