使用cppcheck输出可读的C++静态分析报告

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 &apos;x&apos; 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

4.gitee 完整示例仓库

cppcheck_out_htmlreport: 使用cppcheck 输出 htmlreport 可读报告

  • 17
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值