【问题标题】:clang-format indents function arguments/parameters always with 4 spacesclang-format 缩进函数参数/参数总是有 4 个空格
【发布时间】:2025-11-27 16:20:03
【问题描述】:

我在使用 clang 格式的结果时遇到了一些问题。顺便说一句,我使用的是 v3.8.0.246435。

考虑以下代码示例:

if(foo)
{
   bar();
   foobar(
      arg1,
      arg2,
      arg3,
      arg4,
      arg5,
      arg6);
}

在上面的代码中,所有内容都缩进了 3 个空格。 现在,如果我在此代码上运行 clang-format(有关我的 clang-format 配置,请参阅我的帖子底部),我会得到以下输出:

if(foo)
{
/*V 3 spaces here */
   bar();
   foobar(
       arg1,
       arg2,
       arg3,
       arg4,
       arg5,
       arg6);
   /*^ 4 spaces here */
}

我发现这是一种非常奇怪的行为。我希望在整个代码中使用相同的缩进级别。无论IndentWidth 有什么值,clang-format 似乎总是将函数参数/参数缩进 4 个空格。有没有办法覆盖这种行为?或者这是一个错误?

我的 clang 格式 cfg:

Language: Cpp
SpaceBeforeParens: Never
SpacesInParentheses: false
SpaceAfterCStyleCast: true
SpacesInSquareBrackets: false
AllowShortIfStatementsOnASingleLine: false
PointerAlignment: Right
AlignOperands: true
AlignConsecutiveAssignments: true
AlignAfterOpenBracket: false
UseTab: Never
IndentWidth: 3
TabWidth: 3
ColumnLimit: 100
MaxEmptyLinesToKeep: 4
KeepEmptyLinesAtTheStartOfBlocks: false
BreakBeforeBraces: Stroustrup
BinPackArguments: false
BinPackParameters: false
AllowShortFunctionsOnASingleLine: None
AlignEscapedNewlinesLeft: true

【问题讨论】:

    标签: c clang-format


    【解决方案1】:

    实际上,我发现缺少哪个命令。你必须添加

    ContinuationIndentWidth: 3
    

    或任何您想要的数字,以便它以您想要的方式缩进连续行。默认为 4。

    【讨论】: