[cfe-dev] Match callback invocation order

Billy O'Mahony via cfe-dev cfe-dev at lists.llvm.org
Fri Jun 19 07:46:35 PDT 2020


Hi David,

thanks for that detail. I did have a quick look at the MatchFinder code in
the source but couldn't see where it might be changed (typically the
example of MatchFinder do not involve inheriting and specialising it) or
where I might override it I did inherit from it. But last time I looked at
C++ <algorithm> was the new hotness so that might be a bridge too far for
me right now.

In any case, I've started a new impl using RecusiveASTVisitor and I don't
have the out-of-order callback issue.

/Billy.

On Mon, 15 Jun 2020 at 22:36, David Rector via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> I am not familiar with ASTMatchers, but am with RecursiveASTVisitor and
> the issue seems to what some call the "data recursion" optimization that is
> specific to TraverseStmt.  While e.g. TraverseDecl(Decl *) and
> TraverseType(QualType), for statements the default signature is
> TraverseStmt(Stmt *, DataRecursionQueue *Queue); this queue argument is
> employed to avoid excessive stack depths that apparently occur with some
> very big expressions.  The way it works, I believe, is by enqueuing nested
> statements instead of processing them right away, in exactly the manner you
> describe.
>
> Fortunately, it’s simple to turn off this optimization: just define a
> TraverseStmt(Stmt *S) in your Derived (I guess in this case, add this
> definition to whatever component of ASTMatchers inherits from
> RecursiveASTVisitor<…>).
>
> To avoid this confusion in the future (the same problem vexed me awhile
> back in a different context), It might be wise to alter the default
> TraverseStmt implementation so that, by default, it keeps track of the
> stack depth and only employs the data recursion queue when the stack depths
> really do get too great, and otherwise performs nested traversals in the
> same manner as TraverseDecl and TraverseType, if that makes sense.
>
> Dave
>
>
> In the documentation for MatchFinder it says "The order of matches is
> guaranteed to be equivalent to doing a pre-order traversal on the AST, and
> applying the matchers in the order in which they were added to the
> MatchFinder." but I can't reconcile what I see with what I found on the
> wikipedia for 'pre-order traversal'.
>
> Is this a bug?
>
> How can I influence the traversal order so that my matchers fire in
> source-code order?
>
> Thanks,
> Billy.
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://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/20200619/44bb1d45/attachment.html>


More information about the cfe-dev mailing list