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