xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/anonymous-struct-union.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc struct X {
3*f4a2713aSLionel Sambuc   union {
4*f4a2713aSLionel Sambuc     float f3;
5*f4a2713aSLionel Sambuc     double d2;
6*f4a2713aSLionel Sambuc   } named;
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc   union {
9*f4a2713aSLionel Sambuc     int i;
10*f4a2713aSLionel Sambuc     float f;
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc     union {
13*f4a2713aSLionel Sambuc       float f2;
14*f4a2713aSLionel Sambuc       double d;
15*f4a2713aSLionel Sambuc     };
16*f4a2713aSLionel Sambuc   };
17*f4a2713aSLionel Sambuc 
18*f4a2713aSLionel Sambuc   struct {
19*f4a2713aSLionel Sambuc     int a;
20*f4a2713aSLionel Sambuc     float b;
21*f4a2713aSLionel Sambuc   };
22*f4a2713aSLionel Sambuc };
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc void test_unqual_references(struct X x, const struct X xc) {
25*f4a2713aSLionel Sambuc   x.i = 0;
26*f4a2713aSLionel Sambuc   x.f = 0.0;
27*f4a2713aSLionel Sambuc   x.f2 = x.f;
28*f4a2713aSLionel Sambuc   x.d = x.f;
29*f4a2713aSLionel Sambuc   x.f3 = 0; // expected-error{{no member named 'f3'}}
30*f4a2713aSLionel Sambuc   x.a = 0;
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc   xc.d = 0.0; // expected-error{{read-only variable is not assignable}}
33*f4a2713aSLionel Sambuc   xc.f = 0; // expected-error{{read-only variable is not assignable}}
34*f4a2713aSLionel Sambuc   xc.a = 0; // expected-error{{read-only variable is not assignable}}
35*f4a2713aSLionel Sambuc }
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc struct Redecl {
39*f4a2713aSLionel Sambuc   int x; // expected-note{{previous declaration is here}}
40*f4a2713aSLionel Sambuc   struct y { };
41*f4a2713aSLionel Sambuc 
42*f4a2713aSLionel Sambuc   union {
43*f4a2713aSLionel Sambuc     int x; // expected-error{{member of anonymous union redeclares 'x'}}
44*f4a2713aSLionel Sambuc     float y;
45*f4a2713aSLionel Sambuc     double z; // expected-note{{previous declaration is here}}
46*f4a2713aSLionel Sambuc     double zz; // expected-note{{previous declaration is here}}
47*f4a2713aSLionel Sambuc   };
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc   int z; // expected-error{{duplicate member 'z'}}
50*f4a2713aSLionel Sambuc   void zz(); // expected-error{{duplicate member 'zz'}}
51*f4a2713aSLionel Sambuc };
52*f4a2713aSLionel Sambuc 
53*f4a2713aSLionel Sambuc union { // expected-warning{{declaration does not declare anything}}
54*f4a2713aSLionel Sambuc   int int_val;
55*f4a2713aSLionel Sambuc   float float_val;
56*f4a2713aSLionel Sambuc };
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc static union { // expected-warning{{declaration does not declare anything}}
59*f4a2713aSLionel Sambuc   int int_val2;
60*f4a2713aSLionel Sambuc   float float_val2;
61*f4a2713aSLionel Sambuc };
62*f4a2713aSLionel Sambuc 
63*f4a2713aSLionel Sambuc void f() {
64*f4a2713aSLionel Sambuc   int_val2 = 0; // expected-error{{use of undeclared identifier}}
65*f4a2713aSLionel Sambuc   float_val2 = 0.0; // expected-error{{use of undeclared identifier}}
66*f4a2713aSLionel Sambuc }
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc void g() {
69*f4a2713aSLionel Sambuc   union { // expected-warning{{declaration does not declare anything}}
70*f4a2713aSLionel Sambuc     int i;
71*f4a2713aSLionel Sambuc     float f2;
72*f4a2713aSLionel Sambuc   };
73*f4a2713aSLionel Sambuc   i = 0; // expected-error{{use of undeclared identifier}}
74*f4a2713aSLionel Sambuc   f2 = 0.0; // expected-error{{use of undeclared identifier}}
75*f4a2713aSLionel Sambuc }
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc // <rdar://problem/6483159>
78*f4a2713aSLionel Sambuc struct s0 { union { int f0; }; };
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc // <rdar://problem/6481130>
81*f4a2713aSLionel Sambuc typedef struct { }; // expected-warning{{typedef requires a name}}
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc // PR3675
84*f4a2713aSLionel Sambuc struct s1 {
85*f4a2713aSLionel Sambuc   int f0; // expected-note{{previous declaration is here}}
86*f4a2713aSLionel Sambuc   union {
87*f4a2713aSLionel Sambuc     int f0; // expected-error{{member of anonymous union redeclares 'f0'}}
88*f4a2713aSLionel Sambuc   };
89*f4a2713aSLionel Sambuc };
90*f4a2713aSLionel Sambuc 
91*f4a2713aSLionel Sambuc // PR3680
92*f4a2713aSLionel Sambuc struct {}; // expected-warning{{declaration does not declare anything}}
93*f4a2713aSLionel Sambuc 
94*f4a2713aSLionel Sambuc struct s2 {
95*f4a2713aSLionel Sambuc   union {
96*f4a2713aSLionel Sambuc     int a;
97*f4a2713aSLionel Sambuc   } // expected-warning{{expected ';' at end of declaration list}}
98*f4a2713aSLionel Sambuc }; // expected-error{{expected member name or ';' after declaration specifiers}}
99*f4a2713aSLionel Sambuc 
100*f4a2713aSLionel Sambuc // Make sure we don't a.k.a. anonymous structs.
101*f4a2713aSLionel Sambuc typedef struct {
102*f4a2713aSLionel Sambuc   int x;
103*f4a2713aSLionel Sambuc } a_struct;
104*f4a2713aSLionel Sambuc int tmp = (a_struct) { .x = 0 }; // expected-error {{initializing 'int' with an expression of incompatible type 'a_struct'}}
105*f4a2713aSLionel Sambuc 
106*f4a2713aSLionel Sambuc // This example comes out of the C11 standard; make sure we don't accidentally reject it.
107*f4a2713aSLionel Sambuc struct s {
108*f4a2713aSLionel Sambuc   struct { int i; };
109*f4a2713aSLionel Sambuc   int a[];
110*f4a2713aSLionel Sambuc };
111