You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让clang-format格式化块注释时在星号后添加空格?

解决clang-format块注释换行后*后缺失空格的问题

我们已经配置clang-format,让C代码中的块注释超出最大行长度时自动换行,但遇到了一个问题:

原块注释:

/* This comment is longer than the allowed
 * line length.
 */

被格式化后变成:

/* This comment is longer than the
 *allowed line length.
 */

可见换行后的*与后续文本之间缺少了空格,且已知SpacesInLineCommentPrefix仅对//行注释生效,不适用于块注释。

解决方案

  1. 确认clang-format版本为LLVM 11及以上,旧版本对块注释格式化的支持存在缺陷,可能导致此类问题。
  2. 在项目的.clang-format配置文件中添加或修改以下配置项:
    # 强制块注释每行开头使用*,且*后自动添加空格
    BlockCommentStyle: Star
    # 启用注释自动修复功能
    FixComments: true
    # 设置最大行长度,触发注释换行逻辑(数值可按需调整)
    ColumnLimit: 80
    

配置完成后重新运行clang-format,块注释的格式化结果会变为:

/* This comment is longer than the
 * allowed line length.
 */

内容的提问来源于stack exchange,提问作者Eph

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 17:42:03