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