astutils.cpp: optimized visitAstNodesGeneric() a bit more by avoiding unnecessary checks and std::stack usage (#3732)

This commit is contained in:
Oliver Stöneberg 2022-01-24 21:44:09 +01:00 committed by GitHub
parent b491fcc489
commit 8179226b18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 8 deletions

View File

@ -42,14 +42,12 @@
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )> template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
void visitAstNodesGeneric(T *ast, std::function<ChildrenToVisit(T *)> visitor) void visitAstNodesGeneric(T *ast, std::function<ChildrenToVisit(T *)> visitor)
{ {
std::stack<T *, std::vector<T *>> tokens; if (!ast)
tokens.push(ast); return;
while (!tokens.empty()) {
T *tok = tokens.top();
tokens.pop();
if (!tok)
continue;
std::stack<T *, std::vector<T *>> tokens;
T *tok = ast;
do {
ChildrenToVisit c = visitor(tok); ChildrenToVisit c = visitor(tok);
if (c == ChildrenToVisit::done) if (c == ChildrenToVisit::done)
@ -64,7 +62,13 @@ void visitAstNodesGeneric(T *ast, std::function<ChildrenToVisit(T *)> visitor)
if (t1) if (t1)
tokens.push(t1); tokens.push(t1);
} }
}
if (tokens.empty())
break;
tok = tokens.top();
tokens.pop();
} while (true);
} }
void visitAstNodes(const Token *ast, std::function<ChildrenToVisit(const Token *)> visitor) void visitAstNodes(const Token *ast, std::function<ChildrenToVisit(const Token *)> visitor)