Author: Zhihao Yuan Date: 2021-05-28T18:24:00-05:00 New Revision: 09b75f480d1d578d48307fd7f3b024b66a75712f
URL: https://github.com/llvm/llvm-project/commit/09b75f480d1d578d48307fd7f3b024b66a75712f DIFF: https://github.com/llvm/llvm-project/commit/09b75f480d1d578d48307fd7f3b024b66a75712f.diff LOG: [clang-format] New BreakInheritanceList style AfterComma This inheritance list style has been widely adopted by Symantec, a division of Broadcom Inc. It breaks after the commas that separate the base-specifiers: class Derived : public Base1, private Base2 { }; Differential Revision: https://reviews.llvm.org/D103204 Added: Modified: clang/docs/ClangFormatStyleOptions.rst clang/docs/ReleaseNotes.rst clang/include/clang/Format/Format.h clang/lib/Format/Format.cpp clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 459887705d4ec..bbe2807c2e23a 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -2038,6 +2038,15 @@ the configuration (without a prefix: ``Auto``). Base2 {}; + * ``BILS_AfterComma`` (in configuration: ``AfterComma``) + Break inheritance list only after the commas. + + .. code-block:: c++ + + class Foo : Base1, + Base2 + {}; + **BreakStringLiterals** (``bool``) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 81609fa6efdfd..cbf0f9b0e2c49 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -250,6 +250,9 @@ clang-format accepts ``AllIfsAndElse`` value that allows to put "else if" and "else" short statements on a single line. (Fixes https://llvm.org/PR50019.) +- Option ``BreakInheritanceList`` gets a new style, ``AfterComma``. It breaks + only after the commas that separate the base-specifiers. + - ``git-clang-format`` no longer formats changes to symbolic links. (Fixes https://llvm.org/PR46992.) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 4392b4aa4b43a..c30e357b5d0dd 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1829,7 +1829,14 @@ struct FormatStyle { /// Base2 /// {}; /// \endcode - BILS_AfterColon + BILS_AfterColon, + /// Break inheritance list only after the commas. + /// \code + /// class Foo : Base1, + /// Base2 + /// {}; + /// \endcode + BILS_AfterComma, }; /// The inheritance list style to use. diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index c50786c9d2f7a..c85ecd970ff15 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -240,6 +240,7 @@ struct ScalarEnumerationTraits<FormatStyle::BreakInheritanceListStyle> { IO.enumCase(Value, "BeforeColon", FormatStyle::BILS_BeforeColon); IO.enumCase(Value, "BeforeComma", FormatStyle::BILS_BeforeComma); IO.enumCase(Value, "AfterColon", FormatStyle::BILS_AfterColon); + IO.enumCase(Value, "AfterComma", FormatStyle::BILS_AfterComma); } }; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index d1aa3e422b5a8..daa624000ff6d 100755 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3639,6 +3639,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma && Right.is(TT_InheritanceComma)) return true; + if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma && + Left.is(TT_InheritanceComma)) + return true; if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\"")) // Multiline raw string literals are special wrt. line breaks. The author // has made a deliberate choice and might have aligned the contents of the diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index cf3e7b1df3900..efabaabf6e7f5 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2290,6 +2290,28 @@ TEST_F(FormatTest, BreakInheritanceStyle) { " public aaaaaaaaaaaaaaaaaaa< // break\n" " aaaaaaaaaaaaaaaa> {};", StyleWithInheritanceBreakAfterColon); + + FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); + StyleWithInheritanceBreakAfterComma.BreakInheritanceList = + FormatStyle::BILS_AfterComma; + verifyFormat("class MyClass : public X {};", + StyleWithInheritanceBreakAfterComma); + verifyFormat("class MyClass : public X,\n" + " public Y {};", + StyleWithInheritanceBreakAfterComma); + verifyFormat( + "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" + " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " + "{};", + StyleWithInheritanceBreakAfterComma); + verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" + " aaaaaaaaaaaaaaaa> {};", + StyleWithInheritanceBreakAfterComma); + verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" + " : public OnceBreak,\n" + " public AlwaysBreak,\n" + " EvenBasesFitInOneLine {};", + StyleWithInheritanceBreakAfterComma); } TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { @@ -5585,6 +5607,12 @@ TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", Style); + Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; + verifyFormat( + "class SomeClass\n" + " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", + Style); } #ifndef EXPENSIVE_CHECKS @@ -13505,6 +13533,12 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { "}\n" "}", InheritanceStyle); + InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; + verifyFormat("class Foooooooooooooooooooooo\n" + " : public aaaaaaaaaaaaaaaaaa,\n" + " public bbbbbbbbbbbbbbbbbb {\n" + "}", + InheritanceStyle); InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; verifyFormat("class Foooooooooooooooooooooo:\n" " public aaaaaaaaaaaaaaaaaa,\n" @@ -16843,6 +16877,8 @@ TEST_F(FormatTest, ParsesConfiguration) { BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; + CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, + FormatStyle::BILS_AfterComma); CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, FormatStyle::BILS_BeforeComma); CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits