blob: 585c216f90dee48cb1ab9e758530ebc7ec723cb5 [file] [log] [blame]
Richard Smith53711602011-11-29 09:09:06 +00001// RUN: %clang_cc1 -pedantic -Wall -Wno-comment -verify -fcxx-exceptions -x c++ %s
Ismail Pazarbasi129c44c2014-09-25 21:13:02 +00002// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits -x c++ -std=c++11 %s 2>&1 | FileCheck %s
Nick Lewycky784fad72010-04-24 01:30:46 +00003// RUN: cp %s %t
Richard Smith53711602011-11-29 09:09:06 +00004// RUN: not %clang_cc1 -pedantic -Wall -Wno-comment -fcxx-exceptions -fixit -x c++ %t
5// RUN: %clang_cc1 -fsyntax-only -pedantic -Wall -Werror -Wno-comment -fcxx-exceptions -x c++ %t
Douglas Gregore3e01a22009-04-01 22:41:11 +00006
7/* This is a test of the various code modification hints that are
Douglas Gregor7e702042009-04-02 03:20:30 +00008 provided as part of warning or extension diagnostics. All of the
9 warnings will be fixed by -fixit, and the resulting file should
10 compile cleanly with -Werror -pedantic. */
Douglas Gregore3e01a22009-04-01 22:41:11 +000011
Nick Lewycky784fad72010-04-24 01:30:46 +000012struct C1 {
Douglas Gregor170512f2009-04-01 23:51:29 +000013 virtual void f();
14 static void g();
15};
Douglas Gregore3e01a22009-04-01 22:41:11 +000016struct C2 : virtual public virtual C1 { }; // expected-error{{duplicate}}
17
Douglas Gregor170512f2009-04-01 23:51:29 +000018virtual void C1::f() { } // expected-error{{'virtual' can only be specified inside the class definition}}
19
20static void C1::g() { } // expected-error{{'static' can only be specified inside the class definition}}
21
Richard Smith4b55a9c2014-04-17 03:29:33 +000022template<int Value> struct CT { template<typename> struct Inner; }; // expected-note{{previous use is here}}
Douglas Gregore3e01a22009-04-01 22:41:11 +000023
24CT<10 >> 2> ct; // expected-warning{{require parentheses}}
Douglas Gregor170512f2009-04-01 23:51:29 +000025
26class C3 {
27public:
28 C3(C3, int i = 0); // expected-error{{copy constructor must pass its first argument by reference}}
29};
30
31struct CT<0> { }; // expected-error{{'template<>'}}
32
Richard Smith4e9ea712011-10-20 01:41:28 +000033template<> union CT<1> { }; // expected-error{{tag type}}
Anders Carlsson0b8ea552009-11-10 03:24:44 +000034
Richard Smith4b55a9c2014-04-17 03:29:33 +000035struct CT<2>::Inner<int> { }; // expected-error 2{{'template<>'}}
36
Douglas Gregorf1d70ad2010-02-01 23:46:27 +000037// Access declarations
38class A {
39protected:
40 int foo();
41};
Anders Carlsson0b8ea552009-11-10 03:24:44 +000042
Douglas Gregorf1d70ad2010-02-01 23:46:27 +000043class B : public A {
44 A::foo; // expected-warning{{access declarations are deprecated}}
45};
Douglas Gregord6bc5e62010-03-24 07:14:45 +000046
Richard Smith4e9ea712011-10-20 01:41:28 +000047void f() throw(); // expected-note{{previous}}
Douglas Gregord6bc5e62010-03-24 07:14:45 +000048void f(); // expected-warning{{missing exception specification}}
Douglas Gregor2fb18b72010-04-14 20:27:54 +000049
50namespace rdar7853795 {
51 struct A {
52 bool getNumComponents() const; // expected-note{{declared here}}
Nick Lewycky784fad72010-04-24 01:30:46 +000053 void dump() const {
Douglas Gregor2fb18b72010-04-14 20:27:54 +000054 getNumComponenets(); // expected-error{{use of undeclared identifier 'getNumComponenets'; did you mean 'getNumComponents'?}}
55 }
56 };
57}
Douglas Gregor3465e262010-09-07 14:35:10 +000058
59namespace rdar7796492 {
Benjamin Kramer697067f2012-06-11 16:06:57 +000060 struct A { int x, y; A(); };
Douglas Gregor3465e262010-09-07 14:35:10 +000061
62 A::A()
63 : x(1) y(2) { // expected-error{{missing ',' between base or member initializers}}
64 }
65
66}
Gabor Greif80c21832010-09-08 00:31:13 +000067
Francois Pichet6d76e6c2010-10-01 21:19:28 +000068// extra qualification on member
69class C {
Douglas Gregor43bc0362012-09-13 20:16:20 +000070 int C::foo(); // expected-error {{extra qualification}}
Francois Pichet6d76e6c2010-10-01 21:19:28 +000071};
72
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +000073namespace rdar8488464 {
Richard Trieu4972a6d2012-01-19 22:01:51 +000074int x = 0;
75int x1 &= 0; // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
76int x2 *= 0; // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
77int x3 += 0; // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
78int x4 -= 0; // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
79int x5 != 0; // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
80int x6 /= 0; // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
81int x7 %= 0; // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
82int x8 <= 0; // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
83int x9 <<= 0; // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
84int x10 >= 0; // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
85int x11 >>= 0; // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
86int x12 ^= 0; // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
87int x13 |= 0; // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
88int x14 == 0; // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +000089
90void f() {
Richard Trieu4972a6d2012-01-19 22:01:51 +000091 int x = 0;
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +000092 (void)x;
Richard Trieu4972a6d2012-01-19 22:01:51 +000093 int x1 &= 0; // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
94 (void)x1;
95 int x2 *= 0; // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
96 (void)x2;
97 int x3 += 0; // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
98 (void)x3;
99 int x4 -= 0; // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
100 (void)x4;
101 int x5 != 0; // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
102 (void)x5;
103 int x6 /= 0; // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
104 (void)x6;
105 int x7 %= 0; // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
106 (void)x7;
107 int x8 <= 0; // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
108 (void)x8;
109 int x9 <<= 0; // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
110 (void)x9;
111 int x10 >= 0; // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
112 (void)x10;
113 int x11 >>= 0; // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
114 (void)x11;
115 int x12 ^= 0; // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
116 (void)x12;
117 int x13 |= 0; // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
118 (void)x13;
119 int x14 == 0; // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
120 (void)x14;
121 if (int x = 0) { (void)x; }
122 if (int x1 &= 0) { (void)x1; } // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
123 if (int x2 *= 0) { (void)x2; } // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
124 if (int x3 += 0) { (void)x3; } // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
125 if (int x4 -= 0) { (void)x4; } // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
126 if (int x5 != 0) { (void)x5; } // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
127 if (int x6 /= 0) { (void)x6; } // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
128 if (int x7 %= 0) { (void)x7; } // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
129 if (int x8 <= 0) { (void)x8; } // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
130 if (int x9 <<= 0) { (void)x9; } // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
131 if (int x10 >= 0) { (void)x10; } // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
132 if (int x11 >>= 0) { (void)x11; } // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
133 if (int x12 ^= 0) { (void)x12; } // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
134 if (int x13 |= 0) { (void)x13; } // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
135 if (int x14 == 0) { (void)x14; } // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000136}
137}
138
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000139template <class A>
140class F1 {
141public:
142 template <int B>
143 class Iterator {
144 };
145};
146
147template<class T>
148class F2 {
149 typename F1<T>:: /*template*/ Iterator<0> Mypos; // expected-error {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
150};
151
152template <class T>
153void f(){
154 typename F1<T>:: /*template*/ Iterator<0> Mypos; // expected-error {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
155}
156
Anna Zaks3b402712011-07-28 19:51:27 +0000157// Tests for &/* fixits radar 7113438.
158class AD {};
159class BD: public AD {};
160
161void test (BD &br) {
162 AD* aPtr;
163 BD b;
164 aPtr = b; // expected-error {{assigning to 'AD *' from incompatible type 'BD'; take the address with &}}
Richard Smith4e9ea712011-10-20 01:41:28 +0000165 aPtr = br; // expected-error {{assigning to 'AD *' from incompatible type 'BD'; take the address with &}}
Anna Zaks3b402712011-07-28 19:51:27 +0000166}
167
Richard Smith63168c72012-02-10 11:05:11 +0000168void foo1() const {} // expected-error {{non-member function cannot have 'const' qualifier}}
169void foo2() volatile {} // expected-error {{non-member function cannot have 'volatile' qualifier}}
170void foo3() const volatile {} // expected-error {{non-member function cannot have 'const volatile' qualifier}}
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000171
Richard Smith09f76ee2011-10-19 21:33:05 +0000172struct S { void f(int, char); };
173int itsAComma,
174itsAComma2 = 0,
Richard Smith4e9ea712011-10-20 01:41:28 +0000175oopsAComma(42), // expected-error {{expected ';' at end of declaration}}
Richard Smith09f76ee2011-10-19 21:33:05 +0000176AD oopsMoreCommas() {
Richard Smith4e9ea712011-10-20 01:41:28 +0000177 static int n = 0, // expected-error {{expected ';' at end of declaration}}
Richard Smith09f76ee2011-10-19 21:33:05 +0000178 static char c,
Richard Smith4e9ea712011-10-20 01:41:28 +0000179 &d = c, // expected-error {{expected ';' at end of declaration}}
180 S s, // expected-error {{expected ';' at end of declaration}}
Richard Smith09f76ee2011-10-19 21:33:05 +0000181 s.f(n, d);
Richard Smith4e9ea712011-10-20 01:41:28 +0000182 AD ad, // expected-error {{expected ';' at end of declaration}}
Richard Smith09f76ee2011-10-19 21:33:05 +0000183 return ad;
184}
Richard Smithc8a79032012-01-09 22:31:44 +0000185struct MoreAccidentalCommas {
186 int a : 5,
187 b : 7,
188 : 4, // expected-error {{expected ';' at end of declaration}}
189 char c, // expected-error {{expected ';' at end of declaration}}
190 double d, // expected-error {{expected ';' at end of declaration}}
191 MoreAccidentalCommas *next, // expected-error {{expected ';' at end of declaration}}
192public:
193 int k, // expected-error {{expected ';' at end of declaration}}
194 friend void f(MoreAccidentalCommas) {}
195 int k2, // expected-error {{expected ';' at end of declaration}}
196 virtual void g(), // expected-error {{expected ';' at end of declaration}}
197};
Richard Smith675ea992011-11-29 05:27:40 +0000198
Richard Smith53711602011-11-29 09:09:06 +0000199template<class T> struct Mystery;
200template<class T> typedef Mystery<T>::type getMysteriousThing() { // \
201 expected-error {{function definition declared 'typedef'}} \
202 expected-error {{missing 'typename' prior to dependent}}
203 return Mystery<T>::get();
204}
David Blaikief221e512012-04-02 19:15:28 +0000205
David Blaikie36979832012-04-06 06:28:32 +0000206template<template<typename> Foo, // expected-error {{template template parameter requires 'class' after the parameter list}}
Richard Smith78e1ca62014-06-16 15:51:22 +0000207 template<typename> typename Bar, // expected-warning {{template template parameter using 'typename' is a C++1z extension}}
David Blaikie36979832012-04-06 06:28:32 +0000208 template<typename> struct Baz> // expected-error {{template template parameter requires 'class' after the parameter list}}
David Blaikief221e512012-04-02 19:15:28 +0000209void func();
Kaelyn Uhrainb3967d72012-04-19 23:17:45 +0000210
Kaelyn Uhrainb3967d72012-04-19 23:17:45 +0000211namespace ShadowedTagType {
212class Foo {
213 public:
214 enum Bar { X, Y };
215 void SetBar(Bar bar);
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +0000216 Bar Bar(); // expected-note 2 {{enum 'Bar' is hidden by a non-type declaration of 'Bar' here}}
Kaelyn Uhrainb3967d72012-04-19 23:17:45 +0000217 private:
218 Bar bar_; // expected-error {{must use 'enum' tag to refer to type 'Bar' in this scope}}
219};
220void Foo::SetBar(Bar bar) { bar_ = bar; } // expected-error {{must use 'enum' tag to refer to type 'Bar' in this scope}}
Kaelyn Uhraind7836172012-04-19 23:26:12 +0000221}
Kaelyn Uhrain76e07342012-04-25 19:49:54 +0000222
David Blaikie7665a622012-04-30 18:27:22 +0000223#define NULL __null
224char c = NULL; // expected-warning {{implicit conversion of NULL constant to 'char'}}
David Blaikie9366d2b2012-06-19 21:19:06 +0000225double dbl = NULL; // expected-warning {{implicit conversion of NULL constant to 'double'}}
David Blaikie7665a622012-04-30 18:27:22 +0000226
Kaelyn Uhrain76e07342012-04-25 19:49:54 +0000227namespace arrow_suggest {
228
229template <typename T>
230class wrapped_ptr {
231 public:
232 wrapped_ptr(T* ptr) : ptr_(ptr) {}
233 T* operator->() { return ptr_; }
234 private:
235 T *ptr_;
236};
237
238class Worker {
239 public:
240 void DoSomething();
241};
242
243void test() {
244 wrapped_ptr<Worker> worker(new Worker);
245 worker.DoSomething(); // expected-error {{no member named 'DoSomething' in 'arrow_suggest::wrapped_ptr<arrow_suggest::Worker>'; did you mean to use '->' instead of '.'?}}
246}
247
248} // namespace arrow_suggest
Kaelyn Uhrain1dac08d2012-06-07 23:57:12 +0000249
250// Make sure fixing namespace-qualified identifiers functions properly with
251// namespace-aware typo correction/
252namespace redecl_typo {
253namespace Foo {
254 void BeEvil(); // expected-note {{'BeEvil' declared here}}
255}
256namespace Bar {
257 namespace Foo {
258 bool isGood(); // expected-note {{'Bar::Foo::isGood' declared here}}
259 void beEvil();
260 }
261}
262bool Foo::isGood() { // expected-error {{out-of-line definition of 'isGood' does not match any declaration in namespace 'redecl_typo::Foo'; did you mean 'Bar::Foo::isGood'?}}
263 return true;
264}
265void Foo::beEvil() {} // expected-error {{out-of-line definition of 'beEvil' does not match any declaration in namespace 'redecl_typo::Foo'; did you mean 'BeEvil'?}}
266}
Richard Smith7b3f3222012-06-18 06:11:04 +0000267
268// Test behavior when a template-id is ended by a token which starts with '>'.
269namespace greatergreater {
270 template<typename T> struct S { S(); S(T); };
271 void f(S<int>=0); // expected-error {{a space is required between a right angle bracket and an equals sign (use '> =')}}
272
273 // FIXME: The fix-its here overlap so -fixit mode can't apply the second one.
274 //void f(S<S<int>>=S<int>());
275
276 struct Shr {
277 template<typename T> Shr(T);
278 template<typename T> void operator >>=(T);
279 };
280
281 template<template<typename>> struct TemplateTemplateParam; // expected-error {{requires 'class'}}
282
283 template<typename T> void t();
284 void g() {
285 void (*p)() = &t<int>;
286 (void)(&t<int>==p); // expected-error {{use '> ='}}
287 (void)(&t<int>>=p); // expected-error {{use '> >'}}
288 (void)(&t<S<int>>>=p); // expected-error {{use '> >'}}
289 (Shr)&t<S<int>>>>=p; // expected-error {{use '> >'}}
290
291 // FIXME: We correct this to '&t<int> > >= p;' not '&t<int> >>= p;'
292 //(Shr)&t<int>>>=p;
293
294 // FIXME: The fix-its here overlap.
295 //(void)(&t<S<int>>==p);
296 }
297}
David Blaikiec2ff8e12012-10-11 22:55:07 +0000298
299class foo {
300 static void test() {
301 (void)&i; // expected-error{{must explicitly qualify name of member function when taking its address}}
302 }
303 int i();
304};
David Blaikie5e026f52013-03-20 17:42:13 +0000305
306namespace dtor_fixit {
307 class foo {
308 ~bar() { } // expected-error {{expected the class name after '~' to name a destructor}}
309 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:6-[[@LINE-1]]:9}:"foo"
310 };
Richard Smithefa6f732014-09-06 02:06:12 +0000311
312 class bar {
313 ~bar();
314 };
315 ~bar::bar() {} // expected-error {{'~' in destructor name should be after nested name specifier}}
316 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:4}:""
317 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:9-[[@LINE-2]]:9}:"~"
David Blaikie5e026f52013-03-20 17:42:13 +0000318}
Richard Smith9ce302e2013-07-11 05:10:21 +0000319
320namespace PR5066 {
321 template<typename T> struct X {};
322 X<int *p> x; // expected-error {{type-id cannot have a name}}
323}
Kaelyn Uhrain1bb5dbf2013-07-11 22:38:30 +0000324
Kaelyn Uhrain638264e2013-07-12 21:43:02 +0000325namespace PR5898 {
326 class A {
327 public:
328 const char *str();
329 };
330 const char* foo(A &x)
331 {
332 return x.str.(); // expected-error {{unexpected '.' in function call; perhaps remove the '.'?}}
333 }
334 bool bar(A x, const char *y) {
335 return foo->(x) == y; // expected-error {{unexpected '->' in function call; perhaps remove the '->'?}}
336 }
337}
Kaelyn Uhrain957c8b12013-07-31 20:16:17 +0000338
339namespace PR15045 {
340 class Cl0 {
341 public:
342 int a;
343 };
344
345 int f() {
346 Cl0 c;
347 return c->a; // expected-error {{member reference type 'PR15045::Cl0' is not a pointer; maybe you meant to use '.'?}}
348 }
349}
Ismail Pazarbasi129c44c2014-09-25 21:13:02 +0000350
351namespace curly_after_base_clause {
352struct A { void f(); };
353struct B : A // expected-error{{expected '{' after base class list}}
354 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
355 int i;
356};
357struct C : A // expected-error{{expected '{' after base class list}}
358 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
359 using A::f;
360};
361struct D : A // expected-error{{expected '{' after base class list}}
362 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
363 protected:
364};
365struct E : A // expected-error{{expected '{' after base class list}}
366 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
367 template<typename T> struct inner { };
368};
369struct F : A // expected-error{{expected '{' after base class list}}
370 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
371 F() { }
372};
373#if __cplusplus >= 201103L
374struct G : A // expected-error{{expected '{' after base class list}}
375 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
376 constexpr G(int) { }
377};
378struct H : A // expected-error{{expected '{' after base class list}}
379 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
380 static_assert(true, "");
381};
382#endif
383}
Richard Smitha865a162014-12-19 02:07:47 +0000384
385struct conversion_operator {
386 conversion_operator::* const operator int(); // expected-error {{put the complete type after 'operator'}}
387 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:32}:""
388 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:44-[[@LINE-2]]:44}:" conversion_operator::* const"
389};