diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 33c08d2f5..e2bab8656 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1119,6 +1119,73 @@ static bool isZeroBoundCond(const Token * const cond) 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) { 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") 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); } diff --git a/lib/astutils.h b/lib/astutils.h index 828d2296d..e80e7cf78 100644 --- a/lib/astutils.h +++ b/lib/astutils.h @@ -146,6 +146,11 @@ bool isEqualKnownValue(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 * @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 diff --git a/test/testastutils.cpp b/test/testastutils.cpp index 80cd66e6b..4ccb994e6 100644 --- a/test/testastutils.cpp +++ b/test/testastutils.cpp @@ -42,6 +42,7 @@ private: TEST_CASE(isVariableChanged); TEST_CASE(isVariableChangedByFunctionCall); TEST_CASE(nextAfterAstRightmostLeaf); + TEST_CASE(isUsedAsBool); } 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 + 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(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(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(i)) {} }", "i )")); + ASSERT(Result::True == isUsedAsBool("void f() { int i; if (1+!static_cast(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) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 8415c4382..a932300d4 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -4359,6 +4359,73 @@ private: "}\n"; ASSERT_EQUALS(true, valueOfTok(code, "> 0").isKnown()); 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 &values, MathLib::bigint i) {