Fixed #5058 (False positive: (warning) Comparison of a boolean expression with an integer.)

This commit is contained in:
Daniel Marjamäki 2013-10-05 08:33:33 +02:00
parent 5dbc02250f
commit 348f3fa97f
2 changed files with 34 additions and 2 deletions

View File

@ -349,6 +349,38 @@ void CheckBool::assignBoolToPointerError(const Token *tok)
"Boolean value assigned to pointer.");
}
static bool isBoolExpr(const Token *tok)
{
bool boolExpr = true;
int indentlevel = 0;
for (; tok; tok = tok->previous()) {
if (tok->str() == ")" || tok->str() == "]")
++indentlevel;
else if (tok->str() == "(" || tok->str() == "[") {
if (indentlevel <= 0)
break;
--indentlevel;
} else if (tok->isNumber())
boolExpr = false;
else if (tok->varId() && isNonBoolStdType(tok->variable()))
boolExpr = false;
else if (tok->isArithmeticalOp())
boolExpr = false;
else if (tok->str() == "!" || tok->isComparisonOp())
return true;
else if (indentlevel == 0 && Token::Match(tok,"[;{}=?:&|^,]"))
break;
else if (Token::Match(tok, "&&|%oror%|and|or"))
break;
}
return boolExpr;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CheckBool::checkComparisonOfBoolExpressionWithInt()
@ -388,7 +420,7 @@ void CheckBool::checkComparisonOfBoolExpressionWithInt()
opTok = tok->tokAt(2);
if (Token::Match(opTok, "<|>"))
op = opTok->str()[0];
} else if (Token::Match(tok, "%any% %comp% ! %var%")) {
} else if (Token::Match(tok, "%any% %comp% ! %var%") && !isBoolExpr(tok)) {
numTok = tok;
opTok = tok->next();
if (Token::Match(opTok, "<|>"))

View File

@ -363,7 +363,7 @@ private:
check("void f(int x, int y) {\n"
" return (!y == !x);\n"
"}");
TODO_ASSERT_EQUALS("","[test.cpp:2]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
ASSERT_EQUALS("", errout.str());
}
void comparisonOfBoolExpressionWithInt3() {