Ignore zero valued enum entries from badBitmaskCheck (#5195)

Usage of zero valued enum entries can be used for documenting purposes
and should be ignored just like zeroes expanded from macros.
This commit is contained in:
Anton Lindqvist 2023-06-26 10:43:20 +02:00 committed by GitHub
parent 7507d400de
commit 4ebb8eaf0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -317,8 +317,15 @@ void CheckCondition::checkBadBitmaskCheck()
const bool isZero1 = (tok->astOperand1()->hasKnownIntValue() && tok->astOperand1()->values().front().intvalue == 0);
const bool isZero2 = (tok->astOperand2()->hasKnownIntValue() && tok->astOperand2()->values().front().intvalue == 0);
if (!isZero1 && !isZero2)
continue;
if ((isZero1 || isZero2) && !tok->isExpandedMacro() && !(isZero1 && tok->astOperand1()->isExpandedMacro()) && !(isZero2 && tok->astOperand2()->isExpandedMacro()))
auto isOperandExpanded = [](const Token *op) {
return op->isExpandedMacro() || op->isEnumerator();
};
if (!tok->isExpandedMacro() &&
!(isZero1 && isOperandExpanded(tok->astOperand1())) &&
!(isZero2 && isOperandExpanded(tok->astOperand2())))
badBitmaskCheckError(tok, /*isNoOp*/ true);
}
}

View File

@ -912,6 +912,11 @@ private:
"#endif\n"
" 0;");
ASSERT_EQUALS("", errout.str());
check("enum precedence { PC0, UNARY };\n"
"int x = PC0 | UNARY;\n"
"int y = UNARY | PC0;\n");
ASSERT_EQUALS("", errout.str());
}