bitfield-layout.c revision 8e8fb3be5bd78f0564444eca02b404566a5f3b5d
1// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=i686-apple-darwin9 2// expected-no-diagnostics 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 8struct a {char x; int : 0; char y;}; 9CHECK_SIZE(struct, a, 5) 10CHECK_ALIGN(struct, a, 1) 11 12union b {char x; int : 0; char y;}; 13CHECK_SIZE(union, b, 1) 14CHECK_ALIGN(union, b, 1) 15 16// Unnamed bit-field align 17struct c {char x; int : 20;}; 18CHECK_SIZE(struct, c, 4) 19CHECK_ALIGN(struct, c, 1) 20 21union d {char x; int : 20;}; 22CHECK_SIZE(union, d, 3) 23CHECK_ALIGN(union, d, 1) 24 25// Bit-field packing 26struct __attribute__((packed)) e {int x : 4, y : 30, z : 30;}; 27CHECK_SIZE(struct, e, 8) 28CHECK_ALIGN(struct, e, 1) 29 30// Alignment on bit-fields 31struct f {__attribute((aligned(8))) int x : 30, y : 30, z : 30;}; 32CHECK_SIZE(struct, f, 24) 33CHECK_ALIGN(struct, f, 8) 34 35// Large structure (overflows i32, in bits). 36struct s0 { 37 char a[0x32100000]; 38 int x:30, y:30; 39}; 40 41CHECK_SIZE(struct, s0, 0x32100008) 42CHECK_ALIGN(struct, s0, 4) 43 44