llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Asuka (akasakariko)

<details>
<summary>Changes</summary>

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


---
Full diff: https://github.com/llvm/llvm-project/pull/238619.diff


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.md (+2) 
- (modified) clang/lib/Format/DefinitionBlockSeparator.cpp (+2-1) 
- (modified) clang/unittests/Format/DefinitionBlockSeparatorTest.cpp (+18) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 409d2459b0580..64f523c806fe0 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 5fc5ba04529b5..15c6377989c92 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 d18ab4e5f7965..08b0351268bd7 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;

``````````

</details>


https://github.com/llvm/llvm-project/pull/238619
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to