From 9225bff31d82902639ceba136b6bbbd1c3039eba Mon Sep 17 00:00:00 2001 From: PKEuS Date: Thu, 23 Mar 2017 18:57:48 +0100 Subject: [PATCH] Added (back) support for enums to CheckCondition::checkIncorrectLogicOperator() (#7794) --- lib/checkcondition.cpp | 24 ++++++++++++++++-------- test/testcondition.cpp | 10 ++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 40dc58a0d..539a9fa0f 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -618,22 +618,30 @@ static bool parseComparison(const Token *comp, bool *not1, std::string *op, std: if (!comp) return false; - if (!comp->isComparisonOp() || !comp->astOperand1() || !comp->astOperand2()) { + const Token* op1 = comp->astOperand1(); + const Token* op2 = comp->astOperand2(); + if (!comp->isComparisonOp() || !op1 || !op2) { *op = "!="; *value = "0"; *expr = comp; - } else if (comp->astOperand1()->isLiteral()) { - if (comp->astOperand1()->isExpandedMacro()) + } else if (op1->isLiteral()) { + if (op1->isExpandedMacro()) return false; *op = invertOperatorForOperandSwap(comp->str()); - *value = comp->astOperand1()->str(); - *expr = comp->astOperand2(); + if (op1->enumerator() && op1->enumerator()->value_known) + *value = MathLib::toString(op1->enumerator()->value); + else + *value = op1->str(); + *expr = op2; } else if (comp->astOperand2()->isLiteral()) { - if (comp->astOperand2()->isExpandedMacro()) + if (op2->isExpandedMacro()) return false; *op = comp->str(); - *value = comp->astOperand2()->str(); - *expr = comp->astOperand1(); + if (op2->enumerator() && op2->enumerator()->value_known) + *value = MathLib::toString(op2->enumerator()->value); + else + *value = op2->str(); + *expr = op1; } else { *op = "!="; *value = "0"; diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 61c455214..04746785e 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -62,6 +62,7 @@ private: TEST_CASE(incorrectLogicOperator7); // opposite expressions: (expr || !expr) TEST_CASE(incorrectLogicOperator8); // ! TEST_CASE(incorrectLogicOperator9); + TEST_CASE(incorrectLogicOperator10); // enum TEST_CASE(secondAlwaysTrueFalseWhenFirstTrueError); TEST_CASE(incorrectLogicOp_condSwapping); TEST_CASE(testBug5895); @@ -1123,6 +1124,15 @@ private: ASSERT_EQUALS("", errout.str()); } + void incorrectLogicOperator10() { // #7794 - enum + check("typedef enum { A, B } Type_t;\n" + "void f(Type_t t) {\n" + " if ((t == A) && (t == B))\n" + " {}\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Logical conjunction always evaluates to false: t == 0 && t == 1.\n", errout.str()); + } + void secondAlwaysTrueFalseWhenFirstTrueError() { check("void f(int x) {\n" " if (x > 5 && x != 1)\n"