summaryrefslogtreecommitdiff
path: root/test/CodeGen/override-layout.c
blob: 9907fec31843e8fcc813c1a4326fdc952d86be97 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// RUN: %clang_cc1 -w -fdump-record-layouts-simple %s > %t.layouts
// RUN: %clang_cc1 -w -DPACKED= -DALIGNED16= -fdump-record-layouts-simple -foverride-record-layout=%t.layouts %s > %t.after
// RUN: diff %t.layouts %t.after
// RUN: FileCheck %s < %t.after

// If not explicitly disabled, set PACKED to the packed attribute.
#ifndef PACKED
#  define PACKED __attribute__((packed))
#endif

// If not explicitly disabled, set ALIGNED16 to 16-byte alignment.
#ifndef ALIGNED16
#  define ALIGNED16 __attribute__((aligned(16)))
#endif

// CHECK: Type: struct X0
struct X0 { 
  int x[6] PACKED; 
};

void use_X0() { struct X0 x0; x0.x[5] = sizeof(struct X0); };

// CHECK: Type: struct X1
struct X1 { 
  char x[13]; 
  struct X0 y; 
} PACKED;

void use_X1() { struct X1 x1; x1.x[5] = sizeof(struct X1); };

// CHECK: Type: struct X2
struct PACKED X2 {
  short x;
  int y;
};

void use_X2() { struct X2 x2; x2.y = sizeof(struct X2); };

// CHECK: Type: struct X3
struct X3 {
  short x PACKED;
  int y;
};

void use_X3() { struct X3 x3; x3.y = sizeof(struct X3); };

#pragma pack(push,2)
// CHECK: Type: struct X4
struct X4 {
  int x;
  int y;
};
#pragma pack(pop)

void use_X4() { struct X4 x4; x4.y = sizeof(struct X4); };

// CHECK: Type: struct X5
struct PACKED X5 { double a[19];  signed char b; };

void use_X5() { struct X5 x5; x5.b = sizeof(struct X5); };

// CHECK: Type: struct X6
struct PACKED X6 { long double a; char b; };

void use_X6() { struct X6 x6; x6.b = sizeof(struct X6); };

// CHECK: Type: struct X7
struct X7 {
        unsigned x;
        unsigned char y;
} PACKED;

void use_X7() { struct X7 x7; x7.y = x7.x = sizeof(struct X7); }

// CHECK: Type: union X8
union X8 {
  struct X7 x;
  unsigned y;
} PACKED;

// CHECK: Type: struct X9
struct X9 {
  unsigned int x[2] PACKED;
  unsigned int y;
  unsigned int z PACKED;
};

// CHECK: Type: struct X10
struct X10 {
  unsigned int x[2] PACKED;
  unsigned int y PACKED;
  unsigned int z PACKED;
};

// CHECK: Type: struct X11
struct PACKED X11 {
  unsigned int x[2];
  unsigned int y;
  unsigned int z;
};

// CHECK: Type: struct X12
struct PACKED X12 {
  int x : 24;
};

// CHECK: Type: struct X13
struct PACKED X13 {
  signed x : 10;
  signed y : 10;
};

// CHECK: Type: union X14
union PACKED X14 {
  unsigned long long x : 3;
};

// CHECK: Type: struct X15
struct X15 {
  unsigned x : 16;
  unsigned y : 28 PACKED;
};

// CHECK: Type: struct X16
struct ALIGNED16 X16 {
  int a, b, c;
  int x : 5;
  int y : 29;
};

void use_structs() {
  union X8 x8;
  typedef int X8array[sizeof(union X8)];
  x8.y = sizeof(union X8);
  x8.x.x = x8.y;

  struct X9 x9;
  typedef int X9array[sizeof(struct X9)];
  x9.y = sizeof(struct X9);

  struct X10 x10;
  typedef int X10array[sizeof(struct X10)];
  x10.y = sizeof(struct X10);

  struct X11 x11;
  typedef int X11array[sizeof(struct X11)];
  x11.y = sizeof(struct X11);

  struct X12 x12;
  x12.x = sizeof(struct X12);

  struct X13 x13;
  x13.x = sizeof(struct X13);

  union X14 x14;
  x14.x = sizeof(union X14);

  struct X15 x15;
  x15.x = sizeof(struct X15);

  struct X16 x16;
  x16.x = sizeof(struct X16);
}