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