[cfe-dev] ASTMatcher CXXOperatorCallExpr

Shereen shereent7 at gmail.com
Mon Aug 19 06:29:38 PDT 2013


Hi,

I'm having trouble writing an ASTMatcher for vector like class access.
Matching CXXMemberCallExpr works fine and I can extract all the
information I need like the object name etc.

Unfortunately I haven't found a way yet to do this for
CXXOperatorCallExpr. If I have the following class, is there any way to
write a matcher which matches the call to x[1 + 1] and binds x?

----------------------------------------------
struct X {
  int operator[](int x) { return x; }
};

X x;
int y = x[1 + 1];
----------------------------------------------

I tried something along the lines

------------------------------------------------------------
operatorCallExpr(declRefExpr(to(varDecl().bind("varName"))),
                 hasOverloadedOperatorName("[]"));
------------------------------------------------------------

as the ast contains the declRefExpr to the object

--------------------------------------------------------------------------------------
CXXOperatorCallExpr 0x2dc41c0 <col:9, col:14> 'int'
    |-ImplicitCastExpr 0x2dc41a8 <col:10, col:14> 'int (*)(int)'
<FunctionToPointerDecay>
    | `-DeclRefExpr 0x2dc4158 <col:10, col:14> 'int (int)' lvalue CXXMethod
0x2d92660 'operator[]' 'int (int)'
    |-DeclRefExpr 0x2dc40c8 <col:9> 'struct X' lvalue Var 0x2dc3c90 'x'
'struct X'
    `-BinaryOperator 0x2dc4130 <col:11, col:13> 'int' '+'
      |-IntegerLiteral 0x2dc40f0 <col:11> 'int' 1
      `-IntegerLiteral 0x2dc4110 <col:13> 'int' 1
--------------------------------------------------------------------------------------

bit this doesn't match. Calling getCalleeDecl() on the
CXXOperatorCallExpr also just returns "operator[]".

Is there any other way to get the name of the object operator[] is called
on?

Kind regards,
Shereen



--
View this message in context: http://clang-developers.42468.n3.nabble.com/ASTMatcher-CXXOperatorCallExpr-tp4033959.html
Sent from the Clang Developers mailing list archive at Nabble.com.



More information about the cfe-dev mailing list