【问题标题】:Clang format splits if statement body into multiple linesClang 格式将 if 语句体拆分为多行
【发布时间】:2014-04-26 01:56:02
【问题描述】:

我有以下 .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 格式的标签,如果有人可以添加它,我将不胜感激。
  • 问题是括号。 if (test) call_function_name(test); 但是,是什么让您认为垂直空间被浪费了?垂直空间是需要保护的宝贵资源吗?它不是。顺便说一句,如果它与if 位于同一行,那么祝你在调试器中在call_function_name(test) 语句中设置一个断点。
  • @DavidHammen 是的,问题似乎在括号中。您是否使用行号放置断点?我从不这样做:/
  • @DavidHammen:垂直空间宝贵的资源,不用上下翻页就能看到越多,越容易看懂一块(或多块) ) 的代码。

标签: c++ formatting clang clang-format


【解决方案1】:

如果您省略括号,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;
}

【讨论】:

  • 好吧,我想可以提交一个补丁,以便确定 if 后面是否有单个语句,clang-format 将确定它是否是块中的单个语句,或者不是。当然可能会稍微困难一些。
  • @MatthieuM。我正在更新我的主干以检查最新的主干版本是否仍然发生这种情况(clang-format 是一个非常活跃的项目)。如果是这种情况,我将提交错误报告并将其发布在此处。
【解决方案2】:

较新版本的 clang-format 有一个附加选项“AllowShortBlocksOnASingleLine”,用于控制此行为。

【讨论】:

  • 我已将此标记为答案,因为它是最新的。
猜你喜欢
  • 2021-08-18
  • 2022-12-16
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 1970-01-01
  • 2014-11-24
  • 2018-06-23
  • 2015-05-31
相关资源
最近更新 更多