mcpField
I
include-what-you-use

include-what-you-use

github.com/mohitmishra786開発269495 回インストール18 週間前に更新
リポジトリ

include-what-you-use について

include-what-you-useは、IWYUでC/C++のインクルードを管理するエージェント向けのスキルです。レポートの読み方、自動修正、前方宣言、マッピングファイル、CMake連携を扱います。

主な機能

  1. 01ソースファイルやビルドでIWYUを実行
  2. 02インクルード変更の提案を解釈
  3. 03fix_includeによる修正の適用
  4. 04前方宣言とヘッダーの使い分け
  5. 05マッピングファイルとCMakeの設定

活用シーン

  1. 01未使用のインクルードを削除する
  2. 02直接依存するヘッダーの不足を特定する
  3. 03コンパイルの連鎖を減らす
  4. 04CMakeビルドにインクルード検査を組み込む

インストール

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

$npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill include-what-you-use

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

詳細

使用するものだけをインクルードする(IWYU)

目的

不要な #include ディレクティブを減らすために IWYU を使用する方法、IWYU のレポートやマッピングファイルの解釈方法、前方宣言と完全なインクルードのどちらを選ぶべきか、また、大規模なコードベースでコンパイルの連鎖を減らすために IWYU を CMake ビルドに統合する方法をエージェントに案内します。

トリガー

  • 「include-what-you-use の使い方を教えてください」
  • 「インクルードを修正して C++ のコンパイル時間を短縮するにはどうすればよいですか?」
  • 「IWYU の出力をどう解釈すればよいですか?」
  • 「前方宣言とインクルードのどちらを使うべきですか?」
  • 「IWYU を CMake に統合するにはどうすればよいですか?」
  • 「コンパイルの連鎖とは何ですか? また、それを避けるにはどうすればよいですか?」

ワークフロー

1. IWYU をインストールして実行する
# Install
apt-get install iwyu              # Ubuntu/Debian
brew install include-what-you-use # macOS

# Run on a single file
iwyu -Xiwyu --error main.cpp 2>&1

# Run via compile_commands.json
iwyu_tool.py -p build/ src/main.cpp 2>&1 | tee iwyu.log

# Run on entire project
iwyu_tool.py -p build/ 2>&1 | tee iwyu.log
2. CMake との統合
# CMakeLists.txt — use IWYU as include checker during build
find_program(IWYU_PROGRAM NAMES include-what-you-use iwyu)
if(IWYU_PROGRAM)
    set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE
        ${IWYU_PROGRAM}
        -Xiwyu --mapping_file=${CMAKE_SOURCE_DIR}/iwyu.imp
        -Xiwyu --no_comments
    )
endif()
# Build with IWYU analysis
cmake -S . -B build -DCMAKE_CXX_COMPILER=clang++
cmake --build build 2>&1 | tee iwyu.log
3. IWYU の出力を解釈する
main.cpp should add these lines:
#include <string>   // for std::string
#include "mylib/widget.h"  // for Widget

main.cpp should remove these lines:
- #include <vector>  // lines 5-5
- #include "internal/detail.h"  // lines 8-8

The full include-list for main.cpp:
#include <iostream>  // for std::cout
#include <string>    // for std::string
#include "mylib/widget.h"  // for Widget
---

出力の読み方:

  • should add:使用されているシンボルを提供しているものの、まだインクルードされていないヘッダー
  • should remove:インクルードされているが、そのシンボルが直接使用されていないヘッダー
  • The full include-list:最終的なヘッダー一覧の内容
4. IWYU の修正を自動的に適用する
# fix_include script (comes with IWYU)
fix_include < iwyu.log

# Options
fix_include --nosafe_headers < iwyu.log    # more aggressive — also removes system headers
fix_include --comments < iwyu.log          # preserve // comments in includes
fix_include --dry_run < iwyu.log           # preview changes without applying

# Limit to specific files
fix_include --only_re='src/.*\.cpp' < iwyu.log

# Run and apply in one pipeline
iwyu_tool.py -p build/ 2>&1 | fix_include
5. 前方宣言と完全なインクルード

型全体が必要ない場合、IWYU は前方宣言(定義を伴わないクラス/構造体の宣言)を優先します。

// full_include.h — DON'T include this if only a pointer is used
#include "widget.h"      // full definition: Widget members, vtable, etc.

// forward_decl.h — OK when Widget* or Widget& is sufficient
class Widget;            // forward declaration
void process(Widget *w); // pointer: forward decl is enough

// When forward declaration is sufficient:
// - Pointer or reference parameter: Widget*, Widget&
// - Return type as pointer: Widget*
// - Base class declared elsewhere (but defined in .cpp)

// When full include is required:
// - Inheriting from Widget: class MyWidget : public Widget
// - Accessing Widget members: w.field, w.method()
// - Creating Widget instances: Widget w;
// - Sizeof(Widget)
// - Template instantiation: std::vector<Widget>
// IWYU-friendly header
#pragma once
class Widget;            // forward declare (saves downstream compilation)

class Container {
    Widget *head_;       // pointer: forward decl is enough
public:
    void add(Widget *w);
    Widget *get(int idx);
};
// Container.cpp includes "widget.h" — only .cpp pays the compile cost
6. サードパーティ製ヘッダー用のマッピングファイル

IWYU のマッピングファイルを使うと、間接的なインクルード(#include <vector> が内部 STL ヘッダーによって提供される場合など)について IWYU に教えられます。

# iwyu.imp — IWYU mapping file
[
  # Map internal LLVM headers to public ones
  { "include": ["<llvm/ADT/StringRef.h>", "private",
                 "<llvm/ADT/StringRef.h>", "public"] },

  # Map system headers to POSIX equivalents
  { "include": ["<bits/types.h>", "private", "<sys/types.h>", "public"] },
  { "include": ["<bits/socket.h>", "private", "<sys/socket.h>", "public"] },

  # Symbol → header mappings
  { "symbol": ["std::string", "private", "<string>", "public"] },
  { "symbol": ["NULL",        "private", "<cstddef>", "public"] },
]
# Use mapping file
iwyu -Xiwyu --mapping_file=iwyu.imp main.cpp

# IWYU ships with common mappings
ls /usr/share/include-what-you-use/
# gcc.stl.headers.imp, boost-1.62.imp, libcxx.imp, etc.

iwyu -Xiwyu --mapping_file=/usr/share/include-what-you-use/gcc.stl.headers.imp
7. IWYU が行わないこと

IWYU には限界があるため、注意してください。

# IWYU may give wrong advice for:
# - Macros from headers (hard to track)
# - Template specializations in included headers
# - Headers required for correct ODR linking

# Safe iterative workflow:
# 1. Run IWYU
# 2. Apply fixes with fix_include
# 3. Rebuild and run tests
# 4. Revert any changes that break the build
# 5. Repeat until clean

# Check for compilation cascade: how many TUs include a header
grep -rl '#include "expensive.h"' src/ | wc -l

関連スキル

  • ccache などのコンパイル高速化手法については、skills/build-systems/build-acceleration を使用してください
  • ビルド解析ツールを CMake に統合する方法については、skills/build-systems/cmake を使用してください
  • インクルードの肥大化に対する長期的な解決策として C++20 モジュールを使用するには、skills/compilers/cpp-modules を使用してください

コメント