rajatbajpai created this revision.
rajatbajpai added a reviewer: MyDeveloperDay.
rajatbajpai added a project: clang-format.
rajatbajpai requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This change adds an option AfterOperatorOverloading in SpaceBeforeParensOptions 
to add a space between operator overloading and opening parentheses in 
clang-format.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116283

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/test/Format/space-before-parens.cpp

Index: clang/test/Format/space-before-parens.cpp
===================================================================
--- /dev/null
+++ clang/test/Format/space-before-parens.cpp
@@ -0,0 +1,9 @@
+// RUN: clang-format -style="{SpaceBeforeParens: Custom, SpaceBeforeParensOptions: {AfterOperator: false}}" %s | FileCheck -strict-whitespace -check-prefix=CHECK1 %s
+// Test space between overloaded operator and left parentheses
+// RUN: clang-format -style="{SpaceBeforeParens: Custom, SpaceBeforeParensOptions: {AfterOperator: true}}" %s | FileCheck -strict-whitespace -check-prefix=CHECK2 %s
+class Test {
+  auto func() -> int;
+  // CHECK1: {{^\ \ auto\ operator\+\+\(int\)\ \-\>\ int;$}}
+  // CHECK2: {{^\ \ auto\ operator\+\+\ \(int\)\ \-\>\ int;$}}
+  auto operator++(int) -> int;
+};
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2921,9 +2921,15 @@
 }
 
 bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const {
-  return Style.SpaceBeforeParens == FormatStyle::SBPO_Always ||
-         (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
-          Right.ParameterCount > 0);
+  if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
+    return true;
+  if (Right.is(TT_OverloadedOperatorLParen) &&
+      Style.SpaceBeforeParensOptions.AfterOperatorOverloading)
+    return true;
+  if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
+      Right.ParameterCount > 0)
+    return true;
+  return false;
 }
 
 bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -857,6 +857,8 @@
     IO.mapOptional("AfterIfMacros", Spacing.AfterIfMacros);
     IO.mapOptional("BeforeNonEmptyParentheses",
                    Spacing.BeforeNonEmptyParentheses);
+    IO.mapOptional("AfterOperatorOverloading",
+                   Spacing.AfterOperatorOverloading);
   }
 };
 
Index: clang/include/clang/Format/Format.h
===================================================================
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -3376,12 +3376,20 @@
     ///    f (a);                                 f();
     /// \endcode
     bool BeforeNonEmptyParentheses;
+    /// If ``true``, put a space between operator overloading and opening
+    /// parentheses.
+    /// \code
+    ///    true:                                  false:
+    ///    void operator++ (int a);                 vs.    void operator++(int
+    ///    a);
+    /// \endcode
+    bool AfterOperatorOverloading;
 
     SpaceBeforeParensCustom()
         : AfterControlStatements(false), AfterForeachMacros(false),
           AfterFunctionDeclarationName(false),
           AfterFunctionDefinitionName(false), AfterIfMacros(false),
-          BeforeNonEmptyParentheses(false) {}
+          BeforeNonEmptyParentheses(false), AfterOperatorOverloading(false) {}
 
     bool operator==(const SpaceBeforeParensCustom &Other) const {
       return AfterControlStatements == Other.AfterControlStatements &&
@@ -3390,7 +3398,8 @@
                  Other.AfterFunctionDeclarationName &&
              AfterFunctionDefinitionName == Other.AfterFunctionDefinitionName &&
              AfterIfMacros == Other.AfterIfMacros &&
-             BeforeNonEmptyParentheses == Other.BeforeNonEmptyParentheses;
+             BeforeNonEmptyParentheses == Other.BeforeNonEmptyParentheses &&
+             AfterOperatorOverloading == Other.AfterOperatorOverloading;
     }
   };
 
Index: clang/docs/ClangFormatStyleOptions.rst
===================================================================
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -3761,6 +3761,13 @@
        void f (int a);                 vs.    void f();
        f (a);                                 f();
 
+  * ``bool AfterOperatorOverloading`` If ``true``, put a space between operator overloading and opening parentheses.
+
+    .. code-block:: c++
+
+       true:                                  false:
+       void operator++ (int a);                 vs.    void operator++(int a);
+
 
 **SpaceBeforeRangeBasedForLoopColon** (``Boolean``) :versionbadge:`clang-format 7`
   If ``false``, spaces will be removed before range-based for loop
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to