KitsuneAlex updated this revision to Diff 530303.
KitsuneAlex marked 2 inline comments as done.
KitsuneAlex removed reviewers: lattner, craig.topper, RKSimon, respindola, 
rymiel, owenpan.
KitsuneAlex added a comment.
Herald added reviewers: rymiel, owenpan.

This revision of the diff changes the names of the new style options to 
**SpaceAfterOperatorOverload** and **SpaceAfterOperatorCall**, which clarifies 
their scope a lot better i think.
I also rewrote the logic to apply formatting that fits exactly what their name 
implies respectively,
the new tests i added should cover all common use cases (except i forgot some 
again).


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,43 @@
                Spaces);
 }
 
+TEST_F(FormatTest, SpaceAfterOperatorOverload) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  verifyFormat("foo->operator==();", Style);
+  verifyFormat("foo->Foo::operator==();", Style);
+  Style.SpaceAfterOperatorOverload = true;
+  verifyFormat("bool operator ==();", Style);
+  verifyFormat("bool Foo::operator ==();", Style);
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  verifyFormat("foo->operator==();", Style);
+  verifyFormat("foo->Foo::operator==();", Style);
+}
+
+TEST_F(FormatTest, SpaceAfterOperatorCall) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  verifyFormat("foo->operator==();", Style);
+  verifyFormat("foo->Foo::operator==();", Style);
+  Style.SpaceAfterOperatorCall = true;
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("foo.operator ==();", Style);
+  verifyFormat("foo.Foo::operator ==();", Style);
+  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(SpaceAfterOperatorOverload);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorCall);
   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
@@ -4200,9 +4200,21 @@
     // Space in __attribute__((attr)) ::type.
     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.hasWhitespaceBefore())
+        return Style.SpaceAfterOperatorOverload || Right.is(tok::coloncolon);
+      if (!Left.Previous)
+        return Right.is(tok::coloncolon);
+      FormatToken const *Previous = Left.Previous;
+      while (Previous) {
+        if (!Previous->hasWhitespaceBefore()) {
+          Previous = Previous->Previous;
+          continue;
+        }
+        return Style.SpaceAfterOperatorOverload || Right.is(tok::coloncolon);
+      }
+      return Style.SpaceAfterOperatorCall || 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,9 @@
     IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
     IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
     IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot);
+    IO.mapOptional("SpaceAfterOperatorCall", Style.SpaceAfterOperatorCall);
+    IO.mapOptional("SpaceAfterOperatorOverload",
+                   Style.SpaceAfterOperatorOverload);
     IO.mapOptional("SpaceAfterTemplateKeyword",
                    Style.SpaceAfterTemplateKeyword);
     IO.mapOptional("SpaceAroundPointerQualifiers",
@@ -1439,6 +1442,8 @@
   LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
   LLVMStyle.SpaceAfterCStyleCast = false;
   LLVMStyle.SpaceAfterLogicalNot = false;
+  LLVMStyle.SpaceAfterOperatorOverload = false;
+  LLVMStyle.SpaceAfterOperatorCall = 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,28 @@
   /// \version 9
   bool SpaceAfterLogicalNot;
 
+  /// If \c true, a space will be inserted after the 'operator' keyword
+  /// in every operator overload declaration.
+  /// \code
+  ///    true:                                  false:
+  ///    bool operator ==(...);         vs.     bool operator==(...);
+  ///    bool Foo::operator ==(...);    vs.     bool Foo::operator ==(...);
+  /// \endcode
+  /// \version 17
+  bool SpaceAfterOperatorOverload;
+
+  /// If \c true, a space will be inserted after the 'operator' keyword
+  /// in every operator call expression.
+  /// \code
+  ///    true:                                  false:
+  ///    foo.operator ==(...);         vs.      foo.operator==(...);
+  ///    foo.Foo::operator ==(...);    vs.      foo.Foo::operator==(...);
+  ///    foo->operator ==(...);        vs.      foo->operator==(...);
+  ///    foo->Foo::operator ==(...);   vs.      foo->Foo::operator==(...);
+  /// \endcode
+  /// \version 17
+  bool SpaceAfterOperatorCall;
+
   /// If \c true, a space will be inserted after the 'template' keyword.
   /// \code
   ///    true:                                  false:
@@ -4411,6 +4433,8 @@
            SortJavaStaticImport == R.SortJavaStaticImport &&
            SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
            SpaceAfterLogicalNot == R.SpaceAfterLogicalNot &&
+           SpaceAfterOperatorCall == R.SpaceAfterOperatorCall &&
+           SpaceAfterOperatorOverload == R.SpaceAfterOperatorOverload &&
            SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword &&
            SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
            SpaceBeforeCaseColon == R.SpaceBeforeCaseColon &&
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -708,6 +708,10 @@
 - 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 ``SpaceAfterOperatorOverload`` style option to allow inserting a space
+  after the operator keyword in an operator overload declaration.
+- Add ``SpaceAfterOperatorCall`` style option to allow inserting a space after
+  the operator keyword in an operator call expression.
 
 libclang
 --------
Index: clang/docs/ClangFormatStyleOptions.rst
===================================================================
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -4704,6 +4704,32 @@
      true:                                  false:
      ! someExpression();            vs.     !someExpression();
 
+.. _SpaceAfterOperatorCall:
+
+**SpaceAfterOperatorCall** (``Boolean``) :versionbadge:`clang-format 17` :ref:`¶ <SpaceAfterOperatorCall>`
+  If ``true``, a space will be inserted after the 'operator' keyword
+  in every operator call expression.
+
+  .. code-block:: c++
+
+     true:                                  false:
+     foo.operator ==(...);         vs.      foo.operator==(...);
+     foo.Foo::operator ==(...);    vs.      foo.Foo::operator==(...);
+     foo->operator ==(...);        vs.      foo->operator==(...);
+     foo->Foo::operator ==(...);   vs.      foo->Foo::operator==(...);
+
+.. _SpaceAfterOperatorOverload:
+
+**SpaceAfterOperatorOverload** (``Boolean``) :versionbadge:`clang-format 17` :ref:`¶ <SpaceAfterOperatorOverload>`
+  If ``true``, a space will be inserted after the 'operator' keyword
+  in every operator overload declaration.
+
+  .. code-block:: c++
+
+     true:                                  false:
+     bool operator ==(...);         vs.     bool operator==(...);
+     bool Foo::operator ==(...);    vs.     bool 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