CheckAssignIf: Improved checking for bitwise or

This commit is contained in:
Daniel Marjamäki 2012-09-14 19:13:44 +02:00
parent 9f2e1ab98d
commit a9c1a052b9
3 changed files with 20 additions and 9 deletions

View File

@ -85,10 +85,7 @@ void CheckAssignIf::comparison()
return; return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (tok->str() != "&") if (Token::Match(tok, "&|%or% %num% )| ==|!= %num% &&|%oror%|)")) {
continue;
if (Token::Match(tok, "& %num% )| ==|!= %num% &&|%oror%|)")) {
const MathLib::bigint num1 = MathLib::toLongNumber(tok->strAt(1)); const MathLib::bigint num1 = MathLib::toLongNumber(tok->strAt(1));
if (num1 < 0) if (num1 < 0)
continue; continue;
@ -104,18 +101,19 @@ void CheckAssignIf::comparison()
if (num2 < 0) if (num2 < 0)
continue; continue;
if ((num1 & num2) != num2) { if ((tok->str() == "&" && (num1 & num2) != num2) ||
(tok->str() == "|" && (num1 | num2) != num2)) {
const std::string& op(compareToken->str()); const std::string& op(compareToken->str());
comparisonError(tok, num1, op, num2, op=="==" ? false : true); comparisonError(tok, tok->str(), num1, op, num2, op=="==" ? false : true);
} }
} }
} }
} }
void CheckAssignIf::comparisonError(const Token *tok, MathLib::bigint value1, const std::string &op, MathLib::bigint value2, bool result) void CheckAssignIf::comparisonError(const Token *tok, const std::string &bitop, MathLib::bigint value1, const std::string &op, MathLib::bigint value2, bool result)
{ {
std::ostringstream expression; std::ostringstream expression;
expression << std::hex << "(X & 0x" << value1 << ") " << op << " 0x" << value2; expression << std::hex << "(X " << bitop << " 0x" << value1 << ") " << op << " 0x" << value2;
const std::string errmsg("Expression '" + expression.str() + "' is always " + (result?"true":"false") + ".\n" const std::string errmsg("Expression '" + expression.str() + "' is always " + (result?"true":"false") + ".\n"
"The expression '" + expression.str() + "' is always " + (result?"true":"false") + "The expression '" + expression.str() + "' is always " + (result?"true":"false") +

View File

@ -66,6 +66,7 @@ private:
void assignIfError(const Token *tok, bool result); void assignIfError(const Token *tok, bool result);
void comparisonError(const Token *tok, void comparisonError(const Token *tok,
const std::string &bitop,
MathLib::bigint value1, MathLib::bigint value1,
const std::string &op, const std::string &op,
MathLib::bigint value2, MathLib::bigint value2,
@ -76,7 +77,7 @@ private:
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const { void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
CheckAssignIf c(0, settings, errorLogger); CheckAssignIf c(0, settings, errorLogger);
c.assignIfError(0, false); c.assignIfError(0, false);
c.comparisonError(0, 6, "==", 1, false); c.comparisonError(0, "&", 6, "==", 1, false);
c.multiConditionError(0,1); c.multiConditionError(0,1);
} }

View File

@ -106,6 +106,18 @@ private:
" if (x & 4 != 3);\n" " if (x & 4 != 3);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Expression '(X & 0x4) != 0x3' is always true.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (style) Expression '(X & 0x4) != 0x3' is always true.\n", errout.str());
check("void foo(int x)\n"
"{\n"
" if ((x | 4) == 3);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Expression '(X | 0x4) == 0x3' is always false.\n", errout.str());
check("void foo(int x)\n"
"{\n"
" if ((x | 4) != 3);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Expression '(X | 0x4) != 0x3' is always true.\n", errout.str());
} }
void multicompare() { void multicompare() {