xref: /aosp_15_r20/external/clang/test/SemaCXX/anonymous-union.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -pedantic %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 { // expected-warning{{anonymous types declared in an anonymous union are an extension}}
13*67e74705SXin Li       float f2;
14*67e74705SXin Li       mutable double d;
15*67e74705SXin Li     };
16*67e74705SXin Li   };
17*67e74705SXin Li 
18*67e74705SXin Li   void test_unqual_references();
19*67e74705SXin Li 
20*67e74705SXin Li   struct { // expected-warning{{anonymous structs are a GNU extension}}
21*67e74705SXin Li     int a;
22*67e74705SXin Li     float b;
23*67e74705SXin Li   };
24*67e74705SXin Li 
25*67e74705SXin Li   void test_unqual_references_const() const;
26*67e74705SXin Li 
27*67e74705SXin Li   mutable union { // expected-error{{anonymous union at class scope must not have a storage specifier}}
28*67e74705SXin Li     float c1;
29*67e74705SXin Li     double c2;
30*67e74705SXin Li   };
31*67e74705SXin Li };
32*67e74705SXin Li 
test_unqual_references()33*67e74705SXin Li void X::test_unqual_references() {
34*67e74705SXin Li   i = 0;
35*67e74705SXin Li   f = 0.0;
36*67e74705SXin Li   f2 = f;
37*67e74705SXin Li   d = f;
38*67e74705SXin Li   f3 = 0; // expected-error{{use of undeclared identifier 'f3'}}
39*67e74705SXin Li   a = 0;
40*67e74705SXin Li }
41*67e74705SXin Li 
test_unqual_references_const() const42*67e74705SXin Li void X::test_unqual_references_const() const { // expected-note 2{{member function 'X::test_unqual_references_const' is declared const here}}
43*67e74705SXin Li   d = 0.0;
44*67e74705SXin Li   f2 = 0; // expected-error{{cannot assign to non-static data member within const member function 'test_unqual_references_const'}}
45*67e74705SXin Li   a = 0; // expected-error{{cannot assign to non-static data member within const member function 'test_unqual_references_const'}}
46*67e74705SXin Li }
47*67e74705SXin Li 
test_unqual_references(X x,const X xc)48*67e74705SXin Li void test_unqual_references(X x, const X xc) {
49*67e74705SXin Li   // expected-note@-1 2{{variable 'xc' declared const here}}
50*67e74705SXin Li   x.i = 0;
51*67e74705SXin Li   x.f = 0.0;
52*67e74705SXin Li   x.f2 = x.f;
53*67e74705SXin Li   x.d = x.f;
54*67e74705SXin Li   x.f3 = 0; // expected-error{{no member named 'f3'}}
55*67e74705SXin Li   x.a = 0;
56*67e74705SXin Li 
57*67e74705SXin Li   xc.d = 0.0;
58*67e74705SXin Li   xc.f = 0; // expected-error{{cannot assign to variable 'xc' with const-qualified type 'const X'}}
59*67e74705SXin Li   xc.a = 0; // expected-error{{cannot assign to variable 'xc' with const-qualified type 'const X'}}
60*67e74705SXin Li }
61*67e74705SXin Li 
62*67e74705SXin Li 
63*67e74705SXin Li struct Redecl {
64*67e74705SXin Li   int x; // expected-note{{previous declaration is here}}
65*67e74705SXin Li   class y { }; // expected-note{{previous declaration is here}}
66*67e74705SXin Li 
67*67e74705SXin Li   union {
68*67e74705SXin Li     int x; // expected-error{{member of anonymous union redeclares 'x'}}
69*67e74705SXin Li     float y; // expected-error{{member of anonymous union redeclares 'y'}}
70*67e74705SXin Li     double z; // expected-note{{previous declaration is here}}
71*67e74705SXin Li     double zz; // expected-note{{previous definition is here}}
72*67e74705SXin Li   };
73*67e74705SXin Li 
74*67e74705SXin Li   int z; // expected-error{{duplicate member 'z'}}
75*67e74705SXin Li   void zz(); // expected-error{{redefinition of 'zz' as different kind of symbol}}
76*67e74705SXin Li };
77*67e74705SXin Li 
78*67e74705SXin Li union { // expected-error{{anonymous unions at namespace or global scope must be declared 'static'}}
79*67e74705SXin Li   int int_val;
80*67e74705SXin Li   float float_val;
81*67e74705SXin Li };
82*67e74705SXin Li 
83*67e74705SXin Li static union {
84*67e74705SXin Li   int int_val2; // expected-note{{previous definition is here}}
85*67e74705SXin Li   float float_val2;
86*67e74705SXin Li };
87*67e74705SXin Li 
PR21858()88*67e74705SXin Li void PR21858() {
89*67e74705SXin Li   void int_val2(); // expected-error{{redefinition of 'int_val2' as different kind of symbol}}
90*67e74705SXin Li }
91*67e74705SXin Li 
f()92*67e74705SXin Li void f() {
93*67e74705SXin Li   int_val2 = 0;
94*67e74705SXin Li   float_val2 = 0.0;
95*67e74705SXin Li }
96*67e74705SXin Li 
g()97*67e74705SXin Li void g() {
98*67e74705SXin Li   union {
99*67e74705SXin Li     int i;
100*67e74705SXin Li     float f2;
101*67e74705SXin Li   };
102*67e74705SXin Li   i = 0;
103*67e74705SXin Li   f2 = 0.0;
104*67e74705SXin Li }
105*67e74705SXin Li 
106*67e74705SXin Li struct BadMembers {
107*67e74705SXin Li   union {
108*67e74705SXin Li     struct X { }; // expected-error {{types cannot be declared in an anonymous union}}
109*67e74705SXin Li     struct { int x; int y; } y; // expected-warning{{anonymous types declared in an anonymous union are an extension}}
110*67e74705SXin Li 
111*67e74705SXin Li     void f(); // expected-error{{functions cannot be declared in an anonymous union}}
112*67e74705SXin Li   private: int x1; // expected-error{{anonymous union cannot contain a private data member}}
113*67e74705SXin Li   protected: float x2; // expected-error{{anonymous union cannot contain a protected data member}}
114*67e74705SXin Li   };
115*67e74705SXin Li };
116*67e74705SXin Li 
117*67e74705SXin Li // <rdar://problem/6481130>
118*67e74705SXin Li typedef union { }; // expected-warning{{typedef requires a name}}
119*67e74705SXin Li 
120*67e74705SXin Li // <rdar://problem/7562438>
121*67e74705SXin Li typedef struct objc_module *Foo ;
122*67e74705SXin Li 
123*67e74705SXin Li typedef struct _s {
124*67e74705SXin Li     union {
125*67e74705SXin Li         int a;
126*67e74705SXin Li         int Foo;
127*67e74705SXin Li     };
128*67e74705SXin Li } s, *ps;
129*67e74705SXin Li 
130*67e74705SXin Li // <rdar://problem/7987650>
131*67e74705SXin Li namespace test4 {
132*67e74705SXin Li   class A {
133*67e74705SXin Li     struct { // expected-warning{{anonymous structs are a GNU extension}}
134*67e74705SXin Li       int s0; // expected-note {{declared private here}}
135*67e74705SXin Li       double s1; // expected-note {{declared private here}}
136*67e74705SXin Li       union { // expected-warning{{anonymous types declared in an anonymous struct are an extension}}
137*67e74705SXin Li         int su0; // expected-note {{declared private here}}
138*67e74705SXin Li         double su1; // expected-note {{declared private here}}
139*67e74705SXin Li       };
140*67e74705SXin Li     };
141*67e74705SXin Li     union {
142*67e74705SXin Li       int u0; // expected-note {{declared private here}}
143*67e74705SXin Li       double u1; // expected-note {{declared private here}}
144*67e74705SXin Li       struct { // expected-warning{{anonymous structs are a GNU extension}} expected-warning{{anonymous types declared in an anonymous union are an extension}}
145*67e74705SXin Li         int us0; // expected-note {{declared private here}}
146*67e74705SXin Li         double us1; // expected-note {{declared private here}}
147*67e74705SXin Li       };
148*67e74705SXin Li     };
149*67e74705SXin Li   };
150*67e74705SXin Li 
test()151*67e74705SXin Li   void test() {
152*67e74705SXin Li     A a;
153*67e74705SXin Li     (void) a.s0;  // expected-error {{private member}}
154*67e74705SXin Li     (void) a.s1;  // expected-error {{private member}}
155*67e74705SXin Li     (void) a.su0; // expected-error {{private member}}
156*67e74705SXin Li     (void) a.su1; // expected-error {{private member}}
157*67e74705SXin Li     (void) a.u0;  // expected-error {{private member}}
158*67e74705SXin Li     (void) a.u1;  // expected-error {{private member}}
159*67e74705SXin Li     (void) a.us0; // expected-error {{private member}}
160*67e74705SXin Li     (void) a.us1; // expected-error {{private member}}
161*67e74705SXin Li   }
162*67e74705SXin Li }
163*67e74705SXin Li 
164*67e74705SXin Li typedef void *voidPtr;
165*67e74705SXin Li 
f2()166*67e74705SXin Li void f2() {
167*67e74705SXin Li     union { int **ctxPtr; void **voidPtr; };
168*67e74705SXin Li }
169*67e74705SXin Li 
foo_PR6741()170*67e74705SXin Li void foo_PR6741() {
171*67e74705SXin Li     union {
172*67e74705SXin Li         char *m_a;
173*67e74705SXin Li         int *m_b;
174*67e74705SXin Li     };
175*67e74705SXin Li 
176*67e74705SXin Li     if(1) {
177*67e74705SXin Li         union {
178*67e74705SXin Li             char *m_a;
179*67e74705SXin Li             int *m_b;
180*67e74705SXin Li         };
181*67e74705SXin Li     }
182*67e74705SXin Li }
183*67e74705SXin Li 
184*67e74705SXin Li namespace PR8326 {
185*67e74705SXin Li   template <class T>
186*67e74705SXin Li   class Foo {
187*67e74705SXin Li   public:
Foo()188*67e74705SXin Li     Foo()
189*67e74705SXin Li       : x(0)
190*67e74705SXin Li       , y(1){
191*67e74705SXin Li     }
192*67e74705SXin Li 
193*67e74705SXin Li   private:
194*67e74705SXin Li     const union { // expected-warning{{anonymous union cannot be 'const'}}
195*67e74705SXin Li       struct { // expected-warning{{anonymous structs are a GNU extension}} expected-warning{{declared in an anonymous union}}
196*67e74705SXin Li         T x;
197*67e74705SXin Li         T y;
198*67e74705SXin Li       };
199*67e74705SXin Li       T v[2];
200*67e74705SXin Li     };
201*67e74705SXin Li   };
202*67e74705SXin Li 
203*67e74705SXin Li   Foo<int> baz;
204*67e74705SXin Li }
205*67e74705SXin Li 
206*67e74705SXin Li namespace PR16630 {
207*67e74705SXin Li   struct A { union { int x; float y; }; }; // expected-note {{member is declared here}}
208*67e74705SXin Li   struct B : private A { using A::x; } b; // expected-note 2 {{private}}
foo()209*67e74705SXin Li   void foo () {
210*67e74705SXin Li     b.x = 10;
211*67e74705SXin Li     b.y = 0; // expected-error {{cannot cast 'struct B' to its private base class 'PR16630::A'}} expected-error {{'y' is a private member of 'PR16630::A'}}
212*67e74705SXin Li   }
213*67e74705SXin Li }
214