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