cpp-clang-tidy
Skill8 starsChanged 17 days agoApache-2.0View on GitHub
使用 clang-tidy 基于 Clang AST 对 C/C++ 代码做静态分析,支持规则筛选与自动修复。适用于 C/C++ 项目规范检查、现代化改造、编译期缺陷发现。
In this repository32 other files it gives its agents.
- AGENTS.mdAGENTS.md
- ast-grep-blockSkill
- code-deconstructSkill
- code-detect-dupSkill
- code-detect-problemSkill
- code-part-modificationSkill
- code-refactorSkill
- code-reviewSkill
- cpp-checkSkill
- cpp-tscancodeSkill
- full-analysisSkill
- gdb-heap-analysisSkill
- java-asprofSkill
- java-check-pmdSkill
- java-check-spotbugsSkill
- java-coverageSkill
- java-envSkill
- java-g2mSkill
- jmh-benchSkill
- lets-loopSkill
- long-term-taskSkill
- md2pdfSkill
- merge-agents-mdSkill
- qualitative-assessmentSkill
- requirement-collectSkill
- rust-clippySkill
- rust-flamegraphSkill
- shell-checkSkill
- sql-extractSkill
- sync-dev-specSkill
- sync-repo-docsSkill
- vbox-test-envSkill
The file
Files
Clang-Tidy C/C++ 静态分析技能
本文件中的
templates/...均指本技能目录下的templates/子目录。
工具位置
CLANG_TIDY=/usr/bin/clang-tidy
# 版本: Debian LLVM 19.1.7前置条件
需要 compile_commands.json 编译数据库:
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -B build # CMake
bear -- make # 非 CMake (bear)
compiledb make # 非 CMake (compiledb)执行流程
步骤1:确定路径
SRC_DIR=src
COMPILE_DB=build步骤2:执行分析
clang-tidy src/main.cpp -- -Iinclude # 单文件
clang-tidy -p $COMPILE_DB src/main.cpp # 使用编译数据库
run-clang-tidy -p $COMPILE_DB # 批量检查(推荐)
find $SRC_DIR -name '*.cpp' -o -name '*.c' | \
xargs clang-tidy -p $COMPILE_DB # 手动批量步骤3:自动修复
clang-tidy --fix src/main.cpp -- -Iinclude # 自动修复
clang-tidy --fix-errors src/main.cpp -- -Iinclude # 修复含 error
find $SRC_DIR -name '*.cpp' | \
xargs -I{} clang-tidy --fix-errors {} -p $COMPILE_DB # 批量修复步骤4:输出格式
clang-tidy src/main.cpp -- -Iinclude # 文本(默认)
clang-tidy --format-style=json src/main.cpp -- -Iinclude # JSON
clang-tidy --export-fixes=tidy-fixes.yaml src/main.cpp # YAML 修复建议步骤5:指定检查规则
clang-tidy --checks='bugprone-*,security-*' src/main.cpp # 仅安全和 bugprone
clang-tidy --checks='*,-readability-*,-llvm-*' src/main.cpp # 排除特定规则常用检查规则
| 类别 | 前缀 | 描述 |
| 正确性 | clang-diagnostic-* / clang-analyzer-* | 编译器警告、静态分析 |
| 现代 C++ | modernize-* | 现代 C++ 用法建议 |
| 性能 | performance-* | 性能优化 |
| 可读性 | readability-* | 代码可读性 |
| 安全 | bugprone-* / security-* | 常见错误、安全漏洞 |
| 并发 | concurrency-* | 死锁、竞态条件 |
| 规范 | cert-* / misra-* / google-* | CERT/MISRA/Google 规范 |
配置与集成
- 项目配置:
templates/dot-clang-tidy.yaml→ 复制到项目根目录为.clang-tidy - CI 集成:
templates/ci-github-actions.yaml - CMake 集成:
templates/cmake-integration.cmake - Makefile 集成:
templates/Makefile-tidy
分析结果处理
# 统计问题类型
clang-tidy --format-style=json src/main.cpp -- -Iinclude | \
jq -r '.[].DiagnosticName' | sort | uniq -c | sort -nr
# 提取高危问题
clang-tidy src/main.cpp -- -Iinclude 2>&1 | grep -E '(error|warning):'输出报告模板:templates/report.md
与 Cppcheck 对比
| 特性 | Clang-Tidy | Cppcheck |
| 分析引擎 | Clang AST | 自定义解析器 |
| 编译数据库 | 必需 | 可选 |
| 自动修复 | 支持 | 不支持 |
| 检查规则数 | 500+ | 200+ |
| 分析速度 | 较慢 | 快 |
最佳实践
- 使用
compile_commands.json确保精度 - 项目级
.clang-tidy统一规范 - 渐进式采用:从
bugprone-*,performance-*,modernize-*开始 --fix优先处理可自动修复项,修复后务必编译验证- CI 集成,定期更新
注意事项
- 无编译数据库时需手动指定
-I参数 - 大型项目用
run-clang-tidy -j N并行 - 误报用
// NOLINT或.clang-tidy抑制 --fix可能不完整,修复后需编译验证
技能版本: 1.0.0 | 工具版本: Clang-Tidy 19.1.7 | 适用: C/C++ (需 compile_commands.json)
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

