10110: Fix FP knownConditionTrueFalse (#3053)

This commit is contained in:
Ken-Patrick Lehrmann 2021-01-25 17:23:47 +01:00 committed by GitHub
parent ff9d6499ec
commit 00707455be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 188 additions and 0 deletions

View File

@ -1119,6 +1119,73 @@ static bool isZeroBoundCond(const Token * const cond)
return false; return false;
} }
static bool isForLoopCondition(const Token * const tok)
{
if (!tok)
return false;
const Token *const parent = tok->astParent();
return Token::simpleMatch(parent, ";") && parent->astOperand1() == tok &&
Token::simpleMatch(parent->astParent(), ";") &&
Token::simpleMatch(parent->astParent()->astParent(), "(") &&
parent->astParent()->astParent()->astOperand1()->str() == "for";
}
/**
* Is token used a boolean (cast to a bool, or used as a condition somewhere)
* @param tok
* @param checkingParent true if we are checking a parent. This is used to know
* what we are checking. For instance in `if (i == 2)`, isUsedAsBool("==") is
* true whereas isUsedAsBool("i") is false, but it might call
* isUsedAsBool_internal("==") which must not return true
*/
static bool isUsedAsBool_internal(const Token * const tok, bool checkingParent)
{
if (!tok)
return false;
const Token::Type type = tok->tokType();
if (type == Token::eBitOp || type == Token::eIncDecOp || (type == Token::eArithmeticalOp && !tok->isUnaryOp("*")))
// those operators don't return a bool
return false;
if (type == Token::eComparisonOp) {
if (!checkingParent)
// this operator returns a bool
return true;
if (Token::Match(tok, "==|!="))
return astIsBool(tok->astOperand1()) || astIsBool(tok->astOperand2());
return false;
}
if (type == Token::eLogicalOp)
return true;
if (astIsBool(tok))
return true;
const Token * const parent = tok->astParent();
if (!parent)
return false;
if (parent->str() == "(" && parent->astOperand2() == tok) {
if (Token::Match(parent->astOperand1(), "if|while"))
return true;
if (!parent->isCast()) { // casts are handled via the recursive call, as astIsBool will be true
// is it a call to a function ?
int argnr;
const Token *const func = getTokenArgumentFunction(tok, argnr);
if (!func || !func->function())
return false;
const Variable *var = func->function()->getArgumentVar(argnr);
return var && (var->getTypeName() == "bool");
}
} else if (isForLoopCondition(tok))
return true;
return isUsedAsBool_internal(parent, true);
}
bool isUsedAsBool(const Token * const tok)
{
return isUsedAsBool_internal(tok, false);
}
bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token * const cond2, const Library& library, bool pure, bool followVar, ErrorPath* errors) bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token * const cond2, const Library& library, bool pure, bool followVar, ErrorPath* errors)
{ {
if (!cond1 || !cond2) if (!cond1 || !cond2)
@ -1146,6 +1213,8 @@ bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token
if (cond2->astOperand2() && cond2->astOperand2()->str() == "0") if (cond2->astOperand2() && cond2->astOperand2()->str() == "0")
return isSameExpression(cpp, true, cond1->astOperand1(), cond2->astOperand1(), library, pure, followVar, errors); return isSameExpression(cpp, true, cond1->astOperand1(), cond2->astOperand1(), library, pure, followVar, errors);
} }
if (!isUsedAsBool(cond2))
return false;
return isSameExpression(cpp, true, cond1->astOperand1(), cond2, library, pure, followVar, errors); return isSameExpression(cpp, true, cond1->astOperand1(), cond2, library, pure, followVar, errors);
} }

View File

