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