Fixed #874 (false positive: uninitialized variable)

This commit is contained in:
Daniel Marjamäki 2009-10-30 14:06:40 +01:00
parent 768225bb1b
commit 44a9c9b458
2 changed files with 40 additions and 0 deletions

View File

@ -1258,6 +1258,31 @@ static const Token *uninitvar_checkscope(const Token *tok, const unsigned int va
return 0; return 0;
} }
if (Token::Match(tok, "%var% ("))
{
// is the variable passed as a parameter to some function?
unsigned int parlevel = 0;
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
{
if (tok2->str() == "{")
++parlevel;
else if (tok2->str() == "}")
{
if (parlevel <= 1)
break;
--parlevel;
}
else if (tok2->varId() == varid)
{
// it is possible that the variable is initialized here
init = true;
return 0;
}
}
}
if (Token::Match(tok, "return| %varid% .|", varid)) if (Token::Match(tok, "return| %varid% .|", varid))
return tok; return tok;
} }

View File

@ -933,6 +933,7 @@ private:
void uninitvar1() void uninitvar1()
{ {
// if..
checkUninitVar("static void foo()\n" checkUninitVar("static void foo()\n"
"{\n" "{\n"
" Foo *p;\n" " Foo *p;\n"
@ -963,12 +964,26 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// member variables..
checkUninitVar("class Fred\n" checkUninitVar("class Fred\n"
"{\n" "{\n"
" int i;\n" " int i;\n"
" int a() { return i; }\n" " int a() { return i; }\n"
"};\n"); "};\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// function calls..
checkUninitVar("void assignOne(int &x)\n"
"{ x = 1; }\n"
"\n"
"int f()\n"
"{\n"
" int i;\n"
" assignOne(i);\n"
" return i;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
} }