rajatbajpai updated this revision to Diff 396302.
rajatbajpai added a comment.

Incorporated review comments.

Changelog:

1. Using unit test case instead of lit test case.
2. Placed the option according to alphabetical order.


Repository:
  rG LLVM Github Monorepo

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

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/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -14542,6 +14542,22 @@
   // verifyFormat("X A::operator++ (T);", SomeSpace2);
   verifyFormat("int x = int (y);", SomeSpace2);
   verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
+
+  FormatStyle SpaceAfterOperatorOverloading = getLLVMStyle();
+  SpaceAfterOperatorOverloading.SpaceBeforeParens = FormatStyle::SBPO_Custom;
+  SpaceAfterOperatorOverloading.SpaceBeforeParensOptions
+      .AfterOperatorOverloading = true;
+
+  verifyFormat("auto operator++ () -> int;", SpaceAfterOperatorOverloading);
+  verifyFormat("X A::operator++ ();", SpaceAfterOperatorOverloading);
+  verifyFormat("auto func() -> int;", SpaceAfterOperatorOverloading);
+
+  SpaceAfterOperatorOverloading.SpaceBeforeParensOptions
+      .AfterOperatorOverloading = false;
+
+  verifyFormat("auto operator++() -> int;", SpaceAfterOperatorOverloading);
+  verifyFormat("X A::operator++();", SpaceAfterOperatorOverloading);
+  verifyFormat("auto func() -> int;", SpaceAfterOperatorOverloading);
 }
 
 TEST_F(FormatTest, SpaceAfterLogicalNot) {
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
@@ -855,10 +855,10 @@
     IO.mapOptional("AfterFunctionDeclarationName",
                    Spacing.AfterFunctionDeclarationName);
     IO.mapOptional("AfterIfMacros", Spacing.AfterIfMacros);
-    IO.mapOptional("BeforeNonEmptyParentheses",
-                   Spacing.BeforeNonEmptyParentheses);
     IO.mapOptional("AfterOperatorOverloading",
                    Spacing.AfterOperatorOverloading);
+    IO.mapOptional("BeforeNonEmptyParentheses",
+                   Spacing.BeforeNonEmptyParentheses);
   }
 };
 
Index: clang/include/clang/Format/Format.h
===================================================================
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -3368,6 +3368,13 @@
     ///      <conditional-body>                     <conditional-body>
     /// \endcode
     bool AfterIfMacros;
+    /// 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;
     /// If ``true``, put a space before opening parentheses only if the
     /// parentheses are not empty.
     /// \code
@@ -3376,20 +3383,12 @@
     ///    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), AfterOperatorOverloading(false) {}
+          AfterOperatorOverloading(false), BeforeNonEmptyParentheses(false) {}
 
     bool operator==(const SpaceBeforeParensCustom &Other) const {
       return AfterControlStatements == Other.AfterControlStatements &&
@@ -3398,8 +3397,8 @@
                  Other.AfterFunctionDeclarationName &&
              AfterFunctionDefinitionName == Other.AfterFunctionDefinitionName &&
              AfterIfMacros == Other.AfterIfMacros &&
-             BeforeNonEmptyParentheses == Other.BeforeNonEmptyParentheses &&
-             AfterOperatorOverloading == Other.AfterOperatorOverloading;
+             AfterOperatorOverloading == Other.AfterOperatorOverloading &&
+             BeforeNonEmptyParentheses == Other.BeforeNonEmptyParentheses;
     }
   };
 
Index: clang/docs/ClangFormatStyleOptions.rst
===================================================================
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -3752,21 +3752,21 @@
        IF (...)                        vs.    IF(...)
          <conditional-body>                     <conditional-body>
 
-  * ``bool BeforeNonEmptyParentheses`` If ``true``, put a space before opening parentheses only if the
-    parentheses are not empty.
+  * ``bool AfterOperatorOverloading`` If ``true``, put a space between operator overloading and opening parentheses.
 
     .. code-block:: c++
 
        true:                                  false:
-       void f (int a);                 vs.    void f();
-       f (a);                                 f();
+       void operator++ (int a);        vs.    void operator++(int a);
 
-  * ``bool AfterOperatorOverloading`` If ``true``, put a space between operator overloading and opening parentheses.
+  * ``bool BeforeNonEmptyParentheses`` If ``true``, put a space before opening parentheses only if the
+    parentheses are not empty.
 
     .. code-block:: c++
 
        true:                                  false:
-       void operator++ (int a);                 vs.    void operator++(int a);
+       void f (int a);                 vs.    void f();
+       f (a);                                 f();
 
 
 **SpaceBeforeRangeBasedForLoopColon** (``Boolean``) :versionbadge:`clang-format 7`
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to