Skip to content

[clang-format] Refine style rules with a thread.c formatting example - #11859

Draft
CYFS3 wants to merge 2 commits into
RT-Thread:masterfrom
CYFS3:style/clang-format-discussion-11653
Draft

CYFS3 wants to merge 2 commits into
RT-Thread:masterfrom
CYFS3:style/clang-format-discussion-11653

Conversation

@CYFS3

@CYFS3 CYFS3 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

关联 #11653

当前配置仍关闭连续声明和赋值对齐,并允许短函数、lambda、extern 块和 do-while 保留部分紧凑写法。本 PR 作为讨论用草稿,调整根目录 .clang-format,并以独立提交格式化 src/thread.c,展示新配置在核心代码中的实际效果。

参考评论:

你的解决方案是什么 (what is your solution)

本次配置修改:

  • 开启 AlignConsecutiveDeclarations.Enabled、AlignConsecutiveDeclarations.AlignFunctionPointers 和 AlignConsecutiveAssignments.Enabled。声明和赋值不跨空行、独立注释分组对齐。
  • 将 AlignTrailingComments.Kind 改为 Always,OverEmptyLines 改为 0,尾部注释只在连续分组内对齐。
  • 将 AllowShortFunctionsOnASingleLine、AllowShortLambdasOnASingleLine 改为 None,关闭 AllowShortLoopsOnASingleLine;开启 BraceWrapping.AfterExternBlock、BeforeLambdaBody、BeforeWhile,展开相应语句块。
  • 将 IndentPPDirectives 改为 BeforeHash,沿用 PPIndentWidth: 4,按预处理嵌套层级缩进 #。
  • 将 MaxEmptyLinesToKeep 从 2 改为 1,连续空行最多保留一行。

另外新增一个独立提交,仅使用 clang-format 18.1.8 和本 PR 的根配置格式化 src/thread.c,可单独查看该提交的差异,观察参数/局部变量、赋值、预处理指令和 switch 排版效果。现有 InsertBraces: true 同时为两处单语句 if 补充大括号,已检查语义不变。该文件通过 clang-format 18.1.8、23.1.3 的 --dry-run --Werror 和 git diff --check 检查,未编译整个工程。

以下是样例经过 clang-format 后的实际输出节选。

声明和赋值对齐会同时影响函数参数、局部变量、typedef 和函数指针。例如:

static rt_err_t _thread_init(struct rt_thread *thread,
                             const char       *name,
                             void              (*entry)(void *parameter),
                             void             *parameter,
                             rt_uint32_t       stack_size)
{
    rt_base_t critical_level;
    int       err;
    void      (*callback)(void *parameter);

    critical_level  = 0;
    err             = 1;
    err            += 2;
    critical_level += 1;

预处理指令先采用 BeforeHash,效果如下(头文件保护符由 clang-format 单独识别):

#if defined(RT_USING_DEVICE)
    #define VALUE 1
    #ifdef RT_USING_CONSOLE
        #define OTHER_VALUE 2
    #endif
#endif

do-while 的右大括号与 while 分行:

do
{
    count--;
}
while (count);

需要继续讨论的部分:

  • 是否接受声明全局对齐带来的局部变量、typedef 和函数指针空格填充。这不是“仅对齐函数参数”的配置。
  • 预处理缩进最终采用本 PR 的 BeforeHash,还是保持 # 顶格、只缩进指令文字的 AfterHash。
  • index ++ 仍未实现:clang-format 18.1.8 和 23.1.3 实测都会输出 index++,没有对应配置项。本 PR 不将此行为视为符合该条规范,需另行确定工具扩展或独立处理方式。
  • 格式检查 CI 通过 pip 安装 clang-format,手动格式化工作流通过 apt 安装,当前未统一固定版本;建议后续统一版本以保持结果可复现。

指针已经使用 PointerAlignment: Right;宏、位域和表达式已有对齐配置,继续沿用。行宽、include 顺序和行尾策略保持现状。已有第三方目录的 DisableFormat 和忽略机制继续生效,第三方例外的完整覆盖另行梳理。

上游格式检查已经按 PR 改动行运行,编码规范也已有 clang-format 命令行示例;tools/as.sh 等 astyle 遗留入口的清理另行处理。

验证:使用 clang-format 18.1.8、23.1.3 对 C/C++/头文件样例执行格式化、重复格式化一致性检查及 --dry-run --Werror,覆盖声明/赋值/注释对齐、空函数、lambda、extern、循环、嵌套预处理指令、指针和连续空行。另验证 lwIP 的 DisableFormat 仍生效,git diff --check 通过。本次未编译整个工程。

请提供验证的bsp和config (provide the config and bsp)

  • BSP:
  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用clang-format 源码格式化工具确保格式符合RT-Thread代码规范 This PR has been formatted with clang-format and complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • Use workflow from 保持默认分支(通常为 master)
    Keep the default branch (usually master) in Use workflow from
  • 在 branch 输入框填写 PR 分支 style/clang-format-discussion-11653
    Enter PR branch style/clang-format-discussion-11653 in the branch field
  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将作为独立提交推送至你的分支。
    The formatting changes will be pushed to your branch as a separate commit.

完成后,提交将自动更新至 style/clang-format-discussion-11653 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the style/clang-format-discussion-11653 branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@CYFS3 CYFS3 changed the title [clang-format] Refine alignment, brace wrapping and spacing rules [clang-format] Refine style rules with a thread.c formatting example Oct 9, 2026
@github-actions github-actions Bot added the Kernel PR has src relate code label Oct 9, 2026
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: kernel

Reviewers: @GorrayLi @ReviewSun @hamburger-os @lianux-mm @wdfk-prog @xu18838022837

Changed Files (Click to expand)
  • src/thread.c

📊 Current Review Status (Last Updated: 2026-10-09 10:36 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@CYFS3

CYFS3 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

@BernardXiong @Rbb666 @wdfk-prog
我调整了部分的参数,然后追加了一笔commit,可以看到目前的效果
这个格式化的内容,还是需要尽快收敛,这样也方便主仓后续的审核,因为目前很多个pr都需要追加格式化commit,有点影响log日志。如果这个格式化内容能尽快确定,然后把仓库全格式化一遍。新pr的内容就算有先clangformat也比较好review。

@wdfk-prog wdfk-prog left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Kernel PR has src relate code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants