[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / FixIt / fixit.cpp
blob3c8797ae68532757b3724ed4d52400729e7728c6
1 // RUN: %clang_cc1 -pedantic -Wall -Wno-unused-but-set-variable -Wno-comment -verify -fcxx-exceptions -x c++ -std=c++98 -Wno-c++14-extensions %s
2 // RUN: cp %s %t-98
3 // RUN: not %clang_cc1 -pedantic -Wall -Wno-unused-but-set-variable -Wno-comment -fcxx-exceptions -fixit -x c++ -std=c++98 -Wno-c++14-extensions %t-98
4 // RUN: %clang_cc1 -fsyntax-only -pedantic -Wall -Wno-unused-but-set-variable -Werror -Wno-comment -fcxx-exceptions -x c++ -std=c++98 -Wno-c++14-extensions %t-98
5 // RUN: not %clang_cc1 -fsyntax-only -pedantic -fdiagnostics-parseable-fixits -x c++ -std=c++11 -Wno-c++14-extensions %s 2>&1 | FileCheck %s
6 // RUN: %clang_cc1 -pedantic -Wall -Wno-unused-but-set-variable -Wno-comment -verify -fcxx-exceptions -x c++ -std=c++11 -Wno-c++14-extensions %s
7 // RUN: cp %s %t-11
8 // RUN: not %clang_cc1 -pedantic -Wall -Wno-unused-but-set-variable -Wno-comment -fcxx-exceptions -fixit -x c++ -std=c++11 -Wno-c++14-extensions %t-11
9 // RUN: %clang_cc1 -fsyntax-only -pedantic -Wall -Wno-unused-but-set-variable -Werror -Wno-comment -fcxx-exceptions -x c++ -std=c++11 -Wno-c++14-extensions %t-11
11 /* This is a test of the various code modification hints that are
12 provided as part of warning or extension diagnostics. All of the
13 warnings will be fixed by -fixit, and the resulting file should
14 compile cleanly with -Werror -pedantic. */
16 struct C1 {
17 virtual void f();
18 static void g();
20 struct C2 : virtual public virtual C1 { }; // expected-error{{duplicate}}
22 virtual void C1::f() { } // expected-error{{'virtual' can only be specified inside the class definition}}
24 static void C1::g() { } // expected-error{{'static' can only be specified inside the class definition}}
26 template<int Value> struct CT { template<typename> struct Inner; }; // expected-note{{previous use is here}}
28 // FIXME: In C++11 this gets 'expected unqualified-id' which fixit can't fix.
29 // Probably parses as `CT<10> > 2 > ct;` rather than `CT<(10 >> 2)> ct;`.
30 #if __cplusplus < 201103L
31 CT<10 >> 2> ct; // expected-warning{{require parentheses}}
32 #endif
34 class C3 {
35 public:
36 C3(C3, int i = 0); // expected-error{{copy constructor must pass its first argument by reference}}
39 struct CT<0> { }; // expected-error{{'template<>'}}
41 template<> union CT<1> { }; // expected-error{{tag type}}
43 struct CT<2>::Inner<int> { }; // expected-error 2{{'template<>'}}
45 // Access declarations
46 class A {
47 protected:
48 int foo();
51 class B : public A {
52 #if __cplusplus >= 201103L
53 A::foo; // expected-error{{ISO C++11 does not allow access declarations}}
54 #else
55 A::foo; // expected-warning{{access declarations are deprecated}}
56 #endif
59 void f() throw(); // expected-note{{previous}}
60 void f(); // expected-error{{missing exception specification}}
62 namespace rdar7853795 {
63 struct A {
64 bool getNumComponents() const; // expected-note{{declared here}}
65 void dump() const {
66 getNumComponenets(); // expected-error{{use of undeclared identifier 'getNumComponenets'; did you mean 'getNumComponents'?}}
71 namespace rdar7796492 {
72 struct A { int x, y; A(); };
74 A::A()
75 : x(1) y(2) { // expected-error{{missing ',' between base or member initializers}}
80 // extra qualification on member
81 class C {
82 int C::foo(); // expected-error {{extra qualification}}
85 namespace rdar8488464 {
86 int x = 0;
87 int x1 &= 0; // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
88 int x2 *= 0; // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
89 int x3 += 0; // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
90 int x4 -= 0; // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
91 int x5 != 0; // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
92 int x6 /= 0; // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
93 int x7 %= 0; // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
94 int x8 <= 0; // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
95 int x9 <<= 0; // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
96 int x10 >= 0; // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
97 int x11 >>= 0; // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
98 int x12 ^= 0; // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
99 int x13 |= 0; // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
100 int x14 == 0; // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
102 void f() {
103 int x = 0;
104 (void)x;
105 int x1 &= 0; // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
106 (void)x1;
107 int x2 *= 0; // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
108 (void)x2;
109 int x3 += 0; // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
110 (void)x3;
111 int x4 -= 0; // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
112 (void)x4;
113 int x5 != 0; // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
114 (void)x5;
115 int x6 /= 0; // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
116 (void)x6;
117 int x7 %= 0; // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
118 (void)x7;
119 int x8 <= 0; // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
120 (void)x8;
121 int x9 <<= 0; // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
122 (void)x9;
123 int x10 >= 0; // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
124 (void)x10;
125 int x11 >>= 0; // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
126 (void)x11;
127 int x12 ^= 0; // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
128 (void)x12;
129 int x13 |= 0; // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
130 (void)x13;
131 int x14 == 0; // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
132 (void)x14;
133 if (int x = 0) { (void)x; }
134 if (int x1 &= 0) { (void)x1; } // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
135 if (int x2 *= 0) { (void)x2; } // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
136 if (int x3 += 0) { (void)x3; } // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
137 if (int x4 -= 0) { (void)x4; } // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
138 if (int x5 != 0) { (void)x5; } // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
139 if (int x6 /= 0) { (void)x6; } // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
140 if (int x7 %= 0) { (void)x7; } // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
141 if (int x8 <= 0) { (void)x8; } // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
142 if (int x9 <<= 0) { (void)x9; } // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
143 if (int x10 >= 0) { (void)x10; } // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
144 if (int x11 >>= 0) { (void)x11; } // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
145 if (int x12 ^= 0) { (void)x12; } // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
146 if (int x13 |= 0) { (void)x13; } // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
147 if (int x14 == 0) { (void)x14; } // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
151 template <class A>
152 class F1 {
153 public:
154 template <int B>
155 class Iterator {
159 template<class T>
160 class F2 {
161 typename F1<T>:: /*template*/ Iterator<0> Mypos; // expected-error {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
164 template <class T>
165 void f(){
166 typename F1<T>:: /*template*/ Iterator<0> Mypos; // expected-error {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
169 // Tests for &/* fixits radar 7113438.
170 class AD {};
171 class BD: public AD {};
173 void test (BD &br) {
174 AD* aPtr;
175 BD b;
176 aPtr = b; // expected-error {{assigning to 'AD *' from incompatible type 'BD'; take the address with &}}
177 aPtr = br; // expected-error {{assigning to 'AD *' from incompatible type 'BD'; take the address with &}}
180 void foo1() const {} // expected-error {{non-member function cannot have 'const' qualifier}}
181 void foo2() volatile {} // expected-error {{non-member function cannot have 'volatile' qualifier}}
182 void foo3() const volatile {} // expected-error {{non-member function cannot have 'const volatile' qualifier}}
184 struct S { void f(int, char); };
185 int itsAComma,
186 itsAComma2 = 0,
187 oopsAComma(42), // expected-error {{expected ';' at end of declaration}}
188 AD oopsMoreCommas() {
189 static int n = 0, // expected-error {{expected ';' at end of declaration}}
190 static char c,
191 &d = c, // expected-error {{expected ';' at end of declaration}}
192 S s, // expected-error {{expected ';' at end of declaration}}
193 s.f(n, d);
194 AD ad, // expected-error {{expected ';' at end of declaration}}
195 return ad;
197 struct MoreAccidentalCommas {
198 int a : 5,
199 b : 7,
200 : 4, // expected-error {{expected ';' at end of declaration}}
201 char c, // expected-error {{expected ';' at end of declaration}}
202 double d, // expected-error {{expected ';' at end of declaration}}
203 MoreAccidentalCommas *next, // expected-error {{expected ';' at end of declaration}}
204 public:
205 int k, // expected-error {{expected ';' at end of declaration}}
206 friend void f(MoreAccidentalCommas) {}
207 int k2, // expected-error {{expected ';' at end of declaration}}
208 virtual void g(), // expected-error {{expected ';' at end of declaration}}
211 template<class T> struct Mystery;
212 template<class T> typedef Mystery<T>::type getMysteriousThing() { // \
213 expected-error {{function definition declared 'typedef'}} \
214 expected-error {{missing 'typename' prior to dependent}}
215 return Mystery<T>::get();
218 template<template<typename> Foo, // expected-error {{template template parameter requires 'class' after the parameter list}}
219 template<typename> typename Bar, // expected-warning {{template template parameter using 'typename' is a C++17 extension}}
220 template<typename> struct Baz> // expected-error {{template template parameter requires 'class' after the parameter list}}
221 void func();
223 namespace ShadowedTagType {
224 class Foo {
225 public:
226 enum Bar { X, Y };
227 void SetBar(Bar bar);
228 Bar Bar(); // expected-note 2 {{enum 'Bar' is hidden by a non-type declaration of 'Bar' here}}
229 private:
230 Bar bar_; // expected-error {{must use 'enum' tag to refer to type 'Bar' in this scope}}
232 void Foo::SetBar(Bar bar) { bar_ = bar; } // expected-error {{must use 'enum' tag to refer to type 'Bar' in this scope}}
235 #define NULL __null
236 char c = NULL; // expected-warning {{implicit conversion of NULL constant to 'char'}}
237 double dbl = NULL; // expected-warning {{implicit conversion of NULL constant to 'double'}}
239 namespace arrow_suggest {
241 template <typename T>
242 class wrapped_ptr {
243 public:
244 wrapped_ptr(T* ptr) : ptr_(ptr) {}
245 T* operator->() { return ptr_; }
246 private:
247 T *ptr_;
250 class Worker {
251 public:
252 void DoSomething();
255 void test() {
256 wrapped_ptr<Worker> worker(new Worker);
257 worker.DoSomething(); // expected-error {{no member named 'DoSomething' in 'arrow_suggest::wrapped_ptr<arrow_suggest::Worker>'; did you mean to use '->' instead of '.'?}}
260 } // namespace arrow_suggest
262 // Make sure fixing namespace-qualified identifiers functions properly with
263 // namespace-aware typo correction/
264 namespace redecl_typo {
265 namespace Foo {
266 void BeEvil(); // expected-note {{'BeEvil' declared here}}
268 namespace Bar {
269 namespace Foo {
270 bool isGood(); // expected-note {{'Bar::Foo::isGood' declared here}}
271 void beEvil();
274 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'?}}
275 return true;
277 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'?}}
280 // Test behavior when a template-id is ended by a token which starts with '>'.
281 namespace greatergreater {
282 template<typename T> struct S { S(); S(T); };
283 void f(S<int>=0); // expected-error {{a space is required between a right angle bracket and an equals sign (use '> =')}}
285 // FIXME: The fix-its here overlap so -fixit mode can't apply the second one.
286 //void f(S<S<int>>=S<int>());
288 struct Shr {
289 template<typename T> Shr(T);
290 template<typename T> void operator >>=(T);
293 template<template<typename>> struct TemplateTemplateParam; // expected-error {{requires 'class'}}
295 template <typename T> int t = 0;
296 void g() {
297 int p = 0;
298 (void)(t<int>==p); // expected-error {{use '> ='}}
299 (void)(t<int>>=p); // expected-error {{use '> >'}}
300 #if __cplusplus < 201103L
301 (void)(t<S<int>>>=p); // expected-error {{use '> >'}}
302 (Shr)t<S<int>>>>=p; // expected-error {{use '> >'}}
303 #endif
305 // FIXME: We correct this to 't<int> > >= p;' not 't<int> >>= p;'
306 //(Shr)t<int>>>=p;
308 // FIXME: The fix-its here overlap.
309 //(void)(t<S<int>>==p);
313 class foo {
314 static void test() {
315 (void)&i; // expected-error{{must explicitly qualify name of member function when taking its address}}
317 int i();
320 namespace dtor_fixit {
321 struct foo {
322 ~bar() { } // expected-error {{undeclared identifier 'bar' in destructor name}}
323 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:6-[[@LINE-1]]:9}:"foo"
326 class bar { // expected-note {{found}}
327 ~bar();
329 ~bar::bar() {} // expected-error {{'~' in destructor name should be after nested name specifier}}
330 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:4}:""
331 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:9-[[@LINE-2]]:9}:"~"
333 namespace N {
334 typedef foo T;
335 template <typename T> struct X {};
337 void f(foo *p, N::X<int> *x) {
338 p->~undeclared(); // expected-error {{undeclared}}
339 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:19}:"foo"
341 p->~bar(); // expected-error {{does not match}}
342 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:12}:"foo"
344 // FIXME: This is a bad fixit; it'd be better to suggest replacing 'foo'
345 // with 'T'.
346 p->N::T::~foo(); // expected-warning {{requires the name after '::~' to be found in the same scope as the name before}}
347 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:12-[[@LINE-1]]:12}:"::foo"
349 typedef foo baz; // expected-note {{found}}
350 p->dtor_fixit::foo::~baz(); // expected-warning {{only found in lexical scope}}
351 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:8-[[@LINE-1]]:25}:""
353 // FIXME: This is a bad fixit; it'd be better to suggest adding the
354 // template arguments.
355 x->N::X<int>::~X(); // expected-warning {{same scope}}
356 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:17-[[@LINE-1]]:17}:"::X"
360 namespace PR5066 {
361 template<typename T> struct X {};
362 X<int *p> x; // expected-error {{type-id cannot have a name}}
365 namespace PR5898 {
366 class A {
367 public:
368 const char *str();
370 const char* foo(A &x)
372 return x.str.(); // expected-error {{unexpected '.' in function call; perhaps remove the '.'?}}
374 bool bar(A x, const char *y) {
375 return foo->(x) == y; // expected-error {{unexpected '->' in function call; perhaps remove the '->'?}}
379 namespace PR15045 {
380 class Cl0 {
381 public:
382 int a;
385 int f() {
386 Cl0 c;
387 return c->a; // expected-error {{member reference type 'Cl0' is not a pointer; did you mean to use '.'?}}
391 namespace curly_after_base_clause {
392 struct A { void f(); };
393 struct B : A // expected-error{{expected '{' after base class list}}
394 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
395 int i;
397 struct C : A // expected-error{{expected '{' after base class list}}
398 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
399 using A::f;
401 struct D : A // expected-error{{expected '{' after base class list}}
402 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
403 protected:
405 struct E : A // expected-error{{expected '{' after base class list}}
406 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
407 template<typename T> struct inner { };
409 struct F : A // expected-error{{expected '{' after base class list}}
410 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
411 F() { }
413 #if __cplusplus >= 201103L
414 struct G : A // expected-error{{expected '{' after base class list}}
415 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
416 constexpr G(int) { }
418 struct H : A // expected-error{{expected '{' after base class list}}
419 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:" {"
420 static_assert(true, "");
422 #endif
425 struct conversion_operator {
426 conversion_operator::* const operator int(); // expected-error {{put the complete type after 'operator'}}
427 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:32}:""
428 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:44-[[@LINE-2]]:44}:" conversion_operator::* const"
431 struct const_zero_init {
432 int a;
434 const const_zero_init czi; // expected-error {{default initialization of an object of const type 'const const_zero_init'}}
435 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:26-[[@LINE-1]]:26}:"{}"
436 int use_czi = czi.a;
438 namespace dotPointerDestructor {
440 struct Bar {
441 ~Bar();
444 void bar(Bar *o) {
445 o.~Bar(); // expected-error {{member reference type 'Bar *' is a pointer; did you mean to use '->'}}
446 } // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:4-[[@LINE-1]]:5}:"->"