Author: sstwcw Date: 2026-05-21T18:49:30Z New Revision: e57ade6b092274d75aa9e4a725a1df0dbed8070b
URL: https://github.com/llvm/llvm-project/commit/e57ade6b092274d75aa9e4a725a1df0dbed8070b DIFF: https://github.com/llvm/llvm-project/commit/e57ade6b092274d75aa9e4a725a1df0dbed8070b.diff LOG: [clang-format] Stop indenting the closing brace for the initializer (#197590) new ```C++ SomeStruct // s = { "xxxxxxxxxxxxx", }; ``` old ```C++ SomeStruct // s = { "xxxxxxxxxxxxx", }; ``` See the comment. https://github.com/llvm/llvm-project/pull/192299#issuecomment-4414273071 Added: Modified: clang/lib/Format/ContinuationIndenter.cpp clang/lib/Format/ContinuationIndenter.h clang/unittests/Format/AlignBracketsTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 361072127f8e1..085b48fd1ed78 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, @@ -1981,6 +1999,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..ed347591525ea 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..e7e57bf839c0e 100644 --- a/clang/unittests/Format/AlignBracketsTest.cpp +++ b/clang/unittests/Format/AlignBracketsTest.cpp @@ -608,6 +608,53 @@ 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::SomeStruct{\n" + " \"xxxxxxxxxxxxx\",\n" + " \"yyyyyyyyyyyyy\",\n" + " \"zzzzzzzzzzzzz\",\n" + "};"); + verifyFormat("void x() {\n" + " SomeStruct //\n" + " s = SomeStruct{\n" + " \"xxxxxxxxxxxxx\",\n" + " \"yyyyyyyyyyyyy\",\n" + " \"zzzzzzzzzzzzz\",\n" + " };\n" + "}"); + verifyFormat("void x() {\n" + " SomeStruct //\n" + " s{\n" + " \"xxxxxxxxxxxxx\",\n" + " \"yyyyyyyyyyyyy\",\n" + " \"zzzzzzzzzzzzz\",\n" + " };\n" + "}"); + verifyFormat("SomeArrayT //\n" + " a[3] = {\n" + " {\n" + " foo,\n" + " bar,\n" + " },\n" + " {\n" + " foo,\n" + " bar,\n" + " },\n" + " SomeArrayT{},\n" + "};"); } TEST_F(AlignBracketsTest, AllowAllArgumentsOnNextLineDontAlign) { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
