Fixed #2084 (False positive: object destroyed immediately (struct and function with same name))

Tightened up class definition matching so as not to match C-style struct tags.
This commit is contained in:
Pete Johns 2010-10-13 20:31:41 +11:00
parent a58094e827
commit fbd3d92aa9
2 changed files with 20 additions and 3 deletions

View File

@ -3884,8 +3884,8 @@ bool CheckOther::isIdentifierObjectType(const Token * const tok)
return found->second;
}
const std::string classDef = std::string("class|struct ") + identifier;
const bool result = Token::findmatch(_tokenizer->tokens(), classDef.c_str());
const std::string classDefnOrDecl = std::string("class|struct ") + identifier + " [{:;]";
const bool result = Token::findmatch(_tokenizer->tokens(), classDefnOrDecl.c_str());
isClassResults.insert(std::make_pair(identifier, result));
return result;
}
@ -3893,6 +3893,7 @@ bool CheckOther::isIdentifierObjectType(const Token * const tok)
void CheckOther::checkMisusedScopedObject()
{
const char localClassDefinition[] = "class|struct %var% [{:]";
bool withinFunction = false;
unsigned int depth = 0;
std::string className = "";
@ -3917,7 +3918,7 @@ void CheckOther::checkMisusedScopedObject()
className.clear();
}
}
else if (Token::Match(tok, "class|struct"))
else if (Token::Match(tok, localClassDefinition))
{
className = tok->strAt(1);
isClassResults.insert(std::make_pair(className, false));

View File

@ -115,6 +115,7 @@ private:
TEST_CASE(testMisusedScopeObjectDoesNotPickLocalClassConstructors);
TEST_CASE(testMisusedScopeObjectDoesNotPickUsedObject);
TEST_CASE(trac2071);
TEST_CASE(trac2084);
TEST_CASE(assignmentInAssert);
}
@ -3160,6 +3161,21 @@ private:
ASSERT_EQUALS("", errout.str());
}
void trac2084()
{
check("#include <signal.h>\n"
"\n"
"void f()\n"
"{\n"
" struct sigaction sa;\n"
"\n"
" { sigaction(SIGHUP, &sa, 0); };\n"
" { sigaction(SIGINT, &sa, 0); };\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
}
void trac2071()
{
check("void f() {\n"