Static Analysis

mohitmishra786/low-level-dev-skills/skills/build-systems/static-analysis

作者 mohitmishra786bdc58472fa9f无许可证253 个星标收录于 2026年10月9日更新于 2026年10月9日仓库3个月前更新

Static analysis skill for C/C++ codebases. Use when hardening code quality, triaging noisy builds, running clang-tidy, cppcheck, or scan-build, interpreting check categories, suppressing false positives, or integrating static analysis into CI. Activates on queries about clang-tidy checks, cppcheck, scan-build, compile_commands.json, code hardening, or static analysis warnings.

AI 生成的概览

指导代理运行和分诊 C/C++ 静态分析工具,如 clang-tidy、cppcheck 和 scan-build。

功能
该技能提供针对 C/C++ 代码库选择、运行和分诊静态分析工具的说明,涵盖 clang-tidy、cppcheck 和 scan-build。内容包括生成 compile_commands.json、选择检查类别、编写 .clang-tidy 配置、抑制误报,以及将分析集成到 CI。它还附带一份关于 clang-tidy 检查的参考文件。
适用场景
适用于提升 C/C++ 代码质量、处理静态分析告警过多的构建,或配置 clang-tidy、cppcheck、scan-build 的场景。也适合关于检查类别、误报抑制、compile_commands.json 或将静态分析加入 CI 的问题。
运行要求
不附带脚本,仅为说明文档。运行所述工具需要安装 clang-tidy、cppcheck 和 scan-build,并需要 compile_commands.json 等编译数据库。

Static Analysis

Purpose

Guide agents through selecting, running, and triaging static analysis tools for C/C++ — clang-tidy, cppcheck, and scan-build — including suppression strategies and CI integration.

Triggers

  • "How do I run clang-tidy on my project?"
  • "What clang-tidy checks should I enable?"
  • "cppcheck is reporting false positives — how do I suppress them?"
  • "How do I set up scan-build for deeper analysis?"
  • "My build is noisy with static analysis warnings"
  • "How do I generate compile_commands.json for clang-tidy?"

Workflow

1. Generate compile_commands.json

clang-tidy requires a compilation database:

bash
# CMake (preferred)cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ONln -s build/compile_commands.json .
# Bear (for Make-based projects)bear -- make
# compiledb (alternative for Make)pip install compiledbcompiledb make

2. Run clang-tidy

bash
# Single fileclang-tidy src/foo.c -- -std=c11 -I include/
# Whole project via compile_commands.jsonrun-clang-tidy -p build/ -j$(nproc)
# With specific checks enabledclang-tidy -checks='bugprone-*,modernize-*,performance-*' src/foo.cpp
# Apply auto-fixesclang-tidy -checks='modernize-use-nullptr' -fix src/foo.cpp

3. Check category decision tree

text
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-*
CategoryKey checksWhat it catches
bugprone-*use-after-move, integer-division, suspicious-memset-usageLikely bugs
modernize-*use-nullptr, use-override, use-autoC++11/14/17 idioms
cppcoreguidelines-*avoid-goto, pro-bounds-*, no-mallocC++ Core Guidelines
performance-*unnecessary-copy-initialization, avoid-endlPerformance regressions
clang-analyzer-*core.*, unix.*, security.*Path-sensitive bugs
cert-*err34-c, str51-cppCERT coding standard

4. .clang-tidy configuration file

yaml
# .clang-tidy — place at project rootChecks: >  bugprone-*,  modernize-*,  performance-*,  -modernize-use-trailing-return-type,  -bugprone-easily-swappable-parametersWarningsAsErrors: 'bugprone-*,clang-analyzer-*'HeaderFilterRegex: '^(src|include)/.*'CheckOptions:  - key: modernize-loop-convert.MinConfidence    value: reasonable  - key: readability-identifier-naming.VariableCase    value: camelCase

5. Suppress false positives

cpp
// Suppress a single lineint 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() { /* ... */ }

Or in .clang-tidy:

yaml
# Exclude third-party directoriesHeaderFilterRegex: '^(src|include)/.*'# Disable specific checksChecks: '-bugprone-easily-swappable-parameters'

6. Run cppcheck

bash
# Basic runcppcheck --enable=all --std=c11 src/
# With compile_commands.jsoncppcheck --project=build/compile_commands.json
# Include specific checks and suppress noisecppcheck --enable=warning,performance,portability \         --suppress=missingIncludeSystem \         --suppress=unmatchedSuppression \         --error-exitcode=1 \         src/
# Generate XML report for CIcppcheck --xml --xml-version=2 src/ 2> cppcheck-report.xml
--enable= valueWhat it checks
warningUndefined behaviour, bad practices
performanceRedundant operations, inefficient patterns
portabilityNon-portable constructs
informationConfiguration and usage notes
allEverything above

7. Path-sensitive analysis with scan-build

bash
# Intercept a Make buildscan-build make
# Intercept CMake buildscan-build cmake --build build/
# Show HTML reportscan-view /tmp/scan-build-*/
# With specific checkersscan-build -enable-checker security.insecureAPI.gets \           -enable-checker alpha.unix.cstring.BufferOverlap \           make

scan-build finds deeper bugs than clang-tidy: use-after-free across functions, dead stores from logic errors, null dereferences on complex paths.

8. CI integration

yaml
# 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/

For clang-tidy check details, see references/clang-tidy-checks.md [blocked].

Related skills

  • Use skills/compilers/clang for Clang toolchain and diagnostic flags
  • Use skills/compilers/gcc for GCC warnings as complementary analysis
  • Use skills/runtimes/sanitizers for runtime bug detection alongside static analysis
  • Use skills/build-systems/cmake for CMAKE_EXPORT_COMPILE_COMMANDS setup

来源与署名

来源:mohitmishra786/low-level-dev-skills位于skills/build-systems/static-analysis提交bdc5847

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架