File: bitfield-preferred-type-sizing.cpp

package info (click to toggle)
llvm-toolchain-21 1%3A21.1.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,245,028 kB
  • sloc: cpp: 7,619,726; ansic: 1,434,018; asm: 1,058,748; python: 252,740; f90: 94,671; objc: 70,685; lisp: 42,813; pascal: 18,401; sh: 8,601; ml: 5,111; perl: 4,720; makefile: 3,675; awk: 3,523; javascript: 2,409; xml: 892; fortran: 770
file content (411 lines) | stat: -rw-r--r-- 17,744 bytes parent folder | download | duplicates (3)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// RUN: %clang_cc1 %s      -std=c++23 -triple=x86_64-apple-darwin10 -fsyntax-only -verify=expected,bitfieldwarnings,cpp -Wno-unused-value -Wno-unused-but-set-variable -Wbitfield-width -Wbitfield-enum-conversion
// RUN: %clang_cc1 %s      -std=c++23 -triple=x86_64-apple-darwin10 -fsyntax-only -verify=expected,cpp -Wno-unused-value -Wno-unused-but-set-variable

// RUN: %clang_cc1 %s -x c -std=c23   -triple=x86_64-apple-darwin10 -fsyntax-only -verify=expected,c -Wno-unused-value -Wno-unused-but-set-variable
// RUN: %clang_cc1 %s -x c -std=c23   -triple=x86_64-apple-darwin10 -fsyntax-only -verify=expected,bitfieldwarnings,c -Wno-unused-value -Wno-unused-but-set-variable -Wbitfield-width -Wbitfield-enum-conversion


typedef enum A {
  A_a,
  A_b,
  A_c,
  A_d
} A;

#ifdef __cplusplus
#define DEFINE_ENUM(_Name, _Type, ...) enum class _Name : _Type { __VA_ARGS__ } ;
#define ENUM_CLASS_REF(_Name, _Enum) _Name::_Enum
#else
#define DEFINE_ENUM(_Name, _Type, ...) typedef enum _Name : _Type { __VA_ARGS__ } _Name;
#define ENUM_CLASS_REF(_Name, _Enum) _Enum
#endif

DEFINE_ENUM(B, int, B_a, B_b, B_c, B_d );
DEFINE_ENUM(C, unsigned, C_a, C_b, C_c, C_d );
DEFINE_ENUM(D, unsigned, D_a, D_b);

// Not using templates here so we can more easily distinguish the responsible
// party for each warning

typedef struct S_A {
  A field1 : 1; // #S_A_field1
  A field2 : 2; // #S_A_field2
  A field3 : 8; // #S_A_field3
  __attribute__((preferred_type(A))) // #preferred_S_A_field4
  unsigned field4 : 1; // #S_A_field4
  __attribute__((preferred_type(A)))
  unsigned field5 : 2; // #S_A_field5
  __attribute__((preferred_type(A)))
  unsigned field6 : 8; // #S_A_field6
  __attribute__((preferred_type(A))) // #preferred_S_A_field7
  int field7 : 1; // #S_A_field7
  __attribute__((preferred_type(A))) // #preferred_S_A_field8
  int field8 : 2; // #S_A_field8
  __attribute__((preferred_type(A)))
  int field9 : 8; // #S_A_field9
  __attribute__((preferred_type(A)))
  D field10 : 1; // #S_A_field10
  __attribute__((preferred_type(A))) // #preferred_S_A_field11
  D field11 : 2; // #S_A_field11
  __attribute__((preferred_type(A)))
  D field12 : 8; // #S_A_field12
} S_A;

