[clang] [clang-format] Respect definition separators when MaxEmptyLinesToKeep: 0 (PR #206406)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 28 23:26:45 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: gautamnsankar
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/206406.diff
2 Files Affected:
- (modified) clang/lib/Format/UnwrappedLineFormatter.cpp (+10-2)
- (modified) clang/unittests/Format/DefinitionBlockSeparatorTest.cpp (+34)
``````````diff
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 7ea424349d923..e7ce95b1d5628 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1633,8 +1633,16 @@ static auto computeNewlines(const AnnotatedLine &Line,
const SmallVectorImpl<AnnotatedLine *> &Lines,
const FormatStyle &Style) {
const auto &RootToken = *Line.First;
- auto Newlines =
- std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
+
+ unsigned MaxNewlines = Style.MaxEmptyLinesToKeep + 1;
+ if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always &&
+ RootToken.NewlinesBefore > 1 && PreviousLine &&
+ !RootToken.is(tok::l_brace) && Line.MightBeFunctionDecl &&
+ Line.mightBeFunctionDefinition()) {
+ MaxNewlines = std::max(MaxNewlines, 2u);
+ }
+
+ auto Newlines = std::min(RootToken.NewlinesBefore, MaxNewlines);
// Remove empty lines before "}" where applicable.
if (RootToken.is(tok::r_brace) &&
(!RootToken.Next ||
diff --git a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
index 5e4c574d68dbb..0bdfc433ceee9 100644
--- a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -391,6 +391,40 @@ 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;
+
+ constexpr StringRef Expected = "uint32_t my_function(uint32_t a1)\n"
+ "{\n"
+ " uint32_t b = a1 + 1;\n"
+ " return a1;\n"
+ "}\n"
+ "\n"
+ "uint32_t other_function(uint32_t a1)\n"
+ "{\n"
+ " uint32_t b = a1 + 1;\n"
+ " return a1;\n"
+ "}\n";
+
+ constexpr StringRef Input = "uint32_t my_function(uint32_t a1)\n"
+ "\n"
+ "{\n"
+ " uint32_t b = a1 + 1;\n"
+ " return a1;\n"
+ "}\n"
+ "uint32_t other_function(uint32_t a1)\n"
+ "\n"
+ "{\n"
+ " uint32_t b = a1 + 1;\n"
+ " return a1;\n"
+ "}\n";
+
+ verifyFormat(Input, Style, Expected);
+}
+
TEST_F(DefinitionBlockSeparatorTest, Never) {
FormatStyle Style = getLLVMStyle();
Style.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
``````````
</details>
https://github.com/llvm/llvm-project/pull/206406
More information about the cfe-commits
mailing list