xref: /aosp_15_r20/external/clang/test/Sema/bitfield.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 %s -fsyntax-only -verify -std=c11 -Wno-unused-value
2*67e74705SXin Li 
3*67e74705SXin Li enum e0; // expected-note{{forward declaration of 'enum e0'}}
4*67e74705SXin Li 
5*67e74705SXin Li struct a {
6*67e74705SXin Li   int a : -1; // expected-error{{bit-field 'a' has negative width}}
7*67e74705SXin Li 
8*67e74705SXin Li   // rdar://6081627
9*67e74705SXin Li   int b : 33; // expected-error{{width of bit-field 'b' (33 bits) exceeds width of its type (32 bits)}}
10*67e74705SXin Li 
11*67e74705SXin Li   int c : (1 + 0.25); // expected-error{{expression is not an integer constant expression}}
12*67e74705SXin Li   int d : (int)(1 + 0.25);
13*67e74705SXin Li 
14*67e74705SXin Li   // rdar://6138816
15*67e74705SXin Li   int e : 0;  // expected-error {{bit-field 'e' has zero width}}
16*67e74705SXin Li 
17*67e74705SXin Li   float xx : 4;  // expected-error {{bit-field 'xx' has non-integral type}}
18*67e74705SXin Li 
19*67e74705SXin Li   // PR3607
20*67e74705SXin Li   enum e0 f : 1; // expected-error {{field has incomplete type 'enum e0'}}
21*67e74705SXin Li 
22*67e74705SXin Li   int g : (_Bool)1;
23*67e74705SXin Li 
24*67e74705SXin Li   // PR4017
25*67e74705SXin Li   char : 10;      // expected-error {{width of anonymous bit-field (10 bits) exceeds width of its type (8 bits)}}
26*67e74705SXin Li   unsigned : -2;  // expected-error {{anonymous bit-field has negative width (-2)}}
27*67e74705SXin Li   float : 12;     // expected-error {{anonymous bit-field has non-integral type 'float'}}
28*67e74705SXin Li 
29*67e74705SXin Li   _Bool : 2;   // expected-error {{width of anonymous bit-field (2 bits) exceeds width of its type (1 bit)}}
30*67e74705SXin Li   _Bool h : 5; // expected-error {{width of bit-field 'h' (5 bits) exceeds width of its type (1 bit)}}
31*67e74705SXin Li };
32*67e74705SXin Li 
33*67e74705SXin Li struct b {unsigned x : 2;} x;
34*67e74705SXin Li __typeof__(x.x+1) y;
35*67e74705SXin Li int y;
36*67e74705SXin Li 
37*67e74705SXin Li struct {unsigned x : 2;} x2;
38*67e74705SXin Li __typeof__((x.x+=1)+1) y;
39*67e74705SXin Li __typeof__((0,x.x)+1) y;
40*67e74705SXin Li __typeof__(x.x<<1) y;
41*67e74705SXin Li int y;
42*67e74705SXin Li 
43*67e74705SXin Li struct PR8025 {
44*67e74705SXin Li   double : 2; // expected-error{{anonymous bit-field has non-integral type 'double'}}
45*67e74705SXin Li };
46*67e74705SXin Li 
47*67e74705SXin Li struct Test4 {
48*67e74705SXin Li   unsigned bitX : 4;
49*67e74705SXin Li   unsigned bitY : 4;
50*67e74705SXin Li   unsigned var;
51*67e74705SXin Li };
test4(struct Test4 * t)52*67e74705SXin Li void test4(struct Test4 *t) {
53*67e74705SXin Li   (void) sizeof(t->bitX); // expected-error {{invalid application of 'sizeof' to bit-field}}
54*67e74705SXin Li   (void) sizeof((t->bitY)); // expected-error {{invalid application of 'sizeof' to bit-field}}
55*67e74705SXin Li   (void) sizeof(t->bitX = 4); // not a bitfield designator in C
56*67e74705SXin Li   (void) sizeof(t->bitX += 4); // not a bitfield designator in C
57*67e74705SXin Li   (void) sizeof((void) 0, t->bitX); // not a bitfield designator in C
58*67e74705SXin Li   (void) sizeof(t->var ? t->bitX : t->bitY); // not a bitfield designator in C
59*67e74705SXin Li   (void) sizeof(t->var ? t->bitX : t->bitX); // not a bitfield designator in C
60*67e74705SXin Li }
61*67e74705SXin Li 
62*67e74705SXin Li typedef unsigned Unsigned;
63*67e74705SXin Li typedef signed Signed;
64*67e74705SXin Li 
65*67e74705SXin Li struct Test5 { unsigned n : 2; } t5;
66*67e74705SXin Li // Bitfield is unsigned
67*67e74705SXin Li struct Test5 sometest5 = {-1}; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -1 to 3}}
68*67e74705SXin Li typedef __typeof__(+t5.n) Signed;  // ... but promotes to signed.
69*67e74705SXin Li 
70*67e74705SXin Li typedef __typeof__(t5.n + 0) Signed; // Arithmetic promotes.
71*67e74705SXin Li 
72*67e74705SXin Li typedef __typeof__(+(t5.n = 0)) Signed;  // FIXME: Assignment should not; the result
73*67e74705SXin Li typedef __typeof__(+(t5.n += 0)) Signed; // is a non-bit-field lvalue of type unsigned.
74*67e74705SXin Li typedef __typeof__(+(t5.n *= 0)) Signed;
75*67e74705SXin Li 
76*67e74705SXin Li typedef __typeof__(+(++t5.n)) Signed; // FIXME: Increment is equivalent to compound-assignment.
77*67e74705SXin Li typedef __typeof__(+(--t5.n)) Signed; // This should not promote to signed.
78*67e74705SXin Li 
79*67e74705SXin Li typedef __typeof__(+(t5.n++)) Unsigned; // Post-increment is underspecified, but seems to
80*67e74705SXin Li typedef __typeof__(+(t5.n--)) Unsigned; // also act like compound-assignment.
81*67e74705SXin Li 
82*67e74705SXin Li struct Test6 {
83*67e74705SXin Li   : 0.0; // expected-error{{type name requires a specifier or qualifier}}
84*67e74705SXin Li };
85