agentleFS
hellyguohellyguo/self-ai-spec/skills/cpp-clang-tidy/SKILL.md

cpp-clang-tidy

Skill8 starsChanged 17 days agoApache-2.0View on GitHub

使用 clang-tidy 基于 Clang AST 对 C/C++ 代码做静态分析,支持规则筛选与自动修复。适用于 C/C++ 项目规范检查、现代化改造、编译期缺陷发现。

The file

Files
SKILL.md

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-TidyCppcheck
分析引擎Clang AST自定义解析器
编译数据库必需可选
自动修复支持不支持
检查规则数500+200+
分析速度较慢快

最佳实践

  1. 使用 compile_commands.json 确保精度
  2. 项目级 .clang-tidy 统一规范
  3. 渐进式采用:从 bugprone-*,performance-*,modernize-* 开始
  4. --fix 优先处理可自动修复项,修复后务必编译验证
  5. CI 集成,定期更新

注意事项

  • 无编译数据库时需手动指定 -I 参数
  • 大型项目用 run-clang-tidy -j N 并行
  • 误报用 // NOLINT 或 .clang-tidy 抑制
  • --fix 可能不完整,修复后需编译验证

技能版本: 1.0.0 | 工具版本: Clang-Tidy 19.1.7 | 适用: C/C++ (需 compile_commands.json)

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.