[clang] [clang-format] Apply SpaceBeforeInheritanceColon to enum underlying types (PR #189011)
Tharun V K via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 27 19:10:11 PDT 2026
https://github.com/tharunvk updated https://github.com/llvm/llvm-project/pull/189011
>From 6aefab3858a4d4b6eb2cba5a5c0d9b921d5c7293 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 | 18 ++++++++++++++++++
4 files changed, 26 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..8f2c8efd012c6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -18632,6 +18632,24 @@ 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);
+ verifyFormat("enum : int { E1 };", Style);
+ verifyFormat("typedef enum : int { E2 } E3;", Style);
+ verifyFormat("typedef enum A : int { E4 } B;", Style);
+
+ Style.SpaceBeforeInheritanceColon = false;
+ verifyFormat("enum A: int {};", Style);
+ verifyFormat("enum class B: int {};", Style);
+ verifyFormat("enum: int { E1 };", Style);
+ verifyFormat("typedef enum: int { E2 } E3;", Style);
+ verifyFormat("typedef enum A: int { E4 } B;", Style);
+}
+
TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
FormatStyle Style = getLLVMStyle();
More information about the cfe-commits
mailing list