天天看點

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