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