xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/anonymous-struct-union.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %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 {
13f4a2713aSLionel Sambuc       float f2;
14f4a2713aSLionel Sambuc       double d;
15f4a2713aSLionel Sambuc     };
16f4a2713aSLionel Sambuc   };
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc   struct {
19f4a2713aSLionel Sambuc     int a;
20f4a2713aSLionel Sambuc     float b;
21f4a2713aSLionel Sambuc   };
22f4a2713aSLionel Sambuc };
23f4a2713aSLionel Sambuc 
test_unqual_references(struct X x,const struct X xc)24f4a2713aSLionel Sambuc void test_unqual_references(struct X x, const struct X xc) {
25f4a2713aSLionel Sambuc   x.i = 0;
26f4a2713aSLionel Sambuc   x.f = 0.0;
27f4a2713aSLionel Sambuc   x.f2 = x.f;
28f4a2713aSLionel Sambuc   x.d = x.f;
29f4a2713aSLionel Sambuc   x.f3 = 0; // expected-error{{no member named 'f3'}}
30f4a2713aSLionel Sambuc   x.a = 0;
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc   xc.d = 0.0; // expected-error{{read-only variable is not assignable}}
33f4a2713aSLionel Sambuc   xc.f = 0; // expected-error{{read-only variable is not assignable}}
34f4a2713aSLionel Sambuc   xc.a = 0; // expected-error{{read-only variable is not assignable}}
35f4a2713aSLionel Sambuc }
36f4a2713aSLionel Sambuc 
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc struct Redecl {
39f4a2713aSLionel Sambuc   int x; // expected-note{{previous declaration is here}}
40*0a6a1f1dSLionel Sambuc   struct y { }; // expected-warning{{declaration does not declare anything}}
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc   union {
43f4a2713aSLionel Sambuc     int x; // expected-error{{member of anonymous union redeclares 'x'}}
44f4a2713aSLionel Sambuc     float y;
45f4a2713aSLionel Sambuc     double z; // expected-note{{previous declaration is here}}
46f4a2713aSLionel Sambuc     double zz; // expected-note{{previous declaration is here}}
47f4a2713aSLionel Sambuc   };
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc   int z; // expected-error{{duplicate member 'z'}}
50f4a2713aSLionel Sambuc   void zz(); // expected-error{{duplicate member 'zz'}}
51f4a2713aSLionel Sambuc };
52f4a2713aSLionel Sambuc 
53f4a2713aSLionel Sambuc union { // expected-warning{{declaration does not declare anything}}
54f4a2713aSLionel Sambuc   int int_val;
55f4a2713aSLionel Sambuc   float float_val;
56f4a2713aSLionel Sambuc };
57f4a2713aSLionel Sambuc 
58f4a2713aSLionel Sambuc static union { // expected-warning{{declaration does not declare anything}}
59f4a2713aSLionel Sambuc   int int_val2;
60f4a2713aSLionel Sambuc   float float_val2;
61f4a2713aSLionel Sambuc };
62f4a2713aSLionel Sambuc 
f()63f4a2713aSLionel Sambuc void f() {
64f4a2713aSLionel Sambuc   int_val2 = 0; // expected-error{{use of undeclared identifier}}
65f4a2713aSLionel Sambuc   float_val2 = 0.0; // expected-error{{use of undeclared identifier}}
66f4a2713aSLionel Sambuc }
67f4a2713aSLionel Sambuc 
g()68f4a2713aSLionel Sambuc void g() {
69f4a2713aSLionel Sambuc   union { // expected-warning{{declaration does not declare anything}}
70f4a2713aSLionel Sambuc     int i;
71f4a2713aSLionel Sambuc     float f2;
72f4a2713aSLionel Sambuc   };
73f4a2713aSLionel Sambuc   i = 0; // expected-error{{use of undeclared identifier}}
74f4a2713aSLionel Sambuc   f2 = 0.0; // expected-error{{use of undeclared identifier}}
75f4a2713aSLionel Sambuc }
76f4a2713aSLionel Sambuc 
77f4a2713aSLionel Sambuc // <rdar://problem/6483159>
78f4a2713aSLionel Sambuc struct s0 { union { int f0; }; };
79f4a2713aSLionel Sambuc 
80f4a2713aSLionel Sambuc // <rdar://problem/6481130>
81f4a2713aSLionel Sambuc typedef struct { }; // expected-warning{{typedef requires a name}}
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc // PR3675
84f4a2713aSLionel Sambuc struct s1 {
85f4a2713aSLionel Sambuc   int f0; // expected-note{{previous declaration is here}}
86f4a2713aSLionel Sambuc   union {
87f4a2713aSLionel Sambuc     int f0; // expected-error{{member of anonymous union redeclares 'f0'}}
88f4a2713aSLionel Sambuc   };
89f4a2713aSLionel Sambuc };
90f4a2713aSLionel Sambuc 
91f4a2713aSLionel Sambuc // PR3680
92f4a2713aSLionel Sambuc struct {}; // expected-warning{{declaration does not declare anything}}
93f4a2713aSLionel Sambuc 
94f4a2713aSLionel Sambuc struct s2 {
95f4a2713aSLionel Sambuc   union {
96f4a2713aSLionel Sambuc     int a;
97f4a2713aSLionel Sambuc   } // expected-warning{{expected ';' at end of declaration list}}
98f4a2713aSLionel Sambuc }; // expected-error{{expected member name or ';' after declaration specifiers}}
99f4a2713aSLionel Sambuc 
100f4a2713aSLionel Sambuc // Make sure we don't a.k.a. anonymous structs.
101f4a2713aSLionel Sambuc typedef struct {
102f4a2713aSLionel Sambuc   int x;
103f4a2713aSLionel Sambuc } a_struct;
104f4a2713aSLionel Sambuc int tmp = (a_struct) { .x = 0 }; // expected-error {{initializing 'int' with an expression of incompatible type 'a_struct'}}
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc // This example comes out of the C11 standard; make sure we don't accidentally reject it.
107f4a2713aSLionel Sambuc struct s {
108f4a2713aSLionel Sambuc   struct { int i; };
109f4a2713aSLionel Sambuc   int a[];
110f4a2713aSLionel Sambuc };
111*0a6a1f1dSLionel Sambuc 
112*0a6a1f1dSLionel Sambuc // PR20930
113*0a6a1f1dSLionel Sambuc struct s3 {
114*0a6a1f1dSLionel Sambuc   struct { int A __attribute__((deprecated)); }; // expected-note {{'A' has been explicitly marked deprecated here}}
115*0a6a1f1dSLionel Sambuc };
116*0a6a1f1dSLionel Sambuc 
117*0a6a1f1dSLionel Sambuc void deprecated_anonymous_struct_member(void) {
118*0a6a1f1dSLionel Sambuc   struct s3 s;
119*0a6a1f1dSLionel Sambuc   s.A = 1; // expected-warning {{'A' is deprecated}}
120*0a6a1f1dSLionel Sambuc }
121