xref: /aosp_15_r20/external/clang/test/CodeGen/override-layout.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -w -fdump-record-layouts-simple %s > %t.layouts
2*67e74705SXin Li // RUN: %clang_cc1 -w -DPACKED= -DALIGNED16= -fdump-record-layouts-simple -foverride-record-layout=%t.layouts %s > %t.after
3*67e74705SXin Li // RUN: diff %t.layouts %t.after
4*67e74705SXin Li // RUN: FileCheck %s < %t.after
5*67e74705SXin Li 
6*67e74705SXin Li // If not explicitly disabled, set PACKED to the packed attribute.
7*67e74705SXin Li #ifndef PACKED
8*67e74705SXin Li #  define PACKED __attribute__((packed))
9*67e74705SXin Li #endif
10*67e74705SXin Li 
11*67e74705SXin Li // If not explicitly disabled, set ALIGNED16 to 16-byte alignment.
12*67e74705SXin Li #ifndef ALIGNED16
13*67e74705SXin Li #  define ALIGNED16 __attribute__((aligned(16)))
14*67e74705SXin Li #endif
15*67e74705SXin Li 
16*67e74705SXin Li // CHECK: Type: struct X0
17*67e74705SXin Li struct X0 {
18*67e74705SXin Li   int x[6] PACKED;
19*67e74705SXin Li };
20*67e74705SXin Li 
use_X0()21*67e74705SXin Li void use_X0() { struct X0 x0; x0.x[5] = sizeof(struct X0); };
22*67e74705SXin Li 
23*67e74705SXin Li // CHECK: Type: struct X1
24*67e74705SXin Li struct X1 {
25*67e74705SXin Li   char x[13];
26*67e74705SXin Li   struct X0 y;
27*67e74705SXin Li } PACKED;
28*67e74705SXin Li 
use_X1()29*67e74705SXin Li void use_X1() { struct X1 x1; x1.x[5] = sizeof(struct X1); };
30*67e74705SXin Li 
31*67e74705SXin Li // CHECK: Type: struct X2
32*67e74705SXin Li struct PACKED X2 {
33*67e74705SXin Li   short x;
34*67e74705SXin Li   int y;
35*67e74705SXin Li };
36*67e74705SXin Li 
use_X2()37*67e74705SXin Li void use_X2() { struct X2 x2; x2.y = sizeof(struct X2); };
38*67e74705SXin Li 
39*67e74705SXin Li // CHECK: Type: struct X3
40*67e74705SXin Li struct X3 {
41*67e74705SXin Li   short x PACKED;
42*67e74705SXin Li   int y;
43*67e74705SXin Li };
44*67e74705SXin Li 
use_X3()45*67e74705SXin Li void use_X3() { struct X3 x3; x3.y = sizeof(struct X3); };
46*67e74705SXin Li 
47*67e74705SXin Li #pragma pack(push,2)
48*67e74705SXin Li // CHECK: Type: struct X4
49*67e74705SXin Li struct X4 {
50*67e74705SXin Li   int x;
51*67e74705SXin Li   int y;
52*67e74705SXin Li };
53*67e74705SXin Li #pragma pack(pop)
54*67e74705SXin Li 
use_X4()55*67e74705SXin Li void use_X4() { struct X4 x4; x4.y = sizeof(struct X4); };
56*67e74705SXin Li 
57*67e74705SXin Li // CHECK: Type: struct X5
58*67e74705SXin Li struct PACKED X5 { double a[19];  signed char b; };
59*67e74705SXin Li 
use_X5()60*67e74705SXin Li void use_X5() { struct X5 x5; x5.b = sizeof(struct X5); };
61*67e74705SXin Li 
62*67e74705SXin Li // CHECK: Type: struct X6
63*67e74705SXin Li struct PACKED X6 { long double a; char b; };
64*67e74705SXin Li 
use_X6()65*67e74705SXin Li void use_X6() { struct X6 x6; x6.b = sizeof(struct X6); };
66*67e74705SXin Li 
67*67e74705SXin Li // CHECK: Type: struct X7
68*67e74705SXin Li struct X7 {
69*67e74705SXin Li         unsigned x;
70*67e74705SXin Li         unsigned char y;
71*67e74705SXin Li } PACKED;
72*67e74705SXin Li 
use_X7()73*67e74705SXin Li void use_X7() { struct X7 x7; x7.y = x7.x = sizeof(struct X7); }
74*67e74705SXin Li 
75*67e74705SXin Li // CHECK: Type: union X8
76*67e74705SXin Li union X8 {
77*67e74705SXin Li   struct X7 x;
78*67e74705SXin Li   unsigned y;
79*67e74705SXin Li } PACKED;
80*67e74705SXin Li 
81*67e74705SXin Li // CHECK: Type: struct X9
82*67e74705SXin Li struct X9 {
83*67e74705SXin Li   unsigned int x[2] PACKED;
84*67e74705SXin Li   unsigned int y;
85*67e74705SXin Li   unsigned int z PACKED;
86*67e74705SXin Li };
87*67e74705SXin Li 
88*67e74705SXin Li // CHECK: Type: struct X10
89*67e74705SXin Li struct X10 {
90*67e74705SXin Li   unsigned int x[2] PACKED;
91*67e74705SXin Li   unsigned int y PACKED;
92*67e74705SXin Li   unsigned int z PACKED;
93*67e74705SXin Li };
94*67e74705SXin Li 
95*67e74705SXin Li // CHECK: Type: struct X11
96*67e74705SXin Li struct PACKED X11 {
97*67e74705SXin Li   unsigned int x[2];
98*67e74705SXin Li   unsigned int y;
99*67e74705SXin Li   unsigned int z;
100*67e74705SXin Li };
101*67e74705SXin Li 
102*67e74705SXin Li // CHECK: Type: struct X12
103*67e74705SXin Li struct PACKED X12 {
104*67e74705SXin Li   int x : 24;
105*67e74705SXin Li };
106*67e74705SXin Li 
107*67e74705SXin Li // CHECK: Type: struct X13
108*67e74705SXin Li struct PACKED X13 {
109*67e74705SXin Li   signed x : 10;
110*67e74705SXin Li   signed y : 10;
111*67e74705SXin Li };
112*67e74705SXin Li 
113*67e74705SXin Li // CHECK: Type: union X14
114*67e74705SXin Li union PACKED X14 {
115*67e74705SXin Li   unsigned long long x : 3;
116*67e74705SXin Li };
117*67e74705SXin Li 
118*67e74705SXin Li // CHECK: Type: struct X15
119*67e74705SXin Li struct X15 {
120*67e74705SXin Li   unsigned x : 16;
121*67e74705SXin Li   unsigned y : 28 PACKED;
122*67e74705SXin Li };
123*67e74705SXin Li 
124*67e74705SXin Li // CHECK: Type: struct X16
125*67e74705SXin Li struct ALIGNED16 X16 {
126*67e74705SXin Li   int a, b, c;
127*67e74705SXin Li   int x : 5;
128*67e74705SXin Li   int y : 29;
129*67e74705SXin Li };
130*67e74705SXin Li 
use_structs()131*67e74705SXin Li void use_structs() {
132*67e74705SXin Li   union X8 x8;
133*67e74705SXin Li   typedef int X8array[sizeof(union X8)];
134*67e74705SXin Li   x8.y = sizeof(union X8);
135*67e74705SXin Li   x8.x.x = x8.y;
136*67e74705SXin Li 
137*67e74705SXin Li   struct X9 x9;
138*67e74705SXin Li   typedef int X9array[sizeof(struct X9)];
139*67e74705SXin Li   x9.y = sizeof(struct X9);
140*67e74705SXin Li 
141*67e74705SXin Li   struct X10 x10;
142*67e74705SXin Li   typedef int X10array[sizeof(struct X10)];
143*67e74705SXin Li   x10.y = sizeof(struct X10);
144*67e74705SXin Li 
145*67e74705SXin Li   struct X11 x11;
146*67e74705SXin Li   typedef int X11array[sizeof(struct X11)];
147*67e74705SXin Li   x11.y = sizeof(struct X11);
148*67e74705SXin Li 
149*67e74705SXin Li   struct X12 x12;
150*67e74705SXin Li   x12.x = sizeof(struct X12);
151*67e74705SXin Li 
152*67e74705SXin Li   struct X13 x13;
153*67e74705SXin Li   x13.x = sizeof(struct X13);
154*67e74705SXin Li 
155*67e74705SXin Li   union X14 x14;
156*67e74705SXin Li   x14.x = sizeof(union X14);
157*67e74705SXin Li 
158*67e74705SXin Li   struct X15 x15;
159*67e74705SXin Li   x15.x = sizeof(struct X15);
160*67e74705SXin Li 
161*67e74705SXin Li   struct X16 x16;
162*67e74705SXin Li   x16.x = sizeof(struct X16);
163*67e74705SXin Li }
164