Fixed #2639 (False positive: confusion between struct and function)

This commit is contained in:
Daniel Marjamäki 2011-03-13 08:38:40 +01:00
parent 27f4b8b88b
commit ed11a9e3cb
2 changed files with 27 additions and 1 deletions

View File

@ -2941,6 +2941,23 @@ void CheckOther::checkMathFunctions()
}
}
/** Is there a function with given name? */
static bool isFunction(const std::string &name, const Token *startToken)
{
const std::string pattern1(name + " (");
for (const Token *tok = startToken; tok; tok = tok->next())
{
// skip executable scopes etc
if (tok->str() == "(" || tok->str() == "{")
tok = tok->link();
// function declaration/implementation found
if (Token::simpleMatch(tok, pattern1.c_str()))
return true;
}
return false;
}
void CheckOther::checkMisusedScopedObject()
{
// Skip this check for .c files
@ -2979,7 +2996,7 @@ void CheckOther::checkMisusedScopedObject()
if (Token::Match(tok, "[;{}] %var% (")
&& Token::simpleMatch(tok->tokAt(2)->link(), ") ;")
&& symbolDatabase->isClassOrStruct(tok->next()->str())
)
&& !isFunction(tok->next()->str(), _tokenizer->tokens()))
{
tok = tok->next();
misusedScopeObjectError(tok, tok->str());

View File

@ -1774,6 +1774,15 @@ private:
check(code, "test.c");
ASSERT_EQUALS("", errout.str());
// Ticket #2639
check("struct stat { int a; int b; };\n"
"void stat(const char *fn, struct stat *);\n"
"\n"
"void foo() {\n"
" stat(\"file.txt\", &st);\n"
"}\n");
ASSERT_EQUALS("",errout.str());
}
void testMisusedScopeObjectDoesNotPickNestedClass()