diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -1254,7 +1254,7 @@ if (PreviousLine && RootToken.isAccessSpecifier()) { switch (Style.EmptyLineBeforeAccessModifier) { case FormatStyle::ELBAMS_Never: - if (RootToken.NewlinesBefore > 1) + if (Newlines > 1) Newlines = 1; break; case FormatStyle::ELBAMS_Leave: @@ -1262,7 +1262,7 @@ break; case FormatStyle::ELBAMS_LogicalBlock: if (PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) && - RootToken.NewlinesBefore <= 1) + Newlines <= 1) Newlines = 2; break; case FormatStyle::ELBAMS_Always: { @@ -1272,7 +1272,7 @@ else previousToken = PreviousLine->Last; if ((!previousToken || !previousToken->is(tok::l_brace)) && - RootToken.NewlinesBefore <= 1) + Newlines <= 1) Newlines = 2; } break; } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -8889,6 +8889,8 @@ TEST_F(FormatTest, FormatsAccessModifiers) { FormatStyle Style = getLLVMStyle(); + FormatStyle NoEmptyLines = getLLVMStyle(); + NoEmptyLines.MaxEmptyLinesToKeep = 0; EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); verifyFormat("struct foo {\n" @@ -8940,7 +8942,20 @@ " int j;\n" "};\n", Style); + verifyFormat("struct foo {\n" + "private:\n" + " void f() {}\n" + "\n" + "private:\n" + " int i;\n" + "\n" + "public:\n" + "protected:\n" + " int j;\n" + "};\n", + NoEmptyLines); Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; + NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; verifyFormat("struct foo {\n" "private:\n" " void f() {}\n" @@ -8950,6 +8965,16 @@ " int j;\n" "};\n", Style); + verifyFormat("struct foo {\n" + "private:\n" + " void f() {}\n" + "private:\n" + " int i;\n" + "public:\n" + "protected:\n" + " int j;\n" + "};\n", + NoEmptyLines); verifyFormat("struct foo {\n" "private:\n" " void f() {}\n" @@ -9011,6 +9036,7 @@ "};\n", Style); Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; + NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; verifyFormat("struct foo {\n" "private:\n" " void f() {}\n" @@ -9022,6 +9048,18 @@ " int j;\n" "};\n", Style); + verifyFormat("struct foo {\n" + "private:\n" + " void f() {}\n" + "\n" + "private:\n" + " int i;\n" + "\n" + "public:\n" + "protected:\n" + " int j;\n" + "};\n", + NoEmptyLines); verifyFormat("struct foo {\n" "private:\n" " void f() {}\n"