https://github.com/sstwcw created
https://github.com/llvm/llvm-project/pull/197590
new
```C++
SomeStruct //
s = {
"xxxxxxxxxxxxx",
};
```
old
```C++
SomeStruct //
s = {
"xxxxxxxxxxxxx",
};
```
>From fa371efeb75b6145ea4d9f642438e61064e2f56d Mon Sep 17 00:00:00 2001
From: sstwcw <[email protected]>
Date: Wed, 13 May 2026 23:44:52 +0000
Subject: [PATCH] [clang-format] Stop indenting the closing brace for the
initializer
new
```C++
SomeStruct //
s = {
"xxxxxxxxxxxxx",
};
```
old
```C++
SomeStruct //
s = {
"xxxxxxxxxxxxx",
};
```
---
clang/lib/Format/ContinuationIndenter.cpp | 21 +++++++++++++++++++-
clang/lib/Format/ContinuationIndenter.h | 4 ++++
clang/unittests/Format/AlignBracketsTest.cpp | 19 ++++++++++++++++++
3 files changed, 43 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp
b/clang/lib/Format/ContinuationIndenter.cpp
index 485fe382bda3a..49f5efd28562f 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1528,8 +1528,26 @@ ContinuationIndenter::getNewLineColumn(const LineState
&State) {
State.Stack.size() > 1) {
if (Current.closesBlockOrBlockTypeList(Style))
return State.Stack[State.Stack.size() - 2].NestedBlockIndent;
- if (Current.MatchingParen && Current.MatchingParen->is(BK_BracedInit))
+ if (Current.MatchingParen && Current.MatchingParen->is(BK_BracedInit)) {
+ // The brace should line up with the start of the line in this case. The
+ // stack depth is checked to make sure that the brace is at the top
+ // level. It should contain the levels for the top, the assignment if
+ // there is an equal sign, and the braces.
+ //
+ // SomeStruct //
+ // s = {
+ // "xxxxxxxxxxxxx",
+ // };
+ if ((State.Stack.size() == 2 &&
+ Current.MatchingParen->getPreviousNonComment() &&
+ Current.MatchingParen->getPreviousNonComment()->is(
+ TT_StartOfName)) ||
+ (State.Stack.size() == 3 &&
+ State.Stack[1].precedence == prec::Assignment)) {
+ return State.FirstIndent;
+ }
return State.Stack[State.Stack.size() - 2].LastSpace;
+ }
return State.FirstIndent;
}
// Indent a closing parenthesis at the previous level if followed by a semi,
@@ -1979,6 +1997,7 @@ void
ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
NewParenState.UnindentOperator = false;
NewParenState.NoLineBreak =
NewParenState.NoLineBreak || CurrentState.NoLineBreakInOperand;
+ NewParenState.precedence = PrecedenceLevel;
// Don't propagate AvoidBinPacking into subexpressions of arg/param lists.
if (PrecedenceLevel > prec::Comma)
diff --git a/clang/lib/Format/ContinuationIndenter.h
b/clang/lib/Format/ContinuationIndenter.h
index fcd9b51bc6f8f..d09f3625c718a 100644
--- a/clang/lib/Format/ContinuationIndenter.h
+++ b/clang/lib/Format/ContinuationIndenter.h
@@ -301,6 +301,10 @@ struct ParenState {
/// Used to align further variables if necessary.
unsigned VariablePos = 0;
+ /// The precedence. The outermost level and the levels corresponding to
tokens
+ /// have prec::Unknown.
+ prec::Level precedence = prec::Unknown;
+
/// Whether a newline needs to be inserted before the block's closing
/// brace.
///
diff --git a/clang/unittests/Format/AlignBracketsTest.cpp
b/clang/unittests/Format/AlignBracketsTest.cpp
index fcfcae20e3e11..78d2b8da8b398 100644
--- a/clang/unittests/Format/AlignBracketsTest.cpp
+++ b/clang/unittests/Format/AlignBracketsTest.cpp
@@ -608,6 +608,25 @@ TEST_F(AlignBracketsTest,
AlignAfterOpenBracketBlockIndentInitializers) {
" {baz},\n"
"};",
Style);
+ // The closing brace should be at the start of the line.
+ verifyFormat("SomeStruct //\n"
+ " s = SomeStruct{\n"
+ " \"xxxxxxxxxxxxx\",\n"
+ " \"yyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzz\",\n"
+ "};");
+ verifyFormat("SomeStruct //\n"
+ " s{\n"
+ " \"xxxxxxxxxxxxx\",\n"
+ " \"yyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzz\",\n"
+ "};");
+ verifyFormat("SomeStruct //\n"
+ " s = SomeStruct{\n"
+ " \"xxxxxxxxxxxxx\",\n"
+ " \"yyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzz\",\n"
+ "};");
}
TEST_F(AlignBracketsTest, AllowAllArgumentsOnNextLineDontAlign) {
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits