Skip to content

Commit ef1f3fb

Browse files
committed
Fixed danmar#8173 (ValueFlow: use AST when setting values in assignment RHS ((n=42) && n=='A'))
1 parent 927b14b commit ef1f3fb

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

lib/valueflow.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,16 +1819,24 @@ static bool valueFlowForward(Token * const startToken,
18191819
// bailout: assignment
18201820
else if (Token::Match(tok2->previous(), "!!* %name% %assign%")) {
18211821
// simplify rhs
1822-
for (Token *tok3 = tok2->tokAt(2); tok3; tok3 = tok3->next()) {
1823-
if (tok3->varId() == varid) {
1822+
std::stack<Token *> rhs;
1823+
rhs.push(const_cast<Token *>(tok2->next()->astOperand2()));
1824+
while (!rhs.empty()) {
1825+
Token *rtok = rhs.top();
1826+
rhs.pop();
1827+
if (!rtok)
1828+
continue;
1829+
if (rtok->str() == "(" && Token::Match(rtok->astOperand1(), "sizeof|typeof|typeid"))
1830+
continue;
1831+
if (Token::Match(rtok, "++|--|?|:|;|,"))
1832+
continue;
1833+
if (rtok->varId() == varid) {
18241834
std::list<ValueFlow::Value>::const_iterator it;
18251835
for (it = values.begin(); it != values.end(); ++it)
1826-
setTokenValue(tok3, *it, settings);
1827-
} else if (Token::Match(tok3, "++|--|?|:|;|,"))
1828-
break;
1829-
// Skip sizeof etc
1830-
else if (Token::Match(tok3, "sizeof|typeof|typeid ("))
1831-
tok3 = tok3->linkAt(1);
1836+
setTokenValue(rtok, *it, settings);
1837+
}
1838+
rhs.push(const_cast<Token *>(rtok->astOperand1()));
1839+
rhs.push(const_cast<Token *>(rtok->astOperand2()));
18321840
}
18331841
if (settings->debugwarnings)
18341842
bailout(tokenlist, errorLogger, tok2, "assignment of " + tok2->str());

test/testvalueflow.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,6 +2646,14 @@ class TestValueFlow : public TestFixture {
26462646
"}";
26472647
values = tokenValues(code, "x ; }");
26482648
ASSERT_EQUALS(true, values.empty());
2649+
2650+
// return (#8173)
2651+
code = "int repeat() {\n"
2652+
" const char *n;\n"
2653+
" return((n=42) && *n == 'A');\n"
2654+
"}";
2655+
values = tokenValues(code, "n ==");
2656+
ASSERT_EQUALS(true, values.size() != 1U || !values.front().isUninitValue());
26492657
}
26502658
};
26512659

0 commit comments

Comments
 (0)