超实用静态分析工具PC-lint Plus支持MISRA C 2012

Gimpel软件为MISRA社区提供了近20年的支持,致力于为MISRA C 2012提供可靠的一流支持。

Gimpel软件为MISRA社区提供了近20年的支持,致力于为MISRA C 2012提供可靠的一流支持。

PC-lint Plus 1.3为包括MISRA C 2012的几乎所有可静态执行的准则提供支持,并且其中绝大多数都得到了全面的支持。通过将au-misra3.lnt文件(随PC-lint Plus分发)的引用添加到现有配置中,可以轻松完成对MISRA C 2012合规性的检查。该文件启用与MISRA C 2012指南相对应的消息,并在已发布消息中添加文本,以指定与每个适用消息相关的特定规则。au-misra3.lnt文件是使用标准PC-lint Plus选项语法的,易于阅读的纯文本配置文件,可以轻松地对其进行修改以满足任何单个项目的需求。

PC-lint Plus随附的参考手册包括一个支持矩阵,详细列出了每个指南的支持级别以及每个指南的支持机制。

违规分析与陈述

考虑以下示例,其中包含许多MISRA C 2012违规:

    typedef short int16_t;    typedef int int32_t;    typedef unsigned short uint16_t;    int32_t calc(uint16_t id_1, uint16_t id_2, int16_t *idl, uint16_t idl_size);    int32_t calc(uint16_t id_1, uint16_t id_2, int16_t *idl, uint16_t idl_size) {        if (idl_size && id_1 < idl_size && id_2 < idl_size) return idl[id_1] * idl[id_2] + idl[id_1]; return 0; }

当使用PC-lint Plus分析此示例时,报告的MISRA C 2012违规包括(其中包括):

9046: ‘idl’ is typographically ambiguous with respect to ‘id_1’ when ignoring underscores,

treating ‘1’ as ‘I’, and treating ‘l’ as ‘I’ [MISRA 2012 Directive 4.5, advisory]

int32_t calc(uint16_t id_1, uint16_t id_2, int16_t *idl, uint16_t idl_size);

^


891: previous declaration is here

int32_t calc(uint16_t id_1, uint16_t id_2, int16_t *idl, uint16_t idl_size);

^

904: return statement before end of function ‘calc’ [MISRA 2012 Rule 15.5, advisory]

return idl[id_1] * idl[id_2] + idl[id_1];

^

9012: body should be a compound statement [MISRA 2012 Rule 15.6, required]

return idl[id_1] * idl[id_2] + idl[id_1];

^

9050: dependence placed on operator precedence (operators ‘&&’ and ‘<‘) [MISRA

2012 Rule 12.1, advisory]

if (idl_size && id_1 < idl_size && id_2 < idl_size)

^  ~~~~

9027: an unsigned value is not an appropriate left operand to && [MISRA 2012

Rule 10.1, required]

if (idl_size && id_1 < idl_size && id_2 < idl_size)

~~~~~~~~ ^

9031: cannot assign a composite expression of type ‘signed16’ to an object of

wider type ‘signed32’ [MISRA 2012 Rule 10.6, required]

return idl[id_1] * idl[id_2] + idl[id_1];

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

9050: dependence placed on operator precedence (operators ‘+’ and ‘*’) [MISRA

2012 Rule 12.1, advisory]

return idl[id_1] * idl[id_2] + idl[id_1];

~           ^

818: parameter ‘idl’ of function ‘calc(uint16_t, uint16_t, int16_t *, uint16_t)’

could be pointer to const [MISRA 2012 Rule 8.13, advisory]

