fixit.cpp 15 KB

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