xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/member-expr.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc class X{
4f4a2713aSLionel Sambuc public:
5f4a2713aSLionel Sambuc   enum E {Enumerator}; // expected-note 2{{declared here}}
6f4a2713aSLionel Sambuc   int f();
7f4a2713aSLionel Sambuc   static int mem;
8f4a2713aSLionel Sambuc   static float g();
9f4a2713aSLionel Sambuc };
10f4a2713aSLionel Sambuc 
test(X * xp,X x)11f4a2713aSLionel Sambuc void test(X* xp, X x) {
12f4a2713aSLionel Sambuc   int i1 = x.f();
13f4a2713aSLionel Sambuc   int i2 = xp->f();
14f4a2713aSLionel Sambuc   x.E; // expected-error{{cannot refer to type member 'E' in 'X' with '.'}}
15f4a2713aSLionel Sambuc   xp->E; // expected-error{{cannot refer to type member 'E' in 'X' with '->'}}
16f4a2713aSLionel Sambuc   int i3 = x.Enumerator;
17f4a2713aSLionel Sambuc   int i4 = xp->Enumerator;
18f4a2713aSLionel Sambuc   x.mem = 1;
19f4a2713aSLionel Sambuc   xp->mem = 2;
20f4a2713aSLionel Sambuc   float f1 = x.g();
21f4a2713aSLionel Sambuc   float f2 = xp->g();
22f4a2713aSLionel Sambuc }
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc struct A {
25f4a2713aSLionel Sambuc  int f0;
26f4a2713aSLionel Sambuc };
27f4a2713aSLionel Sambuc struct B {
28f4a2713aSLionel Sambuc  A *f0();
29f4a2713aSLionel Sambuc };
f0(B * b)30f4a2713aSLionel Sambuc int f0(B *b) {
31f4a2713aSLionel Sambuc   return b->f0->f0; // expected-error{{did you mean to call it with no arguments}}
32f4a2713aSLionel Sambuc }
33f4a2713aSLionel Sambuc 
34f4a2713aSLionel Sambuc int i;
35f4a2713aSLionel Sambuc 
36f4a2713aSLionel Sambuc namespace C {
37f4a2713aSLionel Sambuc   int i;
38f4a2713aSLionel Sambuc }
39f4a2713aSLionel Sambuc 
test2(X * xp)40f4a2713aSLionel Sambuc void test2(X *xp) {
41f4a2713aSLionel Sambuc   xp->::i = 7; // expected-error{{qualified member access refers to a member in the global namespace}}
42f4a2713aSLionel Sambuc   xp->C::i = 7; // expected-error{{qualified member access refers to a member in namespace 'C'}}
43f4a2713aSLionel Sambuc }
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc 
46f4a2713aSLionel Sambuc namespace test3 {
47f4a2713aSLionel Sambuc   struct NamespaceDecl;
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc   struct NamedDecl {
50f4a2713aSLionel Sambuc     void *getIdentifier() const;
51f4a2713aSLionel Sambuc   };
52f4a2713aSLionel Sambuc 
53f4a2713aSLionel Sambuc   struct NamespaceDecl : NamedDecl {
isAnonymousNamespacetest3::NamespaceDecl54f4a2713aSLionel Sambuc     bool isAnonymousNamespace() const {
55f4a2713aSLionel Sambuc       return !getIdentifier();
56f4a2713aSLionel Sambuc     }
57f4a2713aSLionel Sambuc   };
58f4a2713aSLionel Sambuc }
59f4a2713aSLionel Sambuc 
60f4a2713aSLionel Sambuc namespace test4 {
61f4a2713aSLionel Sambuc   class X {
62f4a2713aSLionel Sambuc   protected:
63f4a2713aSLionel Sambuc     template<typename T> void f(T);
64f4a2713aSLionel Sambuc   };
65f4a2713aSLionel Sambuc 
66f4a2713aSLionel Sambuc   class Y : public X {
67f4a2713aSLionel Sambuc   public:
68f4a2713aSLionel Sambuc     using X::f;
69f4a2713aSLionel Sambuc   };
70f4a2713aSLionel Sambuc 
test_f(Y y)71f4a2713aSLionel Sambuc   void test_f(Y y) {
72f4a2713aSLionel Sambuc     y.f(17);
73f4a2713aSLionel Sambuc   }
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc 
76f4a2713aSLionel Sambuc namespace test5 {
77f4a2713aSLionel Sambuc   struct A {
78f4a2713aSLionel Sambuc     template <class T> void foo();
79f4a2713aSLionel Sambuc   };
80f4a2713aSLionel Sambuc 
test0(int x)81f4a2713aSLionel Sambuc   void test0(int x) {
82f4a2713aSLionel Sambuc     x.A::foo<int>(); // expected-error {{'int' is not a structure or union}}
83f4a2713aSLionel Sambuc   }
84f4a2713aSLionel Sambuc 
test1(A * x)85f4a2713aSLionel Sambuc   void test1(A *x) {
86f4a2713aSLionel Sambuc     x.A::foo<int>(); // expected-error {{'test5::A *' is a pointer}}
87f4a2713aSLionel Sambuc   }
88f4a2713aSLionel Sambuc 
test2(A & x)89f4a2713aSLionel Sambuc   void test2(A &x) {
90f4a2713aSLionel Sambuc     x->A::foo<int>(); // expected-error {{'test5::A' is not a pointer; maybe you meant to use '.'?}}
91f4a2713aSLionel Sambuc   }
92f4a2713aSLionel Sambuc }
93f4a2713aSLionel Sambuc 
94f4a2713aSLionel Sambuc namespace PR7508 {
95f4a2713aSLionel Sambuc   struct A {
96f4a2713aSLionel Sambuc     struct CleanupScope {};
97f4a2713aSLionel Sambuc     void PopCleanupBlock(); // expected-note{{'PopCleanupBlock' declared here}}
98f4a2713aSLionel Sambuc   };
99f4a2713aSLionel Sambuc 
foo(A & a)100f4a2713aSLionel Sambuc   void foo(A &a) {
101f4a2713aSLionel Sambuc     a.PopCleanupScope(); // expected-error{{no member named 'PopCleanupScope' in 'PR7508::A'; did you mean 'PopCleanupBlock'?}}
102f4a2713aSLionel Sambuc   }
103f4a2713aSLionel Sambuc }
104f4a2713aSLionel Sambuc 
105f4a2713aSLionel Sambuc namespace rdar8231724 {
106f4a2713aSLionel Sambuc   namespace N {
107f4a2713aSLionel Sambuc     template<typename T> struct X1;
108f4a2713aSLionel Sambuc     int i;
109f4a2713aSLionel Sambuc   }
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc   struct X { };
112f4a2713aSLionel Sambuc   struct Y : X { };
113f4a2713aSLionel Sambuc 
114f4a2713aSLionel Sambuc   template<typename T> struct Z { int n; };
115f4a2713aSLionel Sambuc 
f(Y * y)116f4a2713aSLionel Sambuc   void f(Y *y) {
117f4a2713aSLionel Sambuc     y->N::X1<int>; // expected-error{{'rdar8231724::N::X1' is not a member of class 'rdar8231724::Y'}}
118f4a2713aSLionel Sambuc     y->Z<int>::n; // expected-error{{'rdar8231724::Z<int>::n' is not a member of class 'rdar8231724::Y'}}
119f4a2713aSLionel Sambuc     y->template Z<int>::n; // expected-error{{'rdar8231724::Z<int>::n' is not a member of class 'rdar8231724::Y'}} \
120f4a2713aSLionel Sambuc     // expected-warning{{'template' keyword outside of a template}}
121f4a2713aSLionel Sambuc   }
122f4a2713aSLionel Sambuc }
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc namespace PR9025 {
125f4a2713aSLionel Sambuc   struct S { int x; };
126f4a2713aSLionel Sambuc   S fun(); // expected-note{{possible target for call}}
127f4a2713aSLionel Sambuc   int fun(int i); // expected-note{{possible target for call}}
g()128f4a2713aSLionel Sambuc   int g() {
129f4a2713aSLionel Sambuc     return fun.x; // expected-error{{reference to overloaded function could not be resolved; did you mean to call it with no arguments?}}
130f4a2713aSLionel Sambuc   }
131f4a2713aSLionel Sambuc 
132f4a2713aSLionel Sambuc   S fun2(); // expected-note{{possible target for call}}
133f4a2713aSLionel Sambuc   S fun2(int i); // expected-note{{possible target for call}}
g2()134f4a2713aSLionel Sambuc   int g2() {
135f4a2713aSLionel Sambuc     return fun2.x; // expected-error{{reference to overloaded function could not be resolved; did you mean to call it with no arguments?}}
136f4a2713aSLionel Sambuc   }
137f4a2713aSLionel Sambuc 
138f4a2713aSLionel Sambuc   S fun3(int i=0); // expected-note{{possible target for call}}
139f4a2713aSLionel Sambuc   int fun3(int i, int j); // expected-note{{possible target for call}}
g3()140f4a2713aSLionel Sambuc   int g3() {
141f4a2713aSLionel Sambuc     return fun3.x; // expected-error{{reference to overloaded function could not be resolved; did you mean to call it with no arguments?}}
142f4a2713aSLionel Sambuc   }
143f4a2713aSLionel Sambuc 
144f4a2713aSLionel Sambuc   template <typename T> S fun4(); // expected-note{{possible target for call}}
g4()145f4a2713aSLionel Sambuc   int g4() {
146f4a2713aSLionel Sambuc     return fun4.x; // expected-error{{reference to overloaded function could not be resolved; did you mean to call it?}}
147f4a2713aSLionel Sambuc   }
148f4a2713aSLionel Sambuc 
149f4a2713aSLionel Sambuc   S fun5(int i); // expected-note{{possible target for call}}
150f4a2713aSLionel Sambuc   S fun5(float f); // expected-note{{possible target for call}}
g5()151f4a2713aSLionel Sambuc   int g5() {
152f4a2713aSLionel Sambuc     return fun5.x; // expected-error{{reference to overloaded function could not be resolved; did you mean to call it?}}
153f4a2713aSLionel Sambuc   }
154f4a2713aSLionel Sambuc }
155f4a2713aSLionel Sambuc 
156f4a2713aSLionel Sambuc namespace FuncInMemberExpr {
157f4a2713aSLionel Sambuc   struct Vec { int size(); };
158f4a2713aSLionel Sambuc   Vec fun1();
test1()159f4a2713aSLionel Sambuc   int test1() { return fun1.size(); } // expected-error {{base of member reference is a function; perhaps you meant to call it with no arguments}}
160f4a2713aSLionel Sambuc   Vec *fun2();
test2()161f4a2713aSLionel Sambuc   int test2() { return fun2->size(); } // expected-error {{base of member reference is a function; perhaps you meant to call it with no arguments}}
162f4a2713aSLionel Sambuc   Vec fun3(int x = 0);
test3()163f4a2713aSLionel Sambuc   int test3() { return fun3.size(); } // expected-error {{base of member reference is a function; perhaps you meant to call it with no arguments}}
164f4a2713aSLionel Sambuc }
165f4a2713aSLionel Sambuc 
166f4a2713aSLionel Sambuc namespace DotForSemiTypo {
f(int i)167f4a2713aSLionel Sambuc void f(int i) {
168f4a2713aSLionel Sambuc   // If the programmer typo'd '.' for ';', make sure we point at the '.' rather
169f4a2713aSLionel Sambuc   // than the "field name" (whatever the first token on the next line happens to
170f4a2713aSLionel Sambuc   // be).
171f4a2713aSLionel Sambuc   int j = i. // expected-error {{member reference base type 'int' is not a structure or union}}
172f4a2713aSLionel Sambuc   j = 0;
173f4a2713aSLionel Sambuc }
174f4a2713aSLionel Sambuc }
175f4a2713aSLionel Sambuc 
176f4a2713aSLionel Sambuc namespace PR15045 {
177f4a2713aSLionel Sambuc   class Cl0 {
178f4a2713aSLionel Sambuc   public:
179f4a2713aSLionel Sambuc     int a;
180f4a2713aSLionel Sambuc   };
181f4a2713aSLionel Sambuc 
f()182f4a2713aSLionel Sambuc   int f() {
183f4a2713aSLionel Sambuc     Cl0 c;
184f4a2713aSLionel Sambuc     return c->a;  // expected-error {{member reference type 'PR15045::Cl0' is not a pointer; maybe you meant to use '.'?}}
185f4a2713aSLionel Sambuc   }
186f4a2713aSLionel Sambuc 
187f4a2713aSLionel Sambuc   struct bar {
188f4a2713aSLionel Sambuc     void func();  // expected-note {{'func' declared here}}
189f4a2713aSLionel Sambuc   };
190f4a2713aSLionel Sambuc 
191f4a2713aSLionel Sambuc   struct foo {
192f4a2713aSLionel Sambuc     bar operator->();  // expected-note 2 {{'->' applied to return value of the operator->() declared here}}
193f4a2713aSLionel Sambuc   };
194f4a2713aSLionel Sambuc 
call_func(T t)195f4a2713aSLionel Sambuc   template <class T> void call_func(T t) {
196*0a6a1f1dSLionel Sambuc     t->func();  // expected-error-re 2 {{member reference type 'PR15045::bar' is not a pointer{{$}}}} \
197f4a2713aSLionel Sambuc                 // expected-note {{did you mean to use '.' instead?}}
198f4a2713aSLionel Sambuc   }
199f4a2713aSLionel Sambuc 
test_arrow_on_non_pointer_records()200f4a2713aSLionel Sambuc   void test_arrow_on_non_pointer_records() {
201f4a2713aSLionel Sambuc     bar e;
202f4a2713aSLionel Sambuc     foo f;
203f4a2713aSLionel Sambuc 
204f4a2713aSLionel Sambuc     // Show that recovery has happened by also triggering typo correction
205f4a2713aSLionel Sambuc     e->Func();  // expected-error {{member reference type 'PR15045::bar' is not a pointer; maybe you meant to use '.'?}} \
206f4a2713aSLionel Sambuc                 // expected-error {{no member named 'Func' in 'PR15045::bar'; did you mean 'func'?}}
207f4a2713aSLionel Sambuc 
208f4a2713aSLionel Sambuc     // Make sure a fixit isn't given in the case that the '->' isn't actually
209f4a2713aSLionel Sambuc     // the problem (the problem is with the return value of an operator->).
210*0a6a1f1dSLionel Sambuc     f->func();  // expected-error-re {{member reference type 'PR15045::bar' is not a pointer{{$}}}}
211f4a2713aSLionel Sambuc 
212f4a2713aSLionel Sambuc     call_func(e);  // expected-note {{in instantiation of function template specialization 'PR15045::call_func<PR15045::bar>' requested here}}
213f4a2713aSLionel Sambuc 
214f4a2713aSLionel Sambuc     call_func(f);  // expected-note {{in instantiation of function template specialization 'PR15045::call_func<PR15045::foo>' requested here}}
215f4a2713aSLionel Sambuc   }
216f4a2713aSLionel Sambuc }
217f4a2713aSLionel Sambuc 
218f4a2713aSLionel Sambuc namespace pr16676 {
219f4a2713aSLionel Sambuc   struct S { int i; };
220f4a2713aSLionel Sambuc   struct T { S* get_s(); };
f(S * s)221f4a2713aSLionel Sambuc   int f(S* s) {
222f4a2713aSLionel Sambuc     T t;
223f4a2713aSLionel Sambuc     return t.get_s  // expected-error {{reference to non-static member function must be called; did you mean to call it with no arguments?}}
224f4a2713aSLionel Sambuc         .i;  // expected-error {{member reference type 'pr16676::S *' is a pointer; maybe you meant to use '->'}}
225f4a2713aSLionel Sambuc   }
226f4a2713aSLionel Sambuc }
227