[clang] [clang-format] Fix mismatched break in BlockIndent (PR #124998)

Gedare Bloom via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 30 18:58:40 PST 2025


https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/124998

>From 69658e8c2beb787b90ea29765bf428892d4e5ec4 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Wed, 29 Jan 2025 14:53:10 -0700
Subject: [PATCH 1/3] [clang-format] Fix mismatched break in BlockIndent

Near the ColumnLimit a break could be inserted before a right parens with
BlockIndent without a break after the matching left parens. Avoid these
hanging right parens by disallowing breaks before right parens unless
there was a break after the left parens.

Fixes #103306
---
 clang/lib/Format/ContinuationIndenter.cpp | 7 +++++++
 clang/unittests/Format/FormatTest.cpp     | 4 ++++
 2 files changed, 11 insertions(+)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index c311deaa17bb0ea..d0a09d305baaf7f 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -349,6 +349,13 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
     }
   }
 
+  // Allow breaking before the right parens with block indentation if there was
+  // a break after the left parens, which is tracked by BreakBeforeClosingParen
+  if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
+      Current.is(tok::r_paren)) {
+    return CurrentState.BreakBeforeClosingParen;
+  }
+
   // Don't allow breaking before a closing brace of a block-indented braced list
   // initializer if there isn't already a break.
   if (Current.is(tok::r_brace) && Current.MatchingParen &&
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 57f12221cdc7e66..e647c4fff78b0e3 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9608,6 +9608,10 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
       "    \"a aaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa\"\n"
       ");",
       Style);
+  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaa(\n"
+               "    &bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+               ");",
+               Style);
   Style.ColumnLimit = 60;
   verifyFormat("auto lambda =\n"
                "    [&b](\n"

>From f83576121f98831ea894076371fc60aaae3d4402 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Thu, 30 Jan 2025 19:44:03 -0700
Subject: [PATCH 2/3] Accept suggestions from review

---
 clang/lib/Format/ContinuationIndenter.cpp | 2 +-
 clang/unittests/Format/FormatTest.cpp     | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index d0a09d305baaf7f..6f7d213c0b55996 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -350,7 +350,7 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
   }
 
   // Allow breaking before the right parens with block indentation if there was
-  // a break after the left parens, which is tracked by BreakBeforeClosingParen
+  // a break after the left parens, which is tracked by BreakBeforeClosingParen.
   if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
       Current.is(tok::r_paren)) {
     return CurrentState.BreakBeforeClosingParen;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index e647c4fff78b0e3..8bdcfa30f2efb73 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9608,16 +9608,16 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
       "    \"a aaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa\"\n"
       ");",
       Style);
-  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaa(\n"
-               "    &bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
-               ");",
-               Style);
   Style.ColumnLimit = 60;
   verifyFormat("auto lambda =\n"
                "    [&b](\n"
                "        auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
                "    ) {};",
                Style);
+  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(\n"
+               "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+               ");",
+               Style);
 }
 
 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {

>From 0c2c568d4d6d5cc776731fbffbcc7e2613ae1061 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Thu, 30 Jan 2025 19:57:39 -0700
Subject: [PATCH 3/3] fix typo in test

---
 clang/unittests/Format/FormatTest.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 8bdcfa30f2efb73..02347e0f15201a1 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9615,7 +9615,7 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
                "    ) {};",
                Style);
   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(\n"
-               "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+               "    &bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
                ");",
                Style);
 }



More information about the cfe-commits mailing list