Replace and fix findBreakScope with findNextTokenFromBreak

This commit is contained in:
Daniel Marjamäki 2020-12-25 08:43:14 +01:00
parent 8bd783f820
commit b8f45a5c65
4 changed files with 24 additions and 16 deletions

View File

@ -494,10 +494,18 @@ const Token* getCondTokFromEnd(const Token* endBlock)
return getCondTokFromEndImpl(endBlock); return getCondTokFromEndImpl(endBlock);
} }
const Scope *findBreakScope(const Scope *s) { const Token *findNextTokenFromBreak(const Token *breakToken)
while (s && !s->isLoopScope() && s->type != Scope::ScopeType::eSwitch) {
s = s->nestedIn; const Scope *scope = breakToken->scope();
return s; while (scope) {
if (scope->isLoopScope() || scope->type == Scope::ScopeType::eSwitch) {
if (scope->type == Scope::ScopeType::eDo && Token::simpleMatch(scope->bodyEnd, "} while ("))
return scope->bodyEnd->linkAt(2)->next();
return scope->bodyEnd;
}
scope = scope->nestedIn;
}
return nullptr;
} }
bool extractForLoopValues(const Token *forToken, bool extractForLoopValues(const Token *forToken,
@ -2513,10 +2521,10 @@ FwdAnalysis::Result FwdAnalysis::check(const Token* expr, const Token* startToke
// Break => continue checking in outer scope // Break => continue checking in outer scope
while (mWhat!=What::ValueFlow && result.type == FwdAnalysis::Result::Type::BREAK) { while (mWhat!=What::ValueFlow && result.type == FwdAnalysis::Result::Type::BREAK) {
const Scope *breakScope = findBreakScope(result.token->scope()); const Token *scopeEndToken = findNextTokenFromBreak(result.token);
if (!breakScope) if (!scopeEndToken)
break; break;
result = checkRecursive(expr, breakScope->bodyEnd->next(), endToken, exprVarIds, local, false); result = checkRecursive(expr, scopeEndToken->next(), endToken, exprVarIds, local, false);
} }
return result; return result;

View File

@ -115,8 +115,9 @@ const Token* getCondTok(const Token* tok);
Token* getCondTokFromEnd(Token* endBlock); Token* getCondTokFromEnd(Token* endBlock);
const Token* getCondTokFromEnd(const Token* endBlock); const Token* getCondTokFromEnd(const Token* endBlock);
/// For a "break", locate the outer loop/switch scope that is finished /// For a "break" token, locate the next token to execute. The token will
const Scope *findBreakScope(const Scope *scope); /// be either a "}" or a ";".
const Token *findNextTokenFromBreak(const Token *breakToken);
/** /**
* Extract for loop values: loopvar varid, init value, step value, last value (inclusive) * Extract for loop values: loopvar varid, init value, step value, last value (inclusive)

View File

@ -139,10 +139,9 @@ void CheckVaarg::va_list_usage()
} else if (Token::Match(tok, "throw|return")) } else if (Token::Match(tok, "throw|return"))
exitOnEndOfStatement = true; exitOnEndOfStatement = true;
else if (tok->str() == "break") { else if (tok->str() == "break") {
const Scope *breakScope = findBreakScope(tok->scope()); tok = findNextTokenFromBreak(tok);
if (!breakScope) if (!tok)
return; return;
tok = breakScope->bodyEnd;
} else if (tok->str() == "goto" || (mTokenizer->isCPP() && tok->str() == "try")) { } else if (tok->str() == "goto" || (mTokenizer->isCPP() && tok->str() == "try")) {
open = false; open = false;
break; break;

View File

@ -297,10 +297,10 @@ struct ForwardTraversal {
if (!tok) if (!tok)
return Progress::Break; return Progress::Break;
} else if (tok->str() == "break") { } else if (tok->str() == "break") {
const Scope* scope = findBreakScope(tok->scope()); const Token *scopeEndToken = findNextTokenFromBreak(tok);
if (!scope) if (!scopeEndToken)
return Progress::Break; return Progress::Break;
tok = skipTo(tok, scope->bodyEnd, end); tok = skipTo(tok, scopeEndToken, end);
if (!analyzer->lowerToPossible()) if (!analyzer->lowerToPossible())
return Progress::Break; return Progress::Break;
// TODO: Don't break, instead move to the outer scope // TODO: Don't break, instead move to the outer scope