From a36dd12e443224afc896e35a81d7cf829578e787 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Sat, 13 Feb 2016 01:24:19 +0000 Subject: [PATCH] [RecursiveASTVisitor] Introduce dataTraverseStmtPre()/dataTraverseStmtPost() to allow clients to do before/after actions during data recursive visitation. This should fix the asan bot that hits stack overflow in a couple of test/Index tests. llvm-svn: 260785 --- clang/include/clang/AST/RecursiveASTVisitor.h | 17 ++++++++++++++++- clang/lib/Index/IndexBody.cpp | 10 +++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 1e397ae..442882a 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -163,6 +163,18 @@ public: /// otherwise (including when the argument is nullptr). bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue = nullptr); + /// Invoked before visiting a statement or expression via data recursion. + /// + /// \returns false to skip visiting the node, true otherwise. + bool dataTraverseStmtPre(Stmt *S) { return true; } + + /// Invoked after visiting a statement or expression via data recursion. + /// This is not invoked if the previously invoked \c dataTraverseStmtPre + /// returned false. + /// + /// \returns false if the visitation was terminated early, true otherwise. + bool dataTraverseStmtPost(Stmt *S) { return true; } + /// \brief Recursively visit a type, by dispatching to /// Traverse*Type() based on the argument's getTypeClass() property. /// @@ -557,7 +569,10 @@ bool RecursiveASTVisitor::TraverseStmt(Stmt *S, Stmt *CurrS = LocalQueue.pop_back_val(); size_t N = LocalQueue.size(); - TRY_TO(dataTraverseNode(CurrS, &LocalQueue)); + if (getDerived().dataTraverseStmtPre(CurrS)) { + TRY_TO(dataTraverseNode(CurrS, &LocalQueue)); + TRY_TO(dataTraverseStmtPost(CurrS)); + } // Process new children in the order they were added. std::reverse(LocalQueue.begin() + N, LocalQueue.end()); } diff --git a/clang/lib/Index/IndexBody.cpp b/clang/lib/Index/IndexBody.cpp index 56fba28..f716445 100644 --- a/clang/lib/Index/IndexBody.cpp +++ b/clang/lib/Index/IndexBody.cpp @@ -29,11 +29,15 @@ public: bool shouldWalkTypesOfTypeLocs() const { return false; } - bool TraverseStmt(Stmt *S) { + bool dataTraverseStmtPre(Stmt *S) { StmtStack.push_back(S); - bool ret = base::TraverseStmt(S); + return true; + } + + bool dataTraverseStmtPost(Stmt *S) { + assert(StmtStack.back() == S); StmtStack.pop_back(); - return ret; + return true; } bool TraverseTypeLoc(TypeLoc TL) { -- 2.7.4