Fixed #4320 (False positives 'unassignedVariable' and 'uninitvar')

This commit is contained in:
Daniel Marjamäki 2012-12-18 19:02:30 +01:00
parent 1e2fb4f1a4
commit 3913fd8398
4 changed files with 36 additions and 4 deletions

View File

@ -574,7 +574,8 @@ private:
return tok.tokAt(3);
}
else if ((!isC && Token::Match(tok.previous(), "<<|>>")) || Token::simpleMatch(tok.next(), "=")) {
else if ((!isC && (Token::Match(tok.previous(), "<<|>>") || Token::Match(tok.previous(), "[;{}] %var% <<"))) ||
Token::simpleMatch(tok.next(), "=")) {
// TODO: Don't bail out for "<<" and ">>" if these are
// just computations
ExecutionPath::bailOutVar(checks, tok.varId());
@ -1328,12 +1329,17 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer) const
}
if (_tokenizer->isCPP() && Token::Match(vartok->next(), "<<|>>")) {
// Is this calculation done in rhs?
const Token *tok = vartok;
while (tok && Token::Match(tok, "%var%|.|::"))
tok = tok->previous();
if (Token::Match(tok, "[;{}]"))
return false;
// Is variable a known POD type then this is a variable usage,
// otherwise we assume it's not.
const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(vartok->varId());
if (var && var->typeStartToken()->isStandardType())
return true;
return false;
return (var && var->typeStartToken()->isStandardType());
}
if (Token::Match(vartok->next(), "++|--|%op%"))

View File

@ -954,6 +954,10 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
}
}
else if (_tokenizer->isCPP() && Token::Match(tok, "[;{}] %var% <<")) {
variables.erase(tok->next()->varId());
}
else if (Token::Match(tok, "& %var%")) {
if (tok->previous()->isName() || tok->previous()->isNumber()) { // bitop
variables.read(tok->next()->varId(), tok);

View File

@ -377,6 +377,14 @@ private:
"}\n",
"test.c");
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout.str());
// #4320
checkUninitVar("void f() {\n"
" int a;\n"
" a << 1;\n"
" return a;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
checkUninitVar("void a() {\n" // asm
@ -2341,6 +2349,13 @@ private:
" char a = c << 2;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: c\n", errout.str());
// #4320
checkUninitVar2("void f() {\n"
" int a;\n"
" a << 1;\n" // there might be a operator<<
"}\n");
ASSERT_EQUALS("", errout.str());
}
// Handling of unknown types. Assume they are POD in C.

View File

@ -3005,6 +3005,13 @@ private:
" ints << 1;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("int foo() {\n" // #4320
" int x;\n"
" x << 1;\n"
" return x;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void localvarCast() {