Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s |
Douglas Gregor | 4bba1ed | 2009-01-07 16:22:09 +0000 | [diff] [blame] | 2 | struct X { |
| 3 | union { |
| 4 | float f3; |
| 5 | double d2; |
| 6 | } named; |
| 7 | |
| 8 | union { |
| 9 | int i; |
| 10 | float f; |
| 11 | |
Richard Smith | d202924 | 2013-01-31 03:11:12 +0000 | [diff] [blame] | 12 | union { // expected-warning{{anonymous types declared in an anonymous union are an extension}} |
Douglas Gregor | 4bba1ed | 2009-01-07 16:22:09 +0000 | [diff] [blame] | 13 | float f2; |
| 14 | mutable double d; |
| 15 | }; |
| 16 | }; |
| 17 | |
| 18 | void test_unqual_references(); |
| 19 | |
Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 20 | struct { // expected-warning{{anonymous structs are a GNU extension}} |
Douglas Gregor | 4bba1ed | 2009-01-07 16:22:09 +0000 | [diff] [blame] | 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 | |
| 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 | |
Richard Trieu | af7d76c | 2015-04-11 01:53:13 +0000 | [diff] [blame] | 42 | void X::test_unqual_references_const() const { // expected-note 2{{member function 'X::test_unqual_references_const' is declared const here}} |
Douglas Gregor | 4bba1ed | 2009-01-07 16:22:09 +0000 | [diff] [blame] | 43 | d = 0.0; |
Richard Trieu | af7d76c | 2015-04-11 01:53:13 +0000 | [diff] [blame] | 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'}} |
Douglas Gregor | 4bba1ed | 2009-01-07 16:22:09 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void test_unqual_references(X x, const X xc) { |
Richard Trieu | af7d76c | 2015-04-11 01:53:13 +0000 | [diff] [blame] | 49 | // expected-note@-1 2{{variable 'xc' declared const here}} |
Douglas Gregor | 4bba1ed | 2009-01-07 16:22:09 +0000 | [diff] [blame] | 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; |
Richard Trieu | af7d76c | 2015-04-11 01:53:13 +0000 | [diff] [blame] | 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'}} |
Douglas Gregor | 4bba1ed | 2009-01-07 16:22:09 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
| 63 | struct Redecl { |
| 64 | int x; // expected-note{{previous declaration is here}} |
Richard Smith | 0f56118 | 2016-01-06 21:54:29 +0000 | [diff] [blame] | 65 | class y { }; // expected-note{{previous declaration is here}} |
Douglas Gregor | 4bba1ed | 2009-01-07 16:22:09 +0000 | [diff] [blame] | 66 | |
| 67 | union { |
| 68 | int x; // expected-error{{member of anonymous union redeclares 'x'}} |
Richard Smith | 0f56118 | 2016-01-06 21:54:29 +0000 | [diff] [blame] | 69 | float y; // expected-error{{member of anonymous union redeclares 'y'}} |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 70 | double z; // expected-note{{previous declaration is here}} |
Douglas Gregor | 4bba1ed | 2009-01-07 16:22:09 +0000 | [diff] [blame] | 71 | double zz; // expected-note{{previous definition is here}} |
| 72 | }; |
| 73 | |
Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 74 | int z; // expected-error{{duplicate member 'z'}} |
Douglas Gregor | 4bba1ed | 2009-01-07 16:22:09 +0000 | [diff] [blame] | 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 | static union { |
David Majnemer | 867b4d8 | 2014-12-10 23:08:43 +0000 | [diff] [blame] | 84 | int int_val2; // expected-note{{previous definition is here}} |
Douglas Gregor | 4bba1ed | 2009-01-07 16:22:09 +0000 | [diff] [blame] | 85 | float float_val2; |
| 86 | }; |
| 87 | |
David Majnemer | 18fac3b | 2014-12-10 22:58:14 +0000 | [diff] [blame] | 88 | void PR21858() { |
David Majnemer | 867b4d8 | 2014-12-10 23:08:43 +0000 | [diff] [blame] | 89 | void int_val2(); // expected-error{{redefinition of 'int_val2' as different kind of symbol}} |
David Majnemer | 18fac3b | 2014-12-10 22:58:14 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Douglas Gregor | 4bba1ed | 2009-01-07 16:22:09 +0000 | [diff] [blame] | 92 | void f() { |
| 93 | int_val2 = 0; |
| 94 | float_val2 = 0.0; |
| 95 | } |
| 96 | |
| 97 | void g() { |
| 98 | union { |
| 99 | int i; |
Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 100 | float f2; |
Douglas Gregor | 4bba1ed | 2009-01-07 16:22:09 +0000 | [diff] [blame] | 101 | }; |
| 102 | i = 0; |
Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 103 | f2 = 0.0; |
Douglas Gregor | 4bba1ed | 2009-01-07 16:22:09 +0000 | [diff] [blame] | 104 | } |
Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 105 | |
| 106 | struct BadMembers { |
| 107 | union { |
| 108 | struct X { }; // expected-error {{types cannot be declared in an anonymous union}} |
Richard Smith | d202924 | 2013-01-31 03:11:12 +0000 | [diff] [blame] | 109 | struct { int x; int y; } y; // expected-warning{{anonymous types declared in an anonymous union are an extension}} |
Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 110 | |
| 111 | void f(); // expected-error{{functions cannot be declared in an anonymous union}} |
| 112 | private: int x1; // expected-error{{anonymous union cannot contain a private data member}} |
| 113 | protected: float x2; // expected-error{{anonymous union cannot contain a protected data member}} |
| 114 | }; |
| 115 | }; |
Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 116 | |
| 117 | // <rdar://problem/6481130> |
Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 118 | typedef union { }; // expected-warning{{typedef requires a name}} |
Fariborz Jahanian | 53967e2 | 2010-01-22 18:30:17 +0000 | [diff] [blame] | 119 | |
| 120 | // <rdar://problem/7562438> |
| 121 | typedef struct objc_module *Foo ; |
| 122 | |
| 123 | typedef struct _s { |
| 124 | union { |
| 125 | int a; |
| 126 | int Foo; |
| 127 | }; |
| 128 | } s, *ps; |
John McCall | b54367d | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 129 | |
| 130 | // <rdar://problem/7987650> |
| 131 | namespace test4 { |
| 132 | class A { |
Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 133 | struct { // expected-warning{{anonymous structs are a GNU extension}} |
John McCall | b54367d | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 134 | int s0; // expected-note {{declared private here}} |
| 135 | double s1; // expected-note {{declared private here}} |
Richard Smith | d202924 | 2013-01-31 03:11:12 +0000 | [diff] [blame] | 136 | union { // expected-warning{{anonymous types declared in an anonymous struct are an extension}} |
John McCall | b54367d | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 137 | int su0; // expected-note {{declared private here}} |
| 138 | double su1; // expected-note {{declared private here}} |
| 139 | }; |
| 140 | }; |
| 141 | union { |
| 142 | int u0; // expected-note {{declared private here}} |
| 143 | double u1; // expected-note {{declared private here}} |
Richard Smith | d202924 | 2013-01-31 03:11:12 +0000 | [diff] [blame] | 144 | struct { // expected-warning{{anonymous structs are a GNU extension}} expected-warning{{anonymous types declared in an anonymous union are an extension}} |
John McCall | b54367d | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 145 | int us0; // expected-note {{declared private here}} |
| 146 | double us1; // expected-note {{declared private here}} |
| 147 | }; |
| 148 | }; |
| 149 | }; |
| 150 | |
| 151 | void test() { |
| 152 | A a; |
| 153 | (void) a.s0; // expected-error {{private member}} |
| 154 | (void) a.s1; // expected-error {{private member}} |
| 155 | (void) a.su0; // expected-error {{private member}} |
| 156 | (void) a.su1; // expected-error {{private member}} |
| 157 | (void) a.u0; // expected-error {{private member}} |
| 158 | (void) a.u1; // expected-error {{private member}} |
| 159 | (void) a.us0; // expected-error {{private member}} |
| 160 | (void) a.us1; // expected-error {{private member}} |
| 161 | } |
| 162 | } |
Argyrios Kyrtzidis | e619e99 | 2010-09-23 14:26:01 +0000 | [diff] [blame] | 163 | |
| 164 | typedef void *voidPtr; |
| 165 | |
| 166 | void f2() { |
| 167 | union { int **ctxPtr; void **voidPtr; }; |
| 168 | } |
| 169 | |
| 170 | void foo_PR6741() { |
| 171 | union { |
| 172 | char *m_a; |
| 173 | int *m_b; |
| 174 | }; |
| 175 | |
| 176 | if(1) { |
| 177 | union { |
| 178 | char *m_a; |
| 179 | int *m_b; |
| 180 | }; |
| 181 | } |
| 182 | } |
Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 183 | |
| 184 | namespace PR8326 { |
| 185 | template <class T> |
| 186 | class Foo { |
| 187 | public: |
| 188 | Foo() |
| 189 | : x(0) |
| 190 | , y(1){ |
| 191 | } |
| 192 | |
| 193 | private: |
| 194 | const union { // expected-warning{{anonymous union cannot be 'const'}} |
Richard Smith | d202924 | 2013-01-31 03:11:12 +0000 | [diff] [blame] | 195 | struct { // expected-warning{{anonymous structs are a GNU extension}} expected-warning{{declared in an anonymous union}} |
Douglas Gregor | 0f8bc97 | 2011-05-09 23:05:33 +0000 | [diff] [blame] | 196 | T x; |
| 197 | T y; |
| 198 | }; |
| 199 | T v[2]; |
| 200 | }; |
| 201 | }; |
| 202 | |
| 203 | Foo<int> baz; |
| 204 | } |
Eli Friedman | cccd064 | 2013-07-16 00:01:31 +0000 | [diff] [blame] | 205 | |
| 206 | namespace PR16630 { |
| 207 | struct A { union { int x; float y; }; }; // expected-note {{member is declared here}} |
| 208 | struct B : private A { using A::x; } b; // expected-note 2 {{private}} |
| 209 | void foo () { |
| 210 | b.x = 10; |
| 211 | 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 | } |
| 213 | } |