Author: gautamnsankar Date: 2026-08-27T20:47:28+02:00 New Revision: fbfc2d9bcd7dc3cf08dbf7b60e4e476e4f262f9c
URL: https://github.com/llvm/llvm-project/commit/fbfc2d9bcd7dc3cf08dbf7b60e4e476e4f262f9c DIFF: https://github.com/llvm/llvm-project/commit/fbfc2d9bcd7dc3cf08dbf7b60e4e476e4f262f9c.diff LOG: [clang-format] Respect definition separators when MaxEmptyLinesToKeep: 0 (#206406) Fixes #206340. The formatter should remove empty lines before Allman opening braces, but should preserve the required empty line between function definitions. I have added a test case "AlwaysMaxEmptyLinesZeroAllman", and have ensured other test cases run fine along with this one. Added: Modified: clang/lib/Format/DefinitionBlockSeparator.cpp clang/lib/Format/Format.cpp clang/unittests/Format/DefinitionBlockSeparatorTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/DefinitionBlockSeparator.cpp b/clang/lib/Format/DefinitionBlockSeparator.cpp index 6b52b1fc0deff..5fc5ba04529b5 100644 --- a/clang/lib/Format/DefinitionBlockSeparator.cpp +++ b/clang/lib/Format/DefinitionBlockSeparator.cpp @@ -66,6 +66,10 @@ void DefinitionBlockSeparator::separateBlocks( }; unsigned NewlineCount = (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always ? 1 : 0) + 1; + + Style.MaxEmptyLinesToKeep = + std::max(Style.MaxEmptyLinesToKeep, NewlineCount - 1); + WhitespaceManager Whitespaces( Env.getSourceManager(), Style, Style.LineEnding > FormatStyle::LE_CRLF diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index e5533c32899a3..574014f360354 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -4387,12 +4387,6 @@ reformat(const FormatStyle &Style, StringRef Code, } } - if (Style.SeparateDefinitionBlocks != FormatStyle::SDS_Leave) { - Passes.emplace_back([&](const Environment &Env) { - return DefinitionBlockSeparator(Env, Expanded).process(); - }); - } - if (Style.Language == FormatStyle::LK_ObjC && !Style.ObjCPropertyAttributeOrder.empty()) { Passes.emplace_back([&](const Environment &Env) { @@ -4411,6 +4405,12 @@ reformat(const FormatStyle &Style, StringRef Code, return Formatter(Env, Expanded, Status).process(); }); + if (Style.SeparateDefinitionBlocks != FormatStyle::SDS_Leave) { + Passes.emplace_back([&](const Environment &Env) { + return DefinitionBlockSeparator(Env, Expanded).process(); + }); + } + if (Style.isJavaScript() && Style.InsertTrailingCommas == FormatStyle::TCS_Wrapped) { Passes.emplace_back([&](const Environment &Env) { diff --git a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp index 5e4c574d68dbb..d18ab4e5f7965 100644 --- a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp +++ b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp @@ -391,6 +391,35 @@ TEST_F(DefinitionBlockSeparatorTest, Always) { Style, Prefix + Infix + Postfix); } +TEST_F(DefinitionBlockSeparatorTest, AlwaysMaxEmptyLinesZeroAllman) { + FormatStyle Style = getLLVMStyle(); + Style.BreakBeforeBraces = FormatStyle::BS_Allman; + Style.MaxEmptyLinesToKeep = 0; + Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always; + Style.AllowShortFunctionsOnASingleLine = FormatStyle::ShortFunctionStyle(); + + verifyFormat("int my_function(int a)\n" + "\n" + "{\n" + " return a;\n" + "}\n" + "int other_function(int a)\n" + "\n" + "{\n" + " return a;\n" + "}", + Style, + "int my_function(int a)\n" + "{\n" + " return a;\n" + "}\n" + "\n" + "int other_function(int a)\n" + "{\n" + " return a;\n" + "}"); +} + TEST_F(DefinitionBlockSeparatorTest, Never) { FormatStyle Style = getLLVMStyle(); Style.SeparateDefinitionBlocks = FormatStyle::SDS_Never; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
