xref: /minix3/external/bsd/llvm/dist/clang/test/FixIt/fixit.cpp (revision eda6f5931d42c77e1480347b1fc3eef2f8d33806)
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++ %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 
18 virtual void C1::f() { } // expected-error{{'virtual' can only be specified inside the class definition}}
19 
20 static void C1::g() { } // expected-error{{'static' can only be specified inside the class definition}}
21 
22 template<int Value> struct CT { }; // 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 // Access declarations
36 class A {
37 protected:
38   int foo();
39 };
40 
41 class B : public A {
42   A::foo; // expected-warning{{access declarations are deprecated}}
43 };
44 
45 void f() throw(); // expected-note{{previous}}
46 void f(); // expected-warning{{missing exception specification}}
47 
48 namespace rdar7853795 {
49   struct A {
50     bool getNumComponents() const; // expected-note{{declared here}}
51     void dump() const {
52       getNumComponenets(); // expected-error{{use of undeclared identifier 'getNumComponenets'; did you mean 'getNumComponents'?}}
53     }
54   };
55 }
56 
57 namespace rdar7796492 {
58   struct A { int x, y; A(); };
59 
60   A::A()
61     : x(1) y(2) { // expected-error{{missing ',' between base or member initializers}}
62   }
63 
64 }
65 
66 // extra qualification on member
67 class C {
68   int C::foo(); // expected-error {{extra qualification}}
69 };
70 
71 namespace rdar8488464 {
72 int x = 0;
73 int x1 &= 0; // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
74 int x2 *= 0; // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
75 int x3 += 0; // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
76 int x4 -= 0; // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
77 int x5 != 0; // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
78 int x6 /= 0; // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
79 int x7 %= 0; // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
80 int x8 <= 0; // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
81 int x9 <<= 0; // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
82 int x10 >= 0; // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
83 int x11 >>= 0; // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
84 int x12 ^= 0; // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
85 int x13 |= 0; // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
86 int x14 == 0; // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
87 
88 void f() {
89     int x = 0;
90     (void)x;
91     int x1 &= 0; // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
92     (void)x1;
93     int x2 *= 0; // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
94     (void)x2;
95     int x3 += 0; // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
96     (void)x3;
97     int x4 -= 0; // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
98     (void)x4;
99     int x5 != 0; // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
100     (void)x5;
101     int x6 /= 0; // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
102     (void)x6;
103     int x7 %= 0; // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
104     (void)x7;
105     int x8 <= 0; // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
106     (void)x8;
107     int x9 <<= 0; // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
108     (void)x9;
109     int x10 >= 0; // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
110     (void)x10;
111     int x11 >>= 0; // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
112     (void)x11;
113     int x12 ^= 0; // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
114     (void)x12;
115     int x13 |= 0; // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
116     (void)x13;
117     int x14 == 0; // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
118     (void)x14;
119     if (int x = 0)  { (void)x; }
120     if (int x1 &= 0) { (void)x1; } // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
121     if (int x2 *= 0) { (void)x2; } // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
122     if (int x3 += 0) { (void)x3; } // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
123     if (int x4 -= 0) { (void)x4; } // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
124     if (int x5 != 0) { (void)x5; } // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
125     if (int x6 /= 0) { (void)x6; } // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
126     if (int x7 %= 0) { (void)x7; } // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
127     if (int x8 <= 0) { (void)x8; } // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
128     if (int x9 <<= 0) { (void)x9; } // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
129     if (int x10 >= 0) { (void)x10; } // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
130     if (int x11 >>= 0) { (void)x11; } // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
131     if (int x12 ^= 0) { (void)x12; } // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
132     if (int x13 |= 0) { (void)x13; } // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
133     if (int x14 == 0) { (void)x14; } // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
134 }
135 }
136 
137 template <class A>
138 class F1 {
139 public:
140   template <int B>
141   class Iterator {
142   };
143 };
144 
145 template<class T>
146 class F2  {
147   typename F1<T>:: /*template*/  Iterator<0> Mypos; // expected-error {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
148 };
149 
150 template <class T>
151 void f(){
152   typename F1<T>:: /*template*/ Iterator<0> Mypos; // expected-error {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
153 }
154 
155 // Tests for &/* fixits radar 7113438.
156 class AD {};
157 class BD: public AD {};
158 
159 void test (BD &br) {
160   AD* aPtr;
161   BD b;
162   aPtr = b; // expected-error {{assigning to 'AD *' from incompatible type 'BD'; take the address with &}}
163   aPtr = br; // expected-error {{assigning to 'AD *' from incompatible type 'BD'; take the address with &}}
164 }
165 
166 void foo1() const {} // expected-error {{non-member function cannot have 'const' qualifier}}
167 void foo2() volatile {} // expected-error {{non-member function cannot have 'volatile' qualifier}}
168 void foo3() const volatile {} // expected-error {{non-member function cannot have 'const volatile' qualifier}}
169 
170 struct S { void f(int, char); };
171 int itsAComma,
172 itsAComma2 = 0,
173 oopsAComma(42), // expected-error {{expected ';' at end of declaration}}
174 AD oopsMoreCommas() {
175   static int n = 0, // expected-error {{expected ';' at end of declaration}}
176   static char c,
177   &d = c, // expected-error {{expected ';' at end of declaration}}
178   S s, // expected-error {{expected ';' at end of declaration}}
179   s.f(n, d);
180   AD ad, // expected-error {{expected ';' at end of declaration}}
181   return ad;
182 }
183 struct MoreAccidentalCommas {
184   int a : 5,
185       b : 7,
186         : 4, // expected-error {{expected ';' at end of declaration}}
187   char c, // expected-error {{expected ';' at end of declaration}}
188   double d, // expected-error {{expected ';' at end of declaration}}
189   MoreAccidentalCommas *next, // expected-error {{expected ';' at end of declaration}}
190 public:
191   int k, // expected-error {{expected ';' at end of declaration}}
192   friend void f(MoreAccidentalCommas) {}
193   int k2, // expected-error {{expected ';' at end of declaration}}
194   virtual void g(), // expected-error {{expected ';' at end of declaration}}
195 };
196 
197 template<class T> struct Mystery;
198 template<class T> typedef Mystery<T>::type getMysteriousThing() { // \
199   expected-error {{function definition declared 'typedef'}} \
200   expected-error {{missing 'typename' prior to dependent}}
201   return Mystery<T>::get();
202 }
203 
204 template<template<typename> Foo, // expected-error {{template template parameter requires 'class' after the parameter list}}
205          template<typename> typename Bar, // expected-error {{template template parameter requires 'class' after the parameter list}}
206          template<typename> struct Baz> // expected-error {{template template parameter requires 'class' after the parameter list}}
207 void func();
208 
209 namespace ShadowedTagType {
210 class Foo {
211  public:
212   enum Bar { X, Y };
213   void SetBar(Bar bar);
214   Bar Bar(); // expected-note 2 {{enum 'Bar' is hidden by a non-type declaration of 'Bar' here}}
215  private:
216   Bar bar_; // expected-error {{must use 'enum' tag to refer to type 'Bar' in this scope}}
217 };
218 void Foo::SetBar(Bar bar) { bar_ = bar; } // expected-error {{must use 'enum' tag to refer to type 'Bar' in this scope}}
219 }
220 
221 #define NULL __null
222 char c = NULL; // expected-warning {{implicit conversion of NULL constant to 'char'}}
223 double dbl = NULL; // expected-warning {{implicit conversion of NULL constant to 'double'}}
224 
225 namespace arrow_suggest {
226 
227 template <typename T>
228 class wrapped_ptr {
229  public:
230   wrapped_ptr(T* ptr) : ptr_(ptr) {}
231   T* operator->() { return ptr_; }
232  private:
233   T *ptr_;
234 };
235 
236 class Worker {
237  public:
238   void DoSomething();
239 };
240 
241 void test() {
242   wrapped_ptr<Worker> worker(new Worker);
243   worker.DoSomething(); // expected-error {{no member named 'DoSomething' in 'arrow_suggest::wrapped_ptr<arrow_suggest::Worker>'; did you mean to use '->' instead of '.'?}}
244 }
245 
246 } // namespace arrow_suggest
247 
248 // Make sure fixing namespace-qualified identifiers functions properly with
249 // namespace-aware typo correction/
250 namespace redecl_typo {
251 namespace Foo {
252   void BeEvil(); // expected-note {{'BeEvil' declared here}}
253 }
254 namespace Bar {
255   namespace Foo {
256     bool isGood(); // expected-note {{'Bar::Foo::isGood' declared here}}
257     void beEvil();
258   }
259 }
260 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'?}}
261   return true;
262 }
263 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'?}}
264 }
265 
266 // Test behavior when a template-id is ended by a token which starts with '>'.
267 namespace greatergreater {
268   template<typename T> struct S { S(); S(T); };
269   void f(S<int>=0); // expected-error {{a space is required between a right angle bracket and an equals sign (use '> =')}}
270 
271   // FIXME: The fix-its here overlap so -fixit mode can't apply the second one.
272   //void f(S<S<int>>=S<int>());
273 
274   struct Shr {
275     template<typename T> Shr(T);
276     template<typename T> void operator >>=(T);
277   };
278 
279   template<template<typename>> struct TemplateTemplateParam; // expected-error {{requires 'class'}}
280 
281   template<typename T> void t();
282   void g() {
283     void (*p)() = &t<int>;
284     (void)(&t<int>==p); // expected-error {{use '> ='}}
285     (void)(&t<int>>=p); // expected-error {{use '> >'}}
286     (void)(&t<S<int>>>=p); // expected-error {{use '> >'}}
287     (Shr)&t<S<int>>>>=p; // expected-error {{use '> >'}}
288 
289     // FIXME: We correct this to '&t<int> > >= p;' not '&t<int> >>= p;'
290     //(Shr)&t<int>>>=p;
291 
292     // FIXME: The fix-its here overlap.
293     //(void)(&t<S<int>>==p);
294   }
295 }
296 
297 class foo {
298   static void test() {
299     (void)&i; // expected-error{{must explicitly qualify name of member function when taking its address}}
300   }
301   int i();
302 };
303 
304 namespace dtor_fixit {
305   class foo {
306     ~bar() { }  // expected-error {{expected the class name after '~' to name a destructor}}
307     // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:6-[[@LINE-1]]:9}:"foo"
308   };
309 }
310 
311 namespace PR5066 {
312   template<typename T> struct X {};
313   X<int *p> x; // expected-error {{type-id cannot have a name}}
314 }
315 
316 namespace PR5898 {
317   class A {
318   public:
319     const char *str();
320   };
321   const char* foo(A &x)
322   {
323     return x.str.();  // expected-error {{unexpected '.' in function call; perhaps remove the '.'?}}
324   }
325   bool bar(A x, const char *y) {
326     return foo->(x) == y;  // expected-error {{unexpected '->' in function call; perhaps remove the '->'?}}
327   }
328 }
329 
330 namespace PR15045 {
331   class Cl0 {
332   public:
333     int a;
334   };
335 
336   int f() {
337     Cl0 c;
338     return c->a;  // expected-error {{member reference type 'PR15045::Cl0' is not a pointer; maybe you meant to use '.'?}}
339   }
340 }
341