https://github.com/niXman created
https://github.com/llvm/llvm-project/pull/190657
This fix adds two new clang-format style options to control spacing after
constructor initializer punctuation:
- SpaceAfterCtorInitializerColon
- SpaceAfterCtorInitializerComma
These options complement existing `SpaceBeforeCtorInitializerColon` and allow
formatting styles such as `:prev` and `,next`.
New Behavior
1) `SpaceAfterCtorInitializerColon`
Controls whether a space is inserted after the constructor initializer colon
(`:`).
2) `SpaceAfterCtorInitializerComma`
Controls whether a space is inserted after commas inside constructor
initializer lists.
Default values are true for both options (backward-compatible behavior).
Input:
```
struct X {
X()
: prev{nullptr}
, next{nullptr} {}
};
```
Result:
```
struct X {
X()
:prev{nullptr}
,next{nullptr} {}
};
```
>From 189602113f7336b9dca3f9c21e20afe834893f7c Mon Sep 17 00:00:00 2001
From: niXman <[email protected]>
Date: Mon, 6 Apr 2026 22:17:47 +0300
Subject: [PATCH] initial impl
---
clang/docs/ClangFormatStyleOptions.rst | 20 ++++++++++++++++++++
clang/include/clang/Format/Format.h | 20 ++++++++++++++++++++
clang/lib/Format/Format.cpp | 6 ++++++
clang/lib/Format/TokenAnnotator.cpp | 4 ++++
clang/unittests/Format/ConfigParseTest.cpp | 2 ++
clang/unittests/Format/FormatTest.cpp | 6 ++++++
6 files changed, 58 insertions(+)
diff --git a/clang/docs/ClangFormatStyleOptions.rst
b/clang/docs/ClangFormatStyleOptions.rst
index ed4e2aaa26052..84a7569d50c27 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -6678,6 +6678,26 @@ the configuration (without a prefix: ``Auto``).
true: false:
class Foo : Bar {} vs. class Foo: Bar {}
+.. _SpaceAfterCtorInitializerColon:
+
+**SpaceAfterCtorInitializerColon** (``Boolean``) :versionbadge:`clang-format
22` :ref:`¶ <SpaceAfterCtorInitializerColon>`
+ If ``false``, spaces will be removed after constructor initializer colon.
+
+ .. code-block:: c++
+
+ true: false:
+ Foo::Foo() : a(a) {} Foo::Foo() :a(a) {}
+
+.. _SpaceAfterCtorInitializerComma:
+
+**SpaceAfterCtorInitializerComma** (``Boolean``) :versionbadge:`clang-format
22` :ref:`¶ <SpaceAfterCtorInitializerComma>`
+ If ``false``, spaces will be removed after constructor initializer comma.
+
+ .. code-block:: c++
+
+ true: false:
+ Foo::Foo() : a(a), b(b) {} Foo::Foo() : a(a),b(b) {}
+
.. _SpaceBeforeJsonColon:
**SpaceBeforeJsonColon** (``Boolean``) :versionbadge:`clang-format 17` :ref:`¶
<SpaceBeforeJsonColon>`
diff --git a/clang/include/clang/Format/Format.h
b/clang/include/clang/Format/Format.h
index 2028c963ac306..b70b47aa6a5cb 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -4955,6 +4955,22 @@ struct FormatStyle {
/// \version 7
bool SpaceBeforeCtorInitializerColon;
+ /// If ``false``, spaces will be removed after constructor initializer colon.
+ /// \code
+ /// true: false:
+ /// Foo::Foo() : a(a) {} Foo::Foo() :a(a) {}
+ /// \endcode
+ /// \version 22
+ bool SpaceAfterCtorInitializerColon;
+
+ /// If ``false``, spaces will be removed after constructor initializer comma.
+ /// \code
+ /// true: false:
+ /// Foo::Foo() : a(a), b(b) {} Foo::Foo() : a(a),b(b) {}
+ /// \endcode
+ /// \version 22
+ bool SpaceAfterCtorInitializerComma;
+
/// If ``false``, spaces will be removed before inheritance colon.
/// \code
/// true: false:
@@ -5909,6 +5925,10 @@ struct FormatStyle {
SpaceBeforeCpp11BracedList == R.SpaceBeforeCpp11BracedList &&
SpaceBeforeCtorInitializerColon ==
R.SpaceBeforeCtorInitializerColon &&
+ SpaceAfterCtorInitializerColon ==
+ R.SpaceAfterCtorInitializerColon &&
+ SpaceAfterCtorInitializerComma ==
+ R.SpaceAfterCtorInitializerComma &&
SpaceBeforeInheritanceColon == R.SpaceBeforeInheritanceColon &&
SpaceBeforeJsonColon == R.SpaceBeforeJsonColon &&
SpaceBeforeParens == R.SpaceBeforeParens &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 2f67ec86b101a..dd05f27536b07 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1374,6 +1374,10 @@ template <> struct MappingTraits<FormatStyle> {
Style.SpaceBeforeCpp11BracedList);
IO.mapOptional("SpaceBeforeCtorInitializerColon",
Style.SpaceBeforeCtorInitializerColon);
+ IO.mapOptional("SpaceAfterCtorInitializerColon",
+ Style.SpaceAfterCtorInitializerColon);
+ IO.mapOptional("SpaceAfterCtorInitializerComma",
+ Style.SpaceAfterCtorInitializerComma);
IO.mapOptional("SpaceBeforeInheritanceColon",
Style.SpaceBeforeInheritanceColon);
IO.mapOptional("SpaceBeforeJsonColon", Style.SpaceBeforeJsonColon);
@@ -1879,6 +1883,8 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind
Language) {
LLVMStyle.SpaceBeforeCaseColon = false;
LLVMStyle.SpaceBeforeCpp11BracedList = false;
LLVMStyle.SpaceBeforeCtorInitializerColon = true;
+ LLVMStyle.SpaceAfterCtorInitializerColon = true;
+ LLVMStyle.SpaceAfterCtorInitializerComma = true;
LLVMStyle.SpaceBeforeInheritanceColon = true;
LLVMStyle.SpaceBeforeJsonColon = false;
LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
diff --git a/clang/lib/Format/TokenAnnotator.cpp
b/clang/lib/Format/TokenAnnotator.cpp
index 1b588435d6302..32dcc83ce8902 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5521,6 +5521,8 @@ bool TokenAnnotator::spaceRequiredBefore(const
AnnotatedLine &Line,
// In an unexpanded macro call we only find the parentheses and commas
// in a line; the commas and closing parenthesis do not require a space.
(Left.Children.empty() || !Left.MacroParent)) {
+ if (Left.is(TT_CtorInitializerComma))
+ return Style.SpaceAfterCtorInitializerComma;
return true;
}
if (Right.is(tok::comma))
@@ -5529,6 +5531,8 @@ bool TokenAnnotator::spaceRequiredBefore(const
AnnotatedLine &Line,
return true;
if (Right.is(TT_CtorInitializerColon))
return Style.SpaceBeforeCtorInitializerColon;
+ if (Left.is(TT_CtorInitializerColon))
+ return Style.SpaceAfterCtorInitializerColon;
if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
return false;
if (Right.is(TT_RangeBasedForLoopColon) &&
diff --git a/clang/unittests/Format/ConfigParseTest.cpp
b/clang/unittests/Format/ConfigParseTest.cpp
index f270602f32604..fdb3fc49d2c9d 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -220,6 +220,8 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
+ CHECK_PARSE_BOOL(SpaceAfterCtorInitializerColon);
+ CHECK_PARSE_BOOL(SpaceAfterCtorInitializerComma);
CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
CHECK_PARSE_BOOL(SpaceBeforeJsonColon);
CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
diff --git a/clang/unittests/Format/FormatTest.cpp
b/clang/unittests/Format/FormatTest.cpp
index dfc21bcefad53..1a2aad62faba6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -18043,6 +18043,12 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
+ CtorInitializerStyle.SpaceAfterCtorInitializerColon = false;
+ verifyFormat("Foo::Foo():foo(1) {}", CtorInitializerStyle);
+ CtorInitializerStyle.SpaceAfterCtorInitializerComma = false;
+ verifyFormat("Foo::Foo():foo(1),bar(2) {}", CtorInitializerStyle);
+ CtorInitializerStyle.SpaceAfterCtorInitializerColon = true;
+ CtorInitializerStyle.SpaceAfterCtorInitializerComma = true;
verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
verifyFormat("{\n"
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits