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