Fixed #4320 (False positives 'unassignedVariable' and 'uninitvar')
This commit is contained in:
parent
1e2fb4f1a4
commit
3913fd8398
|
@ -574,7 +574,8 @@ private:
|
||||||
return tok.tokAt(3);
|
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
|
// TODO: Don't bail out for "<<" and ">>" if these are
|
||||||
// just computations
|
// just computations
|
||||||
ExecutionPath::bailOutVar(checks, tok.varId());
|
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(), "<<|>>")) {
|
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,
|
// Is variable a known POD type then this is a variable usage,
|
||||||
// otherwise we assume it's not.
|
// otherwise we assume it's not.
|
||||||
const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(vartok->varId());
|
const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(vartok->varId());
|
||||||
if (var && var->typeStartToken()->isStandardType())
|
return (var && var->typeStartToken()->isStandardType());
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Token::Match(vartok->next(), "++|--|%op%"))
|
if (Token::Match(vartok->next(), "++|--|%op%"))
|
||||||
|
|
|
@ -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%")) {
|
else if (Token::Match(tok, "& %var%")) {
|
||||||
if (tok->previous()->isName() || tok->previous()->isNumber()) { // bitop
|
if (tok->previous()->isName() || tok->previous()->isNumber()) { // bitop
|
||||||
variables.read(tok->next()->varId(), tok);
|
variables.read(tok->next()->varId(), tok);
|
||||||
|
|
|
@ -377,6 +377,14 @@ private:
|
||||||
"}\n",
|
"}\n",
|
||||||
"test.c");
|
"test.c");
|
||||||
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout.str());
|
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
|
checkUninitVar("void a() {\n" // asm
|
||||||
|
@ -2341,6 +2349,13 @@ private:
|
||||||
" char a = c << 2;\n"
|
" char a = c << 2;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: c\n", errout.str());
|
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.
|
// Handling of unknown types. Assume they are POD in C.
|
||||||
|
|
|
@ -3005,6 +3005,13 @@ private:
|
||||||
" ints << 1;\n"
|
" ints << 1;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
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() {
|
void localvarCast() {
|
||||||
|
|
Loading…
Reference in New Issue