[英]PC-Lint Misra 10.1 Error on Boolean Expression
PC-Lint version 9.00L looks at this code: PC-Lint版本9.00L的代码如下:
typedef unsigned char boolean_t; //This is actually in a system header file.
...
/* The rest is in the .c file I'm working on. */
boolean_t booleanVal
...
uint8_t maskedVal;
maskedVal = 0; //real code has this assigned based on a bitwise-and
booleanVal = ( maskedVal != 0U );
And gives this error: 并给出此错误:
booleanVal = ( maskedVal != 0U );
^
"LINT: <filename> Note 960: Violates MISRA 2004
Required Rule 10.1, Implicit conversion of integer to smaller type"
I have declared boolean_t as a strong Boolean type using -strong(B, boolean_t )
in my .lnt file. 我在.lnt文件中使用
-strong(B, boolean_t )
将-strong(B, boolean_t )
声明为强布尔类型。
So why is PC-Lint complaining about converting integers when I'm assigning a clearly boolean expression to a clearly boolen variable? 那么,当我为一个明显的布尔变量分配一个明显的布尔表达式时,为什么PC-Lint抱怨转换整数?
The result of ( maskedVal != 0U )
is int
yet even though it is 0
or 1
MISRA complains that it is being forced into the smaller unsigned char
your homebrew boolean type. ( maskedVal != 0U )
是int
,即使它是0
或1
MISRA抱怨说它被强制转换为您的自定义布尔型较小的unsigned char
。
Don't invent your own boolean type. 不要发明自己的布尔类型。 Either use
int
or the formal boolean type available in modern C implementations. 使用
int
或现代C实现中可用的形式布尔类型。
MISRA-C:2004 didn't treat boolean types as a special case, they were just another small integer type like char
etc. Notably, it did not support bool
either since it didn't support C99. MISRA-C:2004并没有将布尔类型视为特殊情况,它们只是另一个小整数类型,例如
char
等。值得注意的是,它也不支持bool
,因为它不支持C99。
All if this was fixed in MISRA-C:2012, where your home-brewed type would be so-called essentially boolean . 如果这一切在MISRA-C:2012中得到解决,则您的自酿类型将被称为本质上为boolean 。 There's no requirement in MISRA-C:2012 that you must use
bool
, although this is recommended, but the guidelines tolerate the use of "home-brewed booleans", given that you can somehow tell your static analyser which type it is. 在MISRA-C:2012中,没有要求您必须使用
bool
,尽管建议这样做,但是由于您可以以某种方式告诉您静态分析器是什么类型,因此该准则允许使用“自制布尔值”。
But since you are using an older version of MISRA-C, booleanVal = ( maskedVal != 0U );
但是由于您使用的是MISRA-C的旧版本,因此
booleanVal = ( maskedVal != 0U );
is an assignment to narrower type from int
, and also from a signed type to an unsigned type. 是从
int
到窄类型的赋值,也是从有符号类型到无符号类型的赋值。
This is a violation of MISRA-C:2004 rule 10.1, but perfectly fine in MISRA-C:2012. 这违反了MISRA-C:2004规则10.1,但在MISRA-C:2012中完全可以。
Notably, you also have an implicit promotion from uint8_t
to unsigned int
, although that shouldn't violate any MISRA rule. 值得注意的是,您还可以从
uint8_t
隐式升级为unsigned int
,尽管这不应违反任何MISRA规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.