[clang] [clang-format] Apply SpaceBeforeInheritanceColon to enum underlying t… (PR #189011)
Tharun V K via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 27 07:44:54 PDT 2026
https://github.com/tharunvk updated https://github.com/llvm/llvm-project/pull/189011
>From 6d7f3afb39a87abceec509627c4bf4cb18b9c2f8 Mon Sep 17 00:00:00 2001
From: Tharun V K <Tharun.V.K at ibm.com>
Date: Fri, 27 Mar 2026 19:47:35 +0530
Subject: [PATCH] [clang-format] Apply SpaceBeforeInheritanceColon to enum
underlying types
---
clang/docs/ClangFormatStyleOptions.rst | 4 +++-
clang/include/clang/Format/Format.h | 4 +++-
clang/lib/Format/TokenAnnotator.cpp | 4 ++--
clang/unittests/Format/FormatTest.cpp | 12 ++++++++++++
4 files changed, 20 insertions(+), 4 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index f637b81bb75bc..69def14d7e371 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -6834,12 +6834,14 @@ the configuration (without a prefix: ``Auto``).
.. _SpaceBeforeInheritanceColon:
**SpaceBeforeInheritanceColon** (``Boolean``) :versionbadge:`clang-format 7` :ref:`¶ <SpaceBeforeInheritanceColon>`
- If ``false``, spaces will be removed before inheritance colon.
+ If ``false``, spaces will be removed before inheritance colon
+ and enum underlying type colon.
.. code-block:: c++
true: false:
class Foo : Bar {} vs. class Foo: Bar {}
+ enum E : int {} enum E: int {}
.. _SpaceBeforeJsonColon:
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 8c90cc2e98121..9247814a3edf2 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -5121,10 +5121,12 @@ struct FormatStyle {
/// \version 7
bool SpaceBeforeCtorInitializerColon;
- /// If ``false``, spaces will be removed before inheritance colon.
+ /// If ``false``, spaces will be removed before inheritance colon
+ /// and enum underlying type colon.
/// \code
/// true: false:
/// class Foo : Bar {} vs. class Foo: Bar {}
+ /// enum E : int {} enum E: int {}
/// \endcode
/// \version 7
bool SpaceBeforeInheritanceColon;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index d2cdc28a7da7b..50f290817eea2 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1444,9 +1444,9 @@ class AnnotatingParser {
Scopes.back() == ST_Class)) {
Tok->setType(TT_BitFieldColon);
} else if (Contexts.size() == 1 &&
- Line.getFirstNonComment()->isNoneOf(tok::kw_enum, tok::kw_case,
+ Line.getFirstNonComment()->isNoneOf(tok::kw_case,
tok::kw_default) &&
- !Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
+ !Line.startsWith(tok::kw_typedef)) {
if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
Prev->ClosesRequiresClause) {
Tok->setType(TT_CtorInitializerColon);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 2701a7fca7346..b01598c7c8907 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -18632,6 +18632,18 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
InvertedSpaceStyle);
}
+TEST_F(FormatTest, EnumUnderlyingTypeUsesInheritanceColonSpacing) {
+ FormatStyle Style = getLLVMStyle();
+
+ Style.SpaceBeforeInheritanceColon = true;
+ verifyFormat("enum A : int {};", Style);
+ verifyFormat("enum class B : int {};", Style);
+
+ Style.SpaceBeforeInheritanceColon = false;
+ verifyFormat("enum A: int {};", Style);
+ verifyFormat("enum class B: int {};", Style);
+}
+
TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
FormatStyle Style = getLLVMStyle();
More information about the cfe-commits
mailing list