From 89a8ab58e92725f73c64a0fb719e30377bffe32d Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 24 Nov 2015 07:13:06 +0000 Subject: [PATCH] Reduce the stack usage per recursive step when RecursiveASTVisitor cannot perform data recursion. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@253958 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/RecursiveASTVisitor.h | 37 +++++++++++-------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/include/clang/AST/RecursiveASTVisitor.h b/include/clang/AST/RecursiveASTVisitor.h index f029c78bea..4da2f67493 100644 --- a/include/clang/AST/RecursiveASTVisitor.h +++ b/include/clang/AST/RecursiveASTVisitor.h @@ -471,27 +471,9 @@ private: /// \brief Process clauses with list of variables. template bool VisitOMPClauseList(T *Node); - bool dataTraverse(Stmt *S); bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue); }; -template -bool RecursiveASTVisitor::dataTraverse(Stmt *S) { - SmallVector Queue; - Queue.push_back(S); - - while (!Queue.empty()) { - Stmt *CurrS = Queue.pop_back_val(); - - size_t N = Queue.size(); - TRY_TO(dataTraverseNode(CurrS, &Queue)); - // Process new children in the order they were added. - std::reverse(Queue.begin() + N, Queue.end()); - } - - return true; -} - template bool RecursiveASTVisitor::dataTraverseNode(Stmt *S, DataRecursionQueue *Queue) { @@ -561,10 +543,23 @@ bool RecursiveASTVisitor::TraverseStmt(Stmt *S, &RecursiveASTVisitor::TraverseStmt>::value) return dataTraverseNode(S, nullptr); - if (!Queue) - return dataTraverse(S); + if (Queue) { + Queue->push_back(S); + return true; + } + + SmallVector LocalQueue; + LocalQueue.push_back(S); + + while (!LocalQueue.empty()) { + Stmt *CurrS = LocalQueue.pop_back_val(); + + size_t N = LocalQueue.size(); + TRY_TO(dataTraverseNode(CurrS, &LocalQueue)); + // Process new children in the order they were added. + std::reverse(LocalQueue.begin() + N, LocalQueue.end()); + } - Queue->push_back(S); return true; } -- 2.50.1