[clang] [clang-format] Stop ctor initializer from being inlined (PR #150361)
Eric Li via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 24 00:44:31 PDT 2025
https://github.com/tJener updated https://github.com/llvm/llvm-project/pull/150361
>From 2c1a695bd602b45f68bdc97043ff2b144372230b Mon Sep 17 00:00:00 2001
From: Eric Li <li.zhe.hua at gmail.com>
Date: Wed, 23 Jul 2025 22:18:05 -0400
Subject: [PATCH 1/2] [clang-format] Stop ctor initializer from being inlined
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.
---
clang/lib/Format/ContinuationIndenter.cpp | 3 ++-
clang/unittests/Format/FormatTest.cpp | 31 +++++++++++++++++++++++
2 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 4010f7fbd25be..83920ea492870 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1724,7 +1724,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 944e7c3fb152a..75bdb9444bc45 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -7771,6 +7771,37 @@ 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"
+ " } {}",
+ "MyClass::MyClass():aaaa{0,}{}", getGoogleStyle());
+ verifyFormat("MyClass::MyClass()\n"
+ " : aaaa(0),\n"
+ " bbbb{\n"
+ " 0,\n"
+ " } {}",
+ "MyClass::MyClass():aaaa(0),bbbb{0,}{}", getGoogleStyle());
+ verifyFormat("MyClass::MyClass()\n"
+ " : aaaa(0),\n"
+ " bbbb{\n"
+ " 0,\n"
+ " },\n"
+ " cccc{\n"
+ " 0,\n"
+ " } {}",
+ "MyClass::MyClass():aaaa(0),bbbb{0,},cccc{0,}{}",
+ getGoogleStyle());
+ verifyFormat("MyClass::MyClass()\n"
+ " : aaaa{\n"
+ " 0,\n"
+ " },\n"
+ " bbbb{\n"
+ " 0,\n"
+ " } {}",
+ "MyClass::MyClass():aaaa{0,},bbbb{0,}{}", getGoogleStyle());
}
TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
>From 8c28f84f8c8a31356526fa37cca4a4bcdc7535da Mon Sep 17 00:00:00 2001
From: Eric Li <li.zhe.hua at gmail.com>
Date: Thu, 24 Jul 2025 03:44:23 -0400
Subject: [PATCH 2/2] fixup
---
clang/unittests/Format/FormatTest.cpp | 23 +----------------------
1 file changed, 1 insertion(+), 22 deletions(-)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 75bdb9444bc45..0d086bb5290e1 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -7773,27 +7773,6 @@ TEST_F(FormatTest, ConstructorInitializers) {
" aaaa(aaaa) {}");
// Braced initializers with trailing commas.
- verifyFormat("MyClass::MyClass()\n"
- " : aaaa{\n"
- " 0,\n"
- " } {}",
- "MyClass::MyClass():aaaa{0,}{}", getGoogleStyle());
- verifyFormat("MyClass::MyClass()\n"
- " : aaaa(0),\n"
- " bbbb{\n"
- " 0,\n"
- " } {}",
- "MyClass::MyClass():aaaa(0),bbbb{0,}{}", getGoogleStyle());
- verifyFormat("MyClass::MyClass()\n"
- " : aaaa(0),\n"
- " bbbb{\n"
- " 0,\n"
- " },\n"
- " cccc{\n"
- " 0,\n"
- " } {}",
- "MyClass::MyClass():aaaa(0),bbbb{0,},cccc{0,}{}",
- getGoogleStyle());
verifyFormat("MyClass::MyClass()\n"
" : aaaa{\n"
" 0,\n"
@@ -7801,7 +7780,7 @@ TEST_F(FormatTest, ConstructorInitializers) {
" bbbb{\n"
" 0,\n"
" } {}",
- "MyClass::MyClass():aaaa{0,},bbbb{0,}{}", getGoogleStyle());
+ "MyClass::MyClass():aaaa{0,},bbbb{0,}{}");
}
TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
More information about the cfe-commits
mailing list