diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/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: diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/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 -------- diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/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); diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/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 = {}; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/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)) diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/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 = {}; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/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);