cppcheck github仓库地址:GitHub - danmar/cppcheck: static analysis of C/C++ code
依赖:python 3.8
pip依赖:pip3 install pygments
pygments安装时可能会出现安装失败问题,建议先更新 pip,然后使用国内源进行安装
python -m pip install --upgrade pip -i http://mirrors.aliyun.com/pypi/simple
pip3 install pygments -i http://mirrors.aliyun.com/pypi/simple
1.输出xml格式报告
cppcheck输出可读的html报告需要做到两步
利用cppcheck输出xml格式的报告
cppcheck -h 能很有帮助的说明文档,有需要的可以先求助help。
cppcheck --enable=all --xml ./ &>err.xml
输出xml格式报告如下:
<?xml version="1.0" encoding="UTF-8"?>
<results>
Checking example.cc...
<error file="example.cc" line="5" id="unassignedVariable" severity="style" msg="Variable 'x' is not assigned a value."/>
<error file="example.cc" line="6" id="uninitvar" severity="error" msg="Uninitialized variable: x"/>
Checking usage of global functions..
<error id="missingInclude" severity="style" msg="Cppcheck cannot find all the include files (use --check-config for details)"/>
</results>
2.将xml转换为html可读格式报告
利用cppchek-htmlreport工具输出html格式的报告
python3 cppchek-htmlreport -h 可以给出易懂的帮助说明
示例:
python.exe cppcheck-htmlreport.py --file=err.xml --title=CSA --report-dir=CodeQualityReport --source-dir=./
参数解释:
source-dir 源码路径
file 生成的xml存放路径,cppchekc是先生成xml,然后把xml转成html
title 输出html标题目录
source-dir 源码目录
report-dir 输出报告目录
输出结果样式:
3.批处理一键执行脚本
@echo off
chcp 65001
rem cppcheck_path:cppcheck目录
rem reportxml_path:xml输出目录
rem sourecode_path:源码目录
rem reporthtml_path:html报告输出目录
set cppcheck_path=D:\check\Cppcheck
set reportxml_path=D:\check\Cppcheck\htmlreport
set sourecode_path=E:\workspace\wing-gif-editor-master
set reporthtml_path=D:\check\Cppcheck\htmlreport\CodeQualityReport
rem 判断xml是否存在,如果存在清空err.xml
if exist %reportxml_path%\err.xml echo. >%reportxml_path%\err.xml
cd /d %sourecode_path%
%cppcheck_path%\cppcheck.exe --enable=all --platform=win64 --xml ./ 2>%reportxml_path%/err.xml
rem 初始化html报告输出目录
rmdir /s /q %reporthtml_path% && md %reporthtml_path%
cd /d %reportxml_path%
python.exe cppcheck-htmlreport.py --file=err.xml --title=CodeQualityReport --report-dir=CodeQualityReport --source-dir=%sourecode_path%\.
rem 打开html报告
start %reporthtml_path%\index.html
pause