blob: 3eac434a36bd3502bb80b49488632151f6a2a074 [file] [log] [blame]
Richard Smith6e1fd332011-11-29 09:09:06 +00001// RUN: %clang_cc1 -pedantic -Wall -Wno-comment -verify -fcxx-exceptions -x c++ %s
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +00002// RUN: cp %s %t
Richard Smith6e1fd332011-11-29 09:09:06 +00003// RUN: not %clang_cc1 -pedantic -Wall -Wno-comment -fcxx-exceptions -fixit -x c++ %t
4// RUN: %clang_cc1 -fsyntax-only -pedantic -Wall -Werror -Wno-comment -fcxx-exceptions -x c++ %t
Douglas Gregor9b3064b2009-04-01 22:41:11 +00005
6/* This is a test of the various code modification hints that are
Douglas Gregorfe057ac2009-04-02 03:20:30 +00007 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. */
Douglas Gregor9b3064b2009-04-01 22:41:11 +000010
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000011struct C1 {
Douglas Gregora3a83512009-04-01 23:51:29 +000012 virtual void f();
13 static void g();
14};
Douglas Gregor9b3064b2009-04-01 22:41:11 +000015struct C2 : virtual public virtual C1 { }; // expected-error{{duplicate}}
16
Douglas Gregora3a83512009-04-01 23:51:29 +000017virtual void C1::f() { } // expected-error{{'virtual' can only be specified inside the class definition}}
18
19static void C1::g() { } // expected-error{{'static' can only be specified inside the class definition}}
20
21template<int Value> struct CT { }; // expected-note{{previous use is here}}
Douglas Gregor9b3064b2009-04-01 22:41:11 +000022
23CT<10 >> 2> ct; // expected-warning{{require parentheses}}
Douglas Gregora3a83512009-04-01 23:51:29 +000024
25class C3 {
26public:
27 C3(C3, int i = 0); // expected-error{{copy constructor must pass its first argument by reference}}
28};
29
30struct CT<0> { }; // expected-error{{'template<>'}}
31
Richard Smith98e13ea2011-10-20 01:41:28 +000032template<> union CT<1> { }; // expected-error{{tag type}}
Anders Carlssonad26b732009-11-10 03:24:44 +000033
Douglas Gregor28485232010-02-01 23:46:27 +000034// Access declarations
35class A {
36protected:
37 int foo();
38};
Anders Carlssonad26b732009-11-10 03:24:44 +000039
Douglas Gregor28485232010-02-01 23:46:27 +000040class B : public A {
41 A::foo; // expected-warning{{access declarations are deprecated}}
42};
Douglas Gregor2eef8292010-03-24 07:14:45 +000043
Richard Smith98e13ea2011-10-20 01:41:28 +000044void f() throw(); // expected-note{{previous}}
Douglas Gregor2eef8292010-03-24 07:14:45 +000045void f(); // expected-warning{{missing exception specification}}
Douglas Gregor1aae80b2010-04-14 20:27:54 +000046
47namespace rdar7853795 {
48 struct A {
49 bool getNumComponents() const; // expected-note{{declared here}}
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000050 void dump() const {
Douglas Gregor1aae80b2010-04-14 20:27:54 +000051 getNumComponenets(); // expected-error{{use of undeclared identifier 'getNumComponenets'; did you mean 'getNumComponents'?}}
52 }
53 };
54}
Douglas Gregorb1f6fa42010-09-07 14:35:10 +000055
56namespace rdar7796492 {
Benjamin Kramerf5485122012-06-11 16:06:57 +000057 struct A { int x, y; A(); };
Douglas Gregorb1f6fa42010-09-07 14:35:10 +000058
59 A::A()
60 : x(1) y(2) { // expected-error{{missing ',' between base or member initializers}}
61 }
62
63}
Gabor Greifa4a301d2010-09-08 00:31:13 +000064
Francois Pichetc71d8eb2010-10-01 21:19:28 +000065// extra qualification on member
66class C {
Richard Smith98e13ea2011-10-20 01:41:28 +000067 int C::foo(); // expected-warning {{extra qualification}}
Francois Pichetc71d8eb2010-10-01 21:19:28 +000068};
69
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +000070namespace rdar8488464 {
Richard Trieufcaf27e2012-01-19 22:01:51 +000071int x = 0;
72int x1 &= 0; // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
73int x2 *= 0; // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
74int x3 += 0; // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
75int x4 -= 0; // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
76int x5 != 0; // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
77int x6 /= 0; // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
78int x7 %= 0; // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
79int x8 <= 0; // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
80int x9 <<= 0; // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
81int x10 >= 0; // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
82int x11 >>= 0; // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
83int x12 ^= 0; // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
84int x13 |= 0; // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
85int x14 == 0; // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +000086
87void f() {
Richard Trieufcaf27e2012-01-19 22:01:51 +000088 int x = 0;
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +000089 (void)x;
Richard Trieufcaf27e2012-01-19 22:01:51 +000090 int x1 &= 0; // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
91 (void)x1;
92 int x2 *= 0; // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
93 (void)x2;
94 int x3 += 0; // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
95 (void)x3;
96 int x4 -= 0; // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
97 (void)x4;
98 int x5 != 0; // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
99 (void)x5;
100 int x6 /= 0; // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
101 (void)x6;
102 int x7 %= 0; // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
103 (void)x7;
104 int x8 <= 0; // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
105 (void)x8;
106 int x9 <<= 0; // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
107 (void)x9;
108 int x10 >= 0; // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
109 (void)x10;
110 int x11 >>= 0; // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
111 (void)x11;
112 int x12 ^= 0; // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
113 (void)x12;
114 int x13 |= 0; // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
115 (void)x13;
116 int x14 == 0; // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
117 (void)x14;
118 if (int x = 0) { (void)x; }
119 if (int x1 &= 0) { (void)x1; } // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
120 if (int x2 *= 0) { (void)x2; } // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
121 if (int x3 += 0) { (void)x3; } // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
122 if (int x4 -= 0) { (void)x4; } // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
123 if (int x5 != 0) { (void)x5; } // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
124 if (int x6 /= 0) { (void)x6; } // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
125 if (int x7 %= 0) { (void)x7; } // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
126 if (int x8 <= 0) { (void)x8; } // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
127 if (int x9 <<= 0) { (void)x9; } // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
128 if (int x10 >= 0) { (void)x10; } // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
129 if (int x11 >>= 0) { (void)x11; } // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
130 if (int x12 ^= 0) { (void)x12; } // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
131 if (int x13 |= 0) { (void)x13; } // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
132 if (int x14 == 0) { (void)x14; } // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000133}
134}
135
Francois Pichet4147d302011-03-27 19:41:34 +0000136template <class A>
137class F1 {
138public:
139 template <int B>
140 class Iterator {
141 };
142};
143
144template<class T>
145class F2 {
146 typename F1<T>:: /*template*/ Iterator<0> Mypos; // expected-error {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
147};
148
149template <class T>
150void f(){
151 typename F1<T>:: /*template*/ Iterator<0> Mypos; // expected-error {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
152}
153
Anna Zaks67221552011-07-28 19:51:27 +0000154// Tests for &/* fixits radar 7113438.
155class AD {};
156class BD: public AD {};
157
158void test (BD &br) {
159 AD* aPtr;
160 BD b;
161 aPtr = b; // expected-error {{assigning to 'AD *' from incompatible type 'BD'; take the address with &}}
Richard Smith98e13ea2011-10-20 01:41:28 +0000162 aPtr = br; // expected-error {{assigning to 'AD *' from incompatible type 'BD'; take the address with &}}
Anna Zaks67221552011-07-28 19:51:27 +0000163}
164
Richard Smithd37b3602012-02-10 11:05:11 +0000165void foo1() const {} // expected-error {{non-member function cannot have 'const' qualifier}}
166void foo2() volatile {} // expected-error {{non-member function cannot have 'volatile' qualifier}}
167void foo3() const volatile {} // expected-error {{non-member function cannot have 'const volatile' qualifier}}
Francois Pichet4147d302011-03-27 19:41:34 +0000168
Richard Smith0706df42011-10-19 21:33:05 +0000169struct S { void f(int, char); };
170int itsAComma,
171itsAComma2 = 0,
Richard Smith98e13ea2011-10-20 01:41:28 +0000172oopsAComma(42), // expected-error {{expected ';' at end of declaration}}
Richard Smith0706df42011-10-19 21:33:05 +0000173AD oopsMoreCommas() {
Richard Smith98e13ea2011-10-20 01:41:28 +0000174 static int n = 0, // expected-error {{expected ';' at end of declaration}}
Richard Smith0706df42011-10-19 21:33:05 +0000175 static char c,
Richard Smith98e13ea2011-10-20 01:41:28 +0000176 &d = c, // expected-error {{expected ';' at end of declaration}}
177 S s, // expected-error {{expected ';' at end of declaration}}
Richard Smith0706df42011-10-19 21:33:05 +0000178 s.f(n, d);
Richard Smith98e13ea2011-10-20 01:41:28 +0000179 AD ad, // expected-error {{expected ';' at end of declaration}}
Richard Smith0706df42011-10-19 21:33:05 +0000180 return ad;
181}
Richard Smith1c94c162012-01-09 22:31:44 +0000182struct MoreAccidentalCommas {
183 int a : 5,
184 b : 7,
185 : 4, // expected-error {{expected ';' at end of declaration}}
186 char c, // expected-error {{expected ';' at end of declaration}}
187 double d, // expected-error {{expected ';' at end of declaration}}
188 MoreAccidentalCommas *next, // expected-error {{expected ';' at end of declaration}}
189public:
190 int k, // expected-error {{expected ';' at end of declaration}}
191 friend void f(MoreAccidentalCommas) {}
192 int k2, // expected-error {{expected ';' at end of declaration}}
193 virtual void g(), // expected-error {{expected ';' at end of declaration}}
194};
Richard Smith874d2532011-11-29 05:27:40 +0000195
Richard Smith6e1fd332011-11-29 09:09:06 +0000196template<class T> struct Mystery;
197template<class T> typedef Mystery<T>::type getMysteriousThing() { // \
198 expected-error {{function definition declared 'typedef'}} \
199 expected-error {{missing 'typename' prior to dependent}}
200 return Mystery<T>::get();
201}
David Blaikie460ef132012-04-02 19:15:28 +0000202
David Blaikie673720d2012-04-06 06:28:32 +0000203template<template<typename> Foo, // expected-error {{template template parameter requires 'class' after the parameter list}}
204 template<typename> typename Bar, // expected-error {{template template parameter requires 'class' after the parameter list}}
205 template<typename> struct Baz> // expected-error {{template template parameter requires 'class' after the parameter list}}
David Blaikie460ef132012-04-02 19:15:28 +0000206void func();
Kaelyn Uhrain434ed262012-04-19 23:17:45 +0000207
Kaelyn Uhrain434ed262012-04-19 23:17:45 +0000208namespace ShadowedTagType {
209class Foo {
210 public:
211 enum Bar { X, Y };
212 void SetBar(Bar bar);
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +0000213 Bar Bar(); // expected-note 2 {{enum 'Bar' is hidden by a non-type declaration of 'Bar' here}}
Kaelyn Uhrain434ed262012-04-19 23:17:45 +0000214 private:
215 Bar bar_; // expected-error {{must use 'enum' tag to refer to type 'Bar' in this scope}}
216};
217void Foo::SetBar(Bar bar) { bar_ = bar; } // expected-error {{must use 'enum' tag to refer to type 'Bar' in this scope}}
Kaelyn Uhrain1e77b652012-04-19 23:26:12 +0000218}
Kaelyn Uhrain2b90f762012-04-25 19:49:54 +0000219
David Blaikie2c0abf42012-04-30 18:27:22 +0000220#define NULL __null
221char c = NULL; // expected-warning {{implicit conversion of NULL constant to 'char'}}
David Blaikieb26331b2012-06-19 21:19:06 +0000222double dbl = NULL; // expected-warning {{implicit conversion of NULL constant to 'double'}}
David Blaikie2c0abf42012-04-30 18:27:22 +0000223
Kaelyn Uhrain2b90f762012-04-25 19:49:54 +0000224namespace arrow_suggest {
225
226template <typename T>
227class wrapped_ptr {
228 public:
229 wrapped_ptr(T* ptr) : ptr_(ptr) {}
230 T* operator->() { return ptr_; }
231 private:
232 T *ptr_;
233};
234
235class Worker {
236 public:
237 void DoSomething();
238};
239
240void test() {
241 wrapped_ptr<Worker> worker(new Worker);
242 worker.DoSomething(); // expected-error {{no member named 'DoSomething' in 'arrow_suggest::wrapped_ptr<arrow_suggest::Worker>'; did you mean to use '->' instead of '.'?}}
243}
244
245} // namespace arrow_suggest
Kaelyn Uhrainb7fe5792012-06-07 23:57:12 +0000246
247// Make sure fixing namespace-qualified identifiers functions properly with
248// namespace-aware typo correction/
249namespace redecl_typo {
250namespace Foo {
251 void BeEvil(); // expected-note {{'BeEvil' declared here}}
252}
253namespace Bar {
254 namespace Foo {
255 bool isGood(); // expected-note {{'Bar::Foo::isGood' declared here}}
256 void beEvil();
257 }
258}
259bool 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'?}}
260 return true;
261}
262void Foo::beEvil() {} // expected-error {{out-of-line definition of 'beEvil' does not match any declaration in namespace 'redecl_typo::Foo'; did you mean 'BeEvil'?}}
263}
Richard Smith19a27022012-06-18 06:11:04 +0000264
265// Test behavior when a template-id is ended by a token which starts with '>'.
266namespace greatergreater {
267 template<typename T> struct S { S(); S(T); };
268 void f(S<int>=0); // expected-error {{a space is required between a right angle bracket and an equals sign (use '> =')}}
269
270 // FIXME: The fix-its here overlap so -fixit mode can't apply the second one.
271 //void f(S<S<int>>=S<int>());
272
273 struct Shr {
274 template<typename T> Shr(T);
275 template<typename T> void operator >>=(T);
276 };
277
278 template<template<typename>> struct TemplateTemplateParam; // expected-error {{requires 'class'}}
279
280 template<typename T> void t();
281 void g() {
282 void (*p)() = &t<int>;
283 (void)(&t<int>==p); // expected-error {{use '> ='}}
284 (void)(&t<int>>=p); // expected-error {{use '> >'}}
285 (void)(&t<S<int>>>=p); // expected-error {{use '> >'}}
286 (Shr)&t<S<int>>>>=p; // expected-error {{use '> >'}}
287
288 // FIXME: We correct this to '&t<int> > >= p;' not '&t<int> >>= p;'
289 //(Shr)&t<int>>>=p;
290
291 // FIXME: The fix-its here overlap.
292 //(void)(&t<S<int>>==p);
293 }
294}