c - 米斯拉违规 12.6

标签 c boolean typedef misra safety-critical

如何消除以下语句中的 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/

相关文章:

ios - 使用 NSDictionary 发送 TRUE/FALSE 值,但不使用 NSNumber 类

java - 如何创建 Boolean 类的 bean 对象

c++ - 编写 C++ API - 如何保持对 API 内部对象的外部引用?

C++ 语法 : Using a typedef to typecast

python - 为变量赋值或重复简单操作的计算量是否更大?

c - 它在C中创建了一个数组,当程序从内存中出来时会发生什么?

c++ - 编写自己的分区恢复

PHP mysql_connect 不返回 boolean 值

c++ - typedef 和模板的 undefined symbol ?

c - 正整数相乘结果为 0 (__int64)