Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 22, 2024

Conversation

gedare
Copy link
Contributor

@gedare gedare commented Sep 14, 2024

Fixes #73584.

@llvmbot
Copy link
Member

llvmbot commented Sep 14, 2024

@llvm/pr-subscribers-clang-format

Author: Gedare Bloom (gedare)

Changes

Fixes #73584.


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

2 Files Affected:

  • (modified) clang/lib/Format/ContinuationIndenter.cpp (+7)
  • (modified) clang/unittests/Format/FormatTest.cpp (+11)
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"

Copy link
Contributor

@mydeveloperday mydeveloperday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks ok to me can we have @owenca or @HazardyKnusperkeks give it the once over, but I'm good with it.

@owenca owenca merged commit df935ff into llvm:main Sep 22, 2024
8 checks passed
@gedare gedare deleted the 73584 branch September 23, 2024 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang-format: Behavior changes between 16.0 and 17.0
4 participants