typedef struct S_B {
  B field1 : 1; // #S_B_field1
  B field2 : 2; // #S_B_field2
  B field3 : 8; // #S_B_field3
  __attribute__((preferred_type(B))) // #preferred_S_B_field4
  unsigned field4 : 1; // #S_B_field4
  __attribute__((preferred_type(B)))
  unsigned field5 : 2; // #S_B_field5
  __attribute__((preferred_type(B)))
  unsigned field6 : 8; // #S_B_field6
  __attribute__((preferred_type(B))) // #preferred_S_B_field7
  int field7 : 1; // #S_B_field7
  __attribute__((preferred_type(B))) // #preferred_S_B_field8
  int field8 : 2; // #S_B_field8
  __attribute__((preferred_type(B)))
  int field9 : 8; // #S_B_field9
  __attribute__((preferred_type(B)))
  D field10 : 1; // #S_B_field10
  __attribute__((preferred_type(B))) // #preferred_S_B_field11
  D field11 : 2; // #S_B_field11
  __attribute__((preferred_type(B)))
  D field12 : 8; // #S_B_field12
} S_B;

typedef struct S_C {
  C field1 : 1; // #S_C_field1
  C field2 : 2; // #S_C_field2
  C field3 : 8; // #S_C_field3
  __attribute__((preferred_type(C))) // #preferred_S_C_field4
  unsigned field4 : 1; // #S_C_field4
  __attribute__((preferred_type(C)))
  unsigned field5 : 2; // #S_C_field5
  __attribute__((preferred_type(C)))
  unsigned field6 : 8; // #S_C_field6
  __attribute__((preferred_type(C))) // #preferred_S_C_field7
  int field7 : 1; // #S_C_field7
  __attribute__((preferred_type(C))) // #preferred_S_C_field8
  int field8 : 2; // #S_C_field8
  __attribute__((preferred_type(C)))
  int field9 : 8; // #S_C_field9
  __attribute__((preferred_type(C)))
  D field10 : 1; // #S_C_field10
  __attribute__((preferred_type(C))) // #preferred_S_C_field11
  D field11 : 2; // #S_C_field11
  __attribute__((preferred_type(C)))
  D field12 : 8; // #S_C_field12
} S_C;

void read_enumA(S_A *s) {
  A x;
  x = s->field1;
  x = s->field2;
  x = s->field3;
  x = (A)s->field4;
  x = (A)s->field5;
  x = (A)s->field6;
  x = (A)s->field7;
  x = (A)s->field8;
  x = (A)s->field9;
  x = (A)s->field10;
  x = (A)s->field11;
  x = (A)s->field12;
}

void read_enumB(S_B *s) {
  B x;
  x = s->field1;
  x = s->field2;
  x = s->field3;
  x = (B)s->field4;
  x = (B)s->field5;
  x = (B)s->field6;
  x = (B)s->field7;
  x = (B)s->field8;
  x = (B)s->field9;
  x = (B)s->field10;
  x = (B)s->field11;
  x = (B)s->field12;
}

void read_enumC(S_C *s) {
  C x;
  x = s->field1;
  x = s->field2;
  x = s->field3;
  x = (C)s->field4;
  x = (C)s->field5;
  x = (C)s->field6;
  x = (C)s->field7;
  x = (C)s->field8;
  x = (C)s->field9;
  x = (C)s->field10;
  x = (C)s->field11;
  x = (C)s->field12;
}

void write_enumA(S_A *s, A x) {
  s->field1 = x;
  // bitfieldwarnings-warning@-1 {{bit-field 'field1' is not wide enough to store all enumerators of 'A'}}
  // bitfieldwarnings-note@#S_A_field1 {{widen this field to 2 bits to store all values of 'A'}}
  s->field2 = x;
  s->field3 = x;
  s->field4 = x;
  // bitfieldwarnings-warning@-1 {{bit-field 'field4' is not wide enough to store all enumerators of 'A'}}
  // bitfieldwarnings-note@#S_A_field4 {{widen this field to 2 bits to store all values of 'A'}}
  s->field5 = x;
  s->field6 = x;
  s->field7 = x;
  // bitfieldwarnings-warning@-1 {{bit-field 'field7' is not wide enough to store all enumerators of 'A'}}
  // bitfieldwarnings-note@#S_A_field7 {{widen this field to 2 bits to store all values of 'A'}}
  s->field8 = x;
  // bitfieldwarnings-warning@-1 {{signed bit-field 'field8' needs an extra bit to represent the largest positive enumerators of 'A'}}
  // bitfieldwarnings-note@#S_A_field8 {{consider making the bit-field type unsigned}}
  s->field9 = x;
  s->field10 = (D)x;
  s->field11 = (D)x;
  s->field12 = (D)x;
}

