[cfe-dev] AST Matcher question -- hasAncestor

Manuel Klimek via cfe-dev cfe-dev at lists.llvm.org
Sun Sep 25 23:41:30 PDT 2016


On Wed, Sep 21, 2016 at 8:19 PM Riyaz Puthiyapurayil via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> I am new to both clang AST Matchers. From an AST Matcher in a clang tidy
> check, what is the fastest way to get the enclosing function or method
> scope (if any) from a matching CallExpr node?
>
>
>
> I tried something like this in clang-query and it seems to work as
> expected:
>
>
>
>    clang-query> match
> callExpr(hasAncestor(functionDecl().bind(“contextFuncDef”)).bind(“someFuncCall”)
>
>
>
> But how does it work internally? Specifically, will it traverse up the AST
> to bind contextFuncDef every time callExpr is matched? That may be too
> expensive for my use. Or alternatively, does it save the matching ancestor
> in contextFuncDef as it descends down the AST?
>
Both. It memoizes the results of hasAncestor on every level.

> Or the more general question would be: are matchers evaluated inside-out
> or outside-in or in some other way depending on the type of matcher. Is
> there any documentation outside of source code describing how matchers are
> implemented?
>
The behavior is "as-if" you just run over the AST and match every node. The
implementation detail to make this fast is that it uses memoization.


>
>
> *-Riyaz*
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160926/bfcac4c6/attachment.html>


More information about the cfe-dev mailing list