简体   繁体   English

运算符'!'不允许的操作数 [MISRA 2012规则10.1,必填]

[英]Unpermitted operand to operator '!' [MISRA 2012 Rule 10.1, required]

Observing below Misra warning for below part of code. 观察下面Misra警告的下面部分代码。

Unpermitted operand to operator '!' 运算符'!'不允许的操作数 [MISRA 2012 Rule 10.1, required] [MISRA 2012规则10.1,必填]

Not sure what could be the fix here to get rid of this warning. 不知道这里有什么解决方法可以消除此警告。

#define C_BYTE unsigned char
C_BYTE SessionStatus;
#define DISCONNECTED   0x10

if((!(SessionStatus & (C_BYTE) DISCONNECTED)))
{
   //Do something
}

I tried a few chances but didn't work as below. 我尝试了几次机会,但没有如下工作。

1) 1)

if((~(SessionStatus & (C_BYTE) DISCONNECTED)))
{
       //Do something
}

2) 2)

if((!(SessionStatus & (C_BYTE) DISCONNECTED)) != 0u)
{
       //Do something
}

The reason for the warning is that MISRA-C rule 10.1 expects the operand to the ! && || 警告的原因是MISRA-C规则10.1期望操作数为! && || ! && || operators to be "essentially boolean". 运算符必须是“基本布尔值”。 In your case it is actually an int , because of implicit type promotion rules . 在您的情况下,由于隐式类型提升规则 ,它实际上是一个int

Your 2nd example almost solved it, but you must convert to essentially boolean before applying ! 您的第二个示例几乎解决了它,但是您必须应用将其转换为布尔值! . That is: 那是:

if(!( (SessionStatus & (C_BYTE)DISCONNECTED) != 0u )) 

This is ok since the result of the != operator is to be regarded as essentially boolean. 这是可以的,因为!=运算符的结果应视为基本布尔值。 So that code is MISRA-C compliant, but a bit hard to read. 因此该代码符合MISRA-C的要求,但有点难以阅读。 I would instead recommend this: 相反,我建议这样做:

#define DISCONNECTED   0x10u // u suffix makes this essentially unsigned
...

bool disconnected = (bool) (SessionStatus & DISCONNECTED);
if(!disconnected)

By adding the u suffix to the integer constant, it is essentially unsigned, same type category as unsigned char. 通过将u后缀添加到整数常量,它实际上是无符号的,与无符号char属于相同的类型类别。 Therefore the & operation is valid without using any casts. 因此, &操作在不使用任何强制转换的情况下是有效的。 However, we are not allowed to implicitly convert from essentially unsigned to essentially boolean, therefore add the cast to bool . 但是,我们不允许将本质上从无符号转换为本质上是布尔值,因此将强制类型转换添加到bool

EDIT 编辑

Since SessionStatus & DISCONNECTED is a "composite expression", MISRA doesn't allow the result to be assigned or cast to a different or wider essential type. 由于SessionStatus & DISCONNECTED是“复合表达式”,因此MISRA不允许将结果分配或强制转换为其他或更广泛的基本类型。 The rationale is that they fear incompetent programmers who believe that the calculation in for example (uint32_t)(u16a + u16b) is carried out with uint32_t because of the cast, which is of course nonsense. 这样做的理由是,他们担心不称职的程序员,他们认为例如(uint32_t)(u16a + u16b)是由于(uint32_t)(u16a + u16b)而使用uint32_t进行的,这当然是胡说八道。 (It would be better to educate the programmers about basic C than to come up with artificial rules, but anyway...) (对程序员进行基本的C方面的教育比提出人为的规则要好,但是无论如何...)

I'd advise to ignore this rule entirely, but if you can't for whatever reason, here's an alternative fix: 我建议您完全忽略此规则,但是如果出于某种原因您不能这样做,则可以选择以下解决方法:

#define DISCONNECTED   0x10u
...

unsigned char disconnected = SessionStatus & DISCONNECTED;
if(!(bool)disconnected)

But of course this is worse code than my first example. 但是,当然,这比我的第一个示例更糟糕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM