fix #1514 (Improve CheckAutoVariables: assign autovariable address to effective parameter)

This commit is contained in:
Robert Reif 2011-07-21 22:37:36 -04:00
parent 4e64d35462
commit 84ca1902a5
2 changed files with 23 additions and 0 deletions

View File

@ -110,6 +110,7 @@ void CheckAutoVariables::autoVariables()
if (var2 && var2->isLocal() && !var2->isStatic()) if (var2 && var2->isLocal() && !var2->isStatic())
errorAutoVariableAssignment(tok); errorAutoVariableAssignment(tok);
} }
tok = tok->tokAt(6);
} }
else if (Token::Match(tok, "[;{}] * %var% = %var% ;")) else if (Token::Match(tok, "[;{}] * %var% = %var% ;"))
{ {
@ -122,6 +123,17 @@ void CheckAutoVariables::autoVariables()
} }
tok = tok->tokAt(4); tok = tok->tokAt(4);
} }
else if (Token::Match(tok, "[;{}] %var% = & %var% ;"))
{
const Variable * var1 = symbolDatabase->getVariableFromVarId(tok->tokAt(1)->varId());
if (var1 && var1->isArgument() && Token::Match(var1->nameToken()->tokAt(-2), "%type% *"))
{
const Variable * var2 = symbolDatabase->getVariableFromVarId(tok->tokAt(4)->varId());
if (var2 && var2->isLocal() && !var2->isStatic())
errorAutoVariableAssignment(tok);
}
tok = tok->tokAt(4);
}
else if (Token::Match(tok, "[;{}] %var% [ %any% ] = & %var%") && errorAv(tok->tokAt(1), tok->tokAt(7))) else if (Token::Match(tok, "[;{}] %var% [ %any% ] = & %var%") && errorAv(tok->tokAt(1), tok->tokAt(7)))
{ {
errorAutoVariableAssignment(tok); errorAutoVariableAssignment(tok);

View File

@ -72,6 +72,7 @@ private:
TEST_CASE(testautovar3); // ticket #2925 TEST_CASE(testautovar3); // ticket #2925
TEST_CASE(testautovar4); // ticket #2928 TEST_CASE(testautovar4); // ticket #2928
TEST_CASE(testautovar5); // ticket #2926 TEST_CASE(testautovar5); // ticket #2926
TEST_CASE(testautovar6); // ticket #1514
TEST_CASE(testautovar_array1); TEST_CASE(testautovar_array1);
TEST_CASE(testautovar_array2); TEST_CASE(testautovar_array2);
TEST_CASE(testautovar_return1); TEST_CASE(testautovar_return1);
@ -186,6 +187,16 @@ private:
ASSERT_EQUALS("[test.cpp:3]: (error) Assigning address of local auto-variable to a function parameter.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Assigning address of local auto-variable to a function parameter.\n", errout.str());
} }
void testautovar6() // ticket #1514
{
check("void foo(int * x)\n"
"{\n"
" int y;\n"
" x = &y;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Assigning address of local auto-variable to a function parameter.\n", errout.str());
}
void testautovar_array1() void testautovar_array1()
{ {
check("void func1(int* arr[2])\n" check("void func1(int* arr[2])\n"