Assign values to pointers with C++11 init (#5055)

* Assign values to pointers with C++11 init

* Handle assigning empty init list
This commit is contained in:
chrchr-github 2023-05-13 14:09:47 +02:00 committed by GitHub
parent 2b74a2084e
commit dc7550ed9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View File

@ -1343,6 +1343,11 @@ 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() &&
(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);
}
return tok->next();
}
@ -5814,7 +5819,7 @@ static void valueFlowAfterAssign(TokenList *tokenlist,
if (tok->str() != "=" && !(isInit = isVariableInit(tok)))
continue;
if (tok->astParent() && !(tok->astParent()->str() == ";" && astIsLHS(tok)))
if (tok->astParent() && !((tok->astParent()->str() == ";" && astIsLHS(tok)) || tok->astParent()->str() == "*"))
continue;
// Lhs should be a variable

View File

@ -5054,6 +5054,46 @@ private:
ASSERT_EQUALS(true, value.isKnown());
ASSERT_EQUALS(1, value.intvalue);
code = "bool f() {\n"
" int* p{};\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* p{ nullptr };\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* 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* p = {};\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 = {};\n"
" return i == 0;\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, "&");