fix false positive introduced in CheckAutoVariables::autoVariables conversion to use symbol database

This commit is contained in:
Robert Reif 2011-04-26 20:16:09 -04:00
parent b0ed595e24
commit 29c73e0571
2 changed files with 21 additions and 9 deletions

View File

@ -41,7 +41,9 @@ bool CheckAutoVariables::errorAv(const Token* left, const Token* right)
{
const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(left->varId());
if (!var || !var->isArgument())
if (!var || !var->isArgument() ||
(!var->isArray() && !Token::Match(var->nameToken()->tokAt(-3), "%type% * *")) ||
(var->isArray() && !Token::Match(var->nameToken()->tokAt(-2), "%type% *")))
return false;
return isAutoVar(right->varId());
@ -80,8 +82,6 @@ void CheckAutoVariables::autoVariables()
continue;
unsigned int indentlevel = 0;
// Which variables have an unknown type?
std::set<unsigned int> unknown_type;
for (const Token *tok = scope->classDef->next()->link(); tok; tok = tok->next())
{
// indentlevel..
@ -95,12 +95,7 @@ void CheckAutoVariables::autoVariables()
}
//Critical assignment
if (Token::Match(tok, "[;{}] %var% = & %var%") && errorAv(tok->tokAt(1), tok->tokAt(4)))
{
errorAutoVariableAssignment(tok);
}
else if (Token::Match(tok, "[;{}] * %var% = & %var%") && errorAv(tok->tokAt(2), tok->tokAt(5)) &&
unknown_type.find(tok->tokAt(5)->varId()) == unknown_type.end())
if (Token::Match(tok, "[;{}] * %var% = & %var%") && errorAv(tok->tokAt(2), tok->tokAt(5)))
{
errorAutoVariableAssignment(tok);
}

View File

@ -104,6 +104,13 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Assigning address of local auto-variable to a function parameter.\n", errout.str());
check("void func1(int **res)\n"
"{\n"
" int num = 2;\n"
" res = &num;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void func1(int **res)\n"
"{\n"
" int num = 2;\n"
@ -124,6 +131,16 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:6]: (error) Assigning address of local auto-variable to a function parameter.\n", errout.str());
check("class Fred {\n"
" void func1(int **res);\n"
"}\n"
"void Fred::func1(int **res)\n"
"{\n"
" int num = 2;\n"
" res = &num;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("class Fred {\n"
" void func1(int **res);\n"
"}\n"