Clang format splits if statement body into multipl

2019-07-04 05:11发布

问题:

I have the following .cpp file:

////////////////////////////////////////////////////////////////////////////////
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;
}

(the slashes delimit 80 characters). Using the following configuration file, clang-format suggests:

////////////////////////////////////////////////////////////////////////////////
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;
}

even tho in the file I allow short if statements to fit into a single line.

  • Did I set any options wrong?

  • Which options can I use to minimize wasted vertical space?

Clang-format's .clang-format file

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

回答1:

Newer versions of clang-format have an additional option "AllowShortBlocksOnASingleLine", which controls this behavior.



回答2:

It appears that clang-format only applies the AllowShortIfStatementsOnASingleLine option if you omit the brackets. I tested the following:

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;
}

And got:

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;
}