| 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
| 2 | |
| 3 | #include <stdint.h> |
| 4 | |
| 5 | extern void f1(int *); |
| 6 | extern void f2(char *); |
| 7 | |
| 8 | struct Ok { |
| 9 | char c; |
| 10 | int x; |
| 11 | }; |
| 12 | |
| 13 | struct __attribute__((packed)) Arguable { |
| 14 | char c0; |
| 15 | int x; |
| 16 | char c1; |
| 17 | }; |
| 18 | |
| 19 | union __attribute__((packed)) UnionArguable { |
| 20 | char c; |
| 21 | int x; |
| 22 | }; |
| 23 | |
| 24 | typedef struct Arguable ArguableT; |
| 25 | |
| 26 | struct Arguable *get_arguable(); |
| 27 | |
| 28 | void to_void(void *); |
| 29 | void to_intptr(intptr_t); |
| 30 | |
| 31 | void g0(void) { |
| 32 | { |
| 33 | struct Ok ok; |
| 34 | f1(&ok.x); // no-warning |
| 35 | f2(&ok.c); // no-warning |
| 36 | } |
| 37 | { |
| 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 |
| 42 | |
| 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 |
| 48 | } |
| 49 | { |
| 50 | union UnionArguable arguable; |
| 51 | f2(&arguable.c); // no-warning |
| 52 | f1(&arguable.x); // expected-warning {{packed member 'x' of class or structure 'UnionArguable'}} |
| 53 | |
| 54 | f1((int *)(void *)&arguable.x); // no-warning |
| 55 | to_void(&arguable.x); // no-warning |
| 56 | to_intptr((intptr_t)&arguable.x); // no-warning |
| 57 | } |
| 58 | { |
| 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 |
| 63 | |
| 64 | f1((int *)(void *)&arguable.x); // no-warning |
| 65 | to_void(&arguable.x); // no-warning |
| 66 | to_intptr((intptr_t)&arguable.x); // no-warning |
| 67 | } |
| 68 | { |
| 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 |
| 73 | |
| 74 | f1((int *)(void *)&arguable->x); // no-warning |
| 75 | to_void(&arguable->c1); // no-warning |
| 76 | to_intptr((intptr_t)&arguable->c1); // no-warning |
| 77 | } |
| 78 | { |
| 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 |
| 83 | |
| 84 | f1((int *)(void *)&(arguable->x)); // no-warning |
| 85 | to_void(&(arguable->c1)); // no-warning |
| 86 | to_intptr((intptr_t)&(arguable->c1)); // no-warning |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | struct S1 { |
| 91 | char c; |
| 92 | int i __attribute__((packed)); |
| 93 | }; |
| 94 | |
| 95 | int *g1(struct S1 *s1) { |
| 96 | return &s1->i; // expected-warning {{packed member 'i' of class or structure 'S1'}} |
| 97 | } |
| 98 | |
| 99 | struct S2_i { |
| 100 | int i; |
| 101 | }; |
| 102 | struct __attribute__((packed)) S2 { |
| 103 | char c; |
| 104 | struct S2_i inner; |
| 105 | }; |
| 106 | |
| 107 | int *g2(struct S2 *s2) { |
| 108 | return &s2->inner.i; // expected-warning {{packed member 'inner' of class or structure 'S2'}} |
| 109 | } |
| 110 | |
| 111 | struct S2_a { |
| 112 | char c; |
| 113 | struct S2_i inner __attribute__((packed)); |
| 114 | }; |
| 115 | |
| 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'}} |
| 118 | } |
| 119 | |
| 120 | struct __attribute__((packed)) S3 { |
| 121 | char c; |
| 122 | struct { |
| 123 | int i; |
| 124 | } inner; |
| 125 | }; |
| 126 | |
| 127 | int *g3(struct S3 *s3) { |
| 128 | return &s3->inner.i; // expected-warning {{packed member 'inner' of class or structure 'S3'}} |
| 129 | } |
| 130 | |
| 131 | struct S4 { |
| 132 | char c; |
| 133 | struct __attribute__((packed)) { |
| 134 | int i; |
| 135 | } inner; |
| 136 | }; |
| 137 | |
| 138 | int *g4(struct S4 *s4) { |
| 139 | return &s4->inner.i; // expected-warning {{packed member 'i' of class or structure 'S4::(anonymous)'}} |
| 140 | } |
| 141 | |
| 142 | struct S5 { |
| 143 | char c; |
| 144 | struct { |
| 145 | char c1; |
| 146 | int i __attribute__((packed)); |
| 147 | } inner; |
| 148 | }; |
| 149 | |
| 150 | int *g5(struct S5 *s5) { |
| 151 | return &s5->inner.i; // expected-warning {{packed member 'i' of class or structure 'S5::(anonymous)'}} |
| 152 | } |
| 153 | |
| 154 | struct __attribute__((packed, aligned(2))) AlignedTo2 { |
| 155 | int x; |
| 156 | }; |
| 157 | |
| 158 | char *g6(struct AlignedTo2 *s) { |
| 159 | return (char *)&s->x; // no-warning |
| 160 | } |
| 161 | |
| 162 | struct __attribute__((packed, aligned(2))) AlignedTo2Bis { |
| 163 | int x; |
| 164 | }; |
| 165 | |
| 166 | struct AlignedTo2Bis* g7(struct AlignedTo2 *s) |
| 167 | { |
| 168 | return (struct AlignedTo2Bis*)&s->x; // no-warning |
| 169 | } |
| 170 | |
| 171 | typedef struct { |
| 172 | char c; |
| 173 | int x; |
| 174 | } __attribute__((packed)) TypedefStructArguable; |
| 175 | |
| 176 | typedef union { |
| 177 | char c; |
| 178 | int x; |
| 179 | } __attribute((packed)) TypedefUnionArguable; |
| 180 | |
| 181 | typedef TypedefStructArguable TypedefStructArguableTheSecond; |
| 182 | |
| 183 | int *typedef1(TypedefStructArguable *s) { |
| 184 | return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefStructArguable'}} |
| 185 | } |
| 186 | |
| 187 | int *typedef2(TypedefStructArguableTheSecond *s) { |
| 188 | return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefStructArguable'}} |
| 189 | } |
| 190 | |
| 191 | int *typedef3(TypedefUnionArguable *s) { |
| 192 | return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefUnionArguable'}} |
| 193 | } |
| 194 | |
| 195 | struct S6 { |
| 196 | union { |
| 197 | char c; |
| 198 | int x; |
| 199 | } __attribute__((packed)); |
| 200 | }; |
| 201 | |
| 202 | int *anonymousInnerUnion(struct S6 *s) { |
| 203 | return &s->x; // expected-warning {{packed member 'x' of class or structure 'S6::(anonymous)'}} |
| 204 | } |
| 205 | |
| 206 | struct S6a { |
| 207 | int a; |
| 208 | int _; |
| 209 | int c; |
| 210 | char __; |
| 211 | int d; |
| 212 | } __attribute__((packed, aligned(16))) s6; |
| 213 | |
| 214 | void g8() |
| 215 | { |
| 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'}} |
| 219 | } |
| 220 | |
| 221 | struct __attribute__((packed, aligned(1))) MisalignedContainee { double d; }; |
| 222 | struct __attribute__((aligned(8))) AlignedContainer { struct MisalignedContainee b; }; |
| 223 | |
| 224 | struct AlignedContainer *p; |
| 225 | double* g9() { |
| 226 | return &p->b.d; // no-warning |
| 227 | } |
| 228 | |
| 229 | union OneUnion |
| 230 | { |
| 231 | uint32_t a; |
| 232 | uint32_t b:1; |
| 233 | }; |
| 234 | |
| 235 | struct __attribute__((packed)) S7 { |
| 236 | uint8_t length; |
| 237 | uint8_t stuff; |
| 238 | uint8_t padding[2]; |
| 239 | union OneUnion one_union; |
| 240 | }; |
| 241 | |
| 242 | union AnotherUnion { |
| 243 | long data; |
| 244 | struct S7 s; |
| 245 | } *au; |
| 246 | |
| 247 | union OneUnion* get_OneUnion(void) |
| 248 | { |
| 249 | return &au->s.one_union; // no-warning |
| 250 | } |
| 251 | |
| 252 | struct __attribute__((packed)) S8 { |
| 253 | uint8_t data1; |
| 254 | uint8_t data2; |
| 255 | uint16_t wider_data; |
| 256 | }; |
| 257 | |
| 258 | #define LE_READ_2(p) \ |
| 259 | ((uint16_t) \ |
| 260 | ((((const uint8_t *)(p))[0] ) | \ |
| 261 | (((const uint8_t *)(p))[1] << 8))) |
| 262 | |
| 263 | uint32_t get_wider_data(struct S8 *s) |
| 264 | { |
| 265 | return LE_READ_2(&s->wider_data); // no-warning |
| 266 | } |
| 267 | |
| 268 | struct S9 { |
| 269 | uint32_t x; |
| 270 | uint8_t y[2]; |
| 271 | uint16_t z; |
| 272 | } __attribute__((__packed__)); |
| 273 | |
| 274 | typedef struct S9 __attribute__((__aligned__(16))) aligned_S9; |
| 275 | |
| 276 | void g10() { |
| 277 | struct S9 x; |
| 278 | struct S9 __attribute__((__aligned__(8))) y; |
| 279 | aligned_S9 z; |
| 280 | |
| 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 |
| 285 | } |
| 286 | |
| 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; |
| 293 | |
| 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; |
| 308 | }; |
| 309 | } __attribute__((aligned(8), packed)) S10Data; |
| 310 | |
| 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; |
| 317 | |
| 318 | void g11(S10Header *hdr); |
| 319 | void g12(S10 *s) { |
| 320 | g11(&s->hdr); // no-warning |
| 321 | } |
| 322 | |
| 323 | struct S11 { |
| 324 | uint32_t x; |
| 325 | } __attribute__((__packed__)); |
| 326 | |
| 327 | void g13(void) { |
| 328 | struct S11 __attribute__((__aligned__(4))) a[4]; |
| 329 | uint32_t *p32; |
| 330 | p32 = &a[0].x; // no-warning |
| 331 | } |
| 332 | |
| 333 | struct Invalid0 { |
| 334 | void *x; |
| 335 | struct fwd f; // expected-error {{incomplete type}} expected-note {{forward declaration}} |
| 336 | } __attribute__((packed)); |
| 337 | |
| 338 | void *g14(struct Invalid0 *ivl) { |
| 339 | return &(ivl->x); |
| 340 | } |
| 341 | |