llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Tharun V K (tharunvk)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/189011.diff


4 Files Affected:

- (modified) clang/docs/ClangFormatStyleOptions.rst (+3-1) 
- (modified) clang/include/clang/Format/Format.h (+3-1) 
- (modified) clang/lib/Format/TokenAnnotator.cpp (+2-2) 
- (modified) clang/unittests/Format/FormatTest.cpp (+12) 


``````````diff
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();
 

``````````

</details>


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

Reply via email to