diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 872545660caa..2812a405b871 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -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; } diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index c1760f32fc32..6b6f4edee238 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -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; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 232a06355810..685c9022dad6 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -1650,18 +1650,21 @@ TEST_F(FormatTest, FormatsEnum) { verifyFormat("enum X f() {\n a();\n return 42;\n}"); verifyFormat("enum {\n" " Bar = Foo::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" "};",