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