Skip to content

Commit

Permalink
Ignore zero valued enum entries from badBitmaskCheck (#5195)
Browse files Browse the repository at this point in the history
Usage of zero valued enum entries can be used for documenting purposes
and should be ignored just like zeroes expanded from macros.
  • Loading branch information
mptre committed Jun 26, 2023
1 parent 7507d40 commit 4ebb8ea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,11 @@ class TestCondition : public TestFixture {
"#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());
}


Expand Down

0 comments on commit 4ebb8ea

Please sign in to comment.