Refactor: Use a template instead of std::function in forward analyzer to help improve debugging (#3551)

This commit is contained in:
Paul Fultz II 2021-11-08 00:46:52 -06:00 committed by GitHub
parent 035c70c441
commit 662ada6930
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 9 deletions

View File

@ -93,8 +93,8 @@ struct ForwardTraversal {
return evalCond(tok, ctx).second; return evalCond(tok, ctx).second;
} }
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )> template<class T, class F, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
Progress traverseTok(T* tok, std::function<Progress(T*)> f, bool traverseUnknown, T** out = nullptr) { Progress traverseTok(T* tok, F f, bool traverseUnknown, T** out = nullptr) {
if (Token::Match(tok, "asm|goto|setjmp|longjmp")) if (Token::Match(tok, "asm|goto|setjmp|longjmp"))
return Break(Analyzer::Terminate::Bail); return Break(Analyzer::Terminate::Bail);
else if (Token::simpleMatch(tok, "continue")) { else if (Token::simpleMatch(tok, "continue")) {
@ -134,8 +134,8 @@ struct ForwardTraversal {
return Progress::Continue; return Progress::Continue;
} }
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )> template<class T, class F, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
Progress traverseRecursive(T* tok, std::function<Progress(T*)> f, bool traverseUnknown, unsigned int recursion=0) { Progress traverseRecursive(T* tok, F f, bool traverseUnknown, unsigned int recursion=0) {
if (!tok) if (!tok)
return Progress::Continue; return Progress::Continue;
if (recursion > 10000) if (recursion > 10000)
@ -206,7 +206,7 @@ struct ForwardTraversal {
} }
Progress updateTok(Token* tok, Token** out = nullptr) { Progress updateTok(Token* tok, Token** out = nullptr) {
std::function<Progress(Token*)> f = [this](Token* tok2) { auto f = [this](Token* tok2) {
return update(tok2); return update(tok2);
}; };
return traverseTok(tok, f, false, out); return traverseTok(tok, f, false, out);
@ -214,14 +214,14 @@ struct ForwardTraversal {
Progress updateRecursive(Token* tok) { Progress updateRecursive(Token* tok) {
forked = false; forked = false;
std::function<Progress(Token*)> f = [this](Token* tok2) { auto f = [this](Token* tok2) {
return update(tok2); return update(tok2);
}; };
return traverseRecursive(tok, f, false); return traverseRecursive(tok, f, false);
} }
template<class T> template<class T, class F>
T* findRange(T* start, const Token* end, std::function<bool(Analyzer::Action)> pred) { T* findRange(T* start, const Token* end, F pred) {
for (T* tok = start; tok && tok != end; tok = tok->next()) { for (T* tok = start; tok && tok != end; tok = tok->next()) {
Analyzer::Action action = analyzer->analyze(tok, Analyzer::Direction::Forward); Analyzer::Action action = analyzer->analyze(tok, Analyzer::Direction::Forward);
if (pred(action)) if (pred(action))
@ -232,7 +232,7 @@ struct ForwardTraversal {
Analyzer::Action analyzeRecursive(const Token* start) { Analyzer::Action analyzeRecursive(const Token* start) {
Analyzer::Action result = Analyzer::Action::None; Analyzer::Action result = Analyzer::Action::None;
std::function<Progress(const Token*)> f = [&](const Token* tok) { auto f = [&](const Token* tok) {
result = analyzer->analyze(tok, Analyzer::Direction::Forward); result = analyzer->analyze(tok, Analyzer::Direction::Forward);
if (result.isModified() || result.isInconclusive()) if (result.isModified() || result.isInconclusive())
return Break(); return Break();