• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 %s -fsyntax-only -verify
2 // RUN: %clang_cc1 %s -fsyntax-only -triple=x86_64-windows-coff -verify
3 
4 // Packed structs.
5 struct s {
6     char a;
7     int b  __attribute__((packed));
8     char c;
9     int d;
10 };
11 
12 extern int a1[sizeof(struct s) == 12 ? 1 : -1];
13 extern int a2[__alignof(struct s) == 4 ? 1 : -1];
14 
15 struct __attribute__((packed)) packed_s {
16     char a;
17     int b  __attribute__((packed));
18     char c;
19     int d;
20 };
21 
22 extern int b1[sizeof(struct packed_s) == 10 ? 1 : -1];
23 extern int b2[__alignof(struct packed_s) == 1 ? 1 : -1];
24 
25 struct fas {
26     char a;
27     int b[];
28 };
29 
30 extern int c1[sizeof(struct fas) == 4 ? 1 : -1];
31 extern int c2[__alignof(struct fas) == 4 ? 1 : -1];
32 
33 struct __attribute__((packed)) packed_fas {
34     char a;
35     int b[];
36 };
37 
38 extern int d1[sizeof(struct packed_fas) == 1 ? 1 : -1];
39 extern int d2[__alignof(struct packed_fas) == 1 ? 1 : -1];
40 
41 struct packed_after_fas {
42     char a;
43     int b[];
44 } __attribute__((packed));
45 
46 extern int d1_2[sizeof(struct packed_after_fas) == 1 ? 1 : -1];
47 extern int d2_2[__alignof(struct packed_after_fas) == 1 ? 1 : -1];
48 
49 // Alignment
50 
51 struct __attribute__((aligned(8))) as1 {
52     char c;
53 };
54 
55 extern int e1[sizeof(struct as1) == 8 ? 1 : -1];
56 extern int e2[__alignof(struct as1) == 8 ? 1 : -1];
57 
58 struct __attribute__((aligned)) as1_2 {
59     char c;
60 };
61 #ifdef __s390x__
62 extern int e1_2[sizeof(struct as1_2) == 8 ? 1 : -1];
63 extern int e2_2[__alignof(struct as1_2) == 8 ? 1 : -1];
64 #else
65 extern int e1_2[sizeof(struct as1_2) == 16 ? 1 : -1];
66 extern int e2_2[__alignof(struct as1_2) == 16 ? 1 : -1];
67 #endif
68 
69 struct as2 {
70     char c;
71     int __attribute__((aligned(8))) a;
72 };
73 
74 extern int f1[sizeof(struct as2) == 16 ? 1 : -1];
75 extern int f2[__alignof(struct as2) == 8 ? 1 : -1];
76 
77 struct __attribute__((packed)) as3 {
78     char c;
79     int a;
80     int __attribute__((aligned(8))) b;
81 };
82 
83 extern int g1[sizeof(struct as3) == 16 ? 1 : -1];
84 extern int g2[__alignof(struct as3) == 8 ? 1 : -1];
85 
86 
87 // rdar://5921025
88 struct packedtest {
89   int ted_likes_cheese;
90   void *args[] __attribute__((packed));
91 };
92 
93 // Packed union
94 union __attribute__((packed)) au4 {char c; int x;};
95 extern int h1[sizeof(union au4) == 4 ? 1 : -1];
96 extern int h2[__alignof(union au4) == 1 ? 1 : -1];
97 
98 // Aligned union
99 union au5 {__attribute__((aligned(4))) char c;};
100 extern int h1[sizeof(union au5) == 4 ? 1 : -1];
101 extern int h2[__alignof(union au5) == 4 ? 1 : -1];
102 
103 // Alignment+packed
104 struct as6 {char c; __attribute__((packed, aligned(2))) int x;};
105 extern int i1[sizeof(struct as6) == 6 ? 1 : -1];
106 extern int i2[__alignof(struct as6) == 2 ? 1 : -1];
107 
108 union au6 {char c; __attribute__((packed, aligned(2))) int x;};
109 extern int k1[sizeof(union au6) == 4 ? 1 : -1];
110 extern int k2[__alignof(union au6) == 2 ? 1 : -1];
111 
112 // Check postfix attributes
113 union au7 {char c; int x;} __attribute__((packed));
114 extern int l1[sizeof(union au7) == 4 ? 1 : -1];
115 extern int l2[__alignof(union au7) == 1 ? 1 : -1];
116 
117 struct packed_fas2 {
118     char a;
119     int b[];
120 } __attribute__((packed));
121 
122 extern int m1[sizeof(struct packed_fas2) == 1 ? 1 : -1];
123 extern int m2[__alignof(struct packed_fas2) == 1 ? 1 : -1];
124 
125 // Attribute aligned can round down typedefs.  PR9253
126 typedef long long  __attribute__((aligned(1))) nt;
127 
128 struct nS {
129   char buf_nr;
130   nt start_lba;
131 };
132 
133 #if defined(_WIN32) && !defined(__declspec) // _MSC_VER is unavailable in cc1.
134 // Alignment doesn't affect packing in MS mode.
135 extern int n1[sizeof(struct nS) == 16 ? 1 : -1];
136 extern int n2[__alignof(struct nS) == 8 ? 1 : -1];
137 #else
138 extern int n1[sizeof(struct nS) == 9 ? 1 : -1];
139 extern int n2[__alignof(struct nS) == 1 ? 1 : -1];
140 #endif
141 
142 // Packed attribute shouldn't be ignored for bit-field of char types.
143 // Note from GCC reference manual: The 4.1, 4.2 and 4.3 series of GCC ignore
144 // the packed attribute on bit-fields of type char. This has been fixed in
145 // GCC 4.4 but the change can lead to differences in the structure layout.
146 // See the documentation of -Wpacked-bitfield-compat for more information.
147 struct packed_chars {
148   char a:4;
149   char b:8 __attribute__ ((packed));
150   // expected-warning@-1 {{'packed' attribute was ignored on bit-fields with single-byte alignment in older versions of GCC and Clang}}
151   char c:4;
152 };
153 
154 #if defined(_WIN32) && !defined(__declspec) // _MSC_VER is unavailable in cc1.
155 // On Windows clang uses MSVC compatible layout in this case.
156 extern int o1[sizeof(struct packed_chars) == 3 ? 1 : -1];
157 extern int o2[__alignof(struct packed_chars) == 1 ? 1 : -1];
158 #else
159 extern int o1[sizeof(struct packed_chars) == 2 ? 1 : -1];
160 extern int o2[__alignof(struct packed_chars) == 1 ? 1 : -1];
161 #endif
162