xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/struct-packed-align.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 %s -fsyntax-only -verify
2*f4a2713aSLionel Sambuc // expected-no-diagnostics
3*f4a2713aSLionel Sambuc 
4*f4a2713aSLionel Sambuc // Packed structs.
5*f4a2713aSLionel Sambuc struct s {
6*f4a2713aSLionel Sambuc     char a;
7*f4a2713aSLionel Sambuc     int b  __attribute__((packed));
8*f4a2713aSLionel Sambuc     char c;
9*f4a2713aSLionel Sambuc     int d;
10*f4a2713aSLionel Sambuc };
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc extern int a1[sizeof(struct s) == 12 ? 1 : -1];
13*f4a2713aSLionel Sambuc extern int a2[__alignof(struct s) == 4 ? 1 : -1];
14*f4a2713aSLionel Sambuc 
15*f4a2713aSLionel Sambuc struct __attribute__((packed)) packed_s {
16*f4a2713aSLionel Sambuc     char a;
17*f4a2713aSLionel Sambuc     int b  __attribute__((packed));
18*f4a2713aSLionel Sambuc     char c;
19*f4a2713aSLionel Sambuc     int d;
20*f4a2713aSLionel Sambuc };
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc extern int b1[sizeof(struct packed_s) == 10 ? 1 : -1];
23*f4a2713aSLionel Sambuc extern int b2[__alignof(struct packed_s) == 1 ? 1 : -1];
24*f4a2713aSLionel Sambuc 
25*f4a2713aSLionel Sambuc struct fas {
26*f4a2713aSLionel Sambuc     char a;
27*f4a2713aSLionel Sambuc     int b[];
28*f4a2713aSLionel Sambuc };
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc extern int c1[sizeof(struct fas) == 4 ? 1 : -1];
31*f4a2713aSLionel Sambuc extern int c2[__alignof(struct fas) == 4 ? 1 : -1];
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc struct __attribute__((packed)) packed_fas {
34*f4a2713aSLionel Sambuc     char a;
35*f4a2713aSLionel Sambuc     int b[];
36*f4a2713aSLionel Sambuc };
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc extern int d1[sizeof(struct packed_fas) == 1 ? 1 : -1];
39*f4a2713aSLionel Sambuc extern int d2[__alignof(struct packed_fas) == 1 ? 1 : -1];
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc struct packed_after_fas {
42*f4a2713aSLionel Sambuc     char a;
43*f4a2713aSLionel Sambuc     int b[];
44*f4a2713aSLionel Sambuc } __attribute__((packed));
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc extern int d1_2[sizeof(struct packed_after_fas) == 1 ? 1 : -1];
47*f4a2713aSLionel Sambuc extern int d2_2[__alignof(struct packed_after_fas) == 1 ? 1 : -1];
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc // Alignment
50*f4a2713aSLionel Sambuc 
51*f4a2713aSLionel Sambuc struct __attribute__((aligned(8))) as1 {
52*f4a2713aSLionel Sambuc     char c;
53*f4a2713aSLionel Sambuc };
54*f4a2713aSLionel Sambuc 
55*f4a2713aSLionel Sambuc extern int e1[sizeof(struct as1) == 8 ? 1 : -1];
56*f4a2713aSLionel Sambuc extern int e2[__alignof(struct as1) == 8 ? 1 : -1];
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc // FIXME: Will need to force arch once max usable alignment isn't hard
59*f4a2713aSLionel Sambuc // coded.
60*f4a2713aSLionel Sambuc struct __attribute__((aligned)) as1_2 {
61*f4a2713aSLionel Sambuc     char c;
62*f4a2713aSLionel Sambuc };
63*f4a2713aSLionel Sambuc extern int e1_2[sizeof(struct as1_2) == 16 ? 1 : -1];
64*f4a2713aSLionel Sambuc extern int e2_2[__alignof(struct as1_2) == 16 ? 1 : -1];
65*f4a2713aSLionel Sambuc 
66*f4a2713aSLionel Sambuc struct as2 {
67*f4a2713aSLionel Sambuc     char c;
68*f4a2713aSLionel Sambuc     int __attribute__((aligned(8))) a;
69*f4a2713aSLionel Sambuc };
70*f4a2713aSLionel Sambuc 
71*f4a2713aSLionel Sambuc extern int f1[sizeof(struct as2) == 16 ? 1 : -1];
72*f4a2713aSLionel Sambuc extern int f2[__alignof(struct as2) == 8 ? 1 : -1];
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc struct __attribute__((packed)) as3 {
75*f4a2713aSLionel Sambuc     char c;
76*f4a2713aSLionel Sambuc     int a;
77*f4a2713aSLionel Sambuc     int __attribute__((aligned(8))) b;
78*f4a2713aSLionel Sambuc };
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc extern int g1[sizeof(struct as3) == 16 ? 1 : -1];
81*f4a2713aSLionel Sambuc extern int g2[__alignof(struct as3) == 8 ? 1 : -1];
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc 
84*f4a2713aSLionel Sambuc // rdar://5921025
85*f4a2713aSLionel Sambuc struct packedtest {
86*f4a2713aSLionel Sambuc   int ted_likes_cheese;
87*f4a2713aSLionel Sambuc   void *args[] __attribute__((packed));
88*f4a2713aSLionel Sambuc };
89*f4a2713aSLionel Sambuc 
90*f4a2713aSLionel Sambuc // Packed union
91*f4a2713aSLionel Sambuc union __attribute__((packed)) au4 {char c; int x;};
92*f4a2713aSLionel Sambuc extern int h1[sizeof(union au4) == 4 ? 1 : -1];
93*f4a2713aSLionel Sambuc extern int h2[__alignof(union au4) == 1 ? 1 : -1];
94*f4a2713aSLionel Sambuc 
95*f4a2713aSLionel Sambuc // Aligned union
96*f4a2713aSLionel Sambuc union au5 {__attribute__((aligned(4))) char c;};
97*f4a2713aSLionel Sambuc extern int h1[sizeof(union au5) == 4 ? 1 : -1];
98*f4a2713aSLionel Sambuc extern int h2[__alignof(union au5) == 4 ? 1 : -1];
99*f4a2713aSLionel Sambuc 
100*f4a2713aSLionel Sambuc // Alignment+packed
101*f4a2713aSLionel Sambuc struct as6 {char c; __attribute__((packed, aligned(2))) int x;};
102*f4a2713aSLionel Sambuc extern int i1[sizeof(struct as6) == 6 ? 1 : -1];
103*f4a2713aSLionel Sambuc extern int i2[__alignof(struct as6) == 2 ? 1 : -1];
104*f4a2713aSLionel Sambuc 
105*f4a2713aSLionel Sambuc union au6 {char c; __attribute__((packed, aligned(2))) int x;};
106*f4a2713aSLionel Sambuc extern int k1[sizeof(union au6) == 4 ? 1 : -1];
107*f4a2713aSLionel Sambuc extern int k2[__alignof(union au6) == 2 ? 1 : -1];
108*f4a2713aSLionel Sambuc 
109*f4a2713aSLionel Sambuc // Check postfix attributes
110*f4a2713aSLionel Sambuc union au7 {char c; int x;} __attribute__((packed));
111*f4a2713aSLionel Sambuc extern int l1[sizeof(union au7) == 4 ? 1 : -1];
112*f4a2713aSLionel Sambuc extern int l2[__alignof(union au7) == 1 ? 1 : -1];
113*f4a2713aSLionel Sambuc 
114*f4a2713aSLionel Sambuc struct packed_fas2 {
115*f4a2713aSLionel Sambuc     char a;
116*f4a2713aSLionel Sambuc     int b[];
117*f4a2713aSLionel Sambuc } __attribute__((packed));
118*f4a2713aSLionel Sambuc 
119*f4a2713aSLionel Sambuc extern int m1[sizeof(struct packed_fas2) == 1 ? 1 : -1];
120*f4a2713aSLionel Sambuc extern int m2[__alignof(struct packed_fas2) == 1 ? 1 : -1];
121*f4a2713aSLionel Sambuc 
122*f4a2713aSLionel Sambuc // Attribute aligned can round down typedefs.  PR9253
123*f4a2713aSLionel Sambuc typedef long long  __attribute__((aligned(1))) nt;
124*f4a2713aSLionel Sambuc 
125*f4a2713aSLionel Sambuc struct nS {
126*f4a2713aSLionel Sambuc   char buf_nr;
127*f4a2713aSLionel Sambuc   nt start_lba;
128*f4a2713aSLionel Sambuc };
129*f4a2713aSLionel Sambuc 
130*f4a2713aSLionel Sambuc extern int n1[sizeof(struct nS) == 9 ? 1 : -1];
131*f4a2713aSLionel Sambuc extern int n2[__alignof(struct nS) == 1 ? 1 : -1];
132*f4a2713aSLionel Sambuc 
133*f4a2713aSLionel Sambuc 
134*f4a2713aSLionel Sambuc 
135*f4a2713aSLionel Sambuc 
136