KitsuneAlex updated this revision to Diff 529635.
KitsuneAlex added a comment.

I took the liberty and extended the unit tests for SpaceAfterTemplateKeyword 
and SpaceAfterOperator keyword to cover both possible cases, which lead to me 
discovering that my code was not quite right because of an early-out return 
path in a calling function.

Everything is fixed and tested now.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152443/new/

https://reviews.llvm.org/D152443

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -22905,6 +22905,16 @@
   FormatStyle Style = getLLVMStyle();
   Style.SpaceAfterTemplateKeyword = false;
   verifyFormat("template<int> void foo();", Style);
+  Style.SpaceAfterTemplateKeyword = true;
+  verifyFormat("template <int> void foo();", Style);
+}
+
+TEST_F(FormatTest, SpaceAfterOperatorKeyword) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SpaceAfterOperatorKeyword = false;
+  verifyFormat("bool operator==();", Style);
+  Style.SpaceAfterOperatorKeyword = true;
+  verifyFormat("bool operator ==();", Style);
 }
 
 TEST_F(FormatTest, TripleAngleBrackets) {
Index: clang/unittests/Format/ConfigParseTest.cpp
===================================================================
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -184,6 +184,7 @@
   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorKeyword);
   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3759,6 +3759,7 @@
       Left.Previous->is(tok::kw_operator)) {
     return false;
   }
+
   // co_await (x), co_yield (x), co_return (x)
   if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
       !Right.isOneOf(tok::semi, tok::r_paren)) {
@@ -4202,7 +4203,7 @@
       return true;
 
     if (Left.is(tok::kw_operator))
-      return Right.is(tok::coloncolon);
+      return Style.SpaceAfterOperatorKeyword || Right.is(tok::coloncolon);
     if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
         !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
       return true;
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1002,6 +1002,8 @@
     IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot);
     IO.mapOptional("SpaceAfterTemplateKeyword",
                    Style.SpaceAfterTemplateKeyword);
+    IO.mapOptional("SpaceAfterOperatorKeyword",
+                   Style.SpaceAfterOperatorKeyword);
     IO.mapOptional("SpaceAroundPointerQualifiers",
                    Style.SpaceAroundPointerQualifiers);
     IO.mapOptional("SpaceBeforeAssignmentOperators",
@@ -1440,6 +1442,7 @@
   LLVMStyle.SpaceAfterCStyleCast = false;
   LLVMStyle.SpaceAfterLogicalNot = false;
   LLVMStyle.SpaceAfterTemplateKeyword = true;
+  LLVMStyle.SpaceAfterOperatorKeyword = false;
   LLVMStyle.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
   LLVMStyle.SpaceBeforeCaseColon = false;
   LLVMStyle.SpaceBeforeCtorInitializerColon = true;
@@ -1731,6 +1734,7 @@
   MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
   MozillaStyle.PointerAlignment = FormatStyle::PAS_Left;
   MozillaStyle.SpaceAfterTemplateKeyword = false;
+  MozillaStyle.SpaceAfterOperatorKeyword = false;
   return MozillaStyle;
 }
 
Index: clang/include/clang/Format/Format.h
===================================================================
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -3700,6 +3700,14 @@
   /// \version 4
   bool SpaceAfterTemplateKeyword;
 
+  /// If \c true, a space will be inserted after the 'operator' keyword.
+  /// \code
+  ///    true:                                  false:
+  ///    bool operator ==(...);         vs.     bool operator==(...);
+  /// \endcode
+  /// \version 17
+  bool SpaceAfterOperatorKeyword;
+
   /// Different ways to put a space before opening parentheses.
   enum SpaceAroundPointerQualifiersStyle : int8_t {
     /// Don't ensure spaces around pointer qualifiers and use PointerAlignment
@@ -4412,6 +4420,7 @@
            SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
            SpaceAfterLogicalNot == R.SpaceAfterLogicalNot &&
            SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword &&
+           SpaceAfterOperatorKeyword == R.SpaceAfterOperatorKeyword &&
            SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
            SpaceBeforeCaseColon == R.SpaceBeforeCaseColon &&
            SpaceBeforeCpp11BracedList == R.SpaceBeforeCpp11BracedList &&
Index: clang/docs/ClangFormatStyleOptions.rst
===================================================================
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -4714,6 +4714,16 @@
      true:                                  false:
      template <int> void foo();     vs.     template<int> void foo();
 
+.. _SpaceAfterOperatorKeyword:
+
+**SpaceAfterOperatorKeyword** (``Boolean``) :versionbadge:`clang-format 17` :ref:`¶ <SpaceAfterOperatorKeyword>`
+  If ``true``, a space will be inserted after the 'operator' keyword.
+
+  .. code-block:: c++
+
+     true:                                  false:
+     bool operator ==(...);         vs.     bool operator==(...);
+
 .. _SpaceAroundPointerQualifiers:
 
 **SpaceAroundPointerQualifiers** (``SpaceAroundPointerQualifiersStyle``) :versionbadge:`clang-format 12` :ref:`¶ <SpaceAroundPointerQualifiers>`
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to