Handle alias variables in CheckClass::initializeVarList() (#6921)

This commit is contained in:
PKEuS 2015-11-09 21:02:06 +01:00
parent aca8a69f5e
commit db342ea910
2 changed files with 58 additions and 0 deletions

View File

@ -730,6 +730,14 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
// Assignment of member variable? // Assignment of member variable?
else if (Token::Match(ftok, "%name% =")) { else if (Token::Match(ftok, "%name% =")) {
assignVar(ftok->str(), scope, usage); assignVar(ftok->str(), scope, usage);
bool bailout = ftok->variable() && ftok->variable()->isReference();
const Token* tok2 = ftok->tokAt(2);
if (tok2->str() == "&") {
tok2 = tok2->next();
bailout = true;
}
if (tok2->variable() && (bailout || tok2->variable()->isArray()) && tok2->strAt(1) != "[")
assignVar(tok2->str(), scope, usage);
} }
// Assignment of array item of member variable? // Assignment of array item of member variable?

View File

@ -104,6 +104,8 @@ private:
TEST_CASE(initvar_destructor); // No variables need to be initialized in a destructor TEST_CASE(initvar_destructor); // No variables need to be initialized in a destructor
TEST_CASE(initvar_func_ret_func_ptr); // ticket #4449 TEST_CASE(initvar_func_ret_func_ptr); // ticket #4449
TEST_CASE(initvar_alias); // #6921
TEST_CASE(operatorEqSTL); TEST_CASE(operatorEqSTL);
TEST_CASE(uninitVar1); TEST_CASE(uninitVar1);
@ -1389,6 +1391,54 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void initvar_alias() { // #6921
check("struct S {\n"
" int a;\n"
" S() {\n"
" int& pa = a;\n"
" pa = 4;\n"
" }\n"
"};");
ASSERT_EQUALS("", errout.str());
check("struct S {\n"
" int a;\n"
" S() {\n"
" int* pa = &a;\n"
" *pa = 4;\n"
" }\n"
"};");
ASSERT_EQUALS("", errout.str());
check("struct S {\n"
" int a[2];\n"
" S() {\n"
" int* pa = a;\n"
" for (int i = 0; i < 2; i++)\n"
" *pa++ = i;\n"
" }\n"
"};");
ASSERT_EQUALS("", errout.str());
check("struct S {\n"
" int* a[2];\n"
" S() {\n"
" int* pa = a[1];\n"
" *pa = 0;\n"
" }\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'S::a' is not initialized in the constructor.\n", errout.str());
check("struct S {\n"
" int a;\n"
" S() {\n"
" int pa = a;\n"
" pa = 4;\n"
" }\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'S::a' is not initialized in the constructor.\n", errout.str());
}
void operatorEqSTL() { void operatorEqSTL() {
check("class Fred\n" check("class Fred\n"
"{\n" "{\n"