Author: Eric Li Date: 2025-07-24T04:06:03-04:00 New Revision: 531cf8298b08eacdf670bac8c28db97a5dc8cb01
URL: https://github.com/llvm/llvm-project/commit/531cf8298b08eacdf670bac8c28db97a5dc8cb01 DIFF: https://github.com/llvm/llvm-project/commit/531cf8298b08eacdf670bac8c28db97a5dc8cb01.diff LOG: [clang-format] Stop ctor initializer from being inlined (#150361) The colon in a constructor's initializer list triggers the inlining of a nested block as if it was a conditional operator expression. This prevents line breaks under certain circumstances when the initializer list contains braced initializers, which in turn prevents the line formatter from finding a solution. In this commit we exclude colons that are a constructor initializer colon from consideration of nested block inlining. Fixes #97242. Fixes #81822. Added: Modified: clang/lib/Format/ContinuationIndenter.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index bf67f9e5fd241..9a10403b858f9 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -1725,7 +1725,8 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, } if (Previous && (Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr) || (Previous->isOneOf(tok::l_paren, tok::comma, tok::colon) && - !Previous->isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)))) { + !Previous->isOneOf(TT_DictLiteral, TT_ObjCMethodExpr, + TT_CtorInitializerColon)))) { CurrentState.NestedBlockInlined = !Newline && hasNestedBlockInlined(Previous, Current, Style); } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index dbf6950446ef0..a5680691b11a2 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -7783,6 +7783,16 @@ TEST_F(FormatTest, ConstructorInitializers) { "Constructor() :\n" " // Comment forcing unwanted break.\n" " aaaa(aaaa) {}"); + + // Braced initializers with trailing commas. + verifyFormat("MyClass::MyClass()\n" + " : aaaa{\n" + " 0,\n" + " },\n" + " bbbb{\n" + " 0,\n" + " } {}", + "MyClass::MyClass():aaaa{0,},bbbb{0,}{}"); } TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits