gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.

This change separates function calls, declarations, definitions, and
overloaded operators from `SpacesInParensOptions.Other` to allow control
over each independently.

Fixes Github Issue \#55428.

Depends on D155529 <https://reviews.llvm.org/D155529>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156360

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
@@ -11040,7 +11040,8 @@
   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
 
   Spaces.SpacesInParensOptions.InCStyleCasts = false;
-  Spaces.SpacesInParensOptions.Other = true;
+  Spaces.SpacesInParensOptions.InFunctionDeclarations = true;
+  Spaces.SpacesInParensOptions.InOverloadedOperators = true;
   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
                Spaces);
@@ -13678,6 +13679,7 @@
   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
   SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
   SpaceBetweenBraces.SpacesInParensOptions.Other = true;
+  SpaceBetweenBraces.SpacesInParensOptions.InFunctionCalls = true;
   SpaceBetweenBraces.SpacesInSquareBrackets = true;
   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
@@ -16749,6 +16751,7 @@
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
   Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  Spaces.SpacesInParensOptions.InFunctionCalls = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -23819,6 +23822,7 @@
   Style.SpacesInParensOptions.InCStyleCasts = true;
   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
   Style.SpacesInParensOptions.InCStyleCasts = false;
+  Style.SpacesInParensOptions.InFunctionCalls = true;
   Style.SpacesInParensOptions.Other = true;
   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
Index: clang/unittests/Format/ConfigParseTest.cpp
===================================================================
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -226,6 +226,10 @@
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InFunctionCalls);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InFunctionDeclarations);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InFunctionDefinitions);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InOverloadedOperators);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, Other);
 }
 
@@ -600,23 +604,23 @@
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
-              FormatStyle::SpacesInParensCustom(true, true, false, false,
-                  true));
+              FormatStyle::SpacesInParensCustom(true, true, false, false, true,
+                true, true, true, true));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
               FormatStyle::SpacesInParensCustom(false, true, false, false,
-                  false));
+                false, false, false, false, false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
               FormatStyle::SpacesInParensCustom(false, false, true, false,
-                  false));
+                false, false, false, false, false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
               FormatStyle::SpacesInParensCustom(false, false, false, true,
-                  false));
+                false, false, false, false, false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
 
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3822,6 +3822,27 @@
         (Right.Next && Right.Next->is(TT_AttributeParen))) {
       return Style.SpacesInParensOptions.InAttributeSpecifiers;
     }
+    // Function declaration or definition
+    if ((Left.Previous && Left.Previous->is(TT_FunctionDeclarationName)) ||
+        (Right.MatchingParen && Right.MatchingParen->Previous &&
+         Right.MatchingParen->Previous->is(TT_FunctionDeclarationName))) {
+      if (Line.MightBeFunctionDecl)
+        if (Line.mightBeFunctionDefinition())
+          return Style.SpacesInParensOptions.InFunctionDefinitions;
+        else
+          return Style.SpacesInParensOptions.InFunctionDeclarations;
+      else
+        return Style.SpacesInParensOptions.Other;
+    }
+    if (Left.is(TT_OverloadedOperatorLParen) ||
+        (Right.MatchingParen &&
+         Right.MatchingParen->is(TT_OverloadedOperatorLParen))) {
+      return Style.SpacesInParensOptions.InOverloadedOperators;
+    }
+    if (Left.ParameterCount > 0 ||
+        (Right.MatchingParen && Right.MatchingParen->ParameterCount > 0)) {
+      return Style.SpacesInParensOptions.InFunctionCalls;
+    }
     return Style.SpacesInParensOptions.Other;
   }
   if (Right.isOneOf(tok::semi, tok::comma))
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -728,6 +728,10 @@
     IO.mapOptional("InCStyleCasts", Spaces.InCStyleCasts);
     IO.mapOptional("InConditionalStatements", Spaces.InConditionalStatements);
     IO.mapOptional("InEmptyParentheses", Spaces.InEmptyParentheses);
+    IO.mapOptional("InFunctionCalls", Spaces.InFunctionCalls);
+    IO.mapOptional("InFunctionDeclarations", Spaces.InFunctionDeclarations);
+    IO.mapOptional("InFunctionDefinitions", Spaces.InFunctionDefinitions);
+    IO.mapOptional("InOverloadedOperators", Spaces.InOverloadedOperators);
     IO.mapOptional("Other", Spaces.Other);
   }
 };
@@ -1161,6 +1165,10 @@
             SpacesInCStyleCastParentheses;
         Style.SpacesInParensOptions.InEmptyParentheses =
             SpaceInEmptyParentheses;
