[clang] [clang-format] Avoid spurious blank lines after configured macros (PR #238619)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 3 17:10:55 PST 2026
https://github.com/akasakariko updated https://github.com/llvm/llvm-project/pull/238619
>From beb848804ddae3439995708907e8c435e627342c Mon Sep 17 00:00:00 2001
From: akasakariko <Kayano04 at outlook.jp>
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