Skip to content

Commit 531cf82

Browse files
authored
[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.
1 parent 78ccaf1 commit 531cf82

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,8 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
17251725
}
17261726
if (Previous && (Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr) ||
17271727
(Previous->isOneOf(tok::l_paren, tok::comma, tok::colon) &&
1728-
!Previous->isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)))) {
1728+
!Previous->isOneOf(TT_DictLiteral, TT_ObjCMethodExpr,
1729+
TT_CtorInitializerColon)))) {
17291730
CurrentState.NestedBlockInlined =
17301731
!Newline && hasNestedBlockInlined(Previous, Current, Style);
17311732
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7783,6 +7783,16 @@ TEST_F(FormatTest, ConstructorInitializers) {
77837783
"Constructor() :\n"
77847784
" // Comment forcing unwanted break.\n"
77857785
" aaaa(aaaa) {}");
7786+
7787+
// Braced initializers with trailing commas.
7788+
verifyFormat("MyClass::MyClass()\n"
7789+
" : aaaa{\n"
7790+
" 0,\n"
7791+
" },\n"
7792+
" bbbb{\n"
7793+
" 0,\n"
7794+
" } {}",
7795+
"MyClass::MyClass():aaaa{0,},bbbb{0,}{}");
77867796
}
77877797

77887798
TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {

0 commit comments

Comments
 (0)