https://github.com/stativ created https://github.com/llvm/llvm-project/pull/196832
When `BILS_No` is used, the inheritance list is handled like any other line. If the inheritance list needs to be broken down, the continuation indent width is used to indent the new lines. The point of this value is to introduce a possibility of a "dumb" formatting, where the inheritance list has no special alignment. >From f658db0e37a789632faca999bfb91869557f164a Mon Sep 17 00:00:00 2001 From: Lukas Jirkovsky <[email protected]> Date: Sat, 9 May 2026 17:18:09 +0200 Subject: [PATCH] [clang-format] Add `BILS_No` value to BreakInheritanceList option. When `BILS_No` is used, the inheritance list is handled like any other line. If the inheritance list needs to be broken down, the continuation indent width is used to indent the new lines. --- clang/docs/ClangFormatStyleOptions.rst | 9 +++++++++ clang/docs/ReleaseNotes.rst | 2 ++ clang/include/clang/Format/Format.h | 7 +++++++ clang/lib/Format/ContinuationIndenter.cpp | 16 +++++++++++----- clang/lib/Format/Format.cpp | 1 + clang/unittests/Format/ConfigParseTest.cpp | 2 ++ clang/unittests/Format/FormatTest.cpp | 15 +++++++++++++++ 7 files changed, 47 insertions(+), 5 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 61f27bcf9dbbc..b96a5dc907bbb 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -3895,6 +3895,15 @@ the configuration (without a prefix: ``Auto``). Possible values: + * ``BILS_No`` (in configuration: ``No``) + Do not break inheritance list. If the line is too long, the wrapped + lines are indented ContinuationIndentWidth spaces. + + .. code-block:: c++ + + class Foo : Base1, Base2 + {}; + * ``BILS_BeforeColon`` (in configuration: ``BeforeColon``) Break inheritance list before the colon and after the commas. diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index c17143e3c0398..d1b355d6d0aa4 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -744,6 +744,8 @@ clang-format line if they exceed the specified count. - Add ``AfterComma`` value to ``BreakConstructorInitializers`` to allow breaking constructor initializers after commas, keeping the colon on the same line. +- Add ``No`` value to ``BreakInheritanceList`` to disable breaking of the inheritance + list. If the list is too long, standard line breaking rules apply. - Extend ``BreakBinaryOperations`` to accept a structured configuration with per-operator break rules and minimum chain length gating via ``PerOperator``. - Add ``AllowShortRecordOnASingleLine`` option and set it to ``EmptyAndAttached`` for LLVM style. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 0e883837ac0e9..88895a24d6391 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -2745,6 +2745,13 @@ struct FormatStyle { /// Different ways to break inheritance list. enum BreakInheritanceListStyle : int8_t { + /// Do not break inheritance list. If the line is too long, the wrapped + /// lines are indented ContinuationIndentWidth spaces. + /// \code + /// class Foo : Base1, Base2 + /// {}; + /// \endcode + BILS_No, /// Break inheritance list before the colon and after the commas. /// \code /// class Foo diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 485fe382bda3a..b50e4fa445f83 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -1109,8 +1109,12 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, if (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None) CurrentState.LastSpace = State.Column; } else if (Previous.is(TT_InheritanceColon)) { - CurrentState.Indent = State.Column; - CurrentState.LastSpace = State.Column; + // Indent relative to the inheritance colon if the inheritance list + // is being broken into separate lines. + if(Style.BreakInheritanceList != FormatStyle::BILS_No) { + CurrentState.Indent = State.Column; + CurrentState.LastSpace = State.Column; + } } else if (Current.is(TT_CSharpGenericTypeConstraintColon)) { CurrentState.ColonPos = State.Column; } else if (Previous.opensScope()) { @@ -1237,6 +1241,8 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, } switch (Style.BreakInheritanceList) { + case FormatStyle::BILS_No: + break; case FormatStyle::BILS_BeforeColon: case FormatStyle::BILS_AfterComma: if (Current.is(TT_InheritanceColon) || Previous.is(TT_InheritanceComma)) { @@ -1859,9 +1865,9 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, else CurrentState.BreakBeforeParameter = false; } - if (Current.is(TT_InheritanceColon)) { - CurrentState.Indent = - State.FirstIndent + Style.ConstructorInitializerIndentWidth; + if (Current.is(TT_InheritanceColon) && Style.BreakInheritanceList != FormatStyle::BILS_No) { + CurrentState.Indent = + State.FirstIndent + Style.ConstructorInitializerIndentWidth; } if (Current.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && Newline) CurrentState.NestedBlockIndent = State.Column + Current.ColumnWidth + 1; diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 74b31810843fc..2919aaf3ae12e 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -364,6 +364,7 @@ template <> struct ScalarEnumerationTraits<FormatStyle::BreakInheritanceListStyle> { static void enumeration(IO &IO, FormatStyle::BreakInheritanceListStyle &Value) { + IO.enumCase(Value, "No", FormatStyle::BILS_No); IO.enumCase(Value, "BeforeColon", FormatStyle::BILS_BeforeColon); IO.enumCase(Value, "BeforeComma", FormatStyle::BILS_BeforeComma); IO.enumCase(Value, "AfterColon", FormatStyle::BILS_AfterColon); diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index ccb9c837d8362..a734b914ccf5f 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -524,6 +524,8 @@ TEST(ConfigParseTest, ParsesConfiguration) { BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; + CHECK_PARSE("BreakInheritanceList: No", BreakInheritanceList, + FormatStyle::BILS_No); CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, FormatStyle::BILS_AfterComma); CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 4245bd1c58153..ba7c0e3dbd2b8 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -3815,6 +3815,21 @@ TEST_F(FormatTest, FormatsClasses) { } TEST_F(FormatTest, BreakInheritanceStyle) { + FormatStyle StyleWithInheritanceBreakNo = getLLVMStyle(); + StyleWithInheritanceBreakNo.BreakInheritanceList = + FormatStyle::BILS_No; + verifyFormat("class MyClass : public X {};", + StyleWithInheritanceBreakNo); + verifyFormat("class MyClass : public X, public Y {};", + StyleWithInheritanceBreakNo); + verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" + " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", + StyleWithInheritanceBreakNo); + verifyFormat("class MyOuterClass {\n" + " class MyClass : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" + " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", + StyleWithInheritanceBreakNo); + FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = FormatStyle::BILS_BeforeComma; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
