[cfe-dev] ASTMatcher CXXOperatorCallExpr

Gábor Kozár kozargabor at gmail.com
Mon Aug 19 09:21:21 PDT 2013


Hi,

You probably want something like this:

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

Writing x(y()) in a matcher expression means that the matched node should
satisfy x() and it should also satisfy y(). So what you were writing (
operatorCallExpr(declRefExpr(...))) would have required a node to be both a
CXXOperatorCallExpr and a DeclRefExpr.

You should also make sure that this does not match on a call like this:
y[x]
Here "y" should be matched, not "x". I _think_ that the above matcher
expression will produce the desired output for this, because I guess the
reference to "y" will be earlier among the parameters than "x", but you
need to verify this.

Hope this helps!

Gabor


2013/8/19 Shereen <shereent7 at gmail.com>

> 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.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130819/42763f14/attachment.html>


More information about the cfe-dev mailing list