17604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor// RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s
2bdae88f629538afd8504496ed28864d61471db69Douglas Gregorstruct X {
3bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  union {
4bdae88f629538afd8504496ed28864d61471db69Douglas Gregor    float f3;
5bdae88f629538afd8504496ed28864d61471db69Douglas Gregor    double d2;
6bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  } named;
7bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
8bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  union {
9bdae88f629538afd8504496ed28864d61471db69Douglas Gregor    int i;
10bdae88f629538afd8504496ed28864d61471db69Douglas Gregor    float f;
11bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
12f2705196d83a162665d4350bf3bb464972974ee3Richard Smith    union { // expected-warning{{anonymous types declared in an anonymous union are an extension}}
13bdae88f629538afd8504496ed28864d61471db69Douglas Gregor      float f2;
14bdae88f629538afd8504496ed28864d61471db69Douglas Gregor      mutable double d;
15bdae88f629538afd8504496ed28864d61471db69Douglas Gregor    };
16bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  };
17bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
18bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  void test_unqual_references();
19bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
207604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor  struct { // expected-warning{{anonymous structs are a GNU extension}}
21bdae88f629538afd8504496ed28864d61471db69Douglas Gregor    int a;
22bdae88f629538afd8504496ed28864d61471db69Douglas Gregor    float b;
23bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  };
24bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
25bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  void test_unqual_references_const() const;
26bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
27bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  mutable union { // expected-error{{anonymous union at class scope must not have a storage specifier}}
28bdae88f629538afd8504496ed28864d61471db69Douglas Gregor    float c1;
29bdae88f629538afd8504496ed28864d61471db69Douglas Gregor    double c2;
30bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  };
31bdae88f629538afd8504496ed28864d61471db69Douglas Gregor};
32bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
33bdae88f629538afd8504496ed28864d61471db69Douglas Gregorvoid X::test_unqual_references() {
34bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  i = 0;
35bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  f = 0.0;
36bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  f2 = f;
37bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  d = f;
38bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  f3 = 0; // expected-error{{use of undeclared identifier 'f3'}}
39bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  a = 0;
40bdae88f629538afd8504496ed28864d61471db69Douglas Gregor}
41bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
42bdae88f629538afd8504496ed28864d61471db69Douglas Gregorvoid X::test_unqual_references_const() const {
43bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  d = 0.0;
44bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  f2 = 0; // expected-error{{read-only variable is not assignable}}
45bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  a = 0; // expected-error{{read-only variable is not assignable}}
46bdae88f629538afd8504496ed28864d61471db69Douglas Gregor}
47bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
48bdae88f629538afd8504496ed28864d61471db69Douglas Gregorvoid test_unqual_references(X x, const X xc) {
49bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  x.i = 0;
50bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  x.f = 0.0;
51bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  x.f2 = x.f;
52bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  x.d = x.f;
53bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  x.f3 = 0; // expected-error{{no member named 'f3'}}
54bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  x.a = 0;
55bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
56bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  xc.d = 0.0;
57bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  xc.f = 0; // expected-error{{read-only variable is not assignable}}
58bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  xc.a = 0; // expected-error{{read-only variable is not assignable}}
59bdae88f629538afd8504496ed28864d61471db69Douglas Gregor}
60bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
61bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
62bdae88f629538afd8504496ed28864d61471db69Douglas Gregorstruct Redecl {
63bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  int x; // expected-note{{previous declaration is here}}
64bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  class y { };
65bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
66bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  union {
67bdae88f629538afd8504496ed28864d61471db69Douglas Gregor    int x; // expected-error{{member of anonymous union redeclares 'x'}}
68bdae88f629538afd8504496ed28864d61471db69Douglas Gregor    float y;
6972de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor    double z; // expected-note{{previous declaration is here}}
70bdae88f629538afd8504496ed28864d61471db69Douglas Gregor    double zz; // expected-note{{previous definition is here}}
71bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  };
72bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
736b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  int z; // expected-error{{duplicate member 'z'}}
74bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  void zz(); // expected-error{{redefinition of 'zz' as different kind of symbol}}
75bdae88f629538afd8504496ed28864d61471db69Douglas Gregor};
76bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
77bdae88f629538afd8504496ed28864d61471db69Douglas Gregorunion { // expected-error{{anonymous unions at namespace or global scope must be declared 'static'}}
78bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  int int_val;
79bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  float float_val;
80bdae88f629538afd8504496ed28864d61471db69Douglas Gregor};
81bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
82bdae88f629538afd8504496ed28864d61471db69Douglas Gregorstatic union {
83bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  int int_val2;
84bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  float float_val2;
85bdae88f629538afd8504496ed28864d61471db69Douglas Gregor};
86bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
87bdae88f629538afd8504496ed28864d61471db69Douglas Gregorvoid f() {
88bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  int_val2 = 0;
89bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  float_val2 = 0.0;
90bdae88f629538afd8504496ed28864d61471db69Douglas Gregor}
91bdae88f629538afd8504496ed28864d61471db69Douglas Gregor
92bdae88f629538afd8504496ed28864d61471db69Douglas Gregorvoid g() {
93bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  union {
94bdae88f629538afd8504496ed28864d61471db69Douglas Gregor    int i;
956b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor    float f2;
96bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  };
97bdae88f629538afd8504496ed28864d61471db69Douglas Gregor  i = 0;
986b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  f2 = 0.0;
99bdae88f629538afd8504496ed28864d61471db69Douglas Gregor}
1006b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor
1016b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregorstruct BadMembers {
1026b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  union {
1036b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor    struct X { }; // expected-error {{types cannot be declared in an anonymous union}}
104f2705196d83a162665d4350bf3bb464972974ee3Richard Smith    struct { int x; int y; } y; // expected-warning{{anonymous types declared in an anonymous union are an extension}}
1056b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor
1066b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor    void f(); // expected-error{{functions cannot be declared in an anonymous union}}
1076b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  private: int x1; // expected-error{{anonymous union cannot contain a private data member}}
1086b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  protected: float x2; // expected-error{{anonymous union cannot contain a protected data member}}
1096b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  };
1106b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor};
1114920f1ffb62b13b88e579476803c093f97f3e17fDouglas Gregor
1124920f1ffb62b13b88e579476803c093f97f3e17fDouglas Gregor// <rdar://problem/6481130>
113c7f811638f8603fa373d2be724e8b1c8ba51ad75Richard Smithtypedef union { }; // expected-warning{{typedef requires a name}}
114588a4ad562d717ccbbdbbc36f57e967c0b1ca147Fariborz Jahanian
115588a4ad562d717ccbbdbbc36f57e967c0b1ca147Fariborz Jahanian// <rdar://problem/7562438>
116588a4ad562d717ccbbdbbc36f57e967c0b1ca147Fariborz Jahaniantypedef struct objc_module *Foo ;
117588a4ad562d717ccbbdbbc36f57e967c0b1ca147Fariborz Jahanian
118588a4ad562d717ccbbdbbc36f57e967c0b1ca147Fariborz Jahaniantypedef struct _s {
119588a4ad562d717ccbbdbbc36f57e967c0b1ca147Fariborz Jahanian    union {
120588a4ad562d717ccbbdbbc36f57e967c0b1ca147Fariborz Jahanian        int a;
121588a4ad562d717ccbbdbbc36f57e967c0b1ca147Fariborz Jahanian        int Foo;
122588a4ad562d717ccbbdbbc36f57e967c0b1ca147Fariborz Jahanian    };
123588a4ad562d717ccbbdbbc36f57e967c0b1ca147Fariborz Jahanian} s, *ps;
124aec0371e62be013a2e6466688ccf6a7460880262John McCall
125aec0371e62be013a2e6466688ccf6a7460880262John McCall// <rdar://problem/7987650>
126aec0371e62be013a2e6466688ccf6a7460880262John McCallnamespace test4 {
127aec0371e62be013a2e6466688ccf6a7460880262John McCall  class A {
1287604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor    struct { // expected-warning{{anonymous structs are a GNU extension}}
129aec0371e62be013a2e6466688ccf6a7460880262John McCall      int s0; // expected-note {{declared private here}}
130aec0371e62be013a2e6466688ccf6a7460880262John McCall      double s1; // expected-note {{declared private here}}
131f2705196d83a162665d4350bf3bb464972974ee3Richard Smith      union { // expected-warning{{anonymous types declared in an anonymous struct are an extension}}
132aec0371e62be013a2e6466688ccf6a7460880262John McCall        int su0; // expected-note {{declared private here}}
133aec0371e62be013a2e6466688ccf6a7460880262John McCall        double su1; // expected-note {{declared private here}}
134aec0371e62be013a2e6466688ccf6a7460880262John McCall      };
135aec0371e62be013a2e6466688ccf6a7460880262John McCall    };
136aec0371e62be013a2e6466688ccf6a7460880262John McCall    union {
137aec0371e62be013a2e6466688ccf6a7460880262John McCall      int u0; // expected-note {{declared private here}}
138aec0371e62be013a2e6466688ccf6a7460880262John McCall      double u1; // expected-note {{declared private here}}
139f2705196d83a162665d4350bf3bb464972974ee3Richard Smith      struct { // expected-warning{{anonymous structs are a GNU extension}} expected-warning{{anonymous types declared in an anonymous union are an extension}}
140aec0371e62be013a2e6466688ccf6a7460880262John McCall        int us0; // expected-note {{declared private here}}
141aec0371e62be013a2e6466688ccf6a7460880262John McCall        double us1; // expected-note {{declared private here}}
142aec0371e62be013a2e6466688ccf6a7460880262John McCall      };
143aec0371e62be013a2e6466688ccf6a7460880262John McCall    };
144aec0371e62be013a2e6466688ccf6a7460880262John McCall  };
145aec0371e62be013a2e6466688ccf6a7460880262John McCall
146aec0371e62be013a2e6466688ccf6a7460880262John McCall  void test() {
147aec0371e62be013a2e6466688ccf6a7460880262John McCall    A a;
148aec0371e62be013a2e6466688ccf6a7460880262John McCall    (void) a.s0;  // expected-error {{private member}}
149aec0371e62be013a2e6466688ccf6a7460880262John McCall    (void) a.s1;  // expected-error {{private member}}
150aec0371e62be013a2e6466688ccf6a7460880262John McCall    (void) a.su0; // expected-error {{private member}}
151aec0371e62be013a2e6466688ccf6a7460880262John McCall    (void) a.su1; // expected-error {{private member}}
152aec0371e62be013a2e6466688ccf6a7460880262John McCall    (void) a.u0;  // expected-error {{private member}}
153aec0371e62be013a2e6466688ccf6a7460880262John McCall    (void) a.u1;  // expected-error {{private member}}
154aec0371e62be013a2e6466688ccf6a7460880262John McCall    (void) a.us0; // expected-error {{private member}}
155aec0371e62be013a2e6466688ccf6a7460880262John McCall    (void) a.us1; // expected-error {{private member}}
156aec0371e62be013a2e6466688ccf6a7460880262John McCall  }
157aec0371e62be013a2e6466688ccf6a7460880262John McCall}
1582b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis
1592b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidistypedef void *voidPtr;
1602b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis
1612b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidisvoid f2() {
1622b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis    union { int **ctxPtr; void **voidPtr; };
1632b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis}
1642b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis
1652b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidisvoid foo_PR6741() {
1662b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis    union {
1672b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis        char *m_a;
1682b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis        int *m_b;
1692b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis    };
1702b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis
1712b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis    if(1) {
1722b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis        union {
1732b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis            char *m_a;
1742b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis            int *m_b;
1752b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis        };
1762b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis    }
1772b64239a8ef4829be7b2c32eff60d8de204b4e2cArgyrios Kyrtzidis}
1787604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor
1797604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregornamespace PR8326 {
1807604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor  template <class T>
1817604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor  class Foo {
1827604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor  public:
1837604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor    Foo()
1847604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor      : x(0)
1857604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor      , y(1){
1867604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor    }
1877604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor
1887604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor  private:
1897604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor    const union { // expected-warning{{anonymous union cannot be 'const'}}
190f2705196d83a162665d4350bf3bb464972974ee3Richard Smith      struct { // expected-warning{{anonymous structs are a GNU extension}} expected-warning{{declared in an anonymous union}}
1917604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor        T x;
1927604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor        T y;
1937604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor      };
1947604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor      T v[2];
1957604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor    };
1967604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor  };
1977604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor
1987604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor  Foo<int> baz;
1997604f64a865bdba86c05cab76bcd47fd10372eb8Douglas Gregor}
200bf03b375d887403e78837c9453fbad45efaef91dEli Friedman
201bf03b375d887403e78837c9453fbad45efaef91dEli Friedmannamespace PR16630 {
202bf03b375d887403e78837c9453fbad45efaef91dEli Friedman  struct A { union { int x; float y; }; }; // expected-note {{member is declared here}}
203bf03b375d887403e78837c9453fbad45efaef91dEli Friedman  struct B : private A { using A::x; } b; // expected-note 2 {{private}}
204bf03b375d887403e78837c9453fbad45efaef91dEli Friedman  void foo () {
205bf03b375d887403e78837c9453fbad45efaef91dEli Friedman    b.x = 10;
206bf03b375d887403e78837c9453fbad45efaef91dEli Friedman    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'}}
207bf03b375d887403e78837c9453fbad45efaef91dEli Friedman  }
208bf03b375d887403e78837c9453fbad45efaef91dEli Friedman}
209