Skip to content

Commit 2b0e492

Browse files
committed
valueFlowAfterAssign: variable initialization
1 parent dc5e93a commit 2b0e492

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

lib/valueflow.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3906,6 +3906,17 @@ static bool isLiteralNumber(const Token *tok, bool cpp)
39063906
return tok->isNumber() || tok->isEnumerator() || tok->str() == "NULL" || (cpp && Token::Match(tok, "false|true|nullptr"));
39073907
}
39083908

3909+
static bool isVariableInit(const Token *tok)
3910+
{
3911+
return tok->str() == "(" &&
3912+
tok->isBinaryOp() &&
3913+
tok->astOperand1()->variable() &&
3914+
tok->astOperand1()->variable()->nameToken() == tok->astOperand1() &&
3915+
tok->astOperand1()->variable()->valueType() &&
3916+
tok->astOperand1()->variable()->valueType()->type >= ValueType::Type::VOID &&
3917+
!Token::simpleMatch(tok->astOperand2(), ",");
3918+
}
3919+
39093920
static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldatabase, ErrorLogger *errorLogger, const Settings *settings)
39103921
{
39113922
for (const Scope * scope : symboldatabase->functionScopes) {
@@ -3918,7 +3929,7 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat
39183929
}
39193930

39203931
// Assignment
3921-
if ((tok->str() != "=") || (tok->astParent()))
3932+
if ((tok->str() != "=" && !isVariableInit(tok)) || (tok->astParent()))
39223933
continue;
39233934

39243935
// Lhs should be a variable

test/testvalueflow.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,12 @@ class TestValueFlow : public TestFixture {
15051505
"}";
15061506
ASSERT_EQUALS(true, testValueOfX(code, 3U, 123));
15071507

1508+
code = "void f() {\n"
1509+
" const int x(321);\n"
1510+
" a = x;\n"
1511+
"}";
1512+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 321));
1513+
15081514
code = "void f() {\n"
15091515
" int x = 9;\n"
15101516
" --x;\n"

0 commit comments

Comments
 (0)