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

clang-format能否为函数调用括号前空格设置例外规则?

clang-format 对特定函数空格规则的支持

clang-format 完全支持你描述的这种代码风格,可通过两种配置方式实现:

方式一:针对特定函数名设置例外

通过自定义括号前空格规则,为指定函数名添加例外:

  • 先将全局的 SpaceBeforeParens 设为 Custom
  • 开启 AfterFunctionName 选项,确保普通函数名与左括号间保留空格
  • 在 Excepting 字段中列出不需要保留空格的函数名,比如 _、C_

对应的配置片段示例:

SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
  AfterFunctionName: true
  Excepting:
    - "_"
    - "C_"

方式二:按函数名长度自动适配

利用 ShortFunctionNames 选项,对短函数名(1-2字符)自动取消空格:
同样基于 SpaceBeforeParens: Custom,开启该选项后,clang-format 会自动识别单字符(如_)、双字符(如C_)的函数名,跳过函数名与左括号间的空格。

配置示例:

SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
  AfterFunctionName: true
  ShortFunctionNames: true

经过上述任意一种配置后,你的示例代码会被格式化符合要求的样式:

return g_strdup (_("24-hour"));

label = g_strdup_printf (C_("timezone desc", "%s (%s)"), ...

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 05:22:03