int32_t calc(uint16_t id_1, uint16_t id_2, int16_t *idl,

报告的每项违规都包括发生违规的位置、消息号和基本问题的文本描述,以及违反的MISRA C 2012规则。例如,在消息中:

904: return statement before end of function ‘calc’ [MISRA 2012 Rule 15.5, advisory]

return idl[id_1] * idl[id_2] + idl[id_1];

^

904是与在函数结束之前出现return语句时发出的消息相关联的PC-lint Plus消息号。在这种情况下,消息的实际文本是“函数‘calc’结束之前的return语句”,它标识了有问题的函数。违反MISRA规则或指令的消息包含在消息文本末尾的方括号中。接下来的两行显示了与消息关联的上下文和位置。

解决违规

PC-lint Plus参考手册》包含每条消息的说明,并经常提供可用于解决问题的其他指导。此信息也可以显示在命令行上。例如,要显示消息904的描述,请运行带有选项-help=904的PC-lint Plus,以使PC-lint Plus显示以下注释:

A return statement was found before the end of a function definition.

Many programming standards require that functions contain a single exit

point located at the end of the function. This can enhance readability

and may make subsequent modification less error prone.

在函数定义结束之前找到了return语句。许多编程标准都要求函数必须包含位于函数末尾的单个出口点。这可以增强可读性,并且可以使后续修改的出错率降低。)

有关指定规则或指令的信息,可查阅MISRA C 2012指南文档。

重写上面的calc函数以解决所报告的违规的一种方法是:

    int32_t calc(uint16_t id_1, uint16_t id_2, const int16_t *id_list, uint16_t idl_size);    int32_t calc(uint16_t id_1, uint16_t id_2, const int16_t *id_list, uint16_t idl_size) {        int32_t result = 0;        if ((idl_size > 0U) && (id_1 < idl_size) && (id_2 < idl_size)) { result = ((int32_t)id_list[id_1] * id_list[id_2]) + id_list[id_1]; } return result; }

处理偏差

偏差是指源代码中的违反规则或指令的情况已被检查并视为可接受的实例。 MISRA C 2012文档包含咨询,必需和强制性准则。合规项目可能不会偏离强制性准则,但可能包含批准的要求和咨询准则偏差。虽然偏差过程因项目而异,但可以使用非常灵活的抑制机制在PC-lint Plus中配置偏差。当引用特定的符号或类型时,或在特定的行上时,可以通过多种方式来抑制大多数消息,例如在文件、函数或语句中。某些类型的禁止操作要求在源代码中添加注释,但大多数不需要。

例如,MISRA 2012规则15.5(上面由消息904报告)是一项建议性规则,建议为每个功能使用一个退出点。可以使用选项-efunc(904,calc)对函数calc禁止显示此消息,该选项可以作为棉绒注释放置在项目配置文件或源代码中。可以以相同的方式抑制来自此功能内的其他消息。评论可以遵循抑制选项,其中可能包括基本原理或形式偏差信息。

库代码

PC-lint Plus区分库代码(默认情况下包括外部和系统头,但可以自定义以包括头和模块的任何子集)和项目代码。默认情况下,PC-lint Plus将同时检查库代码和项目代码是否符合MISRA C2012。通常希望将检查范围限制为项目代码,这很容易在引用au-misra3.lnt文件后,通过使用选项-wlib=4 -wlib=1来重置库警告级别来完成。也可以使用-elib和+elib选项轻松地为库代码启用或禁用单个消息。

基本类型计算

PC-lint Plus可以解释使用+f12选项计算复合表达式的最终基本类型所涉及的步骤。消息9903将在完整的表达式之后发出,以说明表达式中涉及的MISRA C 2012基本类型以及它们如何组合形成新的基本类型。

例如,给定:

int32_t f(int32_t a, int16_t b) {

return (b + b) + (a + b);

}

PC-lint Plus发出:

note 9032: left operand to + is a composite expression of type ‘signed16’

which is smaller than the right operand of type ‘signed32’

[MISRA 2012 Rule 10.7, required]

return (b + b) + (a + b);

~~~~~~~ ^

为什么左侧是signed16,右侧是signed32/p>

+f12和+e9903处理示例会逐步评估表达式,并在每个步骤中涉及相应的基本类型:

info 9903: (signed16 + signed16) + (signed32 + signed16)

info 9903: (signed16) + (signed32)

info 9903: signed32

说明在右操作数中,sign3232 + signed16产生了与左操作数冲突的sign32。

相关推荐:

MISRA C++ 2008

MISRA C 2004

AUTOSAR

CERT C

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年7月23日
下一篇 2020年7月23日

相关推荐

发表回复

登录后才能评论