valueFlowAfterAssign: variable initialization

This commit is contained in:
Daniel Marjamäki 2020-06-14 21:14:05 +02:00
parent dc5e93a103
commit 2b0e4926bc
2 changed files with 18 additions and 1 deletions

View File

@ -3906,6 +3906,17 @@ static bool isLiteralNumber(const Token *tok, bool cpp)
return tok->isNumber() || tok->isEnumerator() || tok->str() == "NULL" || (cpp && Token::Match(tok, "false|true|nullptr"));
}
static bool isVariableInit(const Token *tok)
{
return tok->str() == "(" &&
tok->isBinaryOp() &&
tok->astOperand1()->variable() &&
tok->astOperand1()->variable()->nameToken() == tok->astOperand1() &&
tok->astOperand1()->variable()->valueType() &&
tok->astOperand1()->variable()->valueType()->type >= ValueType::Type::VOID &&
!Token::simpleMatch(tok->astOperand2(), ",");
}
static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldatabase, ErrorLogger *errorLogger, const Settings *settings)
{
for (const Scope * scope : symboldatabase->functionScopes) {
@ -3918,7 +3929,7 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat
}
// Assignment
if ((tok->str() != "=") || (tok->astParent()))
if ((tok->str() != "=" && !isVariableInit(tok)) || (tok->astParent()))
continue;
// Lhs should be a variable

View File

@ -1505,6 +1505,12 @@ private:
"}";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 123));
code = "void f() {\n"
" const int x(321);\n"
" a = x;\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 321));
code = "void f() {\n"
" int x = 9;\n"
" --x;\n"