Skip to content

ci: integrate iwyu checks - #240

Open
kilinchange wants to merge 1 commit into
masterfrom
style/iwyu
Open

kilinchange wants to merge 1 commit into
masterfrom
style/iwyu

Conversation

@kilinchange

@kilinchange kilinchange commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

背景

引入 Include What You Use(IWYU)检查,用于规范 C/C++ 代码中的头文件依赖,减少隐式依赖和不必要的 include,提升代码依赖关系的可维护性。

主要改动

  • 新增 IWYU GitHub Actions workflow:

    • 基于 CPU-only 配置生成 compile_commands.json;
    • 对项目中的 C/C++ translation unit 执行 IWYU 检查;
    • 将 IWYU 检查结果作为 CI 门禁。
  • 新增 scripts/iwyu.py:

    • 支持全量检查项目 translation unit;
    • 支持基于 Git ref 或暂存区改动进行本地增量检查;
    • 支持 --fix-dry-run 预览自动修复结果;
    • 支持 --fix 调用 IWYU 修复工具自动调整 include;
    • 自动过滤 third-party、build 目录及预期编译失败测试等无需检查的文件。
  • 新增 scripts/iwyu.imp:

    • 补充项目使用到的 Eigen 和 libstdc++ header mapping;
    • 将当前使用的 Eigen dense 相关模块统一映射到 Eigen/Dense;
    • 将部分 libstdc++ 内部实现头文件和符号映射到对应的标准公共头文件。
  • 新增 docs/iwyu.md:

    • 说明 IWYU 的检查范围、mapping 策略、本地使用方式、自动修复方式及已知限制。

检查范围

CI 使用 CPU-only 编译配置,关闭 CUDA、NCCL 和 OpenMP 等非必要依赖,并基于生成的 compilation database 执行 IWYU。

当前主要检查以下目录中的 C/C++ translation unit:

  • infini_train
  • example
  • tests
  • tools

CUDA translation unit 暂不纳入 IWYU 检查。

头文件不会单独作为 translation unit 执行 IWYU,而是通过引用它们的源文件间接完成检查。

third-party 源码不会作为检查目标,但项目代码引用 third-party header 时,IWYU 仍可能在分析过程中解析相关头文件。

自动修复

CI 仅执行 IWYU 检查,不自动修改源码。

本地可通过以下命令预览自动修复结果:

python3 scripts/iwyu.py --all --fix-dry-run --build-dir build/lint

@kilinchange
kilinchange force-pushed the style/iwyu branch 4 times, most recently from 82d9600 to b2cf6e1 Compare September 24, 2026 06:44
@kilinchange
kilinchange force-pushed the style/iwyu branch 2 times, most recently from 7a0e165 to f7f3075 Compare September 24, 2026 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants