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