91 changes: 83 additions & 8 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1316,15 +1316,40 @@ TEST_F(FormatTest, FormatsClasses) {
verifyFormat("class ::A::B {};");
}

TEST_F(FormatTest, BreakBeforeInheritanceComma) {
FormatStyle StyleWithInheritanceBreak = getLLVMStyle();
StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true;

verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak);
TEST_F(FormatTest, BreakInheritanceStyle) {
FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
FormatStyle::BILS_BeforeComma;
verifyFormat("class MyClass : public X {};",
StyleWithInheritanceBreakBeforeComma);
verifyFormat("class MyClass\n"
" : public X\n"
" , public Y {};",
StyleWithInheritanceBreak);
StyleWithInheritanceBreakBeforeComma);
verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
" : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
" , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
StyleWithInheritanceBreakBeforeComma);
verifyFormat("struct aaaaaaaaaaaaa\n"
" : public aaaaaaaaaaaaaaaaaaa< // break\n"
" aaaaaaaaaaaaaaaa> {};",
StyleWithInheritanceBreakBeforeComma);

FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
FormatStyle::BILS_AfterColon;
verifyFormat("class MyClass : public X {};",
StyleWithInheritanceBreakAfterColon);
verifyFormat("class MyClass : public X, public Y {};",
StyleWithInheritanceBreakAfterColon);
verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
" public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
" public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
StyleWithInheritanceBreakAfterColon);
verifyFormat("struct aaaaaaaaaaaaa :\n"
" public aaaaaaaaaaaaaaaaaaa< // break\n"
" aaaaaaaaaaaaaaaa> {};",
StyleWithInheritanceBreakAfterColon);
}

TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Expand Down Expand Up @@ -3726,6 +3751,23 @@ TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
Style);

// `ConstructorInitializerIndentWidth` actually applies to InheritanceList as well
Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
verifyFormat("class SomeClass\n"
" : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
Style);
Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
verifyFormat("class SomeClass\n"
" : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
Style);
Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
verifyFormat("class SomeClass :\n"
" public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
Style);
}

#ifndef EXPENSIVE_CHECKS
Expand Down Expand Up @@ -9164,7 +9206,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
" bbbbbbbbbbbbbbbb(2) {}",
CtorInitializerStyle);

FormatStyle InheritanceStyle = getLLVMStyle();
FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
InheritanceStyle.SpaceBeforeInheritanceColon = false;
verifyFormat("class Foo: public Bar {};", InheritanceStyle);
verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
Expand All @@ -9180,6 +9222,29 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
"default:\n"
"}",
InheritanceStyle);
InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
verifyFormat("class Foooooooooooooooooooooo:\n"
" public aaaaaaaaaaaaaaaaaa,\n"
" public bbbbbbbbbbbbbbbbbb {\n"
"}",
InheritanceStyle);
InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
verifyFormat("class Foooooooooooooooooooooo\n"
" : public aaaaaaaaaaaaaaaaaa\n"
" , public bbbbbbbbbbbbbbbbbb {\n"
"}",
InheritanceStyle);
InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
verifyFormat("class Foooooooooooooooooooooo\n"
" : public aaaaaaaaaaaaaaaaaa,\n"
" public bbbbbbbbbbbbbbbbbb {\n"
"}",
InheritanceStyle);
InheritanceStyle.ConstructorInitializerIndentWidth = 0;
verifyFormat("class Foooooooooooooooooooooo\n"
": public aaaaaaaaaaaaaaaaaa,\n"
" public bbbbbbbbbbbbbbbbbb {}",
InheritanceStyle);

FormatStyle ForLoopStyle = getLLVMStyle();
ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
Expand Down Expand Up @@ -10534,7 +10599,6 @@ TEST_F(FormatTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
CHECK_PARSE_BOOL(BreakStringLiterals);
CHECK_PARSE_BOOL(BreakBeforeInheritanceComma)
CHECK_PARSE_BOOL(CompactNamespaces);
CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
CHECK_PARSE_BOOL(DerivePointerAlignment);
Expand Down Expand Up @@ -10651,6 +10715,17 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);

Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
CHECK_PARSE("BreakInheritanceList: BeforeComma",
BreakInheritanceList, FormatStyle::BILS_BeforeComma);
CHECK_PARSE("BreakInheritanceList: AfterColon",
BreakInheritanceList, FormatStyle::BILS_AfterColon);
CHECK_PARSE("BreakInheritanceList: BeforeColon",
BreakInheritanceList, FormatStyle::BILS_BeforeColon);
// For backward compatibility:
CHECK_PARSE("BreakBeforeInheritanceComma: true",
BreakInheritanceList, FormatStyle::BILS_BeforeComma);

Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
FormatStyle::BAS_Align);
Expand Down