fixit.cpp 12 KB

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