[clang] [clang-format] Merge short inline function in macro definition body (PR #140366)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Sat May 17 02:43:17 PDT 2025


https://github.com/owenca created https://github.com/llvm/llvm-project/pull/140366

Fix #62356

>From f8fb158f193d0064ca3c05c081f2f96d1f882c2c Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sat, 17 May 2025 02:39:51 -0700
Subject: [PATCH] [clang-format] Merge short inline function in macro
 definition body

Fix #62356
---
 clang/lib/Format/UnwrappedLineFormatter.cpp | 4 ++--
 clang/unittests/Format/FormatTest.cpp       | 7 +++++++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 1298e3e7bab38..f2ed027b2c047 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -314,8 +314,8 @@ class LineJoiner {
           const AnnotatedLine *Line = nullptr;
           for (auto J = I - 1; J >= AnnotatedLines.begin(); --J) {
             assert(*J);
-            if ((*J)->InPPDirective || (*J)->isComment() ||
-                (*J)->Level > TheLine->Level) {
+            if (((*J)->InPPDirective && !(*J)->InMacroBody) ||
+                (*J)->isComment() || (*J)->Level > TheLine->Level) {
               continue;
             }
             if ((*J)->Level < TheLine->Level ||
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 436beaf68bd2a..4116173b47b51 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -15086,6 +15086,13 @@ TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
                "};",
                MergeInlineOnly);
 
+  MergeInlineOnly.AlignEscapedNewlines = FormatStyle::ENAS_Left;
+  verifyFormat("#define Foo                \\\n"
+               "  struct S {               \\\n"
+               "    void foo() { return; } \\\n"
+               "  }",
+               MergeInlineOnly);
+
   // Also verify behavior when BraceWrapping.AfterFunction = true
   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
   MergeInlineOnly.BraceWrapping.AfterFunction = true;



More information about the cfe-commits mailing list