Debian Codegraph @

clang/test/SemaCXX/ms_struct-bitfield-padding.cpp

raw
1
2 // RUN: %clang_cc1 -fsyntax-only -Wms-bitfield-padding -verify -triple armv8 -std=c++23 %s
3 // RUN: %clang_cc1 -fsyntax-only -DMS_BITFIELDS -fms-layout-compatibility=microsoft -verify=msbitfields -triple armv8-apple-macos10.15 -std=c++23 %s
4
5 // msbitfields-no-diagnostics
6
7 enum Enum1 { Enum1_A, Enum1_B };
8 enum Enum2 { Enum2_A, Enum2_B };
9
10 enum class EnumU32_1 : unsigned { A, B };
11 enum class EnumU32_2 : unsigned { A, B };
12 enum class EnumU64 : unsigned long long { A, B };
13 enum class EnumI32 : int { A, B };
14 enum class EnumU8 : unsigned char { A, B };
15 enum class EnumI8 : char { A, B };
16 enum class EnumU16 : unsigned short { A, B };
17 enum class EnumI16 : short { A, B };
18
19 struct A {
20 unsigned int a : 15;
21 unsigned int b : 15;
22 };
23 static_assert(sizeof(A) == 4);
24
25 struct B {
26 unsigned int a : 15;
27 int b : 15;
28 };
29 static_assert(sizeof(B) == 4);
30
31 struct C {
32 unsigned int a : 15;
33 int b : 15;
34 };
35 static_assert(sizeof(C) == 4);
36
37 struct D {
38 Enum1 a : 15;
39 Enum1 b : 15;
40 };
41 static_assert(sizeof(D) == 4);
42
43 struct E {
44 Enum1 a : 15;
45 Enum2 b : 15;
46 };
47 static_assert(sizeof(E) == 4);
48
49 struct F {
50 EnumU32_1 a : 15;
51 EnumU32_2 b : 15;
52 };
53 static_assert(sizeof(F) == 4);
54
55 struct G {
56 EnumU32_1 a : 15;
57 EnumU64 b : 15;
58 // expected-warning@-1 {{bit-field 'b' of type 'EnumU64' has a different storage size than the preceding bit-field (8 vs 4 bytes) and will not be packed under the Microsoft ABI}}
59 // expected-note@-3 {{preceding bit-field 'a' declared here with type 'EnumU32_1'}}
60 };
61
62 #ifdef MS_BITFIELDS
63 static_assert(sizeof(G) == 16);
64 #else
65 static_assert(sizeof(G) == 8);
66 #endif
67
68 struct H {
69 EnumU32_1 a : 10;
70 EnumI32 b : 10;
71 EnumU32_1 c : 10;
72 };
73 static_assert(sizeof(H) == 4);
74
75 struct I {
76 EnumU8 a : 3;
77 EnumI8 b : 5;
78 EnumU32_1 c : 10;
79 // expected-warning@-1 {{bit-field 'c' of type 'EnumU32_1' has a different storage size than the preceding bit-field (4 vs 1 bytes) and will not be packed under the Microsoft ABI}}
80 // expected-note@-3 {{preceding bit-field 'b' declared here with type 'EnumI8'}}
81 };
82 #ifdef MS_BITFIELDS
83 static_assert(sizeof(I) == 8);
84 #else
85 static_assert(sizeof(I) == 4);
86 #endif
87
88 struct J {
89 EnumU8 : 0;
90 EnumU8 b : 4;
91 };
92 static_assert(sizeof(J) == 1);
93
94 struct K {
95 EnumU8 a : 4;
96 EnumU8 : 0;
97 };
98 static_assert(sizeof(K) == 1);
99
100 struct L {
101 EnumU32_1 a : 10;
102 EnumU32_2 b : 10;
103 EnumU32_1 c : 10;
104 };
105
106 static_assert(sizeof(L) == 4);
107
108 struct M {
109 EnumU32_1 a : 10;
110 EnumI32 b : 10;
111 EnumU32_1 c : 10;
112 };
113
114 static_assert(sizeof(M) == 4);
115
116 struct N {
117 EnumU32_1 a : 10;
118 EnumU64 b : 10;
119 // expected-warning@-1 {{bit-field 'b' of type 'EnumU64' has a different storage size than the preceding bit-field (8 vs 4 bytes) and will not be packed under the Microsoft ABI}}
120 // expected-note@-3 {{preceding bit-field 'a' declared here with type 'EnumU32_1'}}
121 EnumU32_1 c : 10;
122 // expected-warning@-1 {{bit-field 'c' of type 'EnumU32_1' has a different storage size than the preceding bit-field (4 vs 8 bytes) and will not be packed under the Microsoft ABI}}
123 // expected-note@-5 {{preceding bit-field 'b' declared here with type 'EnumU64'}}
124 };
125
126 #ifdef MS_BITFIELDS
127 static_assert(sizeof(N) == 24);
128 #else
129 static_assert(sizeof(N) == 8);
130 #endif
131
132 struct O {
133 EnumU16 a : 10;
134 EnumU32_1 b : 10;
135 // expected-warning@-1 {{bit-field 'b' of type 'EnumU32_1' has a different storage size than the preceding bit-field (4 vs 2 bytes) and will not be packed under the Microsoft ABI}}
136 // expected-note@-3 {{preceding bit-field 'a' declared here with type 'EnumU16'}}
137 };
138 #ifdef MS_BITFIELDS
139 static_assert(sizeof(O) == 8);
140 #else
141 static_assert(sizeof(O) == 4);
142 #endif
143
144 struct P {
145 EnumU32_1 a : 10;
146 EnumU16 b : 10;
147 // expected-warning@-1 {{bit-field 'b' of type 'EnumU16' has a different storage size than the preceding bit-field (2 vs 4 bytes) and will not be packed under the Microsoft ABI}}
148 // expected-note@-3 {{preceding bit-field 'a' declared here with type 'EnumU32_1'}}
149 };
150 #ifdef MS_BITFIELDS
151 static_assert(sizeof(P) == 8);
152 #else
153 static_assert(sizeof(P) == 4);
154 #endif
155
156 struct Q {
157 EnumU8 a : 6;
158 EnumU16 b : 6;
159 // expected-warning@-1 {{bit-field 'b' of type 'EnumU16' has a different storage size than the preceding bit-field (2 vs 1 bytes) and will not be packed under the Microsoft ABI}}
160 // expected-note@-3 {{preceding bit-field 'a' declared here with type 'EnumU8'}}
161 };
162 #ifdef MS_BITFIELDS
163 static_assert(sizeof(Q) == 4);
164 #else
165 static_assert(sizeof(Q) == 2);
166 #endif
167
168 struct R {
169 EnumU16 a : 9;
170 EnumU16 b : 9;
171 EnumU8 c : 6;
172 // expected-warning@-1 {{bit-field 'c' of type 'EnumU8' has a different storage size than the preceding bit-field (1 vs 2 bytes) and will not be packed under the Microsoft ABI}}
173 // expected-note@-3 {{preceding bit-field 'b' declared here with type 'EnumU16'}}
174 };
175
176 #ifdef MS_BITFIELDS
177 static_assert(sizeof(R) == 6);
178 #else
179 static_assert(sizeof(R) == 4);
180 #endif
181
182 struct S {
183 char a : 4;
184 char b : 4;
185 char c : 4;
186 char d : 4;
187 short x : 7;
188 // expected-warning@-1 {{bit-field 'x' of type 'short' has a different storage size than the preceding bit-field (2 vs 1 bytes) and will not be packed under the Microsoft ABI}}
189 // expected-note@-3 {{preceding bit-field 'd' declared here with type 'char'}}
190 // This is a false positive. Reporting this correctly requires duplicating the record layout process
191 // in target and MS layout modes, and it's also unclear if that's the correct choice for users of
192 // this diagnostic.
193 short y : 9;
194 };
195
196 static_assert(sizeof(S) == 4);