blob: c60e42f28eefdb3fc4db8f05f1c98c783d5b62fc [file] [log] [blame]
Richard Smithf391db52017-12-21 20:50:39 +00001// RUN: %clang_cc1 -verify -fsyntax-only -triple i386-linux -Wredundant-parens -pedantic-errors -fcxx-exceptions -fexceptions %s
2// RUN: %clang_cc1 -verify -fsyntax-only -triple i386-linux -Wredundant-parens -pedantic-errors -fcxx-exceptions -fexceptions -std=c++98 %s
3// RUN: %clang_cc1 -verify -fsyntax-only -triple i386-linux -Wredundant-parens -pedantic-errors -fcxx-exceptions -fexceptions -std=c++11 %s
Chris Lattner869c6612009-04-12 22:23:27 +00004
Richard Smithc0a5d5b2014-01-10 21:27:55 +00005const char const *x10; // expected-error {{duplicate 'const' declaration specifier}}
Aaron Ballman3731b332012-08-28 20:55:40 +00006
Chris Lattner869c6612009-04-12 22:23:27 +00007int x(*g); // expected-error {{use of undeclared identifier 'g'}}
Chris Lattner1c428032009-12-07 01:36:53 +00008
Chris Lattner244b96b2009-12-10 02:02:58 +00009struct Type {
10 int Type;
11};
Chris Lattner17c3b1f2009-12-10 01:59:24 +000012
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +000013// rdar://8365458
Fariborz Jahanian2b059992011-04-19 21:42:37 +000014// rdar://9132143
15typedef char bool; // expected-error {{redeclaration of C++ built-in type 'bool'}}
Chris Lattner1c428032009-12-07 01:36:53 +000016
17// PR4451 - We should recover well from the typo of '::' as ':' in a2.
18namespace y {
Chris Lattner3c674cf2009-12-10 02:08:07 +000019 struct a { };
20 typedef int b;
Chris Lattner1c428032009-12-07 01:36:53 +000021}
22
23y::a a1;
24y:a a2; // expected-error {{unexpected ':' in nested name specifier}}
25y::a a3 = a2;
26
27// Some valid colons:
28void foo() {
29y: // label
30 y::a s;
31
32 int a = 4;
33 a = a ? a : a+1;
34}
35
36struct b : y::a {};
37
38template <typename T>
39class someclass {
40
41 int bar() {
42 T *P;
43 return 1 ? P->x : P->y;
44 }
45};
Chris Lattner17c3b1f2009-12-10 01:59:24 +000046
Chris Lattnerf3d3b362010-06-13 05:34:18 +000047class asm_class_test {
48 void foo() __asm__("baz");
49};
50
Charles Li1a88adb2016-04-14 23:47:07 +000051enum { fooenum = 1, };
52#if __cplusplus <= 199711L
53// expected-error@-2 {{commas at the end of enumerator lists are a C++11 extension}}
54#endif
Chris Lattner17c3b1f2009-12-10 01:59:24 +000055
56struct a {
57 int Type : fooenum;
58};
59
Chris Lattner244b96b2009-12-10 02:02:58 +000060void test(struct Type *P) {
61 int Type;
62 Type = 1 ? P->Type : Type;
Chris Lattner3c674cf2009-12-10 02:08:07 +000063
64 Type = (y:b) 4; // expected-error {{unexpected ':' in nested name specifier}}
65 Type = 1 ? (
66 (y:b) // expected-error {{unexpected ':' in nested name specifier}}
67 4) : 5;
Daniel Dunbar8fbe78f2009-12-15 20:14:24 +000068}
Chris Lattner916dbf12010-02-02 00:43:15 +000069
70struct test4 {
71 int x // expected-error {{expected ';' at end of declaration list}}
72 int y;
73 int z // expected-error {{expected ';' at end of declaration list}}
74};
John McCall8bc2a702010-03-01 18:20:46 +000075
Richard Smith09f76ee2011-10-19 21:33:05 +000076// Make sure we know these are legitimate commas and not typos for ';'.
77namespace Commas {
78 struct S {
79 static int a;
80 int c,
81 operator()();
82 };
83
84 int global1,
85 __attribute__(()) global2,
Richard Smithf391db52017-12-21 20:50:39 +000086 (global5), // expected-warning {{redundant parentheses surrounding declarator}}
Richard Smith09f76ee2011-10-19 21:33:05 +000087 *global6,
88 &global7 = global1,
Charles Li1a88adb2016-04-14 23:47:07 +000089 &&global8 = static_cast<int&&>(global1),
90#if __cplusplus <= 199711L
91 // expected-error@-2 2{{rvalue references are a C++11 extension}}
92#endif
93
Richard Smith09f76ee2011-10-19 21:33:05 +000094 S::a,
95 global9,
96 global10 = 0,
97 global11 == 0, // expected-error {{did you mean '='}}
98 global12 __attribute__(()),
99 global13(0),
100 global14[2],
101 global15;
102
103 void g() {
104 static int a,
105 b __asm__("ebx"), // expected-error {{expected ';' at end of declaration}}
106 Statics:return;
107 }
108}
109
John McCall8bc2a702010-03-01 18:20:46 +0000110// PR5825
111struct test5 {};
112::new(static_cast<void*>(0)) test5; // expected-error {{expected unqualified-id}}
Chris Lattner005fc1b2010-04-05 18:18:31 +0000113
114
115// PR6782
116template<class T>
117class Class1;
118
119class Class2 {
Richard Smith404dfb42013-11-19 22:47:36 +0000120} // expected-error {{expected ';' after class}}
Chris Lattner005fc1b2010-04-05 18:18:31 +0000121
Richard Smith404dfb42013-11-19 22:47:36 +0000122typedef Class1<Class2> Type1;
Fariborz Jahanian055068d2010-08-16 17:58:53 +0000123
124// rdar : // 8307865
125struct CodeCompleteConsumer {
126};
127
128void CodeCompleteConsumer::() { // expected-error {{xpected unqualified-id}}
129}
Chris Lattner07865442010-11-09 20:14:26 +0000130
131;
132
Richard Smith4f605af2012-08-18 00:55:03 +0000133// PR4111
134void f(sqrgl); // expected-error {{unknown type name 'sqrgl'}}
135
Richard Smith2603b092012-11-15 22:54:20 +0000136// PR9903
137struct S {
138 typedef void a() { }; // expected-error {{function definition declared 'typedef'}}
139 typedef void c() try { } catch(...) { } // expected-error {{function definition declared 'typedef'}}
140 int n, m;
141 typedef S() : n(1), m(2) { } // expected-error {{function definition declared 'typedef'}}
142};
143
Nico Weber0bba2d12013-01-18 18:41:42 +0000144
145namespace TestIsValidAfterTypeSpecifier {
Richard Smith1ac67d12013-01-19 03:48:05 +0000146struct s {} v;
Nico Weber0bba2d12013-01-18 18:41:42 +0000147
148namespace a {
149struct s operator++(struct s a)
150{ return a; }
151}
152
153namespace b {
154// The newline after s should make no difference.
155struct s
156operator++(struct s a)
157{ return a; }
158}
159
Richard Smith1ac67d12013-01-19 03:48:05 +0000160struct X {
161 struct s
162 friend f();
163 struct s
164 virtual f();
165};
166
167struct s
168&r0 = v;
169struct s
170bitand r2 = v;
171
Nico Weber0bba2d12013-01-18 18:41:42 +0000172}
173
Richard Trieu9c672672013-01-26 02:31:38 +0000174struct DIE {
175 void foo() {}
176};
177
178void test (DIE die, DIE *Die, DIE INT, DIE *FLOAT) {
179 DIE.foo(); // expected-error {{cannot use dot operator on a type}}
180 die.foo();
181
182 DIE->foo(); // expected-error {{cannot use arrow operator on a type}}
183 Die->foo();
184
185 int.foo(); // expected-error {{cannot use dot operator on a type}}
186 INT.foo();
187
188 float->foo(); // expected-error {{cannot use arrow operator on a type}}
189 FLOAT->foo();
190}
191
Richard Smith52c5b872013-01-29 04:13:32 +0000192namespace PR15017 {
Ismail Pazarbasi025f4282014-03-07 22:36:23 +0000193 template<typename T = struct X { int i; }> struct S {}; // expected-error {{'PR15017::X' cannot be defined in a type specifier}}
Richard Smith52c5b872013-01-29 04:13:32 +0000194}
195
Richard Smith4cabd042013-02-22 09:15:49 +0000196// Ensure we produce at least some diagnostic for attributes in C++98.
Charles Li1a88adb2016-04-14 23:47:07 +0000197[[]] struct S;
198#if __cplusplus <= 199711L
199// expected-error@-2 {{expected expression}}
200// expected-error@-3 {{expected unqualified-id}}
201#else
Faisal Valic5089c02017-12-25 22:23:20 +0000202// expected-error@-5 {{misplaced attributes}}
Charles Li1a88adb2016-04-14 23:47:07 +0000203#endif
Richard Smith4cabd042013-02-22 09:15:49 +0000204
Richard Trieu2f586962013-09-05 02:31:33 +0000205namespace test7 {
206 struct Foo {
207 void a();
208 void b();
209 };
210
211 void Foo::
212 // Comment!
213 a() {}
214
215
216 void Foo:: // expected-error {{expected unqualified-id}}
217 // Comment!
218}
219
Benjamin Kramer5fc787f2013-10-10 12:24:40 +0000220void test8() {
221 struct {} o;
222 // This used to crash.
223 (&o)->(); // expected-error{{expected unqualified-id}}
224}
225
Richard Smith9ce302e2013-07-11 05:10:21 +0000226namespace PR5066 {
227 template<typename T> struct X {};
228 X<int N> x; // expected-error {{type-id cannot have a name}}
229
Charles Li1a88adb2016-04-14 23:47:07 +0000230 using T = int (*T)(); // expected-error {{type-id cannot have a name}}
231#if __cplusplus <= 199711L
232 // expected-error@-2 {{alias declarations are a C++11 extensio}}
233#endif
234
Richard Smith9ce302e2013-07-11 05:10:21 +0000235}
236
David Majnemera5e92552013-09-22 01:24:26 +0000237namespace PR17255 {
238void foo() {
Charles Li1a88adb2016-04-14 23:47:07 +0000239 typename A::template B<>; // expected-error {{use of undeclared identifier 'A'}}
Richard Smithc08b6932018-04-27 02:00:13 +0000240 // expected-error@-1 {{'template' keyword not permitted here}}
Charles Li1a88adb2016-04-14 23:47:07 +0000241#if __cplusplus <= 199711L
Richard Smithc08b6932018-04-27 02:00:13 +0000242 // expected-error@-3 {{'template' keyword outside of a template}}
Charles Li1a88adb2016-04-14 23:47:07 +0000243#endif
Richard Smithc08b6932018-04-27 02:00:13 +0000244 // expected-error@-5 {{expected a qualified name after 'typename'}}
David Majnemera5e92552013-09-22 01:24:26 +0000245}
246}
247
Richard Smithfb8b7b92013-10-15 00:00:26 +0000248namespace PR17567 {
249 struct Foobar { // expected-note 2{{declared here}}
250 FooBar(); // expected-error {{missing return type for function 'FooBar'; did you mean the constructor name 'Foobar'?}}
251 ~FooBar(); // expected-error {{expected the class name after '~' to name a destructor}}
252 };
253 FooBar::FooBar() {} // expected-error {{undeclared}} expected-error {{missing return type}}
254 FooBar::~FooBar() {} // expected-error {{undeclared}} expected-error {{expected the class name}}
255}
256
Richard Smithc0a5d5b2014-01-10 21:27:55 +0000257namespace DuplicateFriend {
258 struct A {
259 friend void friend f(); // expected-warning {{duplicate 'friend' declaration specifier}}
260 friend struct B friend; // expected-warning {{duplicate 'friend' declaration specifier}}
Charles Li1a88adb2016-04-14 23:47:07 +0000261#if __cplusplus >= 201103L
262 // expected-error@-2 {{'friend' must appear first in a non-function declaration}}
263#endif
Richard Smithc0a5d5b2014-01-10 21:27:55 +0000264 };
265}
266
Richard Smithf391db52017-12-21 20:50:39 +0000267namespace NNS {
268 struct A {};
269 namespace B { extern A C1, C2, *C3, C4[], C5; }
270 // Do not produce a redundant parentheses warning here; removing these parens
271 // changes the meaning of the program.
272 A (::NNS::B::C1);
273 A (NNS::B::C2); // expected-warning {{redundant parentheses surrounding declarator}}
274 A (*::NNS::B::C3); // expected-warning {{redundant parentheses surrounding declarator}}
275 A (::NNS::B::C4[2]);
276 // Removing one of these sets of parentheses would be reasonable.
277 A ((::NNS::B::C5)); // expected-warning {{redundant parentheses surrounding declarator}}
278
279 void f() {
280 // FIXME: A vexing-parse warning here would be useful.
281 A(::NNS::B::C1); // expected-error {{definition or redeclaration}}
282 A(NNS::B::C1); // expected-warning {{redundant paren}} expected-error {{definition or redeclaration}}
283 }
284}
285
Richard Smithe9d87892017-12-21 22:26:47 +0000286inline namespace ParensAroundFriend { // expected-error 0-1{{C++11}}
287 struct A {};
288 struct B {
289 static A C();
290 };
291 namespace X {
292 struct B {};
293 struct D {
294 // No warning here: while this could be written as
295 // friend (::B::C)();
296 // we do need parentheses *somewhere* here.
297 friend A (::B::C());
298 };
299 }
300}
301
Volodymyr Sapsai9f7b5cc2018-04-10 18:29:47 +0000302namespace rdar37099386 {
303 class A typename A; // expected-error {{expected a qualified name after 'typename'}}
304 // expected-error@-1 {{cannot combine with previous 'class' declaration specifier}}
305}
306
Chris Lattner07865442010-11-09 20:14:26 +0000307// PR8380
308extern "" // expected-error {{unknown linkage language}}
Charles Li1a88adb2016-04-14 23:47:07 +0000309test6a { ;// expected-error {{C++ requires a type specifier for all declarations}}
310#if __cplusplus <= 199711L
311// expected-error@-2 {{expected ';' after top level declarator}}
312#else
313// expected-error@-4 {{expected expression}}
314// expected-note@-5 {{to match this}}
315#endif
Chris Lattner07865442010-11-09 20:14:26 +0000316
317 int test6b;
Charles Li1a88adb2016-04-14 23:47:07 +0000318#if __cplusplus >= 201103L
319// expected-error@+3 {{expected}}
320// expected-error@-3 {{expected ';' after top level declarator}}
321#endif
322