Set values when assigning init list (#5057)

* Assign values to pointers with C++11 init

* Handle assigning empty init list

* Fix #10596 FN uninitdata with value initialization

* Fix test

* Set values when assigning init list
This commit is contained in:
chrchr-github 2023-05-14 17:01:07 +02:00 committed by GitHub
parent d475591665
commit 1999bc68bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 4 deletions

View File

@ -1343,11 +1343,17 @@ static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, b
if (!tok->isTemplateArg())
value.setKnown();
setTokenValue(tok->next(), std::move(value), settings, isInitList);
} else if (Token::Match(tok, "%name% = { }") && tok->variable() &&
} else if (Token::Match(tok, "%name% = {") && tok->variable() &&
(tok->variable()->isPointer() || (tok->variable()->valueType() && tok->variable()->valueType()->isIntegral()))) {
ValueFlow::Value value(0);
value.setKnown();
setTokenValue(tok->tokAt(2), std::move(value), settings, isInitList);
if (Token::simpleMatch(tok->tokAt(3), "}")) {
ValueFlow::Value value(0);
value.setKnown();
setTokenValue(tok->tokAt(2), std::move(value), settings, isInitList);
} else if (tok->tokAt(2)->astOperand1() && tok->tokAt(2)->astOperand1()->hasKnownIntValue()) {
ValueFlow::Value value(tok->tokAt(2)->astOperand1()->getKnownIntValue());
value.setKnown();
setTokenValue(tok->tokAt(2), std::move(value), settings, isInitList);
}
}
return tok->next();
}

View File

@ -5094,6 +5094,22 @@ private:
ASSERT_EQUALS(true, value.isKnown());
ASSERT_EQUALS(1, value.intvalue);
code = "bool f() {\n"
" int* p = { 0 };\n"
" return p == nullptr;\n" // <- known value
"}";
value = valueOfTok(code, "==");
ASSERT_EQUALS(true, value.isKnown());
ASSERT_EQUALS(1, value.intvalue);
code = "bool f() {\n"
" int i = { 1 };\n"
" return i == 1;\n" // <- known value
"}";
value = valueOfTok(code, "==");
ASSERT_EQUALS(true, value.isKnown());
ASSERT_EQUALS(1, value.intvalue);
// calculation with known result
code = "int f(int x) { a = x & 0; }"; // <- & is 0
value = valueOfTok(code, "&");