clang-format: Allow formatting short enums on a single line.

Before:
  enum ShortEnum {
    A,
    B,
    C
  };

After:
  enum ShortEnum { A, B, C };

This seems to be the predominant choice in LLVM/Clang as well as in
Google style.

llvm-svn: 198558
This commit is contained in:
Daniel Jasper 2014-01-05 12:38:10 +00:00
parent 52e4a0e109
commit 9697281eec
3 changed files with 22 additions and 16 deletions

View File

@ -182,10 +182,6 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
State.Stack.back().ObjCSelectorNameFound &&
State.Stack.back().BreakBeforeParameter)
return true;
if (Current.Type == TT_CtorInitializerColon &&
(!Style.AllowShortFunctionsOnASingleLine ||
Style.BreakConstructorInitializersBeforeComma || Style.ColumnLimit != 0))
return true;
if (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0 &&
!Current.isTrailingComment())
return true;
@ -199,6 +195,18 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
(State.Stack.back().BreakBeforeParameter &&
State.Stack.back().ContainsUnwrappedBuilder)))
return true;
// The following could be precomputed as they do not depend on the state.
// However, as they should take effect only if the UnwrappedLine does not fit
// into the ColumnLimit, they are checked here in the ContinuationIndenter.
if (Previous.BlockKind == BK_Block && Previous.is(tok::l_brace) &&
!Current.isOneOf(tok::r_brace, tok::comment))
return true;
if (Current.Type == TT_CtorInitializerColon &&
(!Style.AllowShortFunctionsOnASingleLine ||
Style.BreakConstructorInitializersBeforeComma || Style.ColumnLimit != 0))
return true;
return false;
}

View File

@ -1438,9 +1438,6 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
Style.BreakConstructorInitializersBeforeComma &&
!Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
return true;
} else if (Right.Previous->BlockKind == BK_Block &&
Right.Previous->isNot(tok::r_brace) && Right.isNot(tok::r_brace)) {
return true;
} else if (Right.is(tok::l_brace) && (Right.BlockKind == BK_Block)) {
return Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
Style.BreakBeforeBraces == FormatStyle::BS_GNU;

View File

@ -1650,18 +1650,21 @@ TEST_F(FormatTest, FormatsEnum) {
verifyFormat("enum X f() {\n a();\n return 42;\n}");
verifyFormat("enum {\n"
" Bar = Foo<int, int>::value\n"
"};");
"};",
getLLVMStyleWithColumns(30));
verifyFormat("enum ShortEnum { A, B, C };");
}
TEST_F(FormatTest, FormatsEnumsWithErrors) {
verifyFormat("enum Type {\n"
" One = 0;\n" // These semicolons should be commas.
" One = 0; // These semicolons should be commas.\n"
" Two = 1;\n"
"};");
verifyFormat("namespace n {\n"
"enum Type {\n"
" One,\n"
" Two,\n" // missing };
" Two, // missing };\n"
" int i;\n"
"}\n"
"void g() {}");
@ -1703,13 +1706,11 @@ TEST_F(FormatTest, FormatsEnumClass) {
TEST_F(FormatTest, FormatsEnumTypes) {
verifyFormat("enum X : int {\n"
" A,\n"
" B\n"
"};");
verifyFormat("enum X : std::uint32_t {\n"
" A,\n"
" A, // Force multiple lines.\n"
" B\n"
"};");
verifyFormat("enum X : int { A, B };");
verifyFormat("enum X : std::uint32_t { A, B };");
}
TEST_F(FormatTest, FormatsBitfields) {
@ -6605,7 +6606,7 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
"};",
Tab);
verifyFormat("enum A {\n"
"\ta1,\n"
"\ta1, // Force multiple lines\n"
"\ta2,\n"
"\ta3\n"
"};",