From 2b0e4926bc658964c6f0bd981be7db1df56cd18e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 14 Jun 2020 21:14:05 +0200 Subject: [PATCH] valueFlowAfterAssign: variable initialization --- lib/valueflow.cpp | 13 ++++++++++++- test/testvalueflow.cpp | 6 ++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 7885eb8b4..e4973b072 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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 diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 9fe544132..7f73eed12 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -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"