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

Applied all suggested changes and added a suiting option for aformentioned 
edge-case for call expressions.
Also added the missing release notes to the apropriate section.


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/docs/ReleaseNotes.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
@@ -22901,8 +22901,27 @@
                Spaces);
 }
 
+TEST_F(FormatTest, SpaceAfterOperatorKeyword) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("bool operator==();", Style);
+
+  Style.SpaceAfterOperatorKeyword = true;
+  verifyFormat("bool operator ==();", Style);
+}
+
+TEST_F(FormatTest, SpaceAfterOperatorKeywordInCall) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  
+  Style.SpaceAfterOperatorKeywordInCall = true;
+  verifyFormat("foo.operator ==();", Style);
+  verifyFormat("foo.Foo::operator ==();", Style);
+}
+
 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
   FormatStyle Style = getLLVMStyle();
+  verifyFormat("template <int> void foo();", Style);
   Style.SpaceAfterTemplateKeyword = false;
   verifyFormat("template<int> void foo();", Style);
 }
Index: clang/unittests/Format/ConfigParseTest.cpp
===================================================================
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -183,6 +183,8 @@
   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorKeyword);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorKeywordInCall);
   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
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)) {
@@ -4201,8 +4202,12 @@
     if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon))
       return true;
 
-    if (Left.is(tok::kw_operator))
-      return Right.is(tok::coloncolon);
+    if (Left.is(tok::kw_operator)) {
+      if (Left.Previous && Left.Previous->isOneOf(tok::coloncolon, tok::period))
+        return Style.SpaceAfterOperatorKeywordInCall || 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
@@ -1000,6 +1000,10 @@
     IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
     IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
     IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot);
+    IO.mapOptional("SpaceAfterOperatorKeyword",
+                   Style.SpaceAfterOperatorKeyword);
+    IO.mapOptional("SpaceAfterOperatorKeywordInCall",
+                   Style.SpaceAfterOperatorKeywordInCall);
     IO.mapOptional("SpaceAfterTemplateKeyword",
                    Style.SpaceAfterTemplateKeyword);
     IO.mapOptional("SpaceAroundPointerQualifiers",
@@ -1439,6 +1443,8 @@
   LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
   LLVMStyle.SpaceAfterCStyleCast = false;
   LLVMStyle.SpaceAfterLogicalNot = false;
+  LLVMStyle.SpaceAfterOperatorKeyword = false;
+  LLVMStyle.SpaceAfterOperatorKeywordInCall = false;
   LLVMStyle.SpaceAfterTemplateKeyword = true;
   LLVMStyle.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
   LLVMStyle.SpaceBeforeCaseColon = false;
Index: clang/include/clang/Format/Format.h
===================================================================
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -3692,6 +3692,24 @@
   /// \version 9
   bool SpaceAfterLogicalNot;
 
+  /// 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;
+
+  /// If \c true, a space will be inserted after the 'operator' keyword in a call expression.
+  /// This option defaults to SpaceAfterOperatorKeyword.
+  /// \code
+  ///    true:                                  false:
+  ///    foo.operator ==(...);         vs.      foo.operator==(...);
+  ///    foo.Foo::operator ==(...);    vs.      foo.Foo::operator==(...);
+  /// \endcode
+  /// \version 17
+  bool SpaceAfterOperatorKeywordInCall;
+
   /// If \c true, a space will be inserted after the 'template' keyword.
   /// \code
   ///    true:                                  false:
@@ -4411,6 +4429,8 @@
            SortJavaStaticImport == R.SortJavaStaticImport &&
            SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
            SpaceAfterLogicalNot == R.SpaceAfterLogicalNot &&
+           SpaceAfterOperatorKeyword == R.SpaceAfterOperatorKeyword &&
+           SpaceAfterOperatorKeywordInCall == R.SpaceAfterOperatorKeywordInCall &&
            SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword &&
            SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
            SpaceBeforeCaseColon == R.SpaceBeforeCaseColon &&
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -703,6 +703,8 @@
 - Add ``BracedInitializerIndentWidth`` which can be used to configure
   the indentation level of the contents of braced init lists.
 - Add ``KeepEmptyLinesAtEOF`` to keep empty lines at end of file.
+- Add ``SpaceAfterOperatorKeyword`` style option.
+- Add ``SpaceAfterOperatorKeywordInCall`` style option.
 
 libclang
 --------
Index: clang/docs/ClangFormatStyleOptions.rst
===================================================================
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -4704,6 +4704,27 @@
      true:                                  false:
      ! someExpression();            vs.     !someExpression();
 
+.. _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==(...);
+
+.. _SpaceAfterOperatorKeywordInCall:
+
+**SpaceAfterOperatorKeywordInCall** (``Boolean``) :versionbadge:`clang-format 17` :ref:`¶ <SpaceAfterOperatorKeywordInCall>`
+  If ``true``, a space will be inserted after the 'operator' keyword if the keyword is used in a call expression.
+
+  .. code-block:: c++
+
+     true:                                  false:
+     foo.operator ==(...);         vs.      foo.operator==(...);
+     foo.Foo::operator ==(...);    vs.      foo.Foo::operator==(...);
+
 .. _SpaceAfterTemplateKeyword:
 
 **SpaceAfterTemplateKeyword** (``Boolean``) :versionbadge:`clang-format 4` :ref:`¶ <SpaceAfterTemplateKeyword>`
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to