Refactoring, use early continue

This commit is contained in:
Daniel Marjamäki 2018-04-08 09:30:13 +02:00
parent 795b9f5e0f
commit ee5c60e8f6
1 changed files with 31 additions and 29 deletions

View File

@ -2349,8 +2349,8 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat
static void valueFlowAfterCondition(TokenList *tokenlist, SymbolDatabase* symboldatabase, ErrorLogger *errorLogger, const Settings *settings) static void valueFlowAfterCondition(TokenList *tokenlist, SymbolDatabase* symboldatabase, ErrorLogger *errorLogger, const Settings *settings)
{ {
const std::size_t functions = symboldatabase->functionScopes.size(); const std::size_t functions = symboldatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) { for (std::size_t func = 0; func < functions; ++func) {
const Scope * scope = symboldatabase->functionScopes[i]; const Scope * scope = symboldatabase->functionScopes[func];
std::set<unsigned> aliased; std::set<unsigned> aliased;
for (Token* tok = const_cast<Token*>(scope->classStart); tok != scope->classEnd; tok = tok->next()) { for (Token* tok = const_cast<Token*>(scope->classStart); tok != scope->classEnd; tok = tok->next()) {
const Token * vartok = nullptr; const Token * vartok = nullptr;
@ -2515,9 +2515,11 @@ static void valueFlowAfterCondition(TokenList *tokenlist, SymbolDatabase* symbol
if (Token::Match(tok, "%name%|!=|>|<")) if (Token::Match(tok, "%name%|!=|>|<"))
check_else = true; check_else = true;
// determine startToken based on codeblock if (!check_if && !check_else)
if (check_if || check_else) { continue;
// if astParent is "!" we need to invert codeblock
// if astParent is "!" we need to invert codeblock
{
const Token *parent = tok->astParent(); const Token *parent = tok->astParent();
while (parent && parent->str() == "&&") while (parent && parent->str() == "&&")
parent = parent->astParent(); parent = parent->astParent();
@ -2525,37 +2527,37 @@ static void valueFlowAfterCondition(TokenList *tokenlist, SymbolDatabase* symbol
check_if = !check_if; check_if = !check_if;
check_else = !check_else; check_else = !check_else;
} }
// convert codeblock to a startToken
if (check_if && Token::simpleMatch(top->link(), ") {"))
startTokens[0] = top->link()->next();
if (check_else && Token::simpleMatch(top->link()->linkAt(1), "} else {"))
startTokens[1] = top->link()->linkAt(1)->tokAt(2);
} }
// determine startToken(s)
if (check_if && Token::simpleMatch(top->link(), ") {"))
startTokens[0] = top->link()->next();
if (check_else && Token::simpleMatch(top->link()->linkAt(1), "} else {"))
startTokens[1] = top->link()->linkAt(1)->tokAt(2);
bool bail = false; bool bail = false;
for (int i=0; i<2; i++) { for (int i=0; i<2; i++) {
Token * startToken = startTokens[i]; Token * startToken = startTokens[i];
if (startToken) { if (!startToken)
std::list<ValueFlow::Value> & values = (i==0 ? true_values : false_values); continue;
if (values.size() == 1U && Token::Match(tok, "==|!")) { std::list<ValueFlow::Value> & values = (i==0 ? true_values : false_values);
const Token *parent = tok->astParent(); if (values.size() == 1U && Token::Match(tok, "==|!")) {
while (parent && parent->str() == "&&") const Token *parent = tok->astParent();
parent = parent->astParent(); while (parent && parent->str() == "&&")
if (parent && parent->str() == "(") parent = parent->astParent();
values.front().setKnown(); if (parent && parent->str() == "(")
} values.front().setKnown();
}
valueFlowForward(startTokens[i]->next(), startTokens[i]->link(), var, varid, values, true, false, tokenlist, errorLogger, settings); valueFlowForward(startTokens[i]->next(), startTokens[i]->link(), var, varid, values, true, false, tokenlist, errorLogger, settings);
values.front().setPossible(); values.front().setPossible();
if (isVariableChanged(startTokens[i], startTokens[i]->link(), varid, var->isGlobal(), settings)) { if (isVariableChanged(startTokens[i], startTokens[i]->link(), varid, var->isGlobal(), settings)) {
// TODO: The endToken should not be startTokens[i]->link() in the valueFlowForward call // TODO: The endToken should not be startTokens[i]->link() in the valueFlowForward call
if (settings->debugwarnings) if (settings->debugwarnings)
bailout(tokenlist, errorLogger, startTokens[i]->link(), "valueFlowAfterCondition: " + var->name() + " is changed in conditional block"); bailout(tokenlist, errorLogger, startTokens[i]->link(), "valueFlowAfterCondition: " + var->name() + " is changed in conditional block");
bail = true; bail = true;
break; break;
}
} }
} }
if (bail) if (bail)