https://github.com/tharunvk created 
https://github.com/llvm/llvm-project/pull/189011

This change extends the existing SpaceBeforeInheritanceColon option
to also control spacing before the colon in enum underlying type
declarations.
 
Previously, enum underlying type colons were not configurable.
 
This avoids introducing a new option and keeps behavior consistent
with similar syntax constructs.
 
Fixes #188734
 
Signed off by: Tharun

>From b4d46181be451b9c8458050eaa91d25ab668bf97 Mon Sep 17 00:00:00 2001
From: Tharun V K <[email protected]>
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();
 

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to