add support for using global qualified :: memset on class and class member initialization

This commit is contained in:
Robert Reif 2011-08-04 20:37:27 -04:00
parent 2516aad31d
commit ac6d67dc4d
2 changed files with 43 additions and 5 deletions

View File

@ -331,7 +331,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<std::string>
ftok = ftok->tokAt(2);
}
if (!Token::Match(ftok->next(), "%var%") &&
if (!Token::Match(ftok->next(), "::| %var%") &&
!Token::Match(ftok->next(), "this . %var%") &&
!Token::Match(ftok->next(), "* %var% =") &&
!Token::Match(ftok->next(), "( * this ) . %var%"))
@ -355,17 +355,18 @@ void CheckClass::initializeVarList(const Function &func, std::list<std::string>
ftok = ftok->tokAt(2);
// Clearing all variables..
if (Token::simpleMatch(ftok, "memset ( this ,"))
if (Token::Match(ftok, "::| memset ( this ,"))
{
assignAllVar(usage);
return;
}
// Clearing array..
else if (Token::Match(ftok, "memset ( %var% ,"))
else if (Token::Match(ftok, "::| memset ( %var% ,"))
{
assignVar(ftok->strAt(2), scope, usage);
ftok = ftok->next()->link();
const int offset = ftok->str() == "::" ? 1 : 0;
assignVar(ftok->strAt(2 + offset), scope, usage);
ftok = ftok->tokAt(1 + offset)->link();
continue;
}

View File

@ -76,6 +76,7 @@ private:
TEST_CASE(uninitVarArray4);
TEST_CASE(uninitVarArray5);
TEST_CASE(uninitVarArray6);
TEST_CASE(uninitVarArray7);
TEST_CASE(uninitVarArray2D);
TEST_CASE(uninitVarStruct1); // ticket #2172
TEST_CASE(uninitVarStruct2); // ticket #838
@ -2395,6 +2396,33 @@ private:
ASSERT_EQUALS("", errout.str());
}
void uninitVarArray7()
{
checkUninitVar("class Foo\n"
"{\n"
" int array[10];\n"
"public:\n"
" Foo() { }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Foo::array' is not initialized in the constructor.\n", errout.str());
checkUninitVar("class Foo\n"
"{\n"
" int array[10];\n"
"public:\n"
" Foo() { memset(array, 0, sizeof(array)); }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("class Foo\n"
"{\n"
" int array[10];\n"
"public:\n"
" Foo() { ::memset(array, 0, sizeof(array)); }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void uninitVarArray2D()
{
checkUninitVar("class John\n"
@ -2616,6 +2644,15 @@ private:
"};\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("class Foo\n"
"{\n"
"public:\n"
" int * pointer;\n"
" Foo() { ::memset(this, 0, sizeof(*this)); }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void privateCtor1()