Skip to content

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

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

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)

配置与格式化示例分为两个提交:

  • 第一个提交仅修改 .clang-format;AlignConsecutiveDeclarations.Enabled 保持 false,不添加 AlignFunctionPointers。局部变量、typedef、函数指针和函数参数内部不做声明列对齐;函数参数换行仍按现有规则缩进。
  • 开启 AlignConsecutiveAssignments.Enabled,赋值号对齐不跨空行或独立注释分组;AlignTrailingComments.Kind 设为 Always,OverEmptyLines 设为 0,尾部注释只在连续分组内对齐。
  • 将 AllowShortFunctionsOnASingleLine、AllowShortLambdasOnASingleLine 改为 None,关闭 AllowShortLoopsOnASingleLine;开启 BraceWrapping.AfterExternBlock、BeforeLambdaBody、BeforeWhile,展开相应语句块。
  • 将 IndentPPDirectives 改为 BeforeHash,沿用 PPIndentWidth: 4,按预处理嵌套层级缩进 #。
  • 将 MaxEmptyLinesToKeep 从 2 改为 1,连续空行最多保留一行。
  • 第二个提交仅使用上述配置格式化 src/thread.c。现有 InsertBraces: true 同时为两处单语句 if 补充大括号,已检查语义不变。

变量声明效果:

struct rt_thread *thread;
rt_base_t critical_level;
int err;

预处理指令效果(头文件保护符由 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);

需要继续讨论的部分:

  • 预处理缩进最终采用本 PR 的 BeforeHash,还是保持 # 顶格、只缩进指令文字的 AfterHash。
  • index ++ 仍未实现:clang-format 18.1.8 和 23.1.3 实测都会输出 index++,没有对应配置项,需另行确定工具扩展或独立处理方式。
  • 格式检查 CI 通过 pip 安装 clang-format,手动格式化工作流通过 apt 安装,当前未统一固定版本;建议后续统一版本以保持结果可复现。

指针已经使用 PointerAlignment: Right;宏、位域和表达式已有对齐配置,继续沿用。行宽、include 顺序和行尾策略保持现状。已有第三方目录的 DisableFormat 和忽略机制继续生效,第三方例外的完整覆盖另行梳理。上游格式检查已经按 PR 改动行运行,编码规范也已有 clang-format 命令行示例;astyle 遗留入口的清理另行处理。

验证:src/thread.c 通过 clang-format 18.1.8、23.1.3 的 --dry-run --Werror 检查;两个版本从原始文件格式化得到的结果均与提交内容一致。本轮调整相对前次格式化结果仅改变空白字符,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-14 10:18 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。

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

@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

@CYFS3
CYFS3 marked this pull request as ready for review October 12, 2026 06:34
Comment thread src/thread.c Outdated
{
struct rt_thread *thread;
rt_base_t critical_level;
rt_base_t critical_level;

Copy link
Copy Markdown
Member

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.

这个和上面的变量对齐了,那就改为不对齐吗?

Copy link
Copy Markdown
Member

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.

已修改,但是这个得做取舍
image

@CYFS3
CYFS3 force-pushed the style/clang-format-discussion-11653 branch from 43c2dc5 to 9dbc6bb Compare October 14, 2026 01:56

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.

3 participants