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