天天看点

cppcheck支持MISRA C 2012

1.从官网下载cppcheck安装包然后安装

cppcheck官网:

https://sourceforge.net/projects/cppcheck/

下载windows平台的安装包后,双击安装包文件,安装。默认会安装GUI版本。

安装完成后,需要配置“环境变量”的path参数,包含cppcheck的安装路径。

此外,为了使用MISRA,还需要安装python,需要设置python的环境变量。

2.基本用法

基本用法:

cppcheck --dump somefile.c

python misra.py --rule-texts= somefile.c.dump

3.运行cppcheck命令行工具对代码文件进行静态分析

假设我们的实例代码如下:

#include "stdio.h"

void main()
{
	int i = 0;
	int j = 0;

	for (i = 0; i <0; i++) {
		printf("i = %d", i);
	}

}
           

下面是测试:

d:\MISRA_TEST\myproj_1>cppcheck --dump test.c

Checking test.c …

d:\MISRA_TEST\myproj_1>python “c:\Program Files\Cppcheck\addons\misra.py” test.c.dump

Checking test.c.dump…

Checking test.c.dump, config …

[test.c:3] (style) misra violation (use --rule-texts= to get proper output) (Undefined) [misra-c2012-8.2]

[test.c:9] (style) misra violation (use --rule-texts= to get proper output) (Undefined) [misra-c2012-17.7]

MISRA rules violations found:

Undefined: 2

MISRA rules violated:

misra-c2012-8.2 (-): 1

misra-c2012-17.7 (-): 1

4.使用MISRA规则文本文件时

命令行用法:

d:\MISRA_TEST\myproj_1>python “c:\Program Files\Cppcheck\addons\misra.py” --rule-texts=misra2012_rules.txt test.c.dump

Checking test.c.dump…

Checking test.c.dump, config …

[test.c:3] (style) Function types shall be in prototype form with named parameters (Required) [misra-c2012-8.2]

[test.c:9] (style) The value returned by a function having non-void return type shall be used (Required) [misra-c2012-17.7]

MISRA rules violations found:

Required: 2

MISRA rules violated:

misra-c2012-8.2 (-): 1

misra-c2012-17.7 (-): 1

参考《使用GUI中的MISRA规则?》

参考帖子地址:https://sourceforge.net/p/cppcheck/discussion/general/thread/ccbe9e89/?page=0