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:
parent
2b74a2084e
commit
dc7550ed9f
|
@ -1343,6 +1343,11 @@ static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, b
|
||||||
if (!tok->isTemplateArg())
|
if (!tok->isTemplateArg())
|
||||||
value.setKnown();
|
value.setKnown();
|
||||||
setTokenValue(tok->next(), std::move(value), settings, isInitList);
|
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();
|
return tok->next();
|
||||||
}
|
}
|
||||||
|
@ -5814,7 +5819,7 @@ static void valueFlowAfterAssign(TokenList *tokenlist,
|
||||||
if (tok->str() != "=" && !(isInit = isVariableInit(tok)))
|
if (tok->str() != "=" && !(isInit = isVariableInit(tok)))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (tok->astParent() && !(tok->astParent()->str() == ";" && astIsLHS(tok)))
|
if (tok->astParent() && !((tok->astParent()->str() == ";" && astIsLHS(tok)) || tok->astParent()->str() == "*"))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Lhs should be a variable
|
// Lhs should be a variable
|
||||||
|
|
|
@ -5054,6 +5054,46 @@ private:
|
||||||
ASSERT_EQUALS(true, value.isKnown());
|
ASSERT_EQUALS(true, value.isKnown());
|
||||||
ASSERT_EQUALS(1, value.intvalue);
|
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
|
// calculation with known result
|
||||||
code = "int f(int x) { a = x & 0; }"; // <- & is 0
|
code = "int f(int x) { a = x & 0; }"; // <- & is 0
|
||||||
value = valueOfTok(code, "&");
|
value = valueOfTok(code, "&");
|
||||||
|
|
Loading…
Reference in New Issue