[lld][WebAssembly] Reinstate mistakenly disabled test. NFC
[llvm-project.git] / clang / test / SemaCXX / class-layout.cpp
blob5403bd6e6a6f65b4bf687ad5bf6e167c338cc59b
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-unknown-unknown %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base -fclang-abi-compat=6 -DCLANG_ABI_COMPAT=6
4 // expected-no-diagnostics
6 #define SA(n, p) int a##n[(p) ? 1 : -1]
8 struct A {
9 int a;
10 char b;
13 SA(0, sizeof(A) == 8);
15 struct B : A {
16 char c;
19 SA(1, sizeof(B) == 12);
21 struct C {
22 // Make fields private so C won't be a POD type.
23 private:
24 int a;
25 char b;
28 SA(2, sizeof(C) == 8);
30 struct D : C {
31 char c;
34 SA(3, sizeof(D) == 8);
36 struct __attribute__((packed)) E {
37 char b;
38 int a;
41 SA(4, sizeof(E) == 5);
43 struct __attribute__((packed)) F : E {
44 char d;
47 SA(5, sizeof(F) == 6);
49 struct G { G(); };
50 struct H : G { };
52 SA(6, sizeof(H) == 1);
54 struct I {
55 char b;
56 int a;
57 } __attribute__((packed));
59 SA(6_1, sizeof(I) == 5);
61 // PR5580
62 namespace PR5580 {
64 class A { bool iv0 : 1; };
65 SA(7, sizeof(A) == 1);
67 class B : A { bool iv0 : 1; };
68 SA(8, sizeof(B) == 2);
70 struct C { bool iv0 : 1; };
71 SA(9, sizeof(C) == 1);
73 struct D : C { bool iv0 : 1; };
74 SA(10, sizeof(D) == 2);
78 namespace Test1 {
80 // Test that we don't assert on this hierarchy.
81 struct A { };
82 struct B : A { virtual void b(); };
83 class C : virtual A { int c; };
84 struct D : virtual B { };
85 struct E : C, virtual D { };
86 class F : virtual E { };
87 struct G : virtual E, F { };
89 SA(0, sizeof(G) == 24);
93 namespace Test2 {
95 // Test that this somewhat complex class structure is laid out correctly.
96 struct A { };
97 struct B : A { virtual void b(); };
98 struct C : virtual B { };
99 struct D : virtual A { };
100 struct E : virtual B, D { };
101 struct F : E, virtual C { };
102 struct G : virtual F, A { };
103 struct H { G g; };
105 SA(0, sizeof(H) == 24);
109 namespace PR16537 {
110 namespace test1 {
111 struct pod_in_11_only {
112 private:
113 long long x;
116 struct tail_padded_pod_in_11_only {
117 pod_in_11_only pod11;
118 char tail_padding;
121 struct might_use_tail_padding : public tail_padded_pod_in_11_only {
122 char may_go_into_tail_padding;
125 SA(0, sizeof(might_use_tail_padding) == 16);
128 namespace test2 {
129 struct pod_in_11_only {
130 private:
131 long long x;
134 struct tail_padded_pod_in_11_only {
135 pod_in_11_only pod11 __attribute__((aligned(16)));
138 struct might_use_tail_padding : public tail_padded_pod_in_11_only {
139 char may_go_into_tail_padding;
142 SA(0, sizeof(might_use_tail_padding) == 16);
145 namespace test3 {
146 struct pod_in_11_only {
147 private:
148 long long x;
151 struct tail_padded_pod_in_11_only {
152 pod_in_11_only pod11;
153 char tail_padding;
156 struct second_base {
157 char foo;
160 struct might_use_tail_padding : public tail_padded_pod_in_11_only, public second_base {
163 SA(0, sizeof(might_use_tail_padding) == 16);
166 namespace test4 {
167 struct pod_in_11_only {
168 private:
169 long long x;
172 struct tail_padded_pod_in_11_only {
173 pod_in_11_only pod11;
174 char tail_padding;
177 struct second_base {
178 char foo;
181 struct might_use_tail_padding : public tail_padded_pod_in_11_only, public second_base {
182 char may_go_into_tail_padding;
184 SA(0, sizeof(might_use_tail_padding) == 16);
187 namespace test5 {
188 struct pod_in_11_only {
189 private:
190 long long x;
193 struct pod_in_11_only2 {
194 private:
195 long long x;
198 struct tail_padded_pod_in_11_only {
199 pod_in_11_only pod11;
200 char tail_padding;
203 struct second_base {
204 pod_in_11_only2 two;
205 char foo;
208 struct might_use_tail_padding : public tail_padded_pod_in_11_only, public second_base {
209 char may_go_into_tail_padding;
211 SA(0, sizeof(might_use_tail_padding) == 32);
214 namespace test6 {
215 struct pod_in_11_only {
216 private:
217 long long x;
220 struct pod_in_11_only2 {
221 private:
222 long long x;
225 struct tail_padded_pod_in_11_only {
226 pod_in_11_only pod11;
227 char tail_padding;
230 struct second_base {
231 pod_in_11_only2 two;
232 char foo;
235 struct might_use_tail_padding : public tail_padded_pod_in_11_only, public second_base {
236 char may_go_into_tail_padding;
238 SA(0, sizeof(might_use_tail_padding) == 32);
241 namespace test7 {
242 struct pod_in_11_only {
243 private:
244 long long x;
247 struct tail_padded_pod_in_11_only {
248 pod_in_11_only pod11;
249 pod_in_11_only pod12;
250 char tail_padding;
253 struct might_use_tail_padding : public tail_padded_pod_in_11_only {
254 char may_go_into_tail_padding;
257 SA(0, sizeof(might_use_tail_padding) == 24);
260 namespace test8 {
261 struct pod_in_11_only {
262 private:
263 long long x;
266 struct tail_padded_pod_in_11_only {
267 pod_in_11_only pod11;
268 char tail_padding;
271 struct another_layer {
272 tail_padded_pod_in_11_only pod;
273 char padding;
276 struct might_use_tail_padding : public another_layer {
277 char may_go_into_tail_padding;
280 SA(0, sizeof(might_use_tail_padding) == 24);
283 namespace test9 {
284 struct pod_in_11_only {
285 private:
286 long long x;
289 struct tail_padded_pod_in_11_only {
290 pod_in_11_only pod11;
291 char tail_padding;
294 struct another_layer : tail_padded_pod_in_11_only {
297 struct might_use_tail_padding : public another_layer {
298 char may_go_into_tail_padding;
301 SA(0, sizeof(might_use_tail_padding) == 16);
304 namespace test10 {
305 struct pod_in_11_only {
306 private:
307 long long x;
310 struct A {
311 pod_in_11_only a;
312 char apad;
315 struct B {
316 char b;
319 struct C {
320 pod_in_11_only c;
321 char cpad;
324 struct D {
325 char d;
328 struct might_use_tail_padding : public A, public B, public C, public D {
331 SA(0, sizeof(might_use_tail_padding) == 32);
334 namespace test11 {
335 struct pod_in_11_only {
336 private:
337 long long x;
340 struct A {
341 pod_in_11_only a;
342 char apad;
345 struct B {
346 char b_pre;
347 pod_in_11_only b;
348 char bpad;
351 struct C {
352 char c_pre;
353 pod_in_11_only c;
354 char cpad;
357 struct D {
358 char d_pre;
359 pod_in_11_only d;
360 char dpad;
363 struct might_use_tail_padding : public A, public B, public C, public D {
364 char m;
367 SA(0, sizeof(might_use_tail_padding) == 88);
370 namespace test12 {
371 struct pod_in_11_only {
372 private:
373 long long x;
376 struct A {
377 pod_in_11_only a __attribute__((aligned(128)));
380 struct B {
381 char bpad;
384 struct C {
385 char cpad;
388 struct D {
389 char dpad;
392 struct might_use_tail_padding : public A, public B, public C, public D {
393 char m;
395 SA(0, sizeof(might_use_tail_padding) == 128);
398 namespace test13 {
399 struct pod_in_11_only {
400 private:
401 long long x;
404 struct A {
405 pod_in_11_only a;
406 char apad;
409 struct B {
412 struct C {
413 char c_pre;
414 pod_in_11_only c;
415 char cpad;
418 struct D {
421 struct might_use_tail_padding : public A, public B, public C, public D {
422 char m;
424 SA(0, sizeof(might_use_tail_padding) == 40);
427 namespace test14 {
428 struct pod_in_11_only {
429 private:
430 long long x;
433 struct A {
434 pod_in_11_only a;
435 char apad;
438 struct might_use_tail_padding : public A {
439 struct {
440 int : 0;
441 } x;
443 SA(0, sizeof(might_use_tail_padding) == 16);
446 namespace test15 {
447 struct pod_in_11_only {
448 private:
449 long long x;
452 struct A {
453 pod_in_11_only a;
454 char apad;
457 struct might_use_tail_padding : public A {
458 struct {
459 char a:1;
460 char b:2;
461 char c:2;
462 char d:2;
463 char e:1;
464 } x;
466 SA(0, sizeof(might_use_tail_padding) == 16);
469 namespace test16 {
470 struct pod_in_11_only {
471 private:
472 long long x;
475 struct A {
476 pod_in_11_only a;
477 char apad;
480 struct B {
481 char bpod;
482 pod_in_11_only b;
483 char bpad;
486 struct C : public A, public B {
489 struct D : public C {
492 struct might_use_tail_padding : public D {
493 char m;
495 SA(0, sizeof(might_use_tail_padding) == 40);
498 namespace test17 {
499 struct pod_in_11_only {
500 private:
501 long long x;
504 struct A {
505 pod_in_11_only a __attribute__((aligned(512)));
508 struct B {
509 char bpad;
510 pod_in_11_only foo;
511 char btail;
514 struct C {
515 char cpad;
518 struct D {
519 char dpad;
522 struct might_use_tail_padding : public A, public B, public C, public D {
523 char a;
525 SA(0, sizeof(might_use_tail_padding) == 512);
528 namespace test18 {
529 struct pod_in_11_only {
530 private:
531 long long x;
534 struct A {
535 pod_in_11_only a;
536 char apad;
539 struct B {
540 char bpod;
541 pod_in_11_only b;
542 char bpad;
545 struct A1 {
546 pod_in_11_only a;
547 char apad;
550 struct B1 {
551 char bpod;
552 pod_in_11_only b;
553 char bpad;
556 struct C : public A, public B {
559 struct D : public A1, public B1 {
562 struct E : public D, public C {
565 struct F : public E {
568 struct might_use_tail_padding : public F {
569 char m;
571 SA(0, sizeof(might_use_tail_padding) == 80);
573 } // namespace PR16537
575 namespace PR37275 {
576 struct X { char c; };
578 struct A { int n; };
579 _Static_assert(_Alignof(A) == _Alignof(int), "");
581 // __attribute__((packed)) does not apply to base classes.
582 struct __attribute__((packed)) B : X, A {};
583 #if defined(CLANG_ABI_COMPAT) && CLANG_ABI_COMPAT <= 6
584 _Static_assert(_Alignof(B) == 1, "");
585 _Static_assert(__builtin_offsetof(B, n) == 1, "");
586 #else
587 _Static_assert(_Alignof(B) == _Alignof(int), "");
588 _Static_assert(__builtin_offsetof(B, n) == 4, "");
589 #endif
591 // #pragma pack does, though.
592 #pragma pack(push, 2)
593 struct C : X, A {};
594 _Static_assert(_Alignof(C) == 2, "");
595 _Static_assert(__builtin_offsetof(C, n) == 2, "");
597 struct __attribute__((packed)) D : X, A {};
598 #if defined(CLANG_ABI_COMPAT) && CLANG_ABI_COMPAT <= 6
599 _Static_assert(_Alignof(D) == 1, "");
600 _Static_assert(__builtin_offsetof(D, n) == 1, "");
601 #else
602 _Static_assert(_Alignof(D) == 2, "");
603 _Static_assert(__builtin_offsetof(D, n) == 2, "");
604 #endif
605 #pragma pack(pop)