Fixed #1819 (False positive: auto variable assignment (unseen typedef))

This commit is contained in:
Daniel Marjamäki 2010-10-22 21:12:28 +02:00
parent 9fdc03fc1d
commit c907acf767
2 changed files with 21 additions and 1 deletions

View File

@ -137,6 +137,9 @@ void CheckAutoVariables::autoVariables()
bool begin_function_decl = false; bool begin_function_decl = false;
int bindent = 0; int bindent = 0;
// Which variables have an unknown type?
std::set<unsigned int> unknown_type;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{ {
@ -188,17 +191,24 @@ void CheckAutoVariables::autoVariables()
else if (Token::Match(tok, "%var% %var% ;") && !isExternOrStatic(tok) && isTypeName(tok)) else if (Token::Match(tok, "%var% %var% ;") && !isExternOrStatic(tok) && isTypeName(tok))
{ {
addVD(tok->next()->varId()); addVD(tok->next()->varId());
if (!tok->isStandardType() &&
NULL == Token::findmatch(_tokenizer->tokens(), ("struct|class " + tok->str()).c_str()))
{
unknown_type.insert(tok->next()->varId());
}
} }
else if (Token::Match(tok, "const %var% %var% ;") && !isExternOrStatic(tok) && isTypeName(tok->next())) else if (Token::Match(tok, "const %var% %var% ;") && !isExternOrStatic(tok) && isTypeName(tok->next()))
{ {
addVD(tok->tokAt(2)->varId()); addVD(tok->tokAt(2)->varId());
} }
//Critical assignment //Critical assignment
else if (Token::Match(tok, "[;{}] %var% = & %var%") && errorAv(tok->tokAt(1), tok->tokAt(4))) else if (Token::Match(tok, "[;{}] %var% = & %var%") && errorAv(tok->tokAt(1), tok->tokAt(4)))
{ {
errorAutoVariableAssignment(tok); errorAutoVariableAssignment(tok);
} }
else if (Token::Match(tok, "[;{}] * %var% = & %var%") && errorAv(tok->tokAt(2), tok->tokAt(5))) else if (Token::Match(tok, "[;{}] * %var% = & %var%") && errorAv(tok->tokAt(2), tok->tokAt(5)) &&
unknown_type.find(tok->tokAt(5)->varId()) == unknown_type.end())
{ {
errorAutoVariableAssignment(tok); errorAutoVariableAssignment(tok);
} }

View File

@ -68,6 +68,7 @@ private:
TEST_CASE(testautovar_return); TEST_CASE(testautovar_return);
TEST_CASE(testautovar_extern); TEST_CASE(testautovar_extern);
TEST_CASE(testinvaliddealloc); TEST_CASE(testinvaliddealloc);
TEST_CASE(testassign); // Ticket #1819
TEST_CASE(returnLocalVariable1); TEST_CASE(returnLocalVariable1);
TEST_CASE(returnLocalVariable2); TEST_CASE(returnLocalVariable2);
@ -149,6 +150,15 @@ private:
ASSERT_EQUALS(std::string(""), errout.str()); ASSERT_EQUALS(std::string(""), errout.str());
} }
void testassign()
{
check("void f(EventPtr *eventP, ActionPtr **actionsP) {\n"
" EventPtr event = *eventP;\n"
" *actionsP = &event->actions;\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
void returnLocalVariable1() void returnLocalVariable1()
{ {
check("char *foo()\n" check("char *foo()\n"