[clang] [clang-format] Avoid spurious blank lines after configured macros (PR #238619)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 16:58:55 PST 2026


https://github.com/akasakariko created https://github.com/llvm/llvm-project/pull/238619

Configured `Macros` can make `SeparateDefinitionBlocks: Always` insert an empty line inside a later `if/else` block because block indices in the expansion pass still refer to the unexpanded line sequence

Skip definition block separation during macro expansion and process the reconstructed lines, where those indices are valid

Add a regression using two `AUTO_RETURN` definitions followed by an `if/else` function, plus a release note

Validation on an assertions-enabled build:

- The new regression fails before the fix and passes afterward
- All 1,313 `FormatTests` pass
- All 33 `clang/test/Format` lit tests pass
- The issue reproducer is unchanged after formatting and remains stable on a second pass
- Changed-line formatting and `git diff --check` pass

Fixes #237253

Assisted-by: OpenAI Codex


>From c3fc5d34ba91e59bdc680b335e57a0672767d03b Mon Sep 17 00:00:00 2001
From: akasakariko <148060537+akasakariko at users.noreply.github.com>
Date: Wed, 4 Nov 2026 09:50:52 +0900
Subject: [PATCH] [clang-format] Avoid spurious blank lines after configured
 macros

Block indices in the macro expansion pass still refer to the unexpanded
lines, which can cause SeparateDefinitionBlocks to mistake an if/else
block after macro calls for a function definition

Skip expanded lines in DefinitionBlockSeparator and separate definitions
after macro reconstruction, when the indices match the processed lines

Add a regression for two AUTO_RETURN definitions followed by an if/else
function and document the fix in the release notes

Fixes #237253
Assisted-by: OpenAI Codex
---
 clang/docs/ReleaseNotes.md                     |  2 ++
 clang/lib/Format/DefinitionBlockSeparator.cpp  |  3 ++-
 .../Format/DefinitionBlockSeparatorTest.cpp    | 18 ++++++++++++++++++
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 409d2459b0580b..64f523c806fe01 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1422,6 +1422,8 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 
 ### clang-format
 
+- Fix spurious empty lines with `SeparateDefinitionBlocks: Always` when
+  expanding configured `Macros`. (#GH237253)
 - Add `SpacesInBlockComments` option to control spacing after `/*` and
   before `*/` in ordinary block comments.
 - Add `AfterRequiresExpression` sub-option of `BraceWrapping` to wrap the
diff --git a/clang/lib/Format/DefinitionBlockSeparator.cpp b/clang/lib/Format/DefinitionBlockSeparator.cpp
index 5fc5ba04529b52..15c6377989c92f 100644
--- a/clang/lib/Format/DefinitionBlockSeparator.cpp
+++ b/clang/lib/Format/DefinitionBlockSeparator.cpp
@@ -79,7 +79,8 @@ void DefinitionBlockSeparator::separateBlocks(
           : Style.LineEnding == FormatStyle::LE_CRLF);
   for (unsigned I = 0; I < Lines.size(); ++I) {
     const auto &CurrentLine = Lines[I];
-    if (CurrentLine->InPPDirective)
+    // Block indices refer to the unexpanded lines.
+    if (CurrentLine->InPPDirective || CurrentLine->InMacroExpansion)
       continue;
     FormatToken *TargetToken = nullptr;
     AnnotatedLine *TargetLine;
diff --git a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
index d18ab4e5f79656..08b0351268bd74 100644
--- a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -577,6 +577,24 @@ TEST_F(DefinitionBlockSeparatorTest, TryBlocks) {
                Style, "", /*Inverse=*/false);
 }
 
+TEST_F(DefinitionBlockSeparatorTest, MacroExpansion) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  Style.Macros = {"AUTO_RETURN(a)=-> decltype(auto) { return a; }"};
+  StringRef Code = "auto a(int t) AUTO_RETURN(g(t))\n"
+                   "\n"
+                   "auto b(int t) AUTO_RETURN(g(t))\n"
+                   "\n"
+                   "int f(bool c) {\n"
+                   "  if (c) {\n"
+                   "    return 1;\n"
+                   "  } else {\n"
+                   "    return 2;\n"
+                   "  }\n"
+                   "}";
+  EXPECT_EQ(Code, separateDefinitionBlocks(Code, Style));
+}
+
 TEST_F(DefinitionBlockSeparatorTest, Leave) {
   FormatStyle Style = getLLVMStyle();
   Style.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;



More information about the cfe-commits mailing list