c++ - Clang 格式将 if 语句主体拆分为多行

标签 c++ formatting clang clang-format

我有以下 .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 有一个附加选项“AllowShortBlocksOnASingleLine”,用于控制此行为。

关于c++ - Clang 格式将 if 语句主体拆分为多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22512344/

相关文章:

c# - 读取文本文件并继续格式化

java - 如何在此 java 代码中使用数组,以便我可以按照将值分配给位置的顺序输出值?

objective-c - 具有可修改 AST 的 Objective C 解析器(如 C# 的 NRefactory)

c++ - GCC#pragma 消息被忽略

c++ - 关于 C++ 模板和显式声明

c++ - 如何让 GCC 将 .text 部分编译为 ELF 二进制文件中的可写部分?

c++ - 2 个 pthread 条件变量可以共享同一个互斥锁吗?

c++ - Qt - QPushButton 文本格式

xcode - 如何将来自 clang 静态代码分析的警告视为 Xcode 3 中的错误?

c++ - Clang 真的有这么聪明吗?