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
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];
16 // Zero-width bit-fields
17 struct a
{char x
; int : 0; char y
;};
18 #if defined(__arm__) || defined(__aarch64__)
19 CHECK_SIZE(struct, a
, 8)
20 CHECK_ALIGN(struct, a
, 4)
22 CHECK_SIZE(struct, a
, 5)
23 CHECK_ALIGN(struct, a
, 1)
26 // Zero-width bit-fields with packed
27 struct __attribute__((packed
)) a2
{ short x
: 9; char : 0; int y
: 17; };
28 CHECK_SIZE(struct, a2
, 5)
29 CHECK_ALIGN(struct, a2
, 1)
31 // Zero-width bit-fields at the end of packed struct
32 struct __attribute__((packed
)) a3
{ short x
: 9; int : 0; };
33 #if defined(__arm__) || defined(__aarch64__)
34 CHECK_SIZE(struct, a3
, 4)
35 CHECK_ALIGN(struct, a3
, 4)
37 CHECK_SIZE(struct, a3
, 4)
38 CHECK_ALIGN(struct, a3
, 1)
41 // For comparison, non-zero-width bit-fields at the end of packed struct
42 struct __attribute__((packed
)) a4
{ short x
: 9; int : 1; };
43 CHECK_SIZE(struct, a4
, 2)
44 CHECK_ALIGN(struct, a4
, 1)
46 union b
{char x
; int : 0; char y
;};
47 #if defined(__arm__) || defined(__aarch64__)
48 CHECK_SIZE(union, b
, 4)
49 CHECK_ALIGN(union, b
, 4)
51 CHECK_SIZE(union, b
, 1)
52 CHECK_ALIGN(union, b
, 1)
55 // Unnamed bit-field align
56 struct c
{char x
; int : 20;};
57 #if defined(__arm__) || defined(__aarch64__)
58 CHECK_SIZE(struct, c
, 4)
59 CHECK_ALIGN(struct, c
, 4)
61 CHECK_SIZE(struct, c
, 4)
62 CHECK_ALIGN(struct, c
, 1)
65 union d
{char x
; int : 20;};
66 #if defined(__arm__) || defined(__aarch64__)
67 CHECK_SIZE(union, d
, 4)
68 CHECK_ALIGN(union, d
, 4)
70 CHECK_SIZE(union, d
, 3)
71 CHECK_ALIGN(union, d
, 1)
75 struct __attribute__((packed
)) e
{int x
: 4, y
: 30, z
: 30;};
76 CHECK_SIZE(struct, e
, 8)
77 CHECK_ALIGN(struct, e
, 1)
79 // Alignment on bit-fields
80 struct f
{__attribute((aligned(8))) int x
: 30, y
: 30, z
: 30;};
81 CHECK_SIZE(struct, f
, 24)
82 CHECK_ALIGN(struct, f
, 8)
84 // Large structure (overflows i32, in bits).
90 CHECK_SIZE(struct, s0
, 0x32100008)
91 CHECK_ALIGN(struct, s0
, 4)
93 // Bit-field with explicit align bigger than normal.
96 __attribute__((aligned(16))) int b
: 1;
100 #if defined(__ORBIS__)
101 CHECK_SIZE(struct, g0
, 16);
102 CHECK_ALIGN(struct, g0
, 16);
103 CHECK_OFFSET(struct, g0
, c
, 2);
105 CHECK_SIZE(struct, g0
, 32);
106 CHECK_ALIGN(struct, g0
, 16);
107 CHECK_OFFSET(struct, g0
, c
, 17);
110 // Bit-field with explicit align smaller than normal.
113 __attribute__((aligned(2))) int b
: 1;
117 CHECK_SIZE(struct, g1
, 4);
118 CHECK_ALIGN(struct, g1
, 4);
119 #if defined(__ORBIS__)
120 CHECK_OFFSET(struct, g1
, c
, 2);
122 CHECK_OFFSET(struct, g1
, c
, 3);
125 // Same as above but without explicit align.
132 CHECK_SIZE(struct, g2
, 4);
133 CHECK_ALIGN(struct, g2
, 4);
134 CHECK_OFFSET(struct, g2
, c
, 2);
136 // Explicit attribute align on bit-field has precedence over packed attribute
137 // applied too the struct.
138 struct __attribute__((packed
)) g3
{
140 __attribute__((aligned(16))) int b
: 1;
144 CHECK_ALIGN(struct, g3
, 16);
145 #if defined(__ORBIS__)
146 CHECK_SIZE(struct, g3
, 16);
147 CHECK_OFFSET(struct, g3
, c
, 2);
149 CHECK_SIZE(struct, g3
, 32);
150 CHECK_OFFSET(struct, g3
, c
, 17);
153 struct __attribute__((packed
)) g4
{
155 __attribute__((aligned(2))) int b
: 1;
159 CHECK_SIZE(struct, g4
, 4);
160 CHECK_ALIGN(struct, g4
, 2);
161 #if defined(__ORBIS__)
162 CHECK_OFFSET(struct, g4
, c
, 2);
164 CHECK_OFFSET(struct, g4
, c
, 3);
169 __attribute__((aligned(1))) int n
: 24;
171 CHECK_SIZE(struct, g5
, 4);
172 CHECK_ALIGN(struct, g5
, 4);
174 struct __attribute__((packed
)) g6
{
176 __attribute__((aligned(1))) int n
: 24;
178 CHECK_SIZE(struct, g6
, 4);
179 CHECK_ALIGN(struct, g6
, 1);
183 __attribute__((aligned(1))) int n
: 25;
185 #if defined(__ORBIS__)
186 CHECK_SIZE(struct, g7
, 4);
188 CHECK_SIZE(struct, g7
, 8);
190 CHECK_ALIGN(struct, g7
, 4);
192 struct __attribute__((packed
)) g8
{
194 __attribute__((aligned(1))) int n
: 25;
196 #if defined(__ORBIS__)
197 CHECK_SIZE(struct, g8
, 4);
199 CHECK_SIZE(struct, g8
, 5);
201 CHECK_ALIGN(struct, g8
, 1);
204 __attribute__((aligned(1))) char a
: 2, b
: 2, c
: 2, d
: 2, e
: 2;
207 #if defined(__ORBIS__)
208 CHECK_SIZE(struct, g9
, 8);
210 CHECK_SIZE(struct, g9
, 12);
212 CHECK_ALIGN(struct, g9
, 4);
214 struct __attribute__((packed
)) g10
{
215 __attribute__((aligned(1))) char a
: 2, b
: 2, c
: 2, d
: 2, e
: 2;
218 #if defined(__ORBIS__)
219 CHECK_SIZE(struct, g10
, 6);
221 CHECK_SIZE(struct, g10
, 9);
223 CHECK_ALIGN(struct, g10
, 1);
227 __attribute__((aligned(1))) long long b
: 62;
230 #if defined(__arm__) || defined(__aarch64__) || defined(__x86_64__)
231 CHECK_SIZE(struct, g11
, 24);
232 CHECK_ALIGN(struct, g11
, 8);
233 CHECK_OFFSET(struct, g11
, c
, 16);
235 CHECK_SIZE(struct, g11
, 16);
236 CHECK_ALIGN(struct, g11
, 4);
237 CHECK_OFFSET(struct, g11
, c
, 12);
240 struct __attribute__((packed
)) g12
{
242 __attribute__((aligned(1))) long long b
: 62;
245 CHECK_SIZE(struct, g12
, 10);
246 CHECK_ALIGN(struct, g12
, 1);
247 CHECK_OFFSET(struct, g12
, c
, 9);
251 __attribute__((aligned(1))) long long : 0;
254 #if defined(__arm__) || defined(__aarch64__)
255 CHECK_SIZE(struct, g13
, 16);
256 CHECK_ALIGN(struct, g13
, 8);
257 CHECK_OFFSET(struct, g13
, c
, 8);
258 #elif defined(__x86_64__)
259 CHECK_SIZE(struct, g13
, 9);
260 CHECK_ALIGN(struct, g13
, 1);
261 CHECK_OFFSET(struct, g13
, c
, 8);
263 CHECK_SIZE(struct, g13
, 5);
264 CHECK_ALIGN(struct, g13
, 1);
265 CHECK_OFFSET(struct, g13
, c
, 4);
268 struct __attribute__((packed
)) g14
{
270 __attribute__((aligned(1))) long long : 0;
273 #if defined(__arm__) || defined(__aarch64__)
274 CHECK_SIZE(struct, g14
, 16);
275 CHECK_ALIGN(struct, g14
, 8);
276 CHECK_OFFSET(struct, g14
, c
, 8);
277 #elif defined(__x86_64__)
278 CHECK_SIZE(struct, g14
, 9);
279 CHECK_ALIGN(struct, g14
, 1);
280 CHECK_OFFSET(struct, g14
, c
, 8);
282 CHECK_SIZE(struct, g14
, 5);
283 CHECK_ALIGN(struct, g14
, 1);
284 CHECK_OFFSET(struct, g14
, c
, 4);