blob: f77fa03328f61169394c524c309d6cfcb9d1377a [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// RUN: clang-cc -fsyntax-only -verify %s
Douglas Gregorbdae88f2009-01-07 16:22:09 +00002struct X {
3 union {
4 float f3;
5 double d2;
6 } named;
7
8 union {
9 int i;
10 float f;
11
12 union {
13 float f2;
14 mutable double d;
15 };
16 };
17
18 void test_unqual_references();
19
20 struct {
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
33void 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
42void 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
48void 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
62struct 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;
Douglas Gregor72de6672009-01-08 20:45:30 +000069 double z; // expected-note{{previous declaration is here}}
Douglas Gregorbdae88f2009-01-07 16:22:09 +000070 double zz; // expected-note{{previous definition is here}}
71 };
72
Douglas Gregor6b3945f2009-01-07 19:46:03 +000073 int z; // expected-error{{duplicate member 'z'}}
Douglas Gregorbdae88f2009-01-07 16:22:09 +000074 void zz(); // expected-error{{redefinition of 'zz' as different kind of symbol}}
75};
76
77union { // expected-error{{anonymous unions at namespace or global scope must be declared 'static'}}
78 int int_val;
79 float float_val;
80};
81
82static union {
83 int int_val2;
84 float float_val2;
85};
86
87void f() {
88 int_val2 = 0;
89 float_val2 = 0.0;
90}
91
92void g() {
93 union {
94 int i;
Douglas Gregor6b3945f2009-01-07 19:46:03 +000095 float f2;
Douglas Gregorbdae88f2009-01-07 16:22:09 +000096 };
97 i = 0;
Douglas Gregor6b3945f2009-01-07 19:46:03 +000098 f2 = 0.0;
Douglas Gregorbdae88f2009-01-07 16:22:09 +000099}
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000100
101struct BadMembers {
102 union {
103 struct X { }; // expected-error {{types cannot be declared in an anonymous union}}
104 struct { int x; int y; } y;
105
106 void f(); // expected-error{{functions cannot be declared in an anonymous union}}
107 private: int x1; // expected-error{{anonymous union cannot contain a private data member}}
108 protected: float x2; // expected-error{{anonymous union cannot contain a protected data member}}
109 };
110};
Douglas Gregor4920f1f2009-01-12 22:49:06 +0000111
112// <rdar://problem/6481130>
113typedef union { }; // expected-error{{declaration does not declare anything}}