Fix FP badBitmaskCheck in nested binary expressions (#5316)

If one operand is another binary expression, recursively ensure that no
nested operands are expanded macros.
This commit is contained in:
Anton Lindqvist 2023-08-11 23:06:24 +02:00 committed by GitHub
parent 720ae01898
commit b9cc138e57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -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())))

View File

@ -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());
}