Fix #9549 FP knownConditionTrueFalse (bitshift) (#3616)

This commit is contained in:
chrchr-github 2021-12-14 07:30:57 +01:00 committed by GitHub
parent cb2738a60c
commit 532477cdb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -371,9 +371,15 @@ static const Token *getCastTypeStartToken(const Token *parent)
return nullptr;
}
// does the operation cause a loss of information?
static bool isNonInvertibleOperation(const Token* tok)
{
return tok->isComparisonOp() || Token::Match(tok, "%|/|&|%or%|<<|>>");
}
static bool isComputableValue(const Token* parent, const ValueFlow::Value& value)
{
const bool noninvertible = parent->isComparisonOp() || Token::Match(parent, "%|/|&|%or%");
const bool noninvertible = isNonInvertibleOperation(parent);
if (noninvertible && value.isImpossible())
return false;
if (!value.isIntValue() && !value.isFloatValue() && !value.isTokValue() && !value.isIteratorValue())
@ -669,7 +675,7 @@ static void setTokenValue(Token* tok, ValueFlow::Value value, const Settings* se
parent->astOperand1() &&
parent->astOperand2()) {
const bool noninvertible = parent->isComparisonOp() || Token::Match(parent, "%|/|&|%or%");
const bool noninvertible = isNonInvertibleOperation(parent);
// Skip operators with impossible values that are not invertible
if (noninvertible && value.isImpossible())

View File

@ -3927,6 +3927,16 @@ private:
" if (pD) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #9549
check("void f(const uint32_t v) {\n"
" const uint32_t v16 = v >> 16;\n"
" if (v16) {\n"
" const uint32_t v8 = v16 >> 8;\n"
" if (v8) {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void alwaysTrueInfer() {