ASTMatcher CXXOperatorCallExpr


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?


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:

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!


Hi Gabor,

thanks a lot!

So the operator[] call is transformed to a binary function with the object
as the first argument. That makes sense. Interestingly I tried
has(declRefExpr(...)) before but didn't got a match. Tried it again and now
it is working. Must have had a different problem before and thought it was
caused by the has(declRefExpr(...)).


Gábor Kozár wrote

Has anyone ever tried building clang/llvm with -faddress=sanatize? I was wondering before I waste my time with this endeavor.


Yes, in fact we have a couple of (not public) buildbots that do this


Has anyone ever tried building clang/llvm with -faddress=sanatize? I was
wondering before I waste my time with this endeavor.

You would have more success with -fsanitize=address :slight_smile:


In article <>,
    Gábor Kozár <> writes:

This seems to be the case according to the AST snippet you pasted, and my
cursory examination of the Clang AST documentation. I'm not absolutely sure
though - someone with a better understanding of the Clang AST could maybe
clear this up?

I use clang-check's -ast-dump to see how the AST gets built from a
particular snippet of code. It has been very helpful in understanding
the AST for me.