You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Cppcheck 使用指南
## 基本信息
- **Cppcheck 位置**: `/usr/bin/cppcheck` (系统已安装)
- **项目文件**: `/home/feng/test/math.c`
- **项目目录**: `/home/feng/test/`
## 基本使用命令
### 1. 生成 XML 报告(推荐,用于 cppcheck_to_tests.py
```bash
# 在 WSL 中执行
cppcheck --enable=all --xml --xml-version=2 /home/feng/test/math.c 2> /home/feng/test/report.xml
```
或者更详细的选项:
```bash
cppcheck \
--enable=all \
--xml \
--xml-version=2 \
--suppress=missingIncludeSystem \
--suppress=unusedFunction \
/home/feng/test/math.c \
2> /home/feng/test/report.xml
```
### 2. 生成文本报告
```bash
cppcheck --enable=all /home/feng/test/math.c 2> /home/feng/test/report.txt
```
### 3. 直接查看输出(不保存文件)
```bash
cppcheck --enable=all /home/feng/test/math.c
```
### 4. 检查整个项目目录
如果 `/home/feng/test/` 下有多个文件:
```bash
cppcheck --enable=all --xml --xml-version=2 /home/feng/test/ 2> /home/feng/test/report.xml
```
## 常用选项说明
- `--enable=all`: 启用所有检查(包括 style、performance、portability 等)
- `--xml`: 输出 XML 格式
- `--xml-version=2`: 使用 XML 版本 2推荐
- `--suppress=规则ID`: 抑制特定规则的警告
- `-I <路径>`: 添加头文件搜索路径
- `--std=c99``--std=c11`: 指定 C 标准(对于 .c 文件)
## 完整示例:生成报告并用于测试用例生成
### 步骤 1: 生成 XML 报告
```bash
cd /home/feng/report
cppcheck --enable=all --xml --xml-version=2 /home/feng/test/math.c 2> /home/feng/test/cppcheck_report.xml
```
### 步骤 2: 使用 cppcheck_to_tests.py 生成测试用例
```bash
cd /home/feng/Report-Generation
python3 cppcheck_to_tests.py \
/home/feng/test/cppcheck_report.xml \
--project-root /home/feng/test \
--out ./math_cppcheck_tests \
--model deepseek-chat \
--auto-verify \
--use-templates
```
## 从 Windows PowerShell 执行
如果要从 Windows PowerShell 执行,使用 `wsl` 命令:
```powershell
wsl cppcheck --enable=all --xml --xml-version=2 /home/feng/test/math.c 2> /home/feng/test/report.xml
```
## 验证 cppcheck 版本
```bash
cppcheck --version
```
## 查看帮助
```bash
cppcheck --help
```
## 针对 C 文件的推荐命令
由于 `math.c` 是 C 文件,建议指定 C 标准:
```bash
cppcheck \
--enable=all \
--xml \
--xml-version=2 \
--std=c99 \
--suppress=missingIncludeSystem \
/home/feng/test/math.c \
2> /home/feng/test/report.xml
```