Fixed #6214: non-static member initializer causes false positive

This commit is contained in:
Frank Zingsheim 2014-10-16 09:11:09 +02:00 committed by PKEuS
parent 96e8b83bf8
commit 9dd3bce98b
3 changed files with 40 additions and 3 deletions

View File

@ -1136,6 +1136,17 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const
return false;
}
const Token * Variable::declEndToken() const
{
Token const * declEnd = typeStartToken();
while (declEnd && !Token::Match(declEnd, "[;,)={]")) {
if (declEnd->link() && Token::Match(declEnd,"(|["))
declEnd = declEnd->link();
declEnd = declEnd->next();
}
return declEnd;
}
void Variable::evaluate()
{
const Token* tok = _start;
@ -1204,8 +1215,9 @@ void Variable::evaluate()
// type var = x or
// type var = {x}
// type var = x; gets simplified to: type var ; var = x ;
if ((Token::Match(_name, "%var% ; %var% = ") && _name->strAt(2) == _name->str()) ||
Token::Match(_name, "%var% {"))
Token const * declEnd = declEndToken();
if ((Token::Match(declEnd, "; %var% = ") && declEnd->strAt(1) == _name->str()) ||
Token::Match(declEnd, "=|{"))
setFlag(fHasDefault, true);
}

View File

@ -215,6 +215,15 @@ public:
return _end;
}
/**
* Get end token of variable declaration
* E.g.
* int i[2][3] = ...
* end token ^
* @return variable declaration end token
*/
const Token *declEndToken() const;
/**
* Get name string.
* @return name string

View File

@ -385,8 +385,24 @@ private:
" int x = 0;\n"
" int y = f();\n"
" int z{0};\n"
" int (*pf[2])(){nullptr, nullptr};\n"
" int a[2][3] = {{1,2,3},{4,5,6}};\n"
" int d, e{3};\n"
"};");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::d' is not initialized in the constructor.\n", errout.str());
check("class Fred {\n"
"public:\n"
" Fred() {}\n"
"private:\n"
" int b{1}, c{2};\n"
" int d, e{3};\n"
" int f{4}, g;\n"
"};");
TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::d' is not initialized in the constructor.\n"
"[test.cpp:3]: (warning) Member variable 'Fred::g' is not initialized in the constructor.\n",
"[test.cpp:3]: (warning) Member variable 'Fred::d' is not initialized in the constructor.\n",
errout.str()); // fails due to missing varid
}
void simple12() { // ticket #4620