Fixed #1945 (False positives when a for loop header is in a macro)

This commit is contained in:
Daniel Marjamäki 2010-08-26 21:57:48 +02:00
parent 1d9e484053
commit df87ce9e04
2 changed files with 28 additions and 1 deletions

View File

@ -1935,6 +1935,18 @@ void CheckOther::lookupVar(const Token *tok1, const std::string &varname)
if (Token::simpleMatch(tok, "do {"))
for_or_while = true;
// possible unexpanded macro hiding for/while..
if (Token::Match(tok->previous(), "[;{}] %type% {"))
{
bool upper = true;
for (unsigned int i = 0; i < tok->str().length(); ++i)
{
if (!std::isupper(tok->str()[i]))
upper = false;
}
for_or_while |= upper;
}
if (parlevel == 0 && (tok->str() == ";"))
for_or_while = false;
}
@ -4266,4 +4278,5 @@ void CheckOther::selfAssignmentError(const Token *tok, const std::string &varnam
{
reportError(tok, Severity::style,
"selfAssignment", "Redundant assignment of \"" + varname + "\" to itself");
}
}

View File

@ -60,6 +60,7 @@ private:
TEST_CASE(varScope7);
TEST_CASE(varScope8);
TEST_CASE(varScope9); // classes may have extra side-effects
TEST_CASE(varScope10); // Undefined macro FOR
TEST_CASE(nullpointer1);
TEST_CASE(nullpointer2);
@ -700,6 +701,19 @@ private:
ASSERT_EQUALS("", errout.str());
}
void varScope10()
{
// classes may have extra side effects
varScope("int f()\n"
"{\n"
" int x = 0;\n"
" FOR {\n"
" foo(x++);\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void checkNullPointer(const char code[])
{