[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
This commit is contained in:
Zhihao Yuan 2021-05-26 13:22:44 -07:00 committed by Zhihao Yuan
parent bde21b6245
commit 09b75f480d
No known key found for this signature in database
GPG Key ID: A2E474BDAA37E11C
6 changed files with 60 additions and 1 deletions

View File

@ -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``)

View File

@ -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.)

View File

@ -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.

View File

@ -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);
}
};

View File

@ -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

View File

@ -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,