This is an archive of the discontinued LLVM Phabricator instance.

RecursiveASTVisitor: don't call WalkUp unnecessarily in post-order traversal
ClosedPublic

Authored by gribozavr on Jun 24 2020, 11:01 AM.

Details

Summary

How does RecursiveASTVisitor call the WalkUp callback for expressions?

  • In pre-order traversal mode, RecursiveASTVisitor calls the WalkUp callback from the default implementation of Traverse callbacks.
  • In post-order traversal mode when we don't have a DataRecursionQueue, RecursiveASTVisitor also calls the WalkUp callback from the default implementation of Traverse callbacks.
  • However, in post-order traversal mode when we have a DataRecursionQueue, RecursiveASTVisitor calls the WalkUp callback from PostVisitStmt.

As a result, when the user overrides the Traverse callback, in pre-order
traversal mode they never get the corresponding WalkUp callback. However
in the post-order traversal mode the WalkUp callback is invoked or not
depending on whether the data recursion optimization could be applied.

I had to adjust the implementation of TraverseCXXForRangeStmt in the
syntax tree builder to call the WalkUp method directly, as it was
relying on this behavior. There is an existing test for this
functionality and it prompted me to make this extra fix.

In addition, I had to fix the default implementation implementation of
RecursiveASTVisitor::TraverseSynOrSemInitListExpr to call WalkUpFrom in
the same manner as the implementation generated by the DEF_TRAVERSE_STMT
macro. Without this fix, the InitListExprIsPostOrderNoQueueVisitedTwice
test was failing because WalkUpFromInitListExpr was never called.

Diff Detail

Event Timeline

gribozavr created this revision.Jun 24 2020, 11:01 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 24 2020, 11:01 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
eduucaldas accepted this revision.Jun 25 2020, 4:50 AM
This revision is now accepted and ready to land.Jun 25 2020, 4:50 AM
ymandel added inline comments.Jun 25 2020, 6:33 AM
clang/include/clang/AST/RecursiveASTVisitor.h
335

Why use var-args rather than spelling out the type arguments like you have on lines 339-341 or, simpler, line 351?

339

Given that we've established them to be the same type, why two sets of template arguments?

638–657

Do you explain this logic somewhere? Or do you feel it will be obvious to the reader? (I don't have a good intuition about this class to judge).

gribozavr updated this revision to Diff 273723.Jun 26 2020, 7:11 AM

Addressed review comments, added more fixes that must be committed in the same
change, because splitting them would break tests.

gribozavr2 marked 2 inline comments as done.Jun 26 2020, 7:13 AM
gribozavr2 added a subscriber: gribozavr2.
gribozavr2 added inline comments.
clang/include/clang/AST/RecursiveASTVisitor.h
335

Because all of those things are not relevant. However I do see your point, that in some sense it is a trick to reduce the number of characters, but somewhat hurting readability, so I changed the signature to be explicit.

339

Indeed, simplified the code now!

638–657

Added a comment. The RecursiveASTVisitor is full of tricky logic so no, none of this is obvious.

gribozavr2 edited the summary of this revision. (Show Details)Jun 26 2020, 7:13 AM
ymandel accepted this revision.Jun 26 2020, 7:42 AM
ymandel added inline comments.
clang/include/clang/AST/RecursiveASTVisitor.h
646

delete stray period after "children"

gribozavr updated this revision to Diff 273736.Jun 26 2020, 7:53 AM

Removed a stray period in a comment.

gribozavr2 marked an inline comment as done.Jun 26 2020, 7:53 AM
gribozavr2 added inline comments.
clang/include/clang/AST/RecursiveASTVisitor.h
646

Done.

gribozavr updated this revision to Diff 274027.Jun 29 2020, 2:58 AM

Rebased the patch.

gribozavr updated this revision to Diff 274032.Jun 29 2020, 3:14 AM

Added a FIXME about a regression.

gribozavr updated this revision to Diff 274481.Jun 30 2020, 8:01 AM

Rebased the patch.

eduucaldas accepted this revision.Jun 30 2020, 10:52 PM
This revision was automatically updated to reflect the committed changes.