https://github.com/akasakariko updated https://github.com/llvm/llvm-project/pull/238619
>From beb848804ddae3439995708907e8c435e627342c Mon Sep 17 00:00:00 2001 From: akasakariko <[email protected]> Date: Wed, 4 Nov 2026 09:50:52 +0900 Subject: [PATCH 1/2] [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; >From b41a71436444cfd12837a839ee5a0383590bd504 Mon Sep 17 00:00:00 2001 From: akasakariko <[email protected]> Date: Thu, 5 Nov 2026 10:02:17 +0900 Subject: [PATCH 2/2] [clang-format] Address definition block separator review Clarify why block separation must wait until macro reconstruction, use verifyFormat in the regression, and remove the release note Assisted-by: OpenAI Codex --- clang/docs/ReleaseNotes.md | 2 -- clang/lib/Format/DefinitionBlockSeparator.cpp | 6 ++++- .../Format/DefinitionBlockSeparatorTest.cpp | 23 +++++++++---------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 64f523c806fe01..409d2459b0580b 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1422,8 +1422,6 @@ 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 15c6377989c92f..5d98e168f5320e 100644 --- a/clang/lib/Format/DefinitionBlockSeparator.cpp +++ b/clang/lib/Format/DefinitionBlockSeparator.cpp @@ -79,7 +79,11 @@ void DefinitionBlockSeparator::separateBlocks( : Style.LineEnding == FormatStyle::LE_CRLF); for (unsigned I = 0; I < Lines.size(); ++I) { const auto &CurrentLine = Lines[I]; - // Block indices refer to the unexpanded lines. + // 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; diff --git a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp index 08b0351268bd74..761655cbe560f6 100644 --- a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp +++ b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp @@ -581,18 +581,17 @@ 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)); + 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) { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
