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