File: bitfield-layout.c

package info (click to toggle)
llvm-toolchain-3.7 1%3A3.7.1-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 345,556 kB
  • ctags: 362,199
  • sloc: cpp: 2,156,381; ansic: 458,339; objc: 91,547; python: 89,988; asm: 86,305; sh: 21,479; makefile: 6,853; perl: 5,601; ml: 5,458; pascal: 3,933; lisp: 2,429; xml: 686; cs: 239; php: 202; csh: 117
file content (58 lines) | stat: -rw-r--r-- 1,645 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=i686-apple-darwin9
// expected-no-diagnostics

#define CHECK_SIZE(kind, name, size) extern int name##1[sizeof(kind name) == size ? 1 : -1];
#define CHECK_ALIGN(kind, name, size) extern int name##2[__alignof(kind name) == size ? 1 : -1];

// Zero-width bit-fields
struct a {char x; int : 0; char y;};
CHECK_SIZE(struct, a, 5)
CHECK_ALIGN(struct, a, 1)

// Zero-width bit-fields with packed
struct __attribute__((packed)) a2 { short x : 9; char : 0; int y : 17; };
CHECK_SIZE(struct, a2, 5)
CHECK_ALIGN(struct, a2, 1)

// Zero-width bit-fields at the end of packed struct
struct __attribute__((packed)) a3 { short x : 9; int : 0; };
CHECK_SIZE(struct, a3, 4)
CHECK_ALIGN(struct, a3, 1)

// For comparison, non-zero-width bit-fields at the end of packed struct
struct __attribute__((packed)) a4 { short x : 9; int : 1; };
CHECK_SIZE(struct, a4, 2)
CHECK_ALIGN(struct, a4, 1)

union b {char x; int : 0; char y;};
CHECK_SIZE(union, b, 1)
CHECK_ALIGN(union, b, 1)

// Unnamed bit-field align
struct c {char x; int : 20;};
CHECK_SIZE(struct, c, 4)
CHECK_ALIGN(struct, c, 1)

union d {char x; int : 20;};
CHECK_SIZE(union, d, 3)
CHECK_ALIGN(union, d, 1)

// Bit-field packing
struct __attribute__((packed)) e {int x : 4, y : 30, z : 30;};
CHECK_SIZE(struct, e, 8)
CHECK_ALIGN(struct, e, 1)

// Alignment on bit-fields
struct f {__attribute((aligned(8))) int x : 30, y : 30, z : 30;};
CHECK_SIZE(struct, f, 24)
CHECK_ALIGN(struct, f, 8)

// Large structure (overflows i32, in bits).
struct s0 {
  char a[0x32100000];
  int x:30, y:30;
};

CHECK_SIZE(struct, s0, 0x32100008)
CHECK_ALIGN(struct, s0, 4)