Skip to content

feat: enforce Google-based clang-format style and add style checker#156

Open
chen2021673 wants to merge 1 commit into
masterfrom
format
Open

feat: enforce Google-based clang-format style and add style checker#156
chen2021673 wants to merge 1 commit into
masterfrom
format

Conversation

@chen2021673
Copy link
Copy Markdown
Contributor

@chen2021673 chen2021673 commented May 21, 2026

Summary

将 .clang-format 基础风格从 LLVM 切换为 Google,并增加项目定制规则(include 分组排序、指针右对齐、强制大括号等)
新增 scripts/style_check.py 自定义风格检查脚本,覆盖 CJK 字符检测、thread_local 命名规范等 clang-format 无法覆盖的规则
CI workflow 增加 style check 步骤,并以 advisory 模式集成 IWYU (include-what-you-use) 检查
对现有 29 个源文件按新规则进行格式化(主要是 include 重新分组排序)

Switch .clang-format from LLVM to Google base style with project-specific
rules (include regrouping, pointer alignment, brace insertion). Add
scripts/style_check.py for additional style enforcement and integrate it
into the CI format-check workflow.
#include <nccl.h>
#include <vector>

#include <nccl.h>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c 语言系统库头文件应当放在 c++标准库头文件上一个分组。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NCCL 是第三方通信库,不是 c 语言系统库吧

@@ -1,3 +1,5 @@
#include "infini_train/include/autograd/linear.h"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不是配套头文件,不需要放首行,以及理论上 kernel 层不应当引用 autograd 层头文件。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不需要引用这个头文件,删掉了错误引用及添加规则。但目前头文件的匹配检查机制还是根据后缀来的,是clang-format默认行为。

#include <memory>
#include <tuple>

#include <fcntl.h>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c 语言系统库头文件,位置不对。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread scripts/format.py
class _NoColor:
BLACK = RED = GREEN = YELLOW = BLUE = MAGENTA = CYAN = WHITE = RESET_ALL = ""

Fore = Style = _NoColor()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里没必要这么改吧

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done,删除

Comment thread .clang-format
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件不要动。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我感觉其他不动,关于头文件顺序检查可以保留。但我看头文件检测也都是简单的按规则匹配,有可能出现错误(文件名相同但并不是对应头文件)或后续需要更新(比如添加third_party)。

Comment thread CMakeLists.txt
add_executable(test_maca_allocator test/runtime/test_maca_allocator.cc)
link_infini_train_exe(test_maca_allocator)
endif()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

去掉。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@kilinchange
Copy link
Copy Markdown
Collaborator

  1. 如果希望自动做一些 code style 的检查,建议调研 clang-tidy 和相关规则(https://clang.llvm.org/extra/clang-tidy/ ),理论上 clang-tidy 能做的事情就没必要写 .py 脚本查,做不了的部分再讨论怎么处理;
  2. clang-format 部分涉及到一些历史遗留问题,我近期统一做修改。

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