Fixed #1496 (false positive: Member variable not initialized in the constructor 'X::m_d')

This commit is contained in:
Daniel Marjamäki 2010-03-26 17:19:33 +01:00
parent c0e09c4cb3
commit 258e7e292e
2 changed files with 47 additions and 4 deletions

View File

@ -275,7 +275,7 @@ void CheckClass::initializeVarList(const Token *tok1, const Token *ftok, Var *va
{ {
for (Var *var = varlist; var; var = var->next) for (Var *var = varlist; var; var = var->next)
var->init = true; var->init = true;
break; return;
} }
// Clearing array.. // Clearing array..
@ -286,12 +286,23 @@ void CheckClass::initializeVarList(const Token *tok1, const Token *ftok, Var *va
continue; continue;
} }
else if (ftok->str() == "if")
continue;
// Calling member function? // Calling member function?
else if (Token::Match(ftok, "%var% (")) else if (Token::Match(ftok, "%var% ("))
{ {
// Passing "this" => assume that everything is initialized
for (const Token * tok2 = ftok->next()->link(); tok2 && tok2 != ftok; tok2 = tok2->previous())
{
if (tok2->str() == "this")
{
for (Var *var = varlist; var; var = var->next)
var->init = true;
return;
}
}
if (ftok->str() == "if")
continue;
// No recursive calls! // No recursive calls!
if (std::find(callstack.begin(), callstack.end(), ftok->str()) == callstack.end()) if (std::find(callstack.begin(), callstack.end(), ftok->str()) == callstack.end())
{ {

View File

@ -63,6 +63,8 @@ private:
TEST_CASE(uninitVarHeader3); // Class is defined in header TEST_CASE(uninitVarHeader3); // Class is defined in header
TEST_CASE(uninitVarPublished); // Variables in the published section are auto-initialized TEST_CASE(uninitVarPublished); // Variables in the published section are auto-initialized
TEST_CASE(uninitOperator); // No FP about uninitialized 'operator[]' TEST_CASE(uninitOperator); // No FP about uninitialized 'operator[]'
TEST_CASE(uninitFunction1); // No FP when initialized in function
TEST_CASE(uninitFunction2); // No FP when initialized in function
TEST_CASE(noConstructor1); TEST_CASE(noConstructor1);
TEST_CASE(noConstructor2); TEST_CASE(noConstructor2);
@ -1425,6 +1427,36 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void uninitFunction1()
{
checkUninitVar("class Fred\n"
"{\n"
"public:\n"
" Fred() { init(*this); }\n"
"\n"
" static void init(Fred &f)\n"
" { f.d = 0; }\n"
"\n"
" double d;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void uninitFunction2()
{
checkUninitVar("class Fred\n"
"{\n"
"public:\n"
" Fred() { if (!init(*this)); }\n"
"\n"
" static bool init(Fred &f)\n"
" { f.d = 0; return true; }\n"
"\n"
" double d;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void checkNoConstructor(const char code[]) void checkNoConstructor(const char code[])
{ {
// Tokenize.. // Tokenize..