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