void write_enumB(S_B *s, B x) {
  s->field1 = x;
  // bitfieldwarnings-warning@-1 {{bit-field 'field1' is not wide enough to store all enumerators of 'B'}}
  // bitfieldwarnings-note@#S_B_field1 {{widen this field to 2 bits to store all values of 'B'}}
  s->field2 = x;
  // bitfieldwarnings-warning@-1 {{signed bit-field 'field2' needs an extra bit to represent the largest positive enumerators of 'B'}}
  // bitfieldwarnings-note@#S_B_field2 {{consider making the bit-field type unsigned}}
  s->field3 = x;
  s->field4 = (unsigned)x;
  // expected-warning@-1 {{bit-field 'field4' is not wide enough to store all enumerators of preferred type 'B'}}
  // expected-note@#S_B_field4 {{widen this field to 2 bits to store all values of 'B'}}
  // expected-note@#preferred_S_B_field4 {{preferred type for bit-field 'B' specified here}}
  s->field5 = (unsigned)x;
  s->field6 = (unsigned)x;
  s->field7 = (int)x;
  // expected-warning@-1 {{bit-field 'field7' is not wide enough to store all enumerators of preferred type 'B'}}
  // expected-note@#S_B_field7 {{widen this field to 2 bits to store all values of 'B'}}
  // expected-note@#preferred_S_B_field7 {{preferred type for bit-field 'B' specified here}}
  s->field8 = (int)x;
  // expected-warning@-1 {{signed bit-field 'field8' needs an extra bit to represent the largest positive enumerators of preferred type 'B'}}
  // expected-note@#S_B_field8 {{consider making the bit-field type unsigned}}
  // expected-note@#preferred_S_B_field8 {{preferred type for bit-field 'B' specified here}}
  s->field9 = (int)x;
  s->field10 = (D)x;
  s->field11 = (D)x;
  s->field12 = (D)x;
}

void write_enumC(S_C *s, C x) {
  s->field1 = x;
  // bitfieldwarnings-warning@-1 {{bit-field 'field1' is not wide enough to store all enumerators of 'C'}}
  // bitfieldwarnings-note@#S_C_field1 {{widen this field to 2 bits to store all values of 'C'}}
  s->field2 = x;
  s->field3 = x;
  s->field4 = (unsigned)x;
  // expected-warning@-1 {{bit-field 'field4' is not wide enough to store all enumerators of preferred type 'C'}}
  // expected-note@#S_C_field4 {{widen this field to 2 bits to store all values of 'C'}}
  // expected-note@#preferred_S_C_field4 {{preferred type for bit-field 'C' specified here}}
  s->field5 = (unsigned)x;
  s->field6 = (unsigned)x;
  s->field7 = (int)x;
  // expected-warning@-1 {{bit-field 'field7' is not wide enough to store all enumerators of preferred type 'C'}}
  // expected-note@#S_C_field7 {{widen this field to 2 bits to store all values of 'C'}}
  // expected-note@#preferred_S_C_field7 {{preferred type for bit-field 'C' specified here}}
  s->field8 = (int)x;
  // expected-warning@-1 {{signed bit-field 'field8' needs an extra bit to represent the largest positive enumerators of preferred type 'C'}}
  // expected-note@#S_C_field8 {{consider making the bit-field type unsigned}}
  // expected-note@#preferred_S_C_field8 {{preferred type for bit-field 'C' specified here}}
  s->field9 = (int)x;
  s->field10 = (D)x;
  s->field11 = (D)x;
  s->field12 = (D)x;
}

