[clang] [clang-format] Fix regression with BlockIndent of Braced Initializers (PR #108717)

Gedare Bloom via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 22 13:13:13 PDT 2024


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

>From a95b990e48df19b8b674fe9df6bea803415129bf Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Sat, 14 Sep 2024 13:13:26 -0600
Subject: [PATCH 1/3] [clang-format] Fix regression with BlockIndent of Braced
 Initializers

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

diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index f29f8796ea9290..5c77af2da5add5 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -348,6 +348,13 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
     }
   }
 
+  // Don't allow breaking before a closing right brace of a block-indented
+  // braced list initializer if there was not already a break.
+  if (Current.is(tok::r_brace) && Current.MatchingParen &&
+      Current.isBlockIndentedInitRBrace(Style)) {
+    return CurrentState.BreakBeforeClosingBrace;
+  }
+
   // If binary operators are moved to the next line (including commas for some
   // styles of constructor initializers), that's always ok.
   if (!Current.isOneOf(TT_BinaryOperator, tok::comma) &&
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 5ebf0d7068dd6c..d1cb2b053e33d6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9336,6 +9336,9 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
                "        b));",
                Style);
+  verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n"
+               "    aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)};",
+               Style);
 
   Style.ColumnLimit = 30;
   verifyFormat("for (int foo = 0; foo < FOO;\n"
@@ -9395,6 +9398,9 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
       "fooooooooooo(new FOO::BARRRR(\n"
       "    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
       Style);
+  verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n"
+               "    aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)};",
+               Style);
 
   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
   Style.BinPackArguments = false;
@@ -9441,6 +9447,11 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
       "    aaaaaaaaaaaaaaaa\n"
       ");",
       Style);
+  verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n"
+               "    aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
+               "};",
+               Style);
+
   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
                "    const bool &aaaaaaaaa, const void *aaaaaaaaaa\n"
                ") const {\n"

>From f53155d15961be7e1f992814abe4eda53e9fa9cb Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Sun, 22 Sep 2024 14:12:22 -0600
Subject: [PATCH 2/3] Remove superfluous test cases

---
 clang/unittests/Format/FormatTest.cpp | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index d1cb2b053e33d6..213631e29ae536 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9336,9 +9336,6 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
                "        b));",
                Style);
-  verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n"
-               "    aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)};",
-               Style);
 
   Style.ColumnLimit = 30;
   verifyFormat("for (int foo = 0; foo < FOO;\n"
@@ -9398,9 +9395,6 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
       "fooooooooooo(new FOO::BARRRR(\n"
       "    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
       Style);
-  verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n"
-               "    aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)};",
-               Style);
 
   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
   Style.BinPackArguments = false;

>From 0738a90b387520a59a1d4c2dac28c96fe65c4548 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Sun, 22 Sep 2024 14:13:05 -0600
Subject: [PATCH 3/3] Update clang/lib/Format/ContinuationIndenter.cpp

Co-authored-by: Owen Pan <owenpiano at gmail.com>
---
 clang/lib/Format/ContinuationIndenter.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 5c77af2da5add5..c04cb9d5843ce7 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -348,8 +348,8 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
     }
   }
 
-  // Don't allow breaking before a closing right brace of a block-indented
-  // braced list initializer if there was not already a break.
+  // 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 &&
       Current.isBlockIndentedInitRBrace(Style)) {
     return CurrentState.BreakBeforeClosingBrace;



More information about the cfe-commits mailing list