@ -146,6 +146,11 @@ bool isEqualKnownValue(const Token * const tok1, const Token * const tok2);
bool isDifferentKnownValues(const Token * const tok1, const Token * const tok2); bool isDifferentKnownValues(const Token * const tok1, const Token * const tok2);
/**
* Is token used a boolean, that is to say cast to a bool, or used as a condition in a if/while/for
*/
bool isUsedAsBool(const Token * const tok);
/** /**
* Are two conditions opposite * Are two conditions opposite
* @param isNot do you want to know if cond1 is !cond2 or if cond1 and cond2 are non-overlapping. true: cond1==!cond2 false: cond1==true => cond2==false * @param isNot do you want to know if cond1 is !cond2 or if cond1 and cond2 are non-overlapping. true: cond1==!cond2 false: cond1==true => cond2==false

View File

@ -42,6 +42,7 @@ private:
TEST_CASE(isVariableChanged); TEST_CASE(isVariableChanged);
TEST_CASE(isVariableChangedByFunctionCall); TEST_CASE(isVariableChangedByFunctionCall);
TEST_CASE(nextAfterAstRightmostLeaf); TEST_CASE(nextAfterAstRightmostLeaf);
TEST_CASE(isUsedAsBool);
} }
bool findLambdaEndToken(const char code[]) { bool findLambdaEndToken(const char code[]) {
@ -265,6 +266,52 @@ private:
ASSERT_EQUALS(true, nextAfterAstRightmostLeaf("int * g(int); void f(int a, int b) { int x = g(a)[b + 1]; }", "+", "] ; }")); ASSERT_EQUALS(true, nextAfterAstRightmostLeaf("int * g(int); void f(int a, int b) { int x = g(a)[b + 1]; }", "+", "] ; }"));
ASSERT_EQUALS(true, nextAfterAstRightmostLeaf("int * g(int); void f(int a, int b) { int x = g(a + 1)[b]; }", "+", ") [")); ASSERT_EQUALS(true, nextAfterAstRightmostLeaf("int * g(int); void f(int a, int b) { int x = g(a + 1)[b]; }", "+", ") ["));
} }
enum class Result {False, True, Fail};
Result isUsedAsBool(const char code[], const char pattern[]) {
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
if (!tokenizer.tokenize(istr, "test.cpp"))
return Result::Fail;
const Token * const argtok = Token::findmatch(tokenizer.tokens(), pattern);
if (!argtok)
return Result::Fail;
return ::isUsedAsBool(argtok) ? Result::True : Result::False;
}
void isUsedAsBool() {
ASSERT(Result::True == isUsedAsBool("void f() { bool b = true; }", "b"));
ASSERT(Result::False ==isUsedAsBool("void f() { int i = true; }", "i"));
ASSERT(Result::True == isUsedAsBool("void f() { int i; if (i) {} }", "i )"));
ASSERT(Result::True == isUsedAsBool("void f() { int i; while (i) {} }", "i )"));
ASSERT(Result::True == isUsedAsBool("void f() { int i; for (;i;) {} }", "i ; )"));
ASSERT(Result::False == isUsedAsBool("void f() { int i; for (;;i) {} }", "i )"));
ASSERT(Result::False == isUsedAsBool("void f() { int i; for (i;;) {} }", "i ; ; )"));
ASSERT(Result::True == isUsedAsBool("void f() { int i; for (int j=0; i; ++j) {} }", "i ; ++"));
ASSERT(Result::False == isUsedAsBool("void f() { int i; if (i == 2) {} }", "i =="));
ASSERT(Result::True == isUsedAsBool("void f() { int i; if (i == true) {} }", "i =="));
ASSERT(Result::True == isUsedAsBool("void f() { int i,j; if (i == (j&&f())) {} }", "i =="));
ASSERT(Result::True == isUsedAsBool("void f() { int i; if (!i == 0) {} }", "i =="));
ASSERT(Result::True == isUsedAsBool("void f() { int i; if (!i) {} }", "i )"));
ASSERT(Result::True == isUsedAsBool("void f() { int i; if (!!i) {} }", "i )"));
ASSERT(Result::True == isUsedAsBool("void f() { int i; if (i && f()) {} }", "i &&"));
ASSERT(Result::True == isUsedAsBool("void f() { int i; int j = i && f(); }", "i &&"));
ASSERT(Result::False == isUsedAsBool("void f() { int i; if (i & f()) {} }", "i &"));
ASSERT(Result::True == isUsedAsBool("void f() { int i; if (static_cast<bool>(i)) {} }", "i )"));
ASSERT(Result::True == isUsedAsBool("void f() { int i; if ((bool)i) {} }", "i )"));
ASSERT(Result::True == isUsedAsBool("void f() { int i; if (1+static_cast<bool>(i)) {} }", "i )"));
ASSERT(Result::True == isUsedAsBool("void f() { int i; if (1+(bool)i) {} }", "i )"));
ASSERT(Result::False == isUsedAsBool("void f() { int i; if (1+static_cast<int>(i)) {} }", "i )"));
ASSERT(Result::True == isUsedAsBool("void f() { int i; if (1+!static_cast<int>(i)) {} }", "i )"));
ASSERT(Result::False == isUsedAsBool("void f() { int i; if (1+(int)i) {} }", "i )"));
ASSERT(Result::False == isUsedAsBool("void f() { int i; if (i + 2) {} }", "i +"));
ASSERT(Result::True == isUsedAsBool("void f() { int i; bool b = i; }", "i ; }"));
ASSERT(Result::True == isUsedAsBool("void f(bool b); void f() { int i; f(i); }","i )"));
ASSERT(Result::True == isUsedAsBool("void f() { int *i; if (*i) {} }", "i )"));
ASSERT(Result::True == isUsedAsBool("void f() { int *i; if (*i) {} }", "* i )"));
}
}; };
REGISTER_TEST(TestAstUtils) REGISTER_TEST(TestAstUtils)

View File

@ -4359,6 +4359,73 @@ private:
"}\n"; "}\n";
ASSERT_EQUALS(true, valueOfTok(code, "> 0").isKnown()); ASSERT_EQUALS(true, valueOfTok(code, "> 0").isKnown());
ASSERT_EQUALS(true, valueOfTok(code, "> 0").intvalue == 1); ASSERT_EQUALS(true, valueOfTok(code, "> 0").intvalue == 1);
// FP #10110
code = "enum FENUMS { NONE = 0, CB = 8 };\n"
"bool calc(int x) {\n"
" if (!x) {\n"
" return false;\n"
" }\n"
"\n"
" if (x & CB) {\n"
" return true;\n"
" }\n"
" return false;\n"
"}\n";
ASSERT_EQUALS(false, valueOfTok(code, "& CB").isKnown());
ASSERT_EQUALS(true, testValueOfXImpossible(code, 7U, 0));
code = "enum FENUMS { NONE = 0, CB = 8 };\n"
"bool calc(int x) {\n"
" if (x) {\n"
" return false;\n"
" }\n"
"\n"
" if ((!x) & CB) {\n"
" return true;\n"
" }\n"
" return false;\n"
"}\n";
ASSERT_EQUALS(true, valueOfTok(code, "& CB").isKnown());
ASSERT_EQUALS(true, testValueOfXKnown(code, 7U, 0));
code = "enum FENUMS { NONE = 0, CB = 8 };\n"
"bool calc(int x) {\n"
" if (!!x) {\n"
" return false;\n"
" }\n"
"\n"
" if (x & CB) {\n"
" return true;\n"
" }\n"
" return false;\n"
"}\n";
ASSERT_EQUALS(true, valueOfTok(code, "& CB").isKnown());
ASSERT_EQUALS(true, testValueOfXKnown(code, 7U, 0));
code = "bool calc(bool x) {\n"
" if (!x) {\n"
" return false;\n"
" }\n"
"\n"
" if (x) {\n"
" return true;\n"
" }\n"
" return false;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 6U, 1));
code = "bool calc(bool x) {\n"
" if (x) {\n"
" return false;\n"
" }\n"
"\n"
" if (!x) {\n"
" return true;\n"
" }\n"
" return false;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 6U, 0));
} }
static std::string isPossibleContainerSizeValue(const std::list<ValueFlow::Value> &values, MathLib::bigint i) { static std::string isPossibleContainerSizeValue(const std::list<ValueFlow::Value> &values, MathLib::bigint i) {