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

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 9 11:56:12 PST 2026


Author: Asuka
Date: 2026-11-09T20:56:05+01:00
New Revision: 68b03cf380e61c86aa59f94a10e8ce2d4c10cbb0

URL: https://github.com/llvm/llvm-project/commit/68b03cf380e61c86aa59f94a10e8ce2d4c10cbb0
DIFF: https://github.com/llvm/llvm-project/commit/68b03cf380e61c86aa59f94a10e8ce2d4c10cbb0.diff

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

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

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

Fixes #237253

Assisted-by: OpenAI Codex

Added: 
    

Modified: 
    clang/lib/Format/DefinitionBlockSeparator.cpp
    clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/DefinitionBlockSeparator.cpp b/clang/lib/Format/DefinitionBlockSeparator.cpp
index 5fc5ba04529b52..5d98e168f5320e 100644
--- a/clang/lib/Format/DefinitionBlockSeparator.cpp
+++ b/clang/lib/Format/DefinitionBlockSeparator.cpp
@@ -79,7 +79,12 @@ void DefinitionBlockSeparator::separateBlocks(
           : Style.LineEnding == FormatStyle::LE_CRLF);
   for (unsigned I = 0; I < Lines.size(); ++I) {
     const auto &CurrentLine = Lines[I];
-    if (CurrentLine->InPPDirective)
+    // In the macro expansion pass, MatchingOpeningBlockLineIndex still refers
+    // to the unexpanded line sequence. Expanding a macro into multiple lines
+    // changes line positions, so a closing brace can be matched to the wrong
+    // opening line. Skip this pass and separate blocks in the subsequent pass
+    // over reconstructed macro calls, where the indices are valid.
+    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..761655cbe560f6 100644
--- a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -577,6 +577,23 @@ 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; }"};
+  verifyFormat("auto a(int t) AUTO_RETURN(g(t))\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"
+               "}",
+               Style);
+}
+
 TEST_F(DefinitionBlockSeparatorTest, Leave) {
   FormatStyle Style = getLLVMStyle();
   Style.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;


        


More information about the cfe-commits mailing list