void write_enum_intA(struct S_A *s, int x) {
  s->field1 = (A)x;
  // bitfieldwarnings-warning@-1 {{bit-field 'field1' is not wide enough to store all enumerators of 'A'}}
  // bitfieldwarnings-note@#S_A_field1 {{widen this field to 2 bits to store all values of 'A'}}
  s->field2 = (A)x;
  s->field3 = (A)x;
  s->field4 = x;
  // expected-warning@-1 {{bit-field 'field4' is not wide enough to store all enumerators of preferred type 'A'}}
  // expected-note@#S_A_field4 {{widen this field to 2 bits to store all values of 'A'}}
  // expected-note@#preferred_S_A_field4 {{preferred type for bit-field 'A' specified here}}
  s->field5 = x;
  s->field6 = x;
  s->field7 = x;
  // expected-warning@-1 {{bit-field 'field7' is not wide enough to store all enumerators of preferred type 'A'}}
  // expected-note@#S_A_field7 {{widen this field to 2 bits to store all values of 'A'}}
  // expected-note@#preferred_S_A_field7 {{preferred type for bit-field 'A' specified here}}
  s->field8 = x;
  // expected-warning@-1 {{signed bit-field 'field8' needs an extra bit to represent the largest positive enumerators of preferred type 'A'}}
  // expected-note@#S_A_field8 {{consider making the bit-field type unsigned}}
  // expected-note@#preferred_S_A_field8 {{preferred type for bit-field 'A' specified here}}
  s->field9 = x;
  s->field10 = (D)x;
  s->field11 = (D)x;
  s->field12 = (D)x;
}

void write_enum_intB(struct S_B *s, int x) {
  s->field1 = (B)x;
  // bitfieldwarnings-warning@-1 {{bit-field 'field1' is not wide enough to store all enumerators of 'B'}}
  // bitfieldwarnings-note@#S_B_field1 {{widen this field to 2 bits to store all values of 'B'}}
  s->field2 = (B)x;
  // bitfieldwarnings-warning@-1 {{signed bit-field 'field2' needs an extra bit to represent the largest positive enumerators of 'B'}}
  // bitfieldwarnings-note@#S_B_field2 {{consider making the bit-field type unsigned}}
  s->field3 = (B)x;
  s->field4 = x;
  // expected-warning@-1 {{bit-field 'field4' is not wide enough to store all enumerators of preferred type 'B'}}
  // expected-note@#S_B_field4 {{widen this field to 2 bits to store all values of 'B'}}
  // expected-note@#preferred_S_B_field4 {{preferred type for bit-field 'B' specified here}}
  s->field5 = x;
  s->field6 = x;
  s->field7 = x;
  // expected-warning@-1 {{bit-field 'field7' is not wide enough to store all enumerators of preferred type 'B'}}
  // expected-note@#S_B_field7 {{widen this field to 2 bits to store all values of 'B'}}
  // expected-note@#preferred_S_B_field7 {{preferred type for bit-field 'B' specified here}}
  s->field8 = x;
  // expected-warning@-1 {{signed bit-field 'field8' needs an extra bit to represent the largest positive enumerators of preferred type 'B'}}
  // expected-note@#S_B_field8 {{consider making the bit-field type unsigned}}
  // expected-note@#preferred_S_B_field8 {{preferred type for bit-field 'B' specified here}}
  s->field9 = x;
  s->field10 = (D)x;
  s->field11 = (D)x;
  s->field12 = (D)x;
}

