如何消除以下语句中的 MISRA 违规
typedef unsigned char boolean;
boolean A, B;
A = !B;
Operand of logical ! operator is not an 'effectively Boolean' expression. MISRA-C:2004 Rule 12.6; REFERENCE - ISO:C90-6.3.3.3 Unary Arithmetic Operators
最佳答案
如果您阅读规则 12.6,它会在附录中说明“检查 boolean 表达式”。在那里我们可以阅读
"Boolean-by-enforcement values can be introduced by implementing a specific type enforcement mechanism using a tool. A Boolean type could be associated with a specific typedef, and would then be used for any objects that are Boolean. This could bring many benefts, especially if the checking tool can support it, and in particular it could help avoid confusion between logical operations and integer operations."
MISRA-C:2004 采用 C90,而在 C90 中没有 bool 类型,您必须自己对它进行类型定义,就像您所做的那样。由于您的意图是拥有一个有效的 boolean 类型,因此代码就可以了。事实上,除了强制性建议之外,您的代码还遵循 MISRA 建议。
问题出在您的工具上:它要么不支持根据 MISRA 的建议允许特定的 boolean 类型,要么配置错误。
关于c - 米斯拉违规 12.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21760989/