fixit.cpp revision 6e9d86b31813edafb775c93e5b5595ebce1e71e1
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
12struct C1 {
13  virtual void f();
14  static void g();
15};
16struct C2 : virtual public virtual C1 { }; // expected-error{{duplicate}}
17
18virtual 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
22template<int Value> struct CT { }; // expected-note{{previous use is here}}
23
24CT<10 >> 2> ct; // expected-warning{{require parentheses}}
25
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
33template<> union CT<1> { }; // expected-error{{tag type}}
34
35// Access declarations
36class A {
37protected:
38  int foo();
39};
40
41class B : public A {
42  A::foo; // expected-warning{{access declarations are deprecated}}
43};
44
45void f() throw(); // expected-note{{previous}}
46void f(); // expected-warning{{missing exception specification}}
47
48namespace 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
57namespace 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
67class C {
68  int C::foo(); // expected-error {{extra qualification}}
69};
70
71namespace rdar8488464 {
72int x = 0;
73int x1 &= 0; // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
74int x2 *= 0; // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
75int x3 += 0; // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
76int x4 -= 0; // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
77int x5 != 0; // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
78int x6 /= 0; // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
79int x7 %= 0; // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
80int x8 <= 0; // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
81int x9 <<= 0; // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
82int x10 >= 0; // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
83int x11 >>= 0; // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
84int x12 ^= 0; // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
85int x13 |= 0; // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
86int x14 == 0; // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
87
88void 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
137template <class A>
138class F1 {
139public:
140  template <int B>
141  class Iterator {
142  };
143};
144
145template<class T>
146class F2  {
147  typename F1<T>:: /*template*/  Iterator<0> Mypos; // expected-error {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
148};
149
150template <class T>
151void 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.
156class AD {};
157class BD: public AD {};
158
159void 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
166void foo1() const {} // expected-error {{non-member function cannot have 'const' qualifier}}
167void foo2() volatile {} // expected-error {{non-member function cannot have 'volatile' qualifier}}
168void foo3() const volatile {} // expected-error {{non-member function cannot have 'const volatile' qualifier}}
169
170struct S { void f(int, char); };
171int itsAComma,
172itsAComma2 = 0,
173oopsAComma(42), // expected-error {{expected ';' at end of declaration}}
174AD 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}
183struct 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}}
190public:
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
197template<class T> struct Mystery;
198template<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
204template<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}}
207void func();
208
209namespace ShadowedTagType {
210class 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};
218void 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
222char c = NULL; // expected-warning {{implicit conversion of NULL constant to 'char'}}
223double dbl = NULL; // expected-warning {{implicit conversion of NULL constant to 'double'}}
224
225namespace arrow_suggest {
226
227template <typename T>
228class wrapped_ptr {
229 public:
230  wrapped_ptr(T* ptr) : ptr_(ptr) {}
231  T* operator->() { return ptr_; }
232 private:
233  T *ptr_;
234};
235
236class Worker {
237 public:
238  void DoSomething();
239};
240
241void 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/
250namespace redecl_typo {
251namespace Foo {
252  void BeEvil(); // expected-note {{'BeEvil' declared here}}
253}
254namespace Bar {
255  namespace Foo {
256    bool isGood(); // expected-note {{'Bar::Foo::isGood' declared here}}
257    void beEvil();
258  }
259}
260bool 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}
263void 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 '>'.
267namespace 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
297class 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
304namespace 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
311namespace PR5066 {
312  template<typename T> struct X {};
313  X<int *p> x; // expected-error {{type-id cannot have a name}}
314}
315
316namespace PR15045 {
317  class Cl0 {
318  public:
319    int a;
320  };
321
322  int f() {
323    Cl0 c;
324    return c->a;  // expected-error {{member reference type 'PR15045::Cl0' is not a pointer; maybe you meant to use '.'?}}
325  }
326}
327
328namespace PR5898 {
329  class A {
330  public:
331    const char *str();
332  };
333  const char* foo(A &x)
334  {
335    return x.str.();  // expected-error {{unexpected '.' in function call; perhaps remove the '.'?}}
336  }
337  bool bar(A x, const char *y) {
338    return foo->(x) == y;  // expected-error {{unexpected '->' in function call; perhaps remove the '->'?}}
339  }
340}
341