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 @@ -3662,12 +3662,31 @@ return true; } - if (isAllmanBrace(Left) || isAllmanBrace(Right)) - return (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) || - (Line.startsWith(tok::kw_typedef, tok::kw_enum) && - Style.BraceWrapping.AfterEnum) || + if (isAllmanBrace(Left) || isAllmanBrace(Right)) { + bool lineContainsBreakingTokens = false; + FormatToken *breakingSearchToken = Right.Previous; + while ((breakingSearchToken = breakingSearchToken->Next)) { + bool hasBreakingComma = breakingSearchToken->is(tok::comma) && + breakingSearchToken->Next->is(tok::r_brace); + if (breakingSearchToken->isTrailingComment() || hasBreakingComma) { + lineContainsBreakingTokens = true; + break; + } + } + bool isAllowedByAfterEnum = + (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) || + (Line.startsWith(tok::kw_typedef, tok::kw_enum) && + Style.BraceWrapping.AfterEnum); + bool isLineTooBig = (strlen(Right.TokenText.data()) + + Right.OriginalColumn) > Style.ColumnLimit; + bool isAllowedByShortEnums = !Style.AllowShortEnumsOnASingleLine || + isLineTooBig || lineContainsBreakingTokens; + return (isAllowedByAfterEnum && + (isAllowedByShortEnums || lineContainsBreakingTokens)) || (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) || (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct); + } + if (Left.is(TT_ObjCBlockLBrace) && Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) return true; 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 @@ -13316,6 +13316,7 @@ TEST_F(FormatTest, AllmanBraceBreaking) { FormatStyle AllmanBraceStyle = getLLVMStyle(); AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; + AllmanBraceStyle.AllowShortEnumsOnASingleLine = false; EXPECT_EQ("namespace a\n" "{\n"