forked from cil-project/cil
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathbitfield4.c
More file actions
43 lines (33 loc) · 1.32 KB
/
bitfield4.c
File metadata and controls
43 lines (33 loc) · 1.32 KB
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
#include "testharness.h"
/* Test that integer promotions for bit-fields correctly account for the
bit-field width (c.f. ISO 6.3.1.1).
A bit-field whose values fit in int shall be promoted to int,
regardless of the declared base type. */
struct X {
long x : 7; /* signed 7-bit: range fits in int -> promote to int */
} sx;
struct UX {
unsigned long y : 7; /* unsigned 7-bit: range [0,127] fits in int -> promote to int */
} usx;
int main() {
/* _Generic selects based on type after integer promotion (ISO 6.5.1.1).
For long : 7 bit-field, the promoted type should be int. */
int r1 = _Generic(sx.x + 0, int : 1, default : -1);
if (r1 != 1) E(1);
/* Unary + also triggers integer promotion */
int r2 = _Generic(+sx.x, int : 1, default : -1);
if (r2 != 1) E(2);
/* Unary - also triggers integer promotion */
int r3 = _Generic(-sx.x, int : 1, default : -1);
if (r3 != 1) E(3);
/* Unary ~ also triggers integer promotion */
int r4 = _Generic(~sx.x, int : 1, default : -1);
if (r4 != 1) E(4);
/* unsigned long : 7 should also promote to int (7-bit range [0,127] fits) */
int r5 = _Generic(usx.y + 0, int : 1, default : -1);
if (r5 != 1) E(5);
/* Direct bit-field access as _Generic controlling expression */
int r6 = _Generic(sx.x, int : 1, default : -1);
if (r6 != 1) E(6);
SUCCESS;
}