1 // RUN: %clang_cc1 -pedantic -Wall -Wno-comment -verify -fcxx-exceptions -x c++ %s
2 // RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits -x c++ -std=c++11 %s 2>&1 | FileCheck %s
3 // RUN: cp %s %t
4 // 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
6 
7 /* This is a test of the various code modification hints that are
8    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. */
11 
12 struct C1 {
13   virtual void f();
14   static void g();
15 };
16 struct C2 : virtual public virtual C1 { }; // expected-error{{duplicate}}
17 
f()18 virtual void C1::f() { } // expected-error{{'virtual' can only be specified inside the class definition}}
19 
g()20 static void C1::g() { } // expected-error{{'static' can only be specified inside the class definition}}
21 
22 template<int Value> struct CT { template<typename> struct Inner; }; // expected-note{{previous use is here}}
23 
24 CT<10 >> 2> ct; // expected-warning{{require parentheses}}
25 
26 class C3 {
27 public:
28   C3(C3, int i = 0); // expected-error{{copy constructor must pass its first argument by reference}}
29 };
30 
31 struct CT<0> { }; // expected-error{{'template<>'}}
32 
33 template<> union CT<1> { }; // expected-error{{tag type}}
34 
35 struct CT<2>::Inner<int> { }; // expected-error 2{{'template<>'}}
36 
37 // Access declarations
38 class A {
39 protected:
40   int foo();
41 };
42 
43 class B : public A {
44   A::foo; // expected-warning{{access declarations are deprecated}}
45 };
46 
47 void f() throw(); // expected-note{{previous}}
48 void f(); // expected-warning{{missing exception specification}}
49 
50 namespace rdar7853795 {
51   struct A {
52     bool getNumComponents() const; // expected-note{{declared here}}
dumprdar7853795::A53     void dump() const {
54       getNumComponenets(); // expected-error{{use of undeclared identifier 'getNumComponenets'; did you mean 'getNumComponents'?}}
55     }
56   };
57 }
58 
59 namespace rdar7796492 {
60   struct A { int x, y; A(); };
61 
A()62   A::A()
63     : x(1) y(2) { // expected-error{{missing ',' between base or member initializers}}
64   }
65 
66 }
67 
68 // extra qualification on member
69 class C {
70   int C::foo(); // expected-error {{extra qualification}}
71 };
72 
73 namespace rdar8488464 {
74 int x = 0;
75 int x1 &= 0; // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
76 int x2 *= 0; // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
77 int x3 += 0; // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
78 int x4 -= 0; // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
79 int x5 != 0; // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
80 int x6 /= 0; // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
81 int x7 %= 0; // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
82 int x8 <= 0; // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
83 int x9 <<= 0; // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
84 int x10 >= 0; // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
85 int x11 >>= 0; // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
86 int x12 ^= 0; // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
87 int x13 |= 0; // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
88 int x14 == 0; // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
89 
f()90 void f() {
91     int x = 0;
92     (void)x;
93     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 '='?}}
136 }
137 }
138 
139 template <class A>
140 class F1 {
141 public:
142   template <int B>
143   class Iterator {
144   };
145 };
146 
147 template<class T>
148 class F2  {
149   typename F1<T>:: /*template*/  Iterator<0> Mypos; // expected-error {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
150 };
151 
152 template <class T>
f()153 void f(){
154   typename F1<T>:: /*template*/ Iterator<0> Mypos; // expected-error {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
155 }
156 
157 // Tests for &/* fixits radar 7113438.
158 class AD {};
159 class BD: public AD {};
160 
test(BD & br)161 void 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 &}}
165   aPtr = br; // expected-error {{assigning to 'AD *' from incompatible type 'BD'; take the address with &}}
166 }
167 
foo1() const168 void foo1() const {} // expected-error {{non-member function cannot have 'const' qualifier}}
foo2()169 void foo2() volatile {} // expected-error {{non-member function cannot have 'volatile' qualifier}}
foo3() const170 void foo3() const volatile {} // expected-error {{non-member function cannot have 'const volatile' qualifier}}
171 
172 struct S { void f(int, char); };
173 int itsAComma,
174 itsAComma2 = 0,
175 oopsAComma(42), // expected-error {{expected ';' at end of declaration}}
176 AD oopsMoreCommas() {
177   static int n = 0, // expected-error {{expected ';' at end of declaration}}
178   static char c,
179   &d = c, // expected-error {{expected ';' at end of declaration}}
180   S s, // expected-error {{expected ';' at end of declaration}}
181   s.f(n, d);
182   AD ad, // expected-error {{expected ';' at end of declaration}}
183   return ad;
184 }
185 struct 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}}
192 public:
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 };
198 
199 template<class T> struct Mystery;
200 template<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 }
205 
206 template<template<typename> Foo, // expected-error {{template template parameter requires 'class' after the parameter list}}
207          template<typename> typename Bar, // expected-warning {{template template parameter using 'typename' is a C++1z extension}}
208          template<typename> struct Baz> // expected-error {{template template parameter requires 'class' after the parameter list}}
209 void func();
210 
211 namespace ShadowedTagType {
212 class Foo {
213  public:
214   enum Bar { X, Y };
215   void SetBar(Bar bar);
216   Bar Bar(); // expected-note 2 {{enum 'Bar' is hidden by a non-type declaration of 'Bar' here}}
217  private:
218   Bar bar_; // expected-error {{must use 'enum' tag to refer to type 'Bar' in this scope}}
219 };
SetBar(Bar bar)220 void Foo::SetBar(Bar bar) { bar_ = bar; } // expected-error {{must use 'enum' tag to refer to type 'Bar' in this scope}}
221 }
222 
223 #define NULL __null
224 char c = NULL; // expected-warning {{implicit conversion of NULL constant to 'char'}}
225 double dbl = NULL; // expected-warning {{implicit conversion of NULL constant to 'double'}}
226 
227 namespace arrow_suggest {
228 
229 template <typename T>
230 class wrapped_ptr {
231  public:
wrapped_ptr(T * ptr)232   wrapped_ptr(T* ptr) : ptr_(ptr) {}
operator ->()233   T* operator->() { return ptr_; }
234  private:
235   T *ptr_;
236 };
237 
238 class Worker {
239  public:
240   void DoSomething();
241 };
242 
test()243 void 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
249 
250 // Make sure fixing namespace-qualified identifiers functions properly with
251 // namespace-aware typo correction/
252 namespace redecl_typo {
253 namespace Foo {
254   void BeEvil(); // expected-note {{'BeEvil' declared here}}
255 }
256 namespace Bar {
257   namespace Foo {
258     bool isGood(); // expected-note {{'Bar::Foo::isGood' declared here}}
259     void beEvil();
260   }
261 }
isGood()262 bool 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 }
beEvil()265 void 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 }
267 
268 // Test behavior when a template-id is ended by a token which starts with '>'.
269 namespace 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();
g()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 }
298 
299 class foo {
test()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 };
305 
306 namespace dtor_fixit {
307   class foo {
~bar()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   };
311 
312   class bar {
313     ~bar();
314   };
bar()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}:"~"
318 }
319 
320 namespace PR5066 {
321   template<typename T> struct X {};
322   X<int *p> x; // expected-error {{type-id cannot have a name}}
323 }
324 
325 namespace PR5898 {
326   class A {
327   public:
328     const char *str();
329   };
foo(A & x)330   const char* foo(A &x)
331   {
332     return x.str.();  // expected-error {{unexpected '.' in function call; perhaps remove the '.'?}}
333   }
bar(A x,const char * y)334   bool bar(A x, const char *y) {
335     return foo->(x) == y;  // expected-error {{unexpected '->' in function call; perhaps remove the '->'?}}
336   }
337 }
338 
339 namespace PR15045 {
340   class Cl0 {
341   public:
342     int a;
343   };
344 
f()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 }
350 
351 namespace curly_after_base_clause {
352 struct A { void f(); };
353 struct B : A // expected-error{{expected '{' after base class list}}
354   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
355   int i;
356 };
357 struct C : A // expected-error{{expected '{' after base class list}}
358   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
359   using A::f;
360 };
361 struct D : A // expected-error{{expected '{' after base class list}}
362   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
363     protected:
364 };
365 struct 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 };
369 struct 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
374 struct G : A  // expected-error{{expected '{' after base class list}}
375   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
376   constexpr G(int) { }
377 };
378 struct 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 }
384 
385 struct 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 };
390