void write_enum_intC(struct S_C *s, int x) {
  s->field1 = (C)x;
  // bitfieldwarnings-warning@-1 {{bit-field 'field1' is not wide enough to store all enumerators of 'C'}}
  // bitfieldwarnings-note@#S_C_field1 {{widen this field to 2 bits to store all values of 'C'}}
  s->field2 = (C)x;
  s->field3 = (C)x;
  s->field4 = x;
  // expected-warning@-1 {{bit-field 'field4' is not wide enough to store all enumerators of preferred type 'C'}}
  // expected-note@#S_C_field4 {{widen this field to 2 bits to store all values of 'C'}}
  // expected-note@#preferred_S_C_field4 {{preferred type for bit-field 'C' specified here}}
  s->field5 = x;
  s->field6 = x;
  s->field7 = x;
  // expected-warning@-1 {{bit-field 'field7' is not wide enough to store all enumerators of preferred type 'C'}}
  // expected-note@#S_C_field7 {{widen this field to 2 bits to store all values of 'C'}}
  // expected-note@#preferred_S_C_field7 {{preferred type for bit-field 'C' specified here}}
  s->field8 = x;
  // expected-warning@-1 {{signed bit-field 'field8' needs an extra bit to represent the largest positive enumerators of preferred type 'C'}}
  // expected-note@#S_C_field8 {{consider making the bit-field type unsigned}}
  // expected-note@#preferred_S_C_field8 {{preferred type for bit-field 'C' specified here}}
  s->field9 = x;
  s->field10 = (D)x;
  s->field11 = (D)x;
  s->field12 = (D)x;
}

void write_low_constantA(S_A *s) {
  s->field1 = A_a;
  s->field2 = A_a;
  s->field3 = A_a;
  s->field4 = A_a;
  s->field5 = A_a;
  s->field6 = A_a;
  s->field7 = A_a;
  s->field8 = A_a;
  s->field9 = A_a;
  s->field10 = (D)A_a;
  s->field11 = (D)A_a;
  s->field12 = (D)A_a;
};

void write_low_constantB(S_B *s) {
  s->field1 = ENUM_CLASS_REF(B, B_a);
  s->field2 = ENUM_CLASS_REF(B, B_a);
  s->field3 = ENUM_CLASS_REF(B, B_a);
  s->field4 = (unsigned)ENUM_CLASS_REF(B, B_a);
  s->field5 = (unsigned)ENUM_CLASS_REF(B, B_a);
  s->field6 = (unsigned)ENUM_CLASS_REF(B, B_a);
  s->field7 = (int)ENUM_CLASS_REF(B, B_a);
  s->field8 = (int)ENUM_CLASS_REF(B, B_a);
  s->field9 = (int)ENUM_CLASS_REF(B, B_a);
  s->field10 = (D)ENUM_CLASS_REF(B, B_a);
  s->field11 = (D)ENUM_CLASS_REF(B, B_a);
  s->field12 = (D)ENUM_CLASS_REF(B, B_a);
};

void write_low_constantC(S_C *s) {
  s->field1 = ENUM_CLASS_REF(C, C_a);
  s->field2 = ENUM_CLASS_REF(C, C_a);
  s->field3 = ENUM_CLASS_REF(C, C_a);
  s->field4 = (unsigned)ENUM_CLASS_REF(C, C_a);
  s->field5 = (unsigned)ENUM_CLASS_REF(C, C_a);
  s->field6 = (unsigned)ENUM_CLASS_REF(C, C_a);
  s->field7 = (int)ENUM_CLASS_REF(C, C_a);
  s->field8 = (int)ENUM_CLASS_REF(C, C_a);
  s->field9 = (int)ENUM_CLASS_REF(C, C_a);
  s->field10 = (D)ENUM_CLASS_REF(C, C_a);
  s->field11 = (D)ENUM_CLASS_REF(C, C_a);
  s->field12 = (D)ENUM_CLASS_REF(C, C_a);
};

