150
|
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
|
|
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
|
|
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
|
|
4 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++2a %s
|
|
5
|
|
6 struct S {
|
|
7 S();
|
|
8 #if __cplusplus <= 199711L
|
|
9 // expected-note@-2 {{because type 'S' has a user-provided default constructor}}
|
|
10 #endif
|
|
11 };
|
|
12
|
|
13 struct { // expected-error {{anonymous structs and classes must be class members}} expected-warning {{does not declare anything}}
|
|
14 };
|
|
15
|
|
16 struct E {
|
|
17 struct {
|
|
18 S x;
|
|
19 #if __cplusplus <= 199711L
|
|
20 // expected-error@-2 {{anonymous struct member 'x' has a non-trivial default constructor}}
|
|
21 #endif
|
|
22 };
|
|
23 static struct { // expected-warning {{does not declare anything}}
|
|
24 };
|
|
25 class {
|
|
26 int anon_priv_field; // expected-error {{anonymous struct cannot contain a private data member}}
|
|
27 };
|
|
28 };
|
|
29
|
|
30 template <class T> void foo(T);
|
|
31 typedef struct { // expected-error {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration after its linkage was computed; add a tag name here to establish linkage prior to definition}}
|
|
32 #if __cplusplus <= 199711L
|
|
33 // expected-note@-2 {{declared here}}
|
|
34 #endif
|
|
35
|
|
36 void test() { // expected-note {{type is not C-compatible due to this member declaration}}
|
|
37 foo(this);
|
|
38 #if __cplusplus <= 199711L
|
|
39 // expected-warning@-2 {{template argument uses unnamed type}}
|
|
40 #endif
|
|
41 }
|
|
42 } A; // expected-note {{type is given name 'A' for linkage purposes by this typedef declaration}}
|
|
43
|
|
44 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
|
|
45 int x = 0; // expected-note {{type is not C-compatible due to this default member initializer}} expected-warning 0-1{{extension}}
|
|
46 } B; // expected-note {{type is given name 'B' for linkage purposes by this typedef declaration}}
|
|
47
|
|
48 typedef struct // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
|
|
49 : B { // expected-note {{type is not C-compatible due to this base class}}
|
|
50 } C; // expected-note {{type is given name 'C' for linkage purposes by this typedef declaration}}
|
|
51
|
223
|
52 #if __cplusplus > 201703L && __cplusplus < 202002L
|
150
|
53 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
|
|
54 static_assert([]{ return true; }()); // expected-note {{type is not C-compatible due to this lambda expression}}
|
|
55 } Lambda1; // expected-note {{type is given name 'Lambda1' for linkage purposes by this typedef declaration}}
|
|
56
|
|
57 template<int> struct X {};
|
|
58 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
|
|
59 X<[]{ return 0; }()> x; // expected-note {{type is not C-compatible due to this lambda expression}}
|
|
60 // FIXME: expected-error@-1 {{lambda expression cannot appear}}
|
|
61 } Lambda2; // expected-note {{type is given name 'Lambda2' for linkage purposes by this typedef declaration}}
|
|
62
|
|
63 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
|
|
64 enum E {
|
|
65 a = []{ return 1; }() // expected-note {{type is not C-compatible due to this lambda expression}}
|
|
66 };
|
|
67 } Lambda3; // expected-note {{type is given name 'Lambda3' for linkage purposes by this typedef declaration}}
|
|
68 #endif
|
|
69
|
|
70 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
|
|
71 template<int> void f() {} // expected-note {{type is not C-compatible due to this member declaration}}
|
|
72 } Template; // expected-note {{type is given name 'Template' for linkage purposes by this typedef declaration}}
|
|
73
|
|
74 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
|
|
75 struct U {
|
|
76 void f(); // expected-note {{type is not C-compatible due to this member declaration}}
|
|
77 };
|
|
78 } Nested; // expected-note {{type is given name 'Nested' for linkage purposes by this typedef declaration}}
|
|
79
|
|
80 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
|
|
81 friend void f() {} // expected-note {{type is not C-compatible due to this friend declaration}}
|
|
82 } Friend; // expected-note {{type is given name 'Friend' for linkage purposes by this typedef declaration}}
|
|
83
|
|
84 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
|
|
85 template<typename T> friend void f() {} // expected-note {{type is not C-compatible due to this friend declaration}}
|
|
86 } FriendTemplate; // expected-note {{type is given name 'FriendTemplate' for linkage purposes by this typedef declaration}}
|
|
87
|
|
88 // Check that we don't diagnose the permitted cases:
|
|
89 typedef struct {
|
|
90 // (non-members)
|
|
91 _Static_assert(true, "");
|
|
92 int : 0;
|
|
93 /*empty-declaration*/;
|
|
94
|
|
95 // non-static data members
|
|
96 int a;
|
|
97 // member enumerations
|
|
98 enum E { x, y, z };
|
|
99 // member classes
|
|
100 struct S {};
|
|
101
|
|
102 // recursively
|
|
103 struct T { int a; };
|
|
104 } OK;
|
|
105
|
|
106 // There are still some known permitted cases that require an early linkage
|
|
107 // computation. Ensure we diagnose those too.
|
|
108 namespace ValidButUnsupported {
|
|
109 #if __cplusplus >= 201402L
|
|
110 template<typename T> auto compute_linkage() {
|
|
111 static int n;
|
|
112 return &n;
|
|
113 }
|
|
114
|
|
115 typedef struct { // expected-error {{unsupported: anonymous type given name for linkage purposes by typedef declaration after its linkage was computed; add a tag name here to establish linkage}}
|
|
116 struct X {};
|
|
117 decltype(compute_linkage<X>()) a;
|
|
118 } A; // expected-note {{by this typedef declaration}}
|
|
119 #endif
|
|
120
|
|
121 // This fails in some language modes but not others.
|
|
122 template<typename T> struct Y {
|
|
123 static const int value = 10;
|
|
124 };
|
|
125 typedef struct { // expected-error 0-1{{unsupported}}
|
|
126 enum X {};
|
|
127 int arr[Y<X>::value];
|
|
128 } B; // expected-note 0-1{{by this typedef}}
|
|
129
|
|
130 template<typename T> void f() {}
|
|
131 typedef struct { // expected-error {{unsupported}}
|
|
132 enum X {};
|
|
133 int arr[&f<X> ? 1 : 2];
|
221
|
134 #if __cplusplus < 201103L
|
|
135 // expected-warning@-2 {{folded to constant}}
|
|
136 #endif
|
150
|
137 } C; // expected-note {{by this typedef}}
|
|
138 }
|
173
|
139
|
|
140 namespace ImplicitDecls {
|
|
141 struct Destructor {
|
|
142 ~Destructor() {}
|
|
143 };
|
|
144 typedef struct {
|
|
145 } Empty;
|
|
146
|
|
147 typedef struct {
|
|
148 Destructor x;
|
|
149 } A;
|
|
150
|
|
151 typedef struct {
|
|
152 Empty E;
|
|
153 } B;
|
|
154
|
|
155 typedef struct {
|
|
156 const Empty E;
|
|
157 } C;
|
|
158 } // namespace ImplicitDecls
|
221
|
159
|
|
160 struct {
|
|
161 static int x; // expected-error {{static data member 'x' not allowed in anonymous struct}}
|
|
162 } static_member_1;
|
|
163
|
|
164 class {
|
|
165 struct A {
|
|
166 static int x; // expected-error {{static data member 'x' not allowed in anonymous class}}
|
|
167 } x;
|
|
168 } static_member_2;
|
|
169
|
|
170 union {
|
|
171 struct A {
|
|
172 struct B {
|
|
173 static int x; // expected-error {{static data member 'x' not allowed in anonymous union}}
|
|
174 } x;
|
|
175 } x;
|
|
176 } static_member_3;
|
|
177
|
|
178 // Ensure we don't compute the linkage of a member function just because it
|
|
179 // happens to have the same name as a builtin.
|
|
180 namespace BuiltinName {
|
|
181 // Note that this is not an error: we didn't trigger linkage computation in this example.
|
|
182 typedef struct { // expected-warning {{anonymous non-C-compatible type}}
|
|
183 void memcpy(); // expected-note {{due to this member}}
|
|
184 } A; // expected-note {{given name 'A' for linkage purposes by this typedef}}
|
|
185 }
|
236
|
186 namespace inline_defined_static_member {
|
|
187 typedef struct { // expected-warning {{anonymous non-C-compatible type}}
|
|
188 static void f() { // expected-note {{due to this member}}
|
|
189 }
|
|
190 } A; // expected-note {{given name 'A' for linkage purposes by this typedef}}
|
|
191 }
|
252
|
192
|
|
193 #if __cplusplus > 201103L
|
|
194 namespace GH58800 {
|
|
195 struct A {
|
|
196 union {
|
|
197 struct {
|
|
198 float red = 0.0f;
|
|
199 };
|
|
200 };
|
|
201 };
|
|
202
|
|
203 A GetA() {
|
|
204 A result{};
|
|
205 return result;
|
|
206 }
|
|
207 }
|
|
208 #endif
|