关于 static-analysis
为 Agent 提供 C/C++ 静态分析工具的选择、运行与问题分流指南,适用于代码质量检查和 CI 配置。内容涉及 clang-tidy、cppcheck、scan-build、误报抑制及编译数据库。
主要功能
- 01生成 compile_commands.json
- 02配置并运行 clang-tidy
- 03按目标选择检查类别
- 04使用 cppcheck 和 scan-build
- 05抑制误报并接入 CI
适用场景
- 01为 CMake 项目配置 clang-tidy
- 02排查 cppcheck 误报
- 03在 CI 中运行静态分析
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mohitmishra786/ low-level-dev-skills --skill static-analysis安装后在 Claude Code 中输入/static-analysis确认已加载。
附带文件references/clang-tidy-checks.md
详细介绍
静态分析
目的
指导 Agent 为 C/C++ 选择、运行和分类处理静态分析工具——clang-tidy、cppcheck 和 scan-build,包括抑制策略和 CI 集成。
触发条件
- “如何在我的项目上运行 clang-tidy?”
- “应该启用哪些 clang-tidy 检查?”
- “cppcheck 报告了误报——我该如何抑制它们?”
- “如何设置 scan-build 以进行更深入的分析?”
- “我的构建输出了大量静态分析警告”
- “如何为 clang-tidy 生成 compile_commands.json?”
工作流
1. 生成 compile_commands.json
clang-tidy 需要编译数据库:
# CMake (preferred)
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
ln -s build/compile_commands.json .
# Bear (for Make-based projects)
bear -- make
# compiledb (alternative for Make)
pip install compiledb
compiledb make
2. 运行 clang-tidy
# Single file
clang-tidy src/foo.c -- -std=c11 -I include/
# Whole project via compile_commands.json
run-clang-tidy -p build/ -j$(nproc)
# With specific checks enabled
clang-tidy -checks='bugprone-*,modernize-*,performance-*' src/foo.cpp
# Apply auto-fixes
clang-tidy -checks='modernize-use-nullptr' -fix src/foo.cpp
3. 查看类别决策树
Goal?
├── Find real bugs → bugprone-*, clang-analyzer-*
├── Modernise C++ code → modernize-*
├── Follow core guidelines → cppcoreguidelines-*
├── Catch performance issues → performance-*
├── Security hardening → cert-*, hicpp-*
└── Readability / style → readability-*, llvm-*
| 类别 | 关键检查项 | 检查内容 |
|---|---|---|
bugprone-* | use-after-move、integer-division、suspicious-memset-usage | 可能存在的错误 |
modernize-* | use-nullptr、use-override、use-auto | C++11/14/17 惯用法 |
cppcoreguidelines-* | avoid-goto、pro-bounds-*、no-malloc | C++ Core Guidelines |
performance-* | unnecessary-copy-initialization、avoid-endl | 性能回退 |
clang-analyzer-* | core.*、unix.*、security.* | 路径敏感型错误 |
cert-* | err34-c、str51-cpp | CERT 编码标准 |
4. .clang-tidy 配置文件
# .clang-tidy — place at project root
Checks: >
bugprone-*,
modernize-*,
performance-*,
-modernize-use-trailing-return-type,
-bugprone-easily-swappable-parameters
WarningsAsErrors: 'bugprone-*,clang-analyzer-*'
HeaderFilterRegex: '^(src|include)/.*'
CheckOptions:
- key: modernize-loop-convert.MinConfidence
value: reasonable
- key: readability-identifier-naming.VariableCase
value: camelCase
5. 抑制误报
// Suppress a single line
int result = riskyOp(); // NOLINT(bugprone-signed-char-misuse)
// Suppress a block
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
constexpr int BUFFER_SIZE = 4096;
// Suppress whole function
[[clang::suppress("bugprone-*")]]
void legacy_code() { /* ... */ }
或者在 .clang-tidy 中:
# Exclude third-party directories
HeaderFilterRegex: '^(src|include)/.*'
# Disable specific checks
Checks: '-bugprone-easily-swappable-parameters'
6. 运行 cppcheck
# Basic run
cppcheck --enable=all --std=c11 src/
# With compile_commands.json
cppcheck --project=build/compile_commands.json
# Include specific checks and suppress noise
cppcheck --enable=warning,performance,portability \
--suppress=missingIncludeSystem \
--suppress=unmatchedSuppression \
--error-exitcode=1 \
src/
# Generate XML report for CI
cppcheck --xml --xml-version=2 src/ 2> cppcheck-report.xml
--enable= 值 | 检查内容 |
|---|---|
warning | 未定义行为、不良实践 |
performance | 冗余操作、低效模式 |
portability | 不具可移植性的构造 |
information | 配置和使用说明 |
all | 以上所有内容 |
7. 使用 scan-build 进行路径敏感分析
# Intercept a Make build
scan-build make
# Intercept CMake build
scan-build cmake --build build/
# Show HTML report
scan-view /tmp/scan-build-*/
# With specific checkers
scan-build -enable-checker security.insecureAPI.gets \
-enable-checker alpha.unix.cstring.BufferOverlap \
make
scan-build 能发现比 clang-tidy 更深层的问题:跨函数的释放后使用、逻辑错误导致的无效存储,以及复杂路径上的空指针解引用。
8. CI 集成
# GitHub Actions
- name: Static analysis
run: |
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
run-clang-tidy -p build -j$(nproc) -warnings-as-errors '*'
- name: cppcheck
run: |
cppcheck --enable=warning,performance \
--suppress=missingIncludeSystem \
--error-exitcode=1 \
src/
有关 clang-tidy 检查项的详细信息,请参阅 references/clang-tidy-checks.md。
相关技能
- 使用
skills/compilers/clang了解 Clang 工具链和诊断标志 - 使用
skills/compilers/gcc将 GCC 警告作为补充分析 - 使用
skills/runtimes/sanitizers配合静态分析检测运行时错误 - 使用
skills/build-systems/cmake设置CMAKE_EXPORT_COMPILE_COMMANDS
评论
支持 Markdown