void write_high_constantA(S_A *s) {
  s->field1 = A_d;
  // cpp-warning@-1 {{implicit truncation from 'A' to bit-field changes value from 3 to 1}}
  // c-warning@-2 {{implicit truncation from 'int' to bit-field changes value from 3 to 1}}
  s->field2 = A_d;
  s->field3 = A_d;
  s->field4 = A_d;
  // cpp-warning@-1 {{implicit truncation from 'A' to bit-field changes value from 3 to 1}}
  // c-warning@-2 {{implicit truncation from 'int' to bit-field changes value from 3 to 1}}
  s->field5 = A_d;
  s->field6 = A_d;
  s->field7 = A_d;
  // cpp-warning@-1 {{implicit truncation from 'A' to bit-field changes value from 3 to -1}}
  // c-warning@-2 {{implicit truncation from 'int' to bit-field changes value from 3 to -1}}
  s->field8 = A_d;
  // cpp-warning@-1 {{implicit truncation from 'A' to bit-field changes value from 3 to -1}}
  // c-warning@-2 {{implicit truncation from 'int' to bit-field changes value from 3 to -1}}
  s->field9 = A_d;
  s->field10 = (D)A_d;
  // cpp-warning@-1 {{implicit truncation from 'D' to bit-field changes value from 3 to 1}}
  // c-warning@-2 {{implicit truncation from 'D' (aka 'enum D') to bit-field changes value from 3 to 1}}
  s->field11 = (D)A_d;
  s->field12 = (D)A_d;
};

void write_high_constantB(S_B *s) {
  s->field1 = ENUM_CLASS_REF(B, B_d);
  // cpp-warning@-1 {{implicit truncation from 'B' to bit-field changes value from 3 to 1}}
  // c-warning@-2 {{implicit truncation from 'int' to bit-field changes value from 3 to -1}}
  s->field2 = ENUM_CLASS_REF(B, B_d);
  // c-warning@-1 {{implicit truncation from 'int' to bit-field changes value from 3 to -1}}
  s->field3 = ENUM_CLASS_REF(B, B_d);
  s->field4 = (unsigned)ENUM_CLASS_REF(B, B_d);
  // expected-warning@-1 {{implicit truncation from 'unsigned int' to bit-field changes value from 3 to 1}}
  s->field5 = (unsigned)ENUM_CLASS_REF(B, B_d);
  s->field6 = (unsigned)ENUM_CLASS_REF(B, B_d);
  s->field7 = (int)ENUM_CLASS_REF(B, B_d);
  // expected-warning@-1 {{implicit truncation from 'int' to bit-field changes value from 3 to -1}}
  s->field8 = (int)ENUM_CLASS_REF(B, B_d);
  // expected-warning@-1 {{implicit truncation from 'int' to bit-field changes value from 3 to -1}}
  s->field9 = (int)ENUM_CLASS_REF(B, B_d);
};


void write_high_constantC(S_C *s) {
  s->field1 = ENUM_CLASS_REF(C, C_d);
  // cpp-warning@-1 {{implicit truncation from 'C' to bit-field changes value from 3 to 1}}
  // c-warning@-2 {{implicit truncation from 'unsigned int' to bit-field changes value from 3 to 1}}
  s->field2 = ENUM_CLASS_REF(C, C_d);
  s->field3 = ENUM_CLASS_REF(C, C_d);
  s->field4 = (unsigned)ENUM_CLASS_REF(C, C_d);
  // expected-warning@-1 {{implicit truncation from 'unsigned int' to bit-field changes value from 3 to 1}}
  s->field5 = (unsigned)ENUM_CLASS_REF(C, C_d);
  s->field6 = (unsigned)ENUM_CLASS_REF(C, C_d);
  s->field7 = (int)ENUM_CLASS_REF(C, C_d);
  // expected-warning@-1 {{implicit truncation from 'int' to bit-field changes value from 3 to -1}}
  s->field8 = (int)ENUM_CLASS_REF(C, C_d);
  // expected-warning@-1 {{implicit truncation from 'int' to bit-field changes value from 3 to -1}}
  s->field9 = (int)ENUM_CLASS_REF(C, C_d);
};