Debian Codegraph @

clang/test/Sema/bitfield-layout.c

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