From b9cc138e57d930aa17d782311db9d1eb163dec5d Mon Sep 17 00:00:00 2001 From: Anton Lindqvist Date: Fri, 11 Aug 2023 23:06:24 +0200 Subject: [PATCH] Fix FP badBitmaskCheck in nested binary expressions (#5316) If one operand is another binary expression, recursively ensure that no nested operands are expanded macros. --- lib/checkcondition.cpp | 14 +++++++++++--- test/testcondition.cpp | 5 +++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 477719d3e..be1283232 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -290,6 +290,17 @@ static bool inBooleanFunction(const Token *tok) return false; } +static bool isOperandExpanded(const Token *tok) +{ + if (tok->isExpandedMacro() || tok->isEnumerator()) + return true; + if (tok->astOperand1() && isOperandExpanded(tok->astOperand1())) + return true; + if (tok->astOperand2() && isOperandExpanded(tok->astOperand2())) + return true; + return false; +} + void CheckCondition::checkBadBitmaskCheck() { if (!mSettings->severity.isEnabled(Severity::style)) @@ -320,9 +331,6 @@ void CheckCondition::checkBadBitmaskCheck() if (!isZero1 && !isZero2) continue; - auto isOperandExpanded = [](const Token *op) { - return op->isExpandedMacro() || op->isEnumerator(); - }; if (!tok->isExpandedMacro() && !(isZero1 && isOperandExpanded(tok->astOperand1())) && !(isZero2 && isOperandExpanded(tok->astOperand2()))) diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 27c182c26..c79b50a84 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -916,6 +916,11 @@ private: "int x = PC0 | UNARY;\n" "int y = UNARY | PC0;\n"); ASSERT_EQUALS("", errout.str()); + + check("#define MASK 0\n" + "#define SHIFT 1\n" + "int x = 1 | (MASK << SHIFT);\n"); + ASSERT_EQUALS("", errout.str()); }