Fixed #1105 (improve check: uninitialized variable not detected for 'a = a')

This commit is contained in:
Daniel Marjamäki 2009-12-30 20:15:44 +01:00
parent 3a46ef0487
commit 461d826eff
4 changed files with 51 additions and 11 deletions

View File

@ -1528,6 +1528,20 @@ private:
if (tok.varId())
{
if (Token::Match(tok.previous(), "[;{}] %var% ="))
{
// using same variable rhs?
for (const Token *tok2 = tok.tokAt(2); tok2; tok2 = tok2->next())
{
if (Token::Match(tok2, ";|)|="))
break;
if (Token::Match(tok2, "%var% ("))
break;
if (tok2->varId() && !Token::simpleMatch(tok2->next(), "="))
use(foundError, checks, tok2);
}
}
if (Token::Match(tok.tokAt(-2), "[;{}] *"))
{
if (Token::simpleMatch(tok.next(), "="))

View File

@ -3800,6 +3800,9 @@ bool Tokenizer::simplifyKnownVariables()
if (varid == 0)
continue;
if (tok2->str() == tok2->strAt(2))
continue;
const bool pointeralias(tok2->tokAt(2)->isName());
std::string value(tok2->strAt(2));

View File

@ -1027,6 +1027,13 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: ret\n", errout.str());
checkUninitVar("void f()\n"
"{\n"
" int a;\n"
" a = 5 + a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("static void foo()\n"
"{\n"
" int i;\n"

View File

@ -107,6 +107,7 @@ private:
TEST_CASE(varid10);
TEST_CASE(varid11);
TEST_CASE(varid12);
TEST_CASE(varid13);
TEST_CASE(varidStl);
TEST_CASE(varid_delete);
TEST_CASE(varid_functions);
@ -1087,7 +1088,6 @@ private:
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
// result..
const std::string actual(tokenizer.tokens()->stringifyList(true));
@ -1120,7 +1120,6 @@ private:
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
// result..
const std::string actual(tokenizer.tokens()->stringifyList(true));
@ -1153,7 +1152,6 @@ private:
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
// result..
const std::string actual(tokenizer.tokens()->stringifyList(true));
@ -1181,7 +1179,6 @@ private:
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
// result..
const std::string actual(tokenizer.tokens()->stringifyList(true));
@ -1231,7 +1228,6 @@ private:
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyTokenList();
// result..
@ -1285,7 +1281,6 @@ private:
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
// result..
const std::string actual(tokenizer.tokens()->stringifyList(true));
@ -1340,7 +1335,6 @@ private:
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
// result..
const std::string actual(tokenizer.tokens()->stringifyList(true));
@ -1362,7 +1356,6 @@ private:
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
// result..
const std::string actual(tokenizer.tokens()->stringifyList(true));
@ -1384,7 +1377,6 @@ private:
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
// result..
const std::string actual(tokenizer.tokens()->stringifyList(true));
@ -1406,7 +1398,6 @@ private:
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
// result..
const std::string actual(tokenizer.tokens()->stringifyList(true));
@ -1427,7 +1418,6 @@ private:
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
// result..
const std::string actual(tokenizer.tokens()->stringifyList(true));
@ -1440,6 +1430,32 @@ private:
ASSERT_EQUALS(expected, actual);
}
void varid13()
{
const std::string code("void f()\n"
"{\n"
" int a; int b;\n"
" a = a;\n"
"}\n");
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
// result..
const std::string actual(tokenizer.tokens()->stringifyList(true));
const std::string expected("\n\n##file 0\n"
"1: void f ( )\n"
"2: {\n"
"3: int a@1 ; int b@2 ;\n"
"4: a@1 = a@1 ;\n"
"5: }\n");
ASSERT_EQUALS(expected, actual);
}
void varidStl()
{
const std::string code("list<int> ints;\n"