RecursiveASTVisitor: don't call WalkUp unnecessarily in post-order traversal
authorDmitri Gribenko <gribozavr@gmail.com>
Fri, 3 Jul 2020 10:38:45 +0000 (12:38 +0200)
committerDmitri Gribenko <gribozavr@gmail.com>
Fri, 3 Jul 2020 11:03:19 +0000 (13:03 +0200)
commit94454442c3c15a67ae70ef3a73616632968973fc
treef509fda940b998434d37e0ac878cedbb49be1d9d
parent79889691430d8e76e908706170102a8b46432a07
RecursiveASTVisitor: don't call WalkUp unnecessarily in post-order traversal

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.

Reviewers: eduucaldas, ymandel

Reviewed By: eduucaldas, ymandel

Subscribers: gribozavr2, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D82486
clang/include/clang/AST/RecursiveASTVisitor.h
clang/lib/Tooling/Syntax/BuildTree.cpp
clang/unittests/Tooling/RecursiveASTVisitorTests/Callbacks.cpp