Clang Project

clang_source_code/test/Sema/bitfield-layout.c
1// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=i686-apple-darwin9
2// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=arm-linux-gnueabihf
3// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=aarch64-linux-gnu
4// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=x86_64-pc-linux-gnu
5// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=x86_64-scei-ps4
6// expected-no-diagnostics
7#include <stddef.h>
8
9#define CHECK_SIZE(kind, name, size) \
10  extern int name##_1[sizeof(kind name) == size ? 1 : -1];
11#define CHECK_ALIGN(kind, name, size) \
12  extern int name##_2[__alignof(kind name) == size ? 1 : -1];
13#define CHECK_OFFSET(kind, name, member, offset) \
14  extern int name##_3[offsetof(kind name, member) == offset ? 1 : -1];
15
16// Zero-width bit-fields
17struct a {char x; int : 0; char y;};
18#if defined(__arm__) || defined(__aarch64__)
19CHECK_SIZE(struct, a, 8)
20CHECK_ALIGN(struct, a, 4)
21#else
22CHECK_SIZE(struct, a, 5)
23CHECK_ALIGN(struct, a, 1)
24#endif
25
26// Zero-width bit-fields with packed
27struct __attribute__((packed)) a2 { short x : 9; char : 0; int y : 17; };
28CHECK_SIZE(struct, a2, 5)
29CHECK_ALIGN(struct, a2, 1)
30
31// Zero-width bit-fields at the end of packed struct
32struct __attribute__((packed)) a3 { short x : 9; int : 0; };
33#if defined(__arm__) || defined(__aarch64__)
34CHECK_SIZE(struct, a3, 4)
35CHECK_ALIGN(struct, a3, 4)
36#else
37CHECK_SIZE(struct, a3, 4)
38CHECK_ALIGN(struct, a3, 1)
39#endif
40
41// For comparison, non-zero-width bit-fields at the end of packed struct
42struct __attribute__((packed)) a4 { short x : 9; int : 1; };
43CHECK_SIZE(struct, a4, 2)
44CHECK_ALIGN(struct, a4, 1)
45
46union b {char x; int : 0; char y;};
47#if defined(__arm__) || defined(__aarch64__)
48CHECK_SIZE(union, b, 4)
49CHECK_ALIGN(union, b, 4)
50#else
51CHECK_SIZE(union, b, 1)
52CHECK_ALIGN(union, b, 1)
53#endif
54
55// Unnamed bit-field align
56struct c {char x; int : 20;};
57#if defined(__arm__) || defined(__aarch64__)
58CHECK_SIZE(struct, c, 4)
59CHECK_ALIGN(struct, c, 4)
60#else
61CHECK_SIZE(struct, c, 4)
62CHECK_ALIGN(struct, c, 1)
63#endif
64
65union d {char x; int : 20;};
66#if defined(__arm__) || defined(__aarch64__)
67CHECK_SIZE(union, d, 4)
68CHECK_ALIGN(union, d, 4)
69#else
70CHECK_SIZE(union, d, 3)
71CHECK_ALIGN(union, d, 1)
72#endif
73
74// Bit-field packing
75struct __attribute__((packed)) e {int x : 4, y : 30, z : 30;};
76CHECK_SIZE(struct, e, 8)
77CHECK_ALIGN(struct, e, 1)
78
79// Alignment on bit-fields
80struct f {__attribute((aligned(8))) int x : 30, y : 30, z : 30;};
81CHECK_SIZE(struct, f, 24)
82CHECK_ALIGN(struct, f, 8)
83
84// Large structure (overflows i32, in bits).
85struct s0 {
86  char a[0x32100000];
87  int x:30, y:30;
88};
89
90CHECK_SIZE(struct, s0, 0x32100008)
91CHECK_ALIGN(struct, s0, 4)
92
93// Bit-field with explicit align bigger than normal.
94struct g0 {
95  char a;
96  __attribute__((aligned(16))) int b : 1;
97  char c;
98};
99
100#if defined(__ORBIS__)
101CHECK_SIZE(struct, g0, 16);
102CHECK_ALIGN(struct, g0, 16);
103CHECK_OFFSET(struct, g0, c, 2);
104#else
105CHECK_SIZE(struct, g0, 32);
106CHECK_ALIGN(struct, g0, 16);
107CHECK_OFFSET(struct, g0, c, 17);
108#endif
109
110// Bit-field with explicit align smaller than normal.
111struct g1 {
112  char a;
113  __attribute__((aligned(2))) int b : 1;
114  char c;
115};
116
117CHECK_SIZE(struct, g1, 4);
118CHECK_ALIGN(struct, g1, 4);
119#if defined(__ORBIS__)
120CHECK_OFFSET(struct, g1, c, 2);
121#else
122CHECK_OFFSET(struct, g1, c, 3);
123#endif
124
125// Same as above but without explicit align.
126struct g2 {
127  char a;
128  int b : 1;
129  char c;
130};
131
132CHECK_SIZE(struct, g2, 4);
133CHECK_ALIGN(struct, g2, 4);
134CHECK_OFFSET(struct, g2, c, 2);
135
136// Explicit attribute align on bit-field has precedence over packed attribute
137// applied too the struct.
138struct __attribute__((packed)) g3 {
139  char a;
140  __attribute__((aligned(16))) int b : 1;
141  char c;
142};
143
144CHECK_ALIGN(struct, g3, 16);
145#if defined(__ORBIS__)
146CHECK_SIZE(struct, g3, 16);
147CHECK_OFFSET(struct, g3, c, 2);
148#else
149CHECK_SIZE(struct, g3, 32);
150CHECK_OFFSET(struct, g3, c, 17);
151#endif
152
153struct __attribute__((packed)) g4 {
154  char a;
155  __attribute__((aligned(2))) int b : 1;
156  char c;
157};
158
159CHECK_SIZE(struct, g4, 4);
160CHECK_ALIGN(struct, g4, 2);
161#if defined(__ORBIS__)
162CHECK_OFFSET(struct, g4, c, 2);
163#else
164CHECK_OFFSET(struct, g4, c, 3);
165#endif
166
167struct g5 {
168  char : 1;
169  __attribute__((aligned(1))) int n : 24;
170};
171CHECK_SIZE(struct, g5, 4);
172CHECK_ALIGN(struct, g5, 4);
173
174struct __attribute__((packed)) g6 {
175  char : 1;
176  __attribute__((aligned(1))) int n : 24;
177};
178CHECK_SIZE(struct, g6, 4);
179CHECK_ALIGN(struct, g6, 1);
180
181struct g7 {
182  char : 1;
183  __attribute__((aligned(1))) int n : 25;
184};
185#if defined(__ORBIS__)
186CHECK_SIZE(struct, g7, 4);
187#else
188CHECK_SIZE(struct, g7, 8);
189#endif
190CHECK_ALIGN(struct, g7, 4);
191
192struct __attribute__((packed)) g8 {
193  char : 1;
194  __attribute__((aligned(1))) int n : 25;
195};
196#if defined(__ORBIS__)
197CHECK_SIZE(struct, g8, 4);
198#else
199CHECK_SIZE(struct, g8, 5);
200#endif
201CHECK_ALIGN(struct, g8, 1);
202
203struct g9 {
204  __attribute__((aligned(1))) char a : 2, b : 2, c : 2, d : 2, e : 2;
205  int i;
206};
207#if defined(__ORBIS__)
208CHECK_SIZE(struct, g9, 8);
209#else
210CHECK_SIZE(struct, g9, 12);
211#endif
212CHECK_ALIGN(struct, g9, 4);
213
214struct __attribute__((packed)) g10 {
215  __attribute__((aligned(1))) char a : 2, b : 2, c : 2, d : 2, e : 2;
216  int i;
217};
218#if defined(__ORBIS__)
219CHECK_SIZE(struct, g10, 6);
220#else
221CHECK_SIZE(struct, g10, 9);
222#endif
223CHECK_ALIGN(struct, g10, 1);
224
225struct g11 {
226  char a;
227  __attribute__((aligned(1))) long long b : 62;
228  char c;
229};
230#if defined(__arm__) || defined(__aarch64__) || defined(__x86_64__)
231CHECK_SIZE(struct, g11, 24);
232CHECK_ALIGN(struct, g11, 8);
233CHECK_OFFSET(struct, g11, c, 16);
234#else
235CHECK_SIZE(struct, g11, 16);
236CHECK_ALIGN(struct, g11, 4);
237CHECK_OFFSET(struct, g11, c, 12);
238#endif
239
240struct __attribute__((packed)) g12 {
241  char a;
242  __attribute__((aligned(1))) long long b : 62;
243  char c;
244};
245CHECK_SIZE(struct, g12, 10);
246CHECK_ALIGN(struct, g12, 1);
247CHECK_OFFSET(struct, g12, c, 9);
248
249struct g13 {
250  char a;
251  __attribute__((aligned(1))) long long : 0;
252  char c;
253};
254#if defined(__arm__) || defined(__aarch64__)
255CHECK_SIZE(struct, g13, 16);
256CHECK_ALIGN(struct, g13, 8);
257CHECK_OFFSET(struct, g13, c, 8);
258#elif defined(__x86_64__)
259CHECK_SIZE(struct, g13, 9);
260CHECK_ALIGN(struct, g13, 1);
261CHECK_OFFSET(struct, g13, c, 8);
262#else
263CHECK_SIZE(struct, g13, 5);
264CHECK_ALIGN(struct, g13, 1);
265CHECK_OFFSET(struct, g13, c, 4);
266#endif
267
268struct __attribute__((packed)) g14 {
269  char a;
270  __attribute__((aligned(1))) long long : 0;
271  char c;
272};
273#if defined(__arm__) || defined(__aarch64__)
274CHECK_SIZE(struct, g14, 16);
275CHECK_ALIGN(struct, g14, 8);
276CHECK_OFFSET(struct, g14, c, 8);
277#elif defined(__x86_64__)
278CHECK_SIZE(struct, g14, 9);
279CHECK_ALIGN(struct, g14, 1);
280CHECK_OFFSET(struct, g14, c, 8);
281#else
282CHECK_SIZE(struct, g14, 5);
283CHECK_ALIGN(struct, g14, 1);
284CHECK_OFFSET(struct, g14, c, 4);
285#endif
286