在以下几行,我得到不同的问题来自Parasoft的C/C++测试静态分析工具,并从的IAR Embedded Workbench MISRA检查报告:MISRA 9.2初始化浮动和无符号阵列
[1] static unsigned int array_a[30] = {0U};
[2] static float array_b[20] = {0.0f};
Parasoft的静态分析说:
Not all elements of variable ‘array_a’ are initialized.
Not all elements of variable ‘array_b’ are initialized.
IAR Embedded Workbench在上述语句(及其MISRA检查程序)上没有问题。
我可以让消息变量 'array_b' 走带:
[3] static float array_b[20] = {0};
然而,同样的伎俩不会为'array_a的工作:
[4] static unsigned int array_a[30] = {0};
现在IAR嵌入式工作台MISRA检查器抱怨是因为有符号整数常量0被分配给无符号整数:
Error[Pm127]: a 'U' suffix shall be applied to all constants of 'unsigned' type (MISRA C 2004 rule 10.6)
Parasoft静态分析没有显示上述第4行的任何问题。
我相信这可以归结为“零”的MISRA解释第9.2条:
例外
"All the elements of arrays or structures can be initialized (to zero or NULL)
by giving an explicit initializer for the first element only. If this method
of initialization is chosen then the first element should be initialized
to zero (or NULL), and nested braces need not be used."
哪些检查是正确的?
MISRA规则之间没有冲突。一个工具被打破,另一个是正确的。你的强制转换不会解决任何问题,实际上这些代码不符合MISRA标准,因为它对具有“U”后缀的整数文字很挑剔。 – Lundin
如果您有理由并且每次实施MISRA的偏差程序都不同,那么您可能会偏离MISRA规则。一般来说,个别程序员不得在未经批准的情况下做出偏差,例如通过公司质量体系提出正式要求。就我个人而言,我认为这是实施MISRA的一种相当乏味的方式。相反,我会建议创建一个“全局”文档,其中只包含MISRA-C允许的偏差和基本原理。此文件应在需要时予以保留。 – Lundin