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

文章介绍了如何使用cppcheck工具对C/C++代码进行静态分析,包括通过命令行生成XML格式的错误报告,然后使用cppcheck-htmlreport将其转换为可读的HTML格式。此外,还提供了批处理脚本自动化该过程,并给出了Gitee上的示例仓库链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 可读报告

### 配置和使用 Cppcheck 进行代码静态分析 #### 安装 Cppcheck 工具 为了能够在 VSCode 中顺利运行 Cppcheck,首先需要确保已经在本地环境中正确安装了该工具。可以通过包管理器来完成这一过程,在 Linux 上可以执行如下命令: ```bash sudo apt-get install cppcheck ``` 对于 macOS 用户,则可利用 Homebrew 来简化安装流程。 #### 设置环境变量 如果未将 `cppcheck` 添加到系统的 PATH 变量中,那么还需要手动设置路径以便于后续调用。这一步骤通常是在终端配置文件(如 `.bashrc`, `.zshrc`)里追加相应目录[^1]。 #### 安装 VSCode 插件 接着,在 Visual Studio Code 的扩展市场搜索并安装名为 "C/C++ Extension Pack" 或者专门针对 Cppcheck 的插件,这些插件能够增强 IDE 对 C 和 C++ 文件的支持,并提供更便捷的方式去集成 Cppcheck 功能[^2]。 #### 创建任务配置文件 tasks.json 为了让 VSCode 能够识别并自动触发 Cppcheck 分析,可以在项目根目录下创建或编辑 `.vscode/tasks.json` 文件,定义一个新的构建任务用于启动 Cppcheck 执行静态分析工作。下面是一个简单的例子说明如何编写此 JSON 文件的内容: ```json { "version": "2.0.0", "tasks": [ { "label": "Run Cppcheck", "type": "shell", "command": "/path/to/cppcheck", // 替换成实际的cppcheck位置 "args": [ "--enable=all", "-I./include", "./src" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"], "detail": "Cppcheck static analysis." } ] } ``` 上述配置会扫描 src 文件夹内的源码,并启用尽可能多类型的检测选项;同时通过 `-I` 参数指定了头文件所在的 include 目录以避免不必要的警告信息。注意替换 `/path/to/cppcheck` 为真实的 Cppcheck 命令所在的位置[^3]。 #### 使用 Cppcheck HTML 报告生成功能 (可选) 除了直接在控制台输出结果外,还可以借助额外脚本生成更加直观易读的 HTML 版报告页面。具体做法是先按照官方文档指示下载对应版本的 htmlreport.py 脚本,之后修改上面提到的任务配置中的 command 字段指向 Python 解释器以及该脚本本身即可实现自动化报表生成功能。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值