xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/bitfield-layout.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 %s -fsyntax-only -verify -triple=i686-apple-darwin9
2*f4a2713aSLionel Sambuc // expected-no-diagnostics
3*f4a2713aSLionel Sambuc 
4*f4a2713aSLionel Sambuc #define CHECK_SIZE(kind, name, size) extern int name##1[sizeof(kind name) == size ? 1 : -1];
5*f4a2713aSLionel Sambuc #define CHECK_ALIGN(kind, name, size) extern int name##2[__alignof(kind name) == size ? 1 : -1];
6*f4a2713aSLionel Sambuc 
7*f4a2713aSLionel Sambuc // Zero-width bit-fields
8*f4a2713aSLionel Sambuc struct a {char x; int : 0; char y;};
9*f4a2713aSLionel Sambuc CHECK_SIZE(struct, a, 5)
10*f4a2713aSLionel Sambuc CHECK_ALIGN(struct, a, 1)
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc union b {char x; int : 0; char y;};
13*f4a2713aSLionel Sambuc CHECK_SIZE(union, b, 1)
14*f4a2713aSLionel Sambuc CHECK_ALIGN(union, b, 1)
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc // Unnamed bit-field align
17*f4a2713aSLionel Sambuc struct c {char x; int : 20;};
18*f4a2713aSLionel Sambuc CHECK_SIZE(struct, c, 4)
19*f4a2713aSLionel Sambuc CHECK_ALIGN(struct, c, 1)
20*f4a2713aSLionel Sambuc 
21*f4a2713aSLionel Sambuc union d {char x; int : 20;};
22*f4a2713aSLionel Sambuc CHECK_SIZE(union, d, 3)
23*f4a2713aSLionel Sambuc CHECK_ALIGN(union, d, 1)
24*f4a2713aSLionel Sambuc 
25*f4a2713aSLionel Sambuc // Bit-field packing
26*f4a2713aSLionel Sambuc struct __attribute__((packed)) e {int x : 4, y : 30, z : 30;};
27*f4a2713aSLionel Sambuc CHECK_SIZE(struct, e, 8)
28*f4a2713aSLionel Sambuc CHECK_ALIGN(struct, e, 1)
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc // Alignment on bit-fields
31*f4a2713aSLionel Sambuc struct f {__attribute((aligned(8))) int x : 30, y : 30, z : 30;};
32*f4a2713aSLionel Sambuc CHECK_SIZE(struct, f, 24)
33*f4a2713aSLionel Sambuc CHECK_ALIGN(struct, f, 8)
34*f4a2713aSLionel Sambuc 
35*f4a2713aSLionel Sambuc // Large structure (overflows i32, in bits).
36*f4a2713aSLionel Sambuc struct s0 {
37*f4a2713aSLionel Sambuc   char a[0x32100000];
38*f4a2713aSLionel Sambuc   int x:30, y:30;
39*f4a2713aSLionel Sambuc };
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc CHECK_SIZE(struct, s0, 0x32100008)
42*f4a2713aSLionel Sambuc CHECK_ALIGN(struct, s0, 4)
43*f4a2713aSLionel Sambuc 
44