Daniel Dunbar | 8fbe78f | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 %s -fsyntax-only -verify -triple=i686-apple-darwin9 |
Andy Gibbs | c6e68da | 2012-10-19 12:44:48 +0000 | [diff] [blame] | 2 | // expected-no-diagnostics |
Eli Friedman | 3df5efe | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 3 | |
| 4 | #define CHECK_SIZE(kind, name, size) extern int name##1[sizeof(kind name) == size ? 1 : -1]; |
| 5 | #define CHECK_ALIGN(kind, name, size) extern int name##2[__alignof(kind name) == size ? 1 : -1]; |
| 6 | |
| 7 | // Zero-width bit-fields |
| 8 | struct a {char x; int : 0; char y;}; |
| 9 | CHECK_SIZE(struct, a, 5) |
| 10 | CHECK_ALIGN(struct, a, 1) |
| 11 | |
Yunzhong Gao | 5fd0c9d | 2014-02-13 02:45:10 +0000 | [diff] [blame] | 12 | // Zero-width bit-fields with packed |
| 13 | struct __attribute__((packed)) a2 { short x : 9; char : 0; int y : 17; }; |
| 14 | CHECK_SIZE(struct, a2, 5) |
| 15 | CHECK_ALIGN(struct, a2, 1) |
| 16 | |
| 17 | // Zero-width bit-fields at the end of packed struct |
| 18 | struct __attribute__((packed)) a3 { short x : 9; int : 0; }; |
| 19 | CHECK_SIZE(struct, a3, 4) |
| 20 | CHECK_ALIGN(struct, a3, 1) |
| 21 | |
| 22 | // For comparison, non-zero-width bit-fields at the end of packed struct |
| 23 | struct __attribute__((packed)) a4 { short x : 9; int : 1; }; |
| 24 | CHECK_SIZE(struct, a4, 2) |
| 25 | CHECK_ALIGN(struct, a4, 1) |
| 26 | |
Eli Friedman | 3df5efe | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 27 | union b {char x; int : 0; char y;}; |
| 28 | CHECK_SIZE(union, b, 1) |
| 29 | CHECK_ALIGN(union, b, 1) |
| 30 | |
| 31 | // Unnamed bit-field align |
| 32 | struct c {char x; int : 20;}; |
| 33 | CHECK_SIZE(struct, c, 4) |
| 34 | CHECK_ALIGN(struct, c, 1) |
| 35 | |
| 36 | union d {char x; int : 20;}; |
| 37 | CHECK_SIZE(union, d, 3) |
| 38 | CHECK_ALIGN(union, d, 1) |
| 39 | |
| 40 | // Bit-field packing |
| 41 | struct __attribute__((packed)) e {int x : 4, y : 30, z : 30;}; |
| 42 | CHECK_SIZE(struct, e, 8) |
| 43 | CHECK_ALIGN(struct, e, 1) |
| 44 | |
| 45 | // Alignment on bit-fields |
| 46 | struct f {__attribute((aligned(8))) int x : 30, y : 30, z : 30;}; |
| 47 | CHECK_SIZE(struct, f, 24) |
| 48 | CHECK_ALIGN(struct, f, 8) |
Daniel Dunbar | f35e765 | 2010-06-29 18:34:35 +0000 | [diff] [blame] | 49 | |
| 50 | // Large structure (overflows i32, in bits). |
| 51 | struct s0 { |
| 52 | char a[0x32100000]; |
| 53 | int x:30, y:30; |
| 54 | }; |
| 55 | |
| 56 | CHECK_SIZE(struct, s0, 0x32100008) |
| 57 | CHECK_ALIGN(struct, s0, 4) |
| 58 | |