Daniel Dunbar | 8fbe78f | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Charles Li | 542f04c | 2015-11-11 19:34:47 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s |
| 3 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 4 | |
| 5 | class X{ |
| 6 | public: |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 7 | enum E {Enumerator}; // expected-note 2{{declared here}} |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 8 | int f(); |
| 9 | static int mem; |
| 10 | static float g(); |
| 11 | }; |
| 12 | |
| 13 | void test(X* xp, X x) { |
| 14 | int i1 = x.f(); |
| 15 | int i2 = xp->f(); |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 16 | x.E; // expected-error{{cannot refer to type member 'E' in 'X' with '.'}} |
| 17 | xp->E; // expected-error{{cannot refer to type member 'E' in 'X' with '->'}} |
Douglas Gregor | 6bd18af | 2009-01-16 03:02:29 +0000 | [diff] [blame] | 18 | int i3 = x.Enumerator; |
| 19 | int i4 = xp->Enumerator; |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 20 | x.mem = 1; |
| 21 | xp->mem = 2; |
| 22 | float f1 = x.g(); |
| 23 | float f2 = xp->g(); |
| 24 | } |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 25 | |
| 26 | struct A { |
| 27 | int f0; |
| 28 | }; |
| 29 | struct B { |
| 30 | A *f0(); |
| 31 | }; |
| 32 | int f0(B *b) { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 33 | return b->f0->f0; // expected-error{{did you mean to call it with no arguments}} |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 34 | } |
Douglas Gregor | 0b3d95a | 2009-10-17 22:37:54 +0000 | [diff] [blame] | 35 | |
| 36 | int i; |
| 37 | |
| 38 | namespace C { |
| 39 | int i; |
| 40 | } |
| 41 | |
| 42 | void test2(X *xp) { |
| 43 | xp->::i = 7; // expected-error{{qualified member access refers to a member in the global namespace}} |
| 44 | xp->C::i = 7; // expected-error{{qualified member access refers to a member in namespace 'C'}} |
| 45 | } |
John McCall | a6d407c | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | namespace test3 { |
| 49 | struct NamespaceDecl; |
| 50 | |
| 51 | struct NamedDecl { |
| 52 | void *getIdentifier() const; |
| 53 | }; |
| 54 | |
| 55 | struct NamespaceDecl : NamedDecl { |
| 56 | bool isAnonymousNamespace() const { |
| 57 | return !getIdentifier(); |
| 58 | } |
| 59 | }; |
| 60 | } |
Douglas Gregor | 516d672 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 61 | |
| 62 | namespace test4 { |
| 63 | class X { |
| 64 | protected: |
| 65 | template<typename T> void f(T); |
| 66 | }; |
| 67 | |
| 68 | class Y : public X { |
| 69 | public: |
| 70 | using X::f; |
| 71 | }; |
| 72 | |
| 73 | void test_f(Y y) { |
| 74 | y.f(17); |
| 75 | } |
| 76 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 77 | |
| 78 | namespace test5 { |
| 79 | struct A { |
| 80 | template <class T> void foo(); |
| 81 | }; |
| 82 | |
| 83 | void test0(int x) { |
Kaelyn Uhrain | 957c8b1 | 2013-07-31 20:16:17 +0000 | [diff] [blame] | 84 | x.A::foo<int>(); // expected-error {{'int' is not a structure or union}} |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void test1(A *x) { |
Kaelyn Uhrain | 957c8b1 | 2013-07-31 20:16:17 +0000 | [diff] [blame] | 88 | x.A::foo<int>(); // expected-error {{'test5::A *' is a pointer}} |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void test2(A &x) { |
Eric Christopher | 6e11073 | 2015-04-02 22:10:06 +0000 | [diff] [blame] | 92 | x->A::foo<int>(); // expected-error {{'test5::A' is not a pointer; did you mean to use '.'?}} |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 95 | |
| 96 | namespace PR7508 { |
| 97 | struct A { |
| 98 | struct CleanupScope {}; |
Kaelyn Uhrain | 3658e6a | 2012-01-13 21:28:55 +0000 | [diff] [blame] | 99 | void PopCleanupBlock(); // expected-note{{'PopCleanupBlock' declared here}} |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | void foo(A &a) { |
Kaelyn Uhrain | 3658e6a | 2012-01-13 21:28:55 +0000 | [diff] [blame] | 103 | a.PopCleanupScope(); // expected-error{{no member named 'PopCleanupScope' in 'PR7508::A'; did you mean 'PopCleanupBlock'?}} |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
Douglas Gregor | a9c3e82 | 2010-07-28 22:27:52 +0000 | [diff] [blame] | 106 | |
| 107 | namespace rdar8231724 { |
| 108 | namespace N { |
| 109 | template<typename T> struct X1; |
| 110 | int i; |
| 111 | } |
| 112 | |
| 113 | struct X { }; |
| 114 | struct Y : X { }; |
| 115 | |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 116 | template<typename T> struct Z { int n; }; |
| 117 | |
Douglas Gregor | a9c3e82 | 2010-07-28 22:27:52 +0000 | [diff] [blame] | 118 | void f(Y *y) { |
| 119 | y->N::X1<int>; // expected-error{{'rdar8231724::N::X1' is not a member of class 'rdar8231724::Y'}} |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 120 | y->Z<int>::n; // expected-error{{'rdar8231724::Z<int>::n' is not a member of class 'rdar8231724::Y'}} |
Charles Li | 542f04c | 2015-11-11 19:34:47 +0000 | [diff] [blame] | 121 | y->template Z<int>::n; // expected-error{{'rdar8231724::Z<int>::n' is not a member of class 'rdar8231724::Y'}} |
| 122 | #if __cplusplus <= 199711L // C++03 or earlier modes |
| 123 | // expected-warning@-2{{'template' keyword outside of a template}} |
| 124 | #endif |
Douglas Gregor | a9c3e82 | 2010-07-28 22:27:52 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
Matt Beaumont-Gay | 956fc1c | 2011-02-17 02:54:17 +0000 | [diff] [blame] | 127 | |
| 128 | namespace PR9025 { |
| 129 | struct S { int x; }; |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 130 | S fun(); // expected-note{{possible target for call}} |
| 131 | int fun(int i); // expected-note{{possible target for call}} |
Matt Beaumont-Gay | 956fc1c | 2011-02-17 02:54:17 +0000 | [diff] [blame] | 132 | int g() { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 133 | return fun.x; // expected-error{{reference to overloaded function could not be resolved; did you mean to call it with no arguments?}} |
Matt Beaumont-Gay | 956fc1c | 2011-02-17 02:54:17 +0000 | [diff] [blame] | 134 | } |
| 135 | |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 136 | S fun2(); // expected-note{{possible target for call}} |
| 137 | S fun2(int i); // expected-note{{possible target for call}} |
Matt Beaumont-Gay | 956fc1c | 2011-02-17 02:54:17 +0000 | [diff] [blame] | 138 | int g2() { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 139 | return fun2.x; // expected-error{{reference to overloaded function could not be resolved; did you mean to call it with no arguments?}} |
Matt Beaumont-Gay | 956fc1c | 2011-02-17 02:54:17 +0000 | [diff] [blame] | 140 | } |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 141 | |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 142 | S fun3(int i=0); // expected-note{{possible target for call}} |
| 143 | int fun3(int i, int j); // expected-note{{possible target for call}} |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 144 | int g3() { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 145 | return fun3.x; // expected-error{{reference to overloaded function could not be resolved; did you mean to call it with no arguments?}} |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 146 | } |
Matt Beaumont-Gay | f8bb45f | 2011-03-05 02:42:30 +0000 | [diff] [blame] | 147 | |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 148 | template <typename T> S fun4(); // expected-note{{possible target for call}} |
Matt Beaumont-Gay | f8bb45f | 2011-03-05 02:42:30 +0000 | [diff] [blame] | 149 | int g4() { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 150 | return fun4.x; // expected-error{{reference to overloaded function could not be resolved; did you mean to call it?}} |
Matt Beaumont-Gay | f8bb45f | 2011-03-05 02:42:30 +0000 | [diff] [blame] | 151 | } |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 152 | |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 153 | S fun5(int i); // expected-note{{possible target for call}} |
| 154 | S fun5(float f); // expected-note{{possible target for call}} |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 155 | int g5() { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 156 | return fun5.x; // expected-error{{reference to overloaded function could not be resolved; did you mean to call it?}} |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 157 | } |
Matt Beaumont-Gay | 956fc1c | 2011-02-17 02:54:17 +0000 | [diff] [blame] | 158 | } |
Eli Friedman | 9a766c4 | 2012-01-13 02:20:01 +0000 | [diff] [blame] | 159 | |
| 160 | namespace FuncInMemberExpr { |
| 161 | struct Vec { int size(); }; |
| 162 | Vec fun1(); |
| 163 | int test1() { return fun1.size(); } // expected-error {{base of member reference is a function; perhaps you meant to call it with no arguments}} |
| 164 | Vec *fun2(); |
| 165 | int test2() { return fun2->size(); } // expected-error {{base of member reference is a function; perhaps you meant to call it with no arguments}} |
| 166 | Vec fun3(int x = 0); |
| 167 | int test3() { return fun3.size(); } // expected-error {{base of member reference is a function; perhaps you meant to call it with no arguments}} |
| 168 | } |
Matt Beaumont-Gay | d9f244af | 2012-04-21 01:12:48 +0000 | [diff] [blame] | 169 | |
| 170 | namespace DotForSemiTypo { |
| 171 | void f(int i) { |
| 172 | // If the programmer typo'd '.' for ';', make sure we point at the '.' rather |
| 173 | // than the "field name" (whatever the first token on the next line happens to |
| 174 | // be). |
| 175 | int j = i. // expected-error {{member reference base type 'int' is not a structure or union}} |
| 176 | j = 0; |
| 177 | } |
| 178 | } |
Kaelyn Uhrain | bad7fb0 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 179 | |
| 180 | namespace PR15045 { |
| 181 | class Cl0 { |
| 182 | public: |
| 183 | int a; |
| 184 | }; |
| 185 | |
| 186 | int f() { |
| 187 | Cl0 c; |
Eric Christopher | 6e11073 | 2015-04-02 22:10:06 +0000 | [diff] [blame] | 188 | return c->a; // expected-error {{member reference type 'PR15045::Cl0' is not a pointer; did you mean to use '.'?}} |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | struct bar { |
| 192 | void func(); // expected-note {{'func' declared here}} |
| 193 | }; |
| 194 | |
| 195 | struct foo { |
| 196 | bar operator->(); // expected-note 2 {{'->' applied to return value of the operator->() declared here}} |
| 197 | }; |
| 198 | |
| 199 | template <class T> void call_func(T t) { |
Alp Toker | 6ed7251 | 2013-12-14 01:07:05 +0000 | [diff] [blame] | 200 | t->func(); // expected-error-re 2 {{member reference type 'PR15045::bar' is not a pointer{{$}}}} \ |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 201 | // expected-note {{did you mean to use '.' instead?}} |
| 202 | } |
| 203 | |
| 204 | void test_arrow_on_non_pointer_records() { |
| 205 | bar e; |
| 206 | foo f; |
| 207 | |
| 208 | // Show that recovery has happened by also triggering typo correction |
Eric Christopher | 6e11073 | 2015-04-02 22:10:06 +0000 | [diff] [blame] | 209 | e->Func(); // expected-error {{member reference type 'PR15045::bar' is not a pointer; did you mean to use '.'?}} \ |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 210 | // expected-error {{no member named 'Func' in 'PR15045::bar'; did you mean 'func'?}} |
| 211 | |
| 212 | // Make sure a fixit isn't given in the case that the '->' isn't actually |
| 213 | // the problem (the problem is with the return value of an operator->). |
Alp Toker | 6ed7251 | 2013-12-14 01:07:05 +0000 | [diff] [blame] | 214 | f->func(); // expected-error-re {{member reference type 'PR15045::bar' is not a pointer{{$}}}} |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 215 | |
| 216 | call_func(e); // expected-note {{in instantiation of function template specialization 'PR15045::call_func<PR15045::bar>' requested here}} |
| 217 | |
| 218 | call_func(f); // expected-note {{in instantiation of function template specialization 'PR15045::call_func<PR15045::foo>' requested here}} |
Kaelyn Uhrain | bad7fb0 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
Nick Lewycky | 1e43d95 | 2013-08-21 19:09:44 +0000 | [diff] [blame] | 221 | |
| 222 | namespace pr16676 { |
| 223 | struct S { int i; }; |
| 224 | struct T { S* get_s(); }; |
| 225 | int f(S* s) { |
| 226 | T t; |
| 227 | return t.get_s // expected-error {{reference to non-static member function must be called; did you mean to call it with no arguments?}} |
Eric Christopher | 6e11073 | 2015-04-02 22:10:06 +0000 | [diff] [blame] | 228 | .i; // expected-error {{member reference type 'pr16676::S *' is a pointer; did you mean to use '->'}} |
Nick Lewycky | 1e43d95 | 2013-08-21 19:09:44 +0000 | [diff] [blame] | 229 | } |
| 230 | } |