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 @@ -3663,8 +3663,11 @@ } if (isAllmanBrace(Left) || isAllmanBrace(Right)) { + // 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; - FormatToken *breakingSearchToken = Right.Previous; + const FormatToken *breakingSearchToken = &Right; while ((breakingSearchToken = breakingSearchToken->Next)) { bool hasBreakingComma = breakingSearchToken->is(tok::comma) && breakingSearchToken->Next->is(tok::r_brace); @@ -3674,15 +3677,16 @@ } } bool isAllowedByAfterEnum = - (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) || - (Line.startsWith(tok::kw_typedef, tok::kw_enum) && - Style.BraceWrapping.AfterEnum); + (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; - bool isAllowedByShortEnums = !Style.AllowShortEnumsOnASingleLine || - isLineTooBig || lineContainsBreakingTokens; - return (isAllowedByAfterEnum && - (isAllowedByShortEnums || lineContainsBreakingTokens)) || + // 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); }