[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / Sema / address-packed.c
blobe149a5340beed240db42b41655a8109a8864d29d
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
3 #include <stdint.h>
5 extern void f1(int *);
6 extern void f2(char *);
8 struct Ok {
9 char c;
10 int x;
13 struct __attribute__((packed)) Arguable {
14 char c0;
15 int x;
16 char c1;
19 union __attribute__((packed)) UnionArguable {
20 char c;
21 int x;
24 typedef struct Arguable ArguableT;
26 struct Arguable *get_arguable(void);
28 void to_void(void *);
29 void to_intptr(intptr_t);
31 void g0(void) {
33 struct Ok ok;
34 f1(&ok.x); // no-warning
35 f2(&ok.c); // no-warning
38 struct Arguable arguable;
39 f2(&arguable.c0); // no-warning
40 f1(&arguable.x); // expected-warning {{packed member 'x' of class or structure 'Arguable'}}
41 f2(&arguable.c1); // no-warning
43 f1((int *)(void *)&arguable.x); // no-warning
44 to_void(&arguable.x); // no-warning
45 void *p = &arguable.x; // no-warning
46 to_void(p);
47 to_intptr((intptr_t)p); // no-warning
50 union UnionArguable arguable;
51 f2(&arguable.c); // no-warning
52 f1(&arguable.x); // expected-warning {{packed member 'x' of class or structure 'UnionArguable'}}
54 f1((int *)(void *)&arguable.x); // no-warning
55 to_void(&arguable.x); // no-warning
56 to_intptr((intptr_t)&arguable.x); // no-warning
59 ArguableT arguable;
60 f2(&arguable.c0); // no-warning
61 f1(&arguable.x); // expected-warning {{packed member 'x' of class or structure 'Arguable'}}
62 f2(&arguable.c1); // no-warning
64 f1((int *)(void *)&arguable.x); // no-warning
65 to_void(&arguable.x); // no-warning
66 to_intptr((intptr_t)&arguable.x); // no-warning
69 struct Arguable *arguable = get_arguable();
70 f2(&arguable->c0); // no-warning
71 f1(&arguable->x); // expected-warning {{packed member 'x' of class or structure 'Arguable'}}
72 f2(&arguable->c1); // no-warning
74 f1((int *)(void *)&arguable->x); // no-warning
75 to_void(&arguable->c1); // no-warning
76 to_intptr((intptr_t)&arguable->c1); // no-warning
79 ArguableT *arguable = get_arguable();
80 f2(&(arguable->c0)); // no-warning
81 f1(&(arguable->x)); // expected-warning {{packed member 'x' of class or structure 'Arguable'}}
82 f2(&(arguable->c1)); // no-warning
84 f1((int *)(void *)&(arguable->x)); // no-warning
85 to_void(&(arguable->c1)); // no-warning
86 to_intptr((intptr_t)&(arguable->c1)); // no-warning
90 struct S1 {
91 char c;
92 int i __attribute__((packed));
95 int *g1(struct S1 *s1) {
96 return &s1->i; // expected-warning {{packed member 'i' of class or structure 'S1'}}
99 struct S2_i {
100 int i;
102 struct __attribute__((packed)) S2 {
103 char c;
104 struct S2_i inner;
107 int *g2(struct S2 *s2) {
108 return &s2->inner.i; // expected-warning {{packed member 'inner' of class or structure 'S2'}}
111 struct S2_a {
112 char c;
113 struct S2_i inner __attribute__((packed));
116 int *g2_a(struct S2_a *s2_a) {
117 return &s2_a->inner.i; // expected-warning {{packed member 'inner' of class or structure 'S2_a'}}
120 struct __attribute__((packed)) S3 {
121 char c;
122 struct {
123 int i;
124 } inner;
127 int *g3(struct S3 *s3) {
128 return &s3->inner.i; // expected-warning {{packed member 'inner' of class or structure 'S3'}}
131 struct S4 {
132 char c;
133 struct __attribute__((packed)) {
134 int i;
135 } inner;
138 int *g4(struct S4 *s4) {
139 return &s4->inner.i; // expected-warning {{packed member 'i' of class or structure 'S4::(anonymous)'}}
142 struct S5 {
143 char c;
144 struct {
145 char c1;
146 int i __attribute__((packed));
147 } inner;
150 int *g5(struct S5 *s5) {
151 return &s5->inner.i; // expected-warning {{packed member 'i' of class or structure 'S5::(anonymous)'}}
154 struct __attribute__((packed, aligned(2))) AlignedTo2 {
155 int x;
158 char *g6(struct AlignedTo2 *s) {
159 return (char *)&s->x; // no-warning
162 struct __attribute__((packed, aligned(2))) AlignedTo2Bis {
163 int x;
166 struct AlignedTo2Bis* g7(struct AlignedTo2 *s)
168 return (struct AlignedTo2Bis*)&s->x; // no-warning
171 typedef struct {
172 char c;
173 int x;
174 } __attribute__((packed)) TypedefStructArguable;
176 typedef union {
177 char c;
178 int x;
179 } __attribute((packed)) TypedefUnionArguable;
181 typedef TypedefStructArguable TypedefStructArguableTheSecond;
183 int *typedef1(TypedefStructArguable *s) {
184 return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefStructArguable'}}
187 int *typedef2(TypedefStructArguableTheSecond *s) {
188 return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefStructArguable'}}
191 int *typedef3(TypedefUnionArguable *s) {
192 return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefUnionArguable'}}
195 struct S6 {
196 union {
197 char c;
198 int x;
199 } __attribute__((packed));
202 int *anonymousInnerUnion(struct S6 *s) {
203 return &s->x; // expected-warning {{packed member 'x' of class or structure 'S6::(anonymous)'}}
206 struct S6a {
207 int a;
208 int _;
209 int c;
210 char __;
211 int d;
212 } __attribute__((packed, aligned(16))) s6;
214 void g8(void)
216 f1(&s6.a); // no-warning
217 f1(&s6.c); // no-warning
218 f1(&s6.d); // expected-warning {{packed member 'd' of class or structure 'S6a'}}
221 struct __attribute__((packed, aligned(1))) MisalignedContainee { double d; };
222 struct __attribute__((aligned(8))) AlignedContainer { struct MisalignedContainee b; };
224 struct AlignedContainer *p;
225 double* g9(void) {
226 return &p->b.d; // no-warning
229 union OneUnion
231 uint32_t a;
232 uint32_t b:1;
235 struct __attribute__((packed)) S7 {
236 uint8_t length;
237 uint8_t stuff;
238 uint8_t padding[2];
239 union OneUnion one_union;
242 union AnotherUnion {
243 long data;
244 struct S7 s;
245 } *au;
247 union OneUnion* get_OneUnion(void)
249 return &au->s.one_union; // no-warning
252 struct __attribute__((packed)) S8 {
253 uint8_t data1;
254 uint8_t data2;
255 uint16_t wider_data;
258 #define LE_READ_2(p) \
259 ((uint16_t) \
260 ((((const uint8_t *)(p))[0] ) | \
261 (((const uint8_t *)(p))[1] << 8)))
263 uint32_t get_wider_data(struct S8 *s)
265 return LE_READ_2(&s->wider_data); // no-warning
268 struct S9 {
269 uint32_t x;
270 uint8_t y[2];
271 uint16_t z;
272 } __attribute__((__packed__));
274 typedef struct S9 __attribute__((__aligned__(16))) aligned_S9;
276 void g10(void) {
277 struct S9 x;
278 struct S9 __attribute__((__aligned__(8))) y;
279 aligned_S9 z;
281 uint32_t *p32;
282 p32 = &x.x; // expected-warning {{packed member 'x' of class or structure 'S9'}}
283 p32 = &y.x; // no-warning
284 p32 = &z.x; // no-warning
287 typedef struct {
288 uint32_t msgh_bits;
289 uint32_t msgh_size;
290 int32_t msgh_voucher_port;
291 int32_t msgh_id;
292 } S10Header;
294 typedef struct {
295 uint32_t t;
296 uint64_t m;
297 uint32_t p;
298 union {
299 struct {
300 uint32_t a;
301 double z;
302 } __attribute__((aligned(8), packed)) a;
303 struct {
304 uint32_t b;
305 double z;
306 uint32_t a;
307 } __attribute__((aligned(8), packed)) b;
309 } __attribute__((aligned(8), packed)) S10Data;
311 typedef struct {
312 S10Header hdr;
313 uint32_t size;
314 uint8_t count;
315 S10Data data[] __attribute__((aligned(8)));
316 } __attribute__((aligned(8), packed)) S10;
318 void g11(S10Header *hdr);
319 void g12(S10 *s) {
320 g11(&s->hdr); // no-warning
323 struct S11 {
324 uint32_t x;
325 } __attribute__((__packed__));
327 void g13(void) {
328 struct S11 __attribute__((__aligned__(4))) a[4];
329 uint32_t *p32;
330 p32 = &a[0].x; // no-warning
333 struct Invalid0 {
334 void *x;
335 struct fwd f; // expected-error {{incomplete type}} expected-note {{forward declaration}}
336 } __attribute__((packed));
338 void *g14(struct Invalid0 *ivl) {
339 return &(ivl->x);