diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3666,29 +3666,31 @@ // The tokens that could force an enum to not be on a single line are a // trailing comment and a trailing comma on the last case. This checks for // those. - bool lineContainsBreakingTokens = false; - const FormatToken *breakingSearchToken = &Right; - while ((breakingSearchToken = breakingSearchToken->Next)) { - bool hasBreakingComma = breakingSearchToken->is(tok::comma) && - breakingSearchToken->Next->is(tok::r_brace); - if (breakingSearchToken->isTrailingComment() || hasBreakingComma) { - lineContainsBreakingTokens = true; - break; + auto isAllowedByShortEnums = [&]() { + if (!Style.AllowShortEnumsOnASingleLine || + (strlen(Right.TokenText.data()) + Right.OriginalColumn) > + Style.ColumnLimit) + return true; + + const FormatToken *breakingSearchToken = &Right; + while ((breakingSearchToken = breakingSearchToken->Next)) { + bool hasBreakingComma = breakingSearchToken->is(tok::comma) && + breakingSearchToken->Next->is(tok::r_brace); + if (breakingSearchToken->isTrailingComment() || hasBreakingComma) { + return true; + } } - } + + return false; + }; bool isAllowedByAfterEnum = (Line.startsWith(tok::kw_enum) || Line.startsWith(tok::kw_typedef, tok::kw_enum)) && Style.BraceWrapping.AfterEnum; - bool isLineTooBig = (strlen(Right.TokenText.data()) + - Right.OriginalColumn) > Style.ColumnLimit; - // AllowShortEnumsOnASingleLine is ignored if the line is too long or - // contains breaking tokens. - bool isAllowedByShortEnums = isLineTooBig || lineContainsBreakingTokens || - !Style.AllowShortEnumsOnASingleLine; - return (isAllowedByAfterEnum && isAllowedByShortEnums) || - (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) || - (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct); + return (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) || + (Line.startsWith(tok::kw_struct) && + Style.BraceWrapping.AfterStruct) || + (isAllowedByAfterEnum && isAllowedByShortEnums()); } if (Left.is(TT_ObjCBlockLBrace) && diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -1393,6 +1393,21 @@ Style); } +TEST_F(FormatTest, AfterEnumShortEnums) { + FormatStyle Style = getLLVMStyle(); + Style.BreakBeforeBraces = FormatStyle::BS_Custom; + + Style.AllowShortEnumsOnASingleLine = true; + Style.BraceWrapping.AfterEnum = true; + verifyFormat("enum\n" + "{\n" + " A,\n" + " B,\n" + "} ShortEnum;", + Style); + verifyFormat("enum { A, B } ShortEnum;", Style); +} + TEST_F(FormatTest, ShortCaseLabels) { FormatStyle Style = getLLVMStyle(); Style.AllowShortCaseLabelsOnASingleLine = true;