Fix #11139, #11817 FP unreadVariable (#5221)

This commit is contained in:
chrchr-github 2023-07-07 20:18:15 +02:00 committed by GitHub
parent e73183a182
commit d6d3c7b3fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View File

@ -364,6 +364,8 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const
const Result &result1 = checkRecursive(expr, tok->tokAt(2), tok->linkAt(1), exprVarIds, local, inInnerClass, depth);
if (result1.type == Result::Type::READ || result1.type == Result::Type::BAILOUT)
return result1;
if (mWhat == What::UnusedValue && result1.type == Result::Type::WRITE && expr->variable() && expr->variable()->isReference())
return result1;
if (mWhat == What::ValueFlow && result1.type == Result::Type::WRITE)
mValueFlowKnown = false;
if (mWhat == What::Reassign && result1.type == Result::Type::BREAK) {
@ -530,6 +532,10 @@ bool FwdAnalysis::possiblyAliased(const Token *expr, const Token *startToken) co
addrOf = tok->astOperand1();
else if (Token::simpleMatch(tok, "std :: ref ("))
addrOf = tok->tokAt(3)->astOperand2();
else if (tok->valueType() && tok->valueType()->pointer &&
(Token::Match(tok, "%var% = %var% ;") || Token::Match(tok, "%var% {|( %var% }|)")) &&
Token::Match(expr->previous(), "%varid% [", tok->tokAt(2)->varId()))
addrOf = tok->tokAt(2);
else
continue;

View File

@ -165,6 +165,8 @@ private:
TEST_CASE(localvaralias19); // ticket #9828
TEST_CASE(localvaralias20); // ticket #10966
TEST_CASE(localvaralias21);
TEST_CASE(localvaralias22);
TEST_CASE(localvaralias23);
TEST_CASE(localvarasm);
TEST_CASE(localvarstatic);
TEST_CASE(localvarextern);
@ -5037,6 +5039,36 @@ private:
ASSERT_EQUALS("", errout.str());
}
void localvaralias22() { // #11139
functionVariableUsage("int f() {\n"
" int x[1], *p = x;\n"
" x[0] = 42;\n"
" return *p;\n"
"}\n"
"int g() {\n"
" int x[1], *p{ x };\n"
" x[0] = 42;\n"
" return *p;\n"
"}\n"
"int h() {\n"
" int x[1], *p(x);\n"
" x[0] = 42;\n"
" return *p;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void localvaralias23() { // #11817
functionVariableUsage("void f(int& r, bool a, bool b) {\n"
" int& e = r;\n"
" if (a)\n"
" e = 42;\n"
" else if (b)\n"
" e = 1;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void localvarasm() {
functionVariableUsage("void foo(int &b)\n"