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