assignif: Fixed testcase. parse while loops if variable is local and not external.

This commit is contained in:
Daniel Marjamäki 2012-09-29 10:54:09 +02:00
parent b6153a00ee
commit 10aa667648
3 changed files with 17 additions and 5 deletions

View File

@ -36,6 +36,8 @@ void CheckAssignIf::assignIf()
if (!_settings->isEnabled("style")) if (!_settings->isEnabled("style"))
return; return;
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (tok->str() != "=") if (tok->str() != "=")
continue; continue;
@ -51,7 +53,12 @@ void CheckAssignIf::assignIf()
if (num < 0) if (num < 0)
continue; continue;
assignIfParseScope(tok, tok->tokAt(4), varid, bitop, num); bool islocal = false;
const Variable *var = symbolDatabase->getVariableFromVarId(varid);
if (var && var->isLocal())
islocal = true;
assignIfParseScope(tok, tok->tokAt(4), varid, islocal, bitop, num);
} }
} }
} }
@ -60,6 +67,7 @@ void CheckAssignIf::assignIf()
bool CheckAssignIf::assignIfParseScope(const Token * const assignTok, bool CheckAssignIf::assignIfParseScope(const Token * const assignTok,
const Token * const startTok, const Token * const startTok,
const unsigned int varid, const unsigned int varid,
const bool islocal,
const char bitop, const char bitop,
const MathLib::bigint num) const MathLib::bigint num)
{ {
@ -68,7 +76,10 @@ bool CheckAssignIf::assignIfParseScope(const Token * const assignTok,
return true; return true;
if (tok2->str() == "}") if (tok2->str() == "}")
return false; return false;
if (Token::Match(tok2, "if (")) { if (Token::Match(tok2, "if|while (")) {
if (!islocal && tok2->str() == "while")
continue;
// parse condition // parse condition
const Token * const end = tok2->next()->link(); const Token * const end = tok2->next()->link();
for (; tok2 != end; tok2 = tok2->next()) { for (; tok2 != end; tok2 = tok2->next()) {
@ -87,10 +98,10 @@ bool CheckAssignIf::assignIfParseScope(const Token * const assignTok,
} }
} }
bool ret1 = assignIfParseScope(assignTok, end->tokAt(2), varid, bitop, num); bool ret1 = assignIfParseScope(assignTok, end->tokAt(2), varid, islocal, bitop, num);
bool ret2 = false; bool ret2 = false;
if (Token::simpleMatch(end->next()->link(), "} else {")) if (Token::simpleMatch(end->next()->link(), "} else {"))
ret2 = assignIfParseScope(assignTok, end->next()->link()->tokAt(3), varid, bitop, num); ret2 = assignIfParseScope(assignTok, end->next()->link()->tokAt(3), varid, islocal, bitop, num);
if (ret1 || ret2) if (ret1 || ret2)
return true; return true;
} }

View File

@ -59,6 +59,7 @@ public:
bool assignIfParseScope(const Token * const assignTok, bool assignIfParseScope(const Token * const assignTok,
const Token * const startTok, const Token * const startTok,
const unsigned int varid, const unsigned int varid,
const bool islocal,
const char bitop, const char bitop,
const MathLib::bigint num); const MathLib::bigint num);

View File

@ -119,7 +119,7 @@ private:
" int y = x & 7;\n" " int y = x & 7;\n"
" while (y==8);\n" // local variable => always false " while (y==8);\n" // local variable => always false
"}"); "}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (style) Mismatching assignment and comparison, comparison 'y==8' is always false.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Mismatching assignment and comparison, comparison 'y==8' is always false.\n", errout.str());
check("void f(int x) {\n" check("void f(int x) {\n"
" extern int y; y = x & 7;\n" " extern int y; y = x & 7;\n"