mcpField
S
static-analysis

static-analysis

github.com/mohitmishra786テスト・品質2691.2k 回インストール18 週間前に更新
リポジトリ

static-analysis について

C/C++ の静的解析ツールを選び、実行し、結果を整理するためのエージェント向けガイドです。clang-tidy、cppcheck、scan-buildに加え、警告の抑制やCI設定を扱います。

主な機能

  1. 01compile_commands.json の生成
  2. 02clang-tidy の設定と実行
  3. 03目的に応じたチェック選択
  4. 04cppcheck と scan-build の実行
  5. 05誤検知の抑制と CI 設定

活用シーン

  1. 01CMake プロジェクトで clang-tidy を設定
  2. 02cppcheck の誤検知を整理
  3. 03CI で静的解析を実行

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill static-analysis

インストール後、Claude Code で/static-analysisを入力して読み込まれたか確認します。

同梱ファイルreferences/clang-tidy-checks.md

詳細

静的解析

目的

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-autoC++11/14/17 の慣用的な書き方
cppcoreguidelines-*avoid-goto、pro-bounds-*、no-mallocC++ Core Guidelines に関する問題
performance-*unnecessary-copy-initialization、avoid-endlパフォーマンスの低下
clang-analyzer-*core.*、unix.*、security.*パス依存のバグ
cert-*err34-c、str51-cppCERT コーディング標準に関する問題
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 よりも詳細なバグを検出します。たとえば、関数をまたぐ use-after-free、ロジックエラーによるデッドストア、複雑なパスでのヌルポインター参照などです。

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 を参照してください。

関連スキル

  • Clang ツールチェーンと診断フラグについては、skills/compilers/clang を使用してください
  • 補完的な解析として GCC の警告を確認するには、skills/compilers/gcc を使用してください
  • 静的解析と併せて実行時のバグを検出するには、skills/runtimes/sanitizers を使用してください
  • CMAKE_EXPORT_COMPILE_COMMANDS の設定については、skills/build-systems/cmake を使用してください

コメント