+        Style.SpacesInParensOptions.InFunctionCalls = true;
+        Style.SpacesInParensOptions.InFunctionDeclarations = true;
+        Style.SpacesInParensOptions.InFunctionDefinitions = true;
+        Style.SpacesInParensOptions.InOverloadedOperators = true;
         Style.SpacesInParensOptions.Other = true;
       } else {
         Style.SpacesInParensOptions = {};
Index: clang/include/clang/Format/Format.h
===================================================================
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -4334,30 +4334,69 @@
     ///    }                                    }
     /// \endcode
     bool InEmptyParentheses;
-    /// Put a space in parentheses not covered by preceding options.
+    /// Put a space in parentheses of function calls.
     /// \code
     ///    true:                                  false:
     ///    t f( Deleted & ) & = delete;   vs.     t f(Deleted &) & = delete;
     /// \endcode
+    bool InFunctionCalls;
+    /// Put a space in parentheses of function declarations.
+    /// \code
+    ///    true:                                  false:
+    ///    void foo( int bar );           vs.     void foo(int bar);
+    /// \endcode
+    bool InFunctionDeclarations;
+    /// Put a space in parentheses of function definitions.
+    /// \code
+    ///    true:                                  false:
+    ///    void foo( int bar ) { }        vs.     void foo(int bar) { }
+    /// \endcode
+    bool InFunctionDefinitions;
+    /// Put a space in parentheses of overloaded operators.
+    /// \code
+    ///    true:                                  false:
+    ///    void operator++( int a )       vs.     void operator++(int a)
+    ///    object.operator++( 10 )        vs.     object.operator++(10)
+    /// \endcode
+    bool InOverloadedOperators;
+    /// Put a space in parentheses not covered by preceding options.
+    /// \code
+    ///    true:                                  false:
+    ///    x = ( y + z );                 vs.     x = (y+z);
+    /// \endcode
     bool Other;
 
     SpacesInParensCustom()
         : InAttributeSpecifiers(false), InConditionalStatements(false),
-          InCStyleCasts(false), InEmptyParentheses(false), Other(false) {}
+          InCStyleCasts(false), InEmptyParentheses(false),
+          InFunctionCalls(false), InFunctionDeclarations(false),
+          InFunctionDefinitions(false), InOverloadedOperators(false),
+          Other(false) {}
 
     SpacesInParensCustom(bool InAttributeSpecifiers,
                          bool InConditionalStatements, bool InCStyleCasts,
-                         bool InEmptyParentheses, bool Other)
+                         bool InEmptyParentheses, bool InFunctionCalls,
+                         bool InFunctionDeclarations,
+                         bool InFunctionDefinitions, bool InOverloadedOperators,
+                         bool Other)
         : InAttributeSpecifiers(InAttributeSpecifiers),
           InConditionalStatements(InConditionalStatements),
           InCStyleCasts(InCStyleCasts), InEmptyParentheses(InEmptyParentheses),
-          Other(Other) {}
+          InFunctionCalls(InFunctionCalls),
+          InFunctionDeclarations(InFunctionDeclarations),
+          InFunctionDefinitions(InFunctionDefinitions),
+          InOverloadedOperators(InOverloadedOperators), Other(Other) {}
 
     bool operator==(const SpacesInParensCustom &R) const {
       return InAttributeSpecifiers == R.InAttributeSpecifiers &&
              InConditionalStatements == R.InConditionalStatements &&
              InCStyleCasts == R.InCStyleCasts &&
-             InEmptyParentheses == R.InEmptyParentheses && Other == R.Other;
+             InEmptyParentheses == R.InEmptyParentheses &&
+             InFunctionCalls == R.InFunctionCalls &&
+             InFunctionDeclarations == R.InFunctionDeclarations &&
+             InFunctionDefinitions == R.InFunctionDefinitions &&
+             InOverloadedOperators == R.InOverloadedOperators &&
+             Other == R.Other;
     }
     bool operator!=(const SpacesInParensCustom &R) const {
       return !(*this == R);
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -173,6 +173,9 @@
 ------------
 - Add ``InAttributeSpecifiers`` style option to ``SpacesInParensOptions``
   to control addition of spaces after the ``__attribute__`` keyword.
+- Add ``InFunctionCalls``, ``InFunctionDeclarations``,
+  ``InFunctionDefinitions``, and ``InOverloadedOperators`` style options to
+  ``SpacesInParensOptions`` to control addition of spaces.
 
 libclang
 --------
Index: clang/docs/ClangFormatStyleOptions.rst
===================================================================
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -5432,13 +5432,42 @@
          }                                    }
        }                                    }
 
-  * ``bool Other`` Put a space in parentheses not covered by preceding options.
+  * ``bool InFunctionCalls`` Put a space in parentheses of function calls.
 
     .. code-block:: c++
 
        true:                                  false:
        t f( Deleted & ) & = delete;   vs.     t f(Deleted &) & = delete;
 
+  * ``bool InFunctionDeclarations`` Put a space in parentheses of function declarations.
+
+    .. code-block:: c++
+
+       true:                                  false:
+       void foo( int bar );           vs.     void foo(int bar);
+
+  * ``bool InFunctionDefinitions`` Put a space in parentheses of function definitions.
+
+    .. code-block:: c++
+
+       true:                                  false:
+       void foo( int bar ) { }        vs.     void foo(int bar) { }
+
+  * ``bool InOverloadedOperators`` Put a space in parentheses of overloaded operators.
+
+    .. code-block:: c++
+
+       true:                                  false:
+       void operator++( int a )       vs.     void operator++(int a)
+       object.operator++( 10 )        vs.     object.operator++(10)
+
+  * ``bool Other`` Put a space in parentheses not covered by preceding options.
+
+    .. code-block:: c++
+
+       true:                                  false:
+       x = ( y + z );                 vs.     x = (y+z);
+
 
 .. _SpacesInParentheses:
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to