Uninitialized variables: Fixed false positive when using ?:

This commit is contained in:
Daniel Marjamäki 2013-11-17 12:42:53 +01:00
parent 77e09d72a1
commit 71e61fb1ed
2 changed files with 25 additions and 1 deletions

View File

@ -1649,7 +1649,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool cpp
} }
} }
if (Token::Match(vartok->previous(), "++|--|?|:|%cop%")) { if (Token::Match(vartok->previous(), "++|--|%cop%")) {
if (cpp && vartok->previous()->str() == ">>") { if (cpp && vartok->previous()->str() == ">>") {
// assume that variable is initialized // assume that variable is initialized
return false; return false;
@ -1688,6 +1688,23 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool cpp
if (Token::Match(vartok->previous(), "= %var% ;|%cop%")) if (Token::Match(vartok->previous(), "= %var% ;|%cop%"))
return true; return true;
if (Token::Match(vartok->previous(), "? %var%")) {
// this is only variable usage if variable is either:
// * unconditionally uninitialized
// * used in both rhs and lhs of ':' operator
bool rhs = false;
for (const Token *tok2 = vartok; tok2; tok2 = tok2->next()) {
if (tok2->str() == "(")
tok2 = tok2->link();
else if (tok2->str() == ":")
rhs = true;
else if (Token::Match(tok2, "[)];,{}]"))
break;
else if (rhs && tok2->varId() == vartok->varId())
return true;
}
}
bool unknown = false; bool unknown = false;
if (pointer && CheckNullPointer::isPointerDeRef(vartok, unknown)) { if (pointer && CheckNullPointer::isPointerDeRef(vartok, unknown)) {
// function parameter? // function parameter?

View File

@ -2143,6 +2143,13 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar2("int f(int a) {\n"
" int x;\n"
" if (a==3) { x=2; }\n"
" y = (a==3) ? x : a;\n"
"}");
ASSERT_EQUALS("", errout.str());
// = { .. } // = { .. }
checkUninitVar2("int f() {\n" checkUninitVar2("int f() {\n"
" int a;\n" " int a;\n"