Clang格式化将if语句体拆分成多行

6

我有以下的.cpp文件:

////////////////////////////////////////////////////////////////////////////////
void call_long_function_name(bool) {}
void sf(bool) {}
int main() {
  bool test = true;
  if (test) { call_function_name(test); }
  if (test) { sf(test); }
  return 0;
}

(斜杠分隔80个字符)。使用以下配置文件,clang-format建议:

////////////////////////////////////////////////////////////////////////////////
void call_long_function_name(bool) {}
void sf(bool) {}
int main() {
  bool test = true;
  if (test) { 
    call_function_name(test); 
  }
  if (test) { 
    sf(test); 
  }
  return 0;
}

尽管在文件中我允许短的if语句放在一行内,但是您觉得我设置了错误的选项吗?

  • 哪些选项可以用来最小化浪费的垂直空间?

Clang-format的.clang-format文件

BasedOnStyle: Google
AccessModifierOffset: -1
AlignEscapedNewlinesLeft: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackParameters: true
BreakBeforeBinaryOperators: true
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
ColumnLimit: 80
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 0
Cpp11BracedListStyle: true
DerivePointerBinding: true
IndentCaseLabels: true
IndentFunctionDeclarationAfterType: true
IndentWidth: 2
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PointerBindsToType: true
SpaceAfterControlStatementKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInAngles:  false
Standard: Cpp11
TabWidth: 2
UseTab: Never

目前还没有 clang-format 标签,如果有人能添加它,我将不胜感激。 - gnzlbg
1
问题在于括号。if (test) call_function_name(test); 然而,请问您认为垂直空间是浪费的吗?难道它不是一种珍贵的资源需要节约吗?并非如此。顺便说一下,如果call_function_name(test)if在同一行上,则很难在调试器中在该语句处设置断点。 - David Hammen
@DavidHammen 是的,看起来问题出在括号上。你是用行号设置断点吗?我从来不这样做 :/ - gnzlbg
1
@DavidHammen:垂直空间确实是一种宝贵的资源,我能够在不上下滚动页面的情况下看到更多的内容,这样就更容易理解代码片段(或多个代码片段)。 - Matthieu M.
2个回答

10
新版本的clang-format有一个额外选项"AllowShortBlocksOnASingleLine",它控制这种行为。

我将此标记为答案,因为它更为最新。 - gnzlbg

6

看起来,clang-format 只有在省略括号时才应用 AllowShortIfStatementsOnASingleLine 选项。我测试了以下代码:

void call_long_function_name(bool) {}
void call_long_super_duper_long_really_really_long_way_long_function_name(bool) {}
void sf(bool) {}
int main() {
  bool test = true;
  if (test) 
    call_function_name(test);
  if (test)
    sf(test);
  if (test)
    call_long_super_duper_long_really_really_long_way_long_function_name(test);
  if (test) {
    return 0;
  }
  return 0;
}

并得到:

void call_long_function_name(bool) {}
void call_long_super_duper_long_really_really_long_way_long_function_name(
bool) {}
void sf(bool) {}
int main() {
  bool test = true;
  if (test) call_function_name(test);
  if (test) sf(test);
  if (test)
    call_long_super_duper_long_really_really_long_way_long_function_name(test);
  if (test) {
    return 0;
  }
  return 0;
}

嗯,我想可以提交一个补丁,这样clang-format就不是确定if后面是否只有一条语句,而是判断这是否是一个块中的单个语句。当然可能会稍微难一些。 - Matthieu M.
@MatthieuM。我正在更新我的主干代码,以检查最新的主干版本是否仍然存在这个问题(clang-format是一个非常活跃的项目)。如果是这种情况,我将提交一个错误报告并在此处发布。 - gnzlbg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接