diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -5116,16 +5116,8 @@ **SpaceInEmptyParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ ` If ``true``, spaces may be inserted into ``()``. - - .. code-block:: c++ - - true: false: - void f( ) { vs. void f() { - int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()}; - if (true) { if (true) { - f( ); f(); - } } - } } + This option is **deprecated**. See ``InEmptyParentheses`` of + ``SpacesInParensOptions``. .. _SpacesBeforeTrailingComments: @@ -5182,23 +5174,16 @@ **SpacesInCStyleCastParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ ` If ``true``, spaces may be inserted into C style casts. - - .. code-block:: c++ - - true: false: - x = ( int32 )y vs. x = (int32)y + This option is **deprecated**. See ``InCStyleCasts`` of + ``SpacesInParensOptions``. .. _SpacesInConditionalStatement: **SpacesInConditionalStatement** (``Boolean``) :versionbadge:`clang-format 10` :ref:`¶ ` If ``true``, spaces will be inserted around if/for/switch/while conditions. - - .. code-block:: c++ - - true: false: - if ( a ) { ... } vs. if (a) { ... } - while ( i < 5 ) { ... } while (i < 5) { ... } + This option is **deprecated**. See ``InConditionalStatements`` of + ``SpacesInParensOptions``. .. _SpacesInContainerLiterals: @@ -5259,15 +5244,104 @@ * ``unsigned Maximum`` The maximum number of spaces at the start of the comment. -.. _SpacesInParentheses: +.. _SpacesInParens: -**SpacesInParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ ` - If ``true``, spaces will be inserted after ``(`` and before ``)``. +**SpacesInParens** (``SpacesInParensStyle``) :versionbadge:`clang-format 17` :ref:`¶ ` + Defines in which cases spaces will be inserted after ``(`` and before + ``)``. + + Possible values: + + * ``SIPO_Never`` (in configuration: ``Never``) + Never put a space in parentheses. + + .. code-block:: c++ + + void f() { + if(true) { + f(); + } + } + + * ``SIPO_Custom`` (in configuration: ``Custom``) + Configure each individual space in parentheses in + `SpacesInParensOptions`. + + + +.. _SpacesInParensOptions: + +**SpacesInParensOptions** (``SpacesInParensCustom``) :versionbadge:`clang-format 17` :ref:`¶ ` + Control of individual spaces in parentheses. + + If ``SpacesInParens`` is set to ``Custom``, use this to specify + how each individual space in parentheses case should be handled. + Otherwise, this is ignored. + + .. code-block:: yaml + + # Example of usage: + SpacesInParens: Custom + SpacesInParensOptions: + InConditionalStatements: true + InEmptyParentheses: true + + Nested configuration flags: + + Precise control over the spacing in parentheses. .. code-block:: c++ - true: false: - t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete; + # Should be declared this way: + SpacesInParens: Custom + SpacesInParensOptions: + InConditionalStatements: true + Other: true + + * ``bool InConditionalStatements`` Put a space in parentheses only inside conditional statements + (``for/if/while/switch...``). + + .. code-block:: c++ + + true: false: + if ( a ) { ... } vs. if (a) { ... } + while ( i < 5 ) { ... } while (i < 5) { ... } + + * ``bool InCStyleCasts`` Put a space in C style casts. + + .. code-block:: c++ + + true: false: + x = ( int32 )y vs. x = (int32)y + + * ``bool InEmptyParentheses`` Put a space in parentheses only if the parentheses are empty i.e. '()' + + .. code-block:: c++ + + true: false: + void f( ) { vs. void f() { + int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()}; + if (true) { if (true) { + f( ); f(); + } } + } } + + * ``bool Other`` Put a space in parentheses not covered by preceding options. + + .. code-block:: c++ + + true: false: + t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete; + + +.. _SpacesInParentheses: + +**SpacesInParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ ` + If ``true'', spaces will be inserted after ``(`` and before ``)``. + This option is **deprecated**. The previous behavior is preserved by using + ``SpacesInParens`` with ``Custom`` and by setting all + ``SpacesInParensOptions`` to ``true`` except for ``InCStyleCasts`` and + ``InEmptyParentheses``. .. _SpacesInSquareBrackets: diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -826,6 +826,9 @@ the indentation level of the contents of braced init lists. - Add ``KeepEmptyLinesAtEOF`` to keep empty lines at end of file. - Add ``RemoveParentheses`` to remove redundant parentheses. +- Add ``SpacesInParens`` style with ``SpacesInParensOptions`` to replace + ``SpacesInConditionalStatement``, ``SpacesInCStyleCastParentheses``, + ``SpaceInEmptyParentheses``, and ``SpacesInParentheses``. 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 @@ -4053,17 +4053,10 @@ bool SpaceInEmptyBlock; /// If ``true``, spaces may be inserted into ``()``. - /// \code - /// true: false: - /// void f( ) { vs. void f() { - /// int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()}; - /// if (true) { if (true) { - /// f( ); f(); - /// } } - /// } } - /// \endcode + /// This option is **deprecated**. See ``InEmptyParentheses`` of + /// ``SpacesInParensOptions``. /// \version 3.7 - bool SpaceInEmptyParentheses; + // bool SpaceInEmptyParentheses; /// The number of spaces before trailing line comments /// (``//`` - comments). @@ -4110,13 +4103,10 @@ /// If ``true``, spaces will be inserted around if/for/switch/while /// conditions. - /// \code - /// true: false: - /// if ( a ) { ... } vs. if (a) { ... } - /// while ( i < 5 ) { ... } while (i < 5) { ... } - /// \endcode + /// This option is **deprecated**. See ``InConditionalStatements`` of + /// ``SpacesInParensOptions``. /// \version 10 - bool SpacesInConditionalStatement; + // bool SpacesInConditionalStatement; /// If ``true``, spaces are inserted inside container literals (e.g. ObjC and /// Javascript array and dict literals). For JSON, use @@ -4130,12 +4120,10 @@ bool SpacesInContainerLiterals; /// If ``true``, spaces may be inserted into C style casts. - /// \code - /// true: false: - /// x = ( int32 )y vs. x = (int32)y - /// \endcode + /// This option is **deprecated**. See ``InCStyleCasts`` of + /// ``SpacesInParensOptions``. /// \version 3.7 - bool SpacesInCStyleCastParentheses; + // bool SpacesInCStyleCastParentheses; /// Control of spaces within a single line comment. struct SpacesInLineComment { @@ -4179,13 +4167,112 @@ /// \version 13 SpacesInLineComment SpacesInLineCommentPrefix; - /// If ``true``, spaces will be inserted after ``(`` and before ``)``. + /// Different ways to put a space before opening and closing parentheses. + enum SpacesInParensStyle : int8_t { + /// Never put a space in parentheses. + /// \code + /// void f() { + /// if(true) { + /// f(); + /// } + /// } + /// \endcode + SIPO_Never, + /// Configure each individual space in parentheses in + /// `SpacesInParensOptions`. + SIPO_Custom, + }; + + /// If ``true'', spaces will be inserted after ``(`` and before ``)``. + /// This option is **deprecated**. The previous behavior is preserved by using + /// ``SpacesInParens`` with ``Custom`` and by setting all + /// ``SpacesInParensOptions`` to ``true`` except for ``InCStyleCasts`` and + /// ``InEmptyParentheses``. + /// \version 3.7 + // bool SpacesInParentheses; + + /// Defines in which cases spaces will be inserted after ``(`` and before + /// ``)``. + /// \version 17 + SpacesInParensStyle SpacesInParens; + + /// Precise control over the spacing in parentheses. /// \code - /// true: false: - /// t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete; + /// # Should be declared this way: + /// SpacesInParens: Custom + /// SpacesInParensOptions: + /// InConditionalStatements: true + /// Other: true /// \endcode - /// \version 3.7 - bool SpacesInParentheses; + struct SpacesInParensCustom { + /// Put a space in parentheses only inside conditional statements + /// (``for/if/while/switch...``). + /// \code + /// true: false: + /// if ( a ) { ... } vs. if (a) { ... } + /// while ( i < 5 ) { ... } while (i < 5) { ... } + /// \endcode + bool InConditionalStatements; + /// Put a space in C style casts. + /// \code + /// true: false: + /// x = ( int32 )y vs. x = (int32)y + /// \endcode + bool InCStyleCasts; + /// Put a space in parentheses only if the parentheses are empty i.e. '()' + /// \code + /// true: false: + /// void f( ) { vs. void f() { + /// int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()}; + /// if (true) { if (true) { + /// f( ); f(); + /// } } + /// } } + /// \endcode + bool InEmptyParentheses; + /// Put a space in parentheses not covered by preceding options. + /// \code + /// true: false: + /// t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete; + /// \endcode + bool Other; + + SpacesInParensCustom() + : InConditionalStatements(false), InCStyleCasts(false), + InEmptyParentheses(false), Other(false) {} + + SpacesInParensCustom(bool InConditionalStatements, bool InCStyleCasts, + bool InEmptyParentheses, bool Other) + : InConditionalStatements(InConditionalStatements), + InCStyleCasts(InCStyleCasts), + InEmptyParentheses(InEmptyParentheses), + Other(Other) {} + + bool operator==(const SpacesInParensCustom &R) const { + return InConditionalStatements == R.InConditionalStatements && + InCStyleCasts == R.InCStyleCasts && + InEmptyParentheses == R.InEmptyParentheses && + Other == R.Other; + } + bool operator!=(const SpacesInParensCustom &R) const { + return !(*this == R); + } + }; + + /// Control of individual spaces in parentheses. + /// + /// If ``SpacesInParens`` is set to ``Custom``, use this to specify + /// how each individual space in parentheses case should be handled. + /// Otherwise, this is ignored. + /// \code{.yaml} + /// # Example of usage: + /// SpacesInParens: Custom + /// SpacesInParensOptions: + /// InConditionalStatements: true + /// InEmptyParentheses: true + /// \endcode + /// \version 17 + SpacesInParensCustom SpacesInParensOptions; /// If ``true``, spaces will be inserted after ``[`` and before ``]``. /// Lambdas without arguments or unspecified size array declarations will not @@ -4477,17 +4564,15 @@ R.SpaceBeforeRangeBasedForLoopColon && SpaceBeforeSquareBrackets == R.SpaceBeforeSquareBrackets && SpaceInEmptyBlock == R.SpaceInEmptyBlock && - SpaceInEmptyParentheses == R.SpaceInEmptyParentheses && SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments && SpacesInAngles == R.SpacesInAngles && - SpacesInConditionalStatement == R.SpacesInConditionalStatement && SpacesInContainerLiterals == R.SpacesInContainerLiterals && - SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses && SpacesInLineCommentPrefix.Minimum == R.SpacesInLineCommentPrefix.Minimum && SpacesInLineCommentPrefix.Maximum == R.SpacesInLineCommentPrefix.Maximum && - SpacesInParentheses == R.SpacesInParentheses && + SpacesInParens == R.SpacesInParens && + SpacesInParensOptions == R.SpacesInParensOptions && SpacesInSquareBrackets == R.SpacesInSquareBrackets && Standard == R.Standard && StatementAttributeLikeMacros == R.StatementAttributeLikeMacros && 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 @@ -711,6 +711,22 @@ } }; +template <> struct MappingTraits { + static void mapping(IO &IO, FormatStyle::SpacesInParensCustom &Spaces) { + IO.mapOptional("InCStyleCasts", Spaces.InCStyleCasts); + IO.mapOptional("InConditionalStatements", Spaces.InConditionalStatements); + IO.mapOptional("InEmptyParentheses", Spaces.InEmptyParentheses); + IO.mapOptional("Other", Spaces.Other); + } +}; + +template <> struct ScalarEnumerationTraits { + static void enumeration(IO &IO, FormatStyle::SpacesInParensStyle &Value) { + IO.enumCase(Value, "Never", FormatStyle::SIPO_Never); + IO.enumCase(Value, "Custom", FormatStyle::SIPO_Custom); + } +}; + template <> struct ScalarEnumerationTraits { static void enumeration(IO &IO, FormatStyle::TrailingCommaStyle &Value) { IO.enumCase(Value, "None", FormatStyle::TCS_None); @@ -826,6 +842,11 @@ bool DeriveLineEnding = true; bool UseCRLF = false; + bool SpaceInEmptyParentheses = false; + bool SpacesInConditionalStatement = false; + bool SpacesInCStyleCastParentheses = false; + bool SpacesInParentheses = false; + // For backward compatibility. if (!IO.outputting()) { IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlines); @@ -844,6 +865,12 @@ IO.mapOptional("PointerBindsToType", Style.PointerAlignment); IO.mapOptional("SpaceAfterControlStatementKeyword", Style.SpaceBeforeParens); + IO.mapOptional("SpaceInEmptyParentheses", SpaceInEmptyParentheses); + IO.mapOptional("SpacesInConditionalStatement", + SpacesInConditionalStatement); + IO.mapOptional("SpacesInCStyleCastParentheses", + SpacesInCStyleCastParentheses); + IO.mapOptional("SpacesInParentheses", SpacesInParentheses); IO.mapOptional("UseCRLF", UseCRLF); } @@ -1032,19 +1059,15 @@ IO.mapOptional("SpaceBeforeSquareBrackets", Style.SpaceBeforeSquareBrackets); IO.mapOptional("SpaceInEmptyBlock", Style.SpaceInEmptyBlock); - IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses); IO.mapOptional("SpacesBeforeTrailingComments", Style.SpacesBeforeTrailingComments); IO.mapOptional("SpacesInAngles", Style.SpacesInAngles); - IO.mapOptional("SpacesInConditionalStatement", - Style.SpacesInConditionalStatement); IO.mapOptional("SpacesInContainerLiterals", Style.SpacesInContainerLiterals); - IO.mapOptional("SpacesInCStyleCastParentheses", - Style.SpacesInCStyleCastParentheses); IO.mapOptional("SpacesInLineCommentPrefix", Style.SpacesInLineCommentPrefix); - IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses); + IO.mapOptional("SpacesInParens", Style.SpacesInParens); + IO.mapOptional("SpacesInParensOptions", Style.SpacesInParensOptions); IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets); IO.mapOptional("Standard", Style.Standard); IO.mapOptional("StatementAttributeLikeMacros", @@ -1110,6 +1133,30 @@ else if (UseCRLF) Style.LineEnding = FormatStyle::LE_DeriveCRLF; } + + if (Style.SpacesInParens != FormatStyle::SIPO_Custom && + (SpacesInParentheses || SpaceInEmptyParentheses || + SpacesInConditionalStatement || SpacesInCStyleCastParentheses)) { + if (SpacesInParentheses) { + // set all options except InCStyleCasts and InEmptyParentheses + // to true for backward compatibility. + Style.SpacesInParensOptions.InConditionalStatements = true; + Style.SpacesInParensOptions.InCStyleCasts = + SpacesInCStyleCastParentheses; + Style.SpacesInParensOptions.InEmptyParentheses = + SpaceInEmptyParentheses; + Style.SpacesInParensOptions.Other = true; + } else { + Style.SpacesInParensOptions = {}; + Style.SpacesInParensOptions.InConditionalStatements = + SpacesInConditionalStatement; + Style.SpacesInParensOptions.InCStyleCasts = + SpacesInCStyleCastParentheses; + Style.SpacesInParensOptions.InEmptyParentheses = + SpaceInEmptyParentheses; + } + Style.SpacesInParens = FormatStyle::SIPO_Custom; + } } }; @@ -1314,6 +1361,14 @@ } } +static void expandPresetsSpacesInParens(FormatStyle &Expanded) { + if (Expanded.SpacesInParens == FormatStyle::SIPO_Custom) + return; + assert(Expanded.SpacesInParens == FormatStyle::SIPO_Never); + // Reset all flags + Expanded.SpacesInParensOptions = {}; +} + FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { FormatStyle LLVMStyle; LLVMStyle.InheritsParentConfig = false; @@ -1467,15 +1522,12 @@ LLVMStyle.SpaceBeforeCpp11BracedList = false; LLVMStyle.SpaceBeforeSquareBrackets = false; LLVMStyle.SpaceInEmptyBlock = false; - LLVMStyle.SpaceInEmptyParentheses = false; LLVMStyle.SpacesBeforeTrailingComments = 1; LLVMStyle.SpacesInAngles = FormatStyle::SIAS_Never; LLVMStyle.SpacesInContainerLiterals = true; - LLVMStyle.SpacesInCStyleCastParentheses = false; LLVMStyle.SpacesInLineCommentPrefix = {/*Minimum=*/1, /*Maximum=*/-1u}; - LLVMStyle.SpacesInParentheses = false; + LLVMStyle.SpacesInParens = FormatStyle::SIPO_Never; LLVMStyle.SpacesInSquareBrackets = false; - LLVMStyle.SpacesInConditionalStatement = false; LLVMStyle.Standard = FormatStyle::LS_Latest; LLVMStyle.StatementAttributeLikeMacros.push_back("Q_EMIT"); LLVMStyle.StatementMacros.push_back("Q_UNUSED"); @@ -1957,6 +2009,7 @@ FormatStyle NonConstStyle = Style; expandPresetsBraceWrapping(NonConstStyle); expandPresetsSpaceBeforeParens(NonConstStyle); + expandPresetsSpacesInParens(NonConstStyle); Output << NonConstStyle; return Stream.str(); @@ -3482,6 +3535,7 @@ FormatStyle Expanded = Style; expandPresetsBraceWrapping(Expanded); expandPresetsSpaceBeforeParens(Expanded); + expandPresetsSpacesInParens(Expanded); Expanded.InsertBraces = false; Expanded.RemoveBracesLLVM = false; Expanded.RemoveParentheses = FormatStyle::RPS_Leave; 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 @@ -3355,7 +3355,9 @@ if (Current->is(TT_LineComment)) { if (Prev->is(BK_BracedInit) && Prev->opensScope()) { Current->SpacesRequiredBefore = - (Style.Cpp11BracedListStyle && !Style.SpacesInParentheses) ? 0 : 1; + (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other) + ? 0 + : 1; } else if (Prev->is(TT_VerilogMultiLineListLParen)) { Current->SpacesRequiredBefore = 0; } else { @@ -3769,9 +3771,9 @@ if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) || (Left.is(tok::l_brace) && Left.isNot(BK_Block) && Right.is(tok::r_brace) && Right.isNot(BK_Block))) { - return Style.SpaceInEmptyParentheses; + return Style.SpacesInParensOptions.InEmptyParentheses; } - if (Style.SpacesInConditionalStatement) { + if (Style.SpacesInParensOptions.InConditionalStatements) { const FormatToken *LeftParen = nullptr; if (Left.is(tok::l_paren)) LeftParen = &Left; @@ -3810,8 +3812,8 @@ if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) { return (Right.is(TT_CastRParen) || (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) - ? Style.SpacesInCStyleCastParentheses - : Style.SpacesInParentheses; + ? Style.SpacesInParensOptions.InCStyleCasts + : Style.SpacesInParensOptions.Other; } if (Right.isOneOf(tok::semi, tok::comma)) return false; @@ -4037,7 +4039,8 @@ if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) || (Right.is(tok::r_brace) && Right.MatchingParen && Right.MatchingParen->isNot(BK_Block))) { - return Style.Cpp11BracedListStyle ? Style.SpacesInParentheses : true; + return Style.Cpp11BracedListStyle ? Style.SpacesInParensOptions.Other + : true; } if (Left.is(TT_BlockComment)) { // No whitespace in x(/*foo=*/1), except for JavaScript. @@ -4699,7 +4702,7 @@ !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square, tok::kw___super, TT_TemplateOpener, TT_TemplateCloser)) || - (Left.is(tok::l_paren) && Style.SpacesInParentheses); + (Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other); } if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser))) return ShouldAddSpacesInAngles(); 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 @@ -175,13 +175,9 @@ CHECK_PARSE_BOOL(ReflowComments); CHECK_PARSE_BOOL(RemoveBracesLLVM); CHECK_PARSE_BOOL(RemoveSemicolon); - CHECK_PARSE_BOOL(SpacesInParentheses); CHECK_PARSE_BOOL(SpacesInSquareBrackets); - CHECK_PARSE_BOOL(SpacesInConditionalStatement); CHECK_PARSE_BOOL(SpaceInEmptyBlock); - CHECK_PARSE_BOOL(SpaceInEmptyParentheses); CHECK_PARSE_BOOL(SpacesInContainerLiterals); - CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); CHECK_PARSE_BOOL(SpaceAfterCStyleCast); CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); CHECK_PARSE_BOOL(SpaceAfterLogicalNot); @@ -221,6 +217,10 @@ CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros); CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator); CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses); + CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts); + CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements); + CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses); + CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, Other); } #undef CHECK_PARSE_BOOL @@ -586,6 +586,30 @@ SpaceBeforeParens, FormatStyle::SBPO_ControlStatementsExceptControlMacros); + // For backward compatibility: + Style.SpacesInParens = FormatStyle::SIPO_Never; + Style.SpacesInParensOptions = {}; + CHECK_PARSE("SpacesInParentheses: true", SpacesInParens, + FormatStyle::SIPO_Custom); + Style.SpacesInParens = FormatStyle::SIPO_Never; + Style.SpacesInParensOptions = {}; + CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions, + FormatStyle::SpacesInParensCustom(true, false, false, true)); + Style.SpacesInParens = FormatStyle::SIPO_Never; + Style.SpacesInParensOptions = {}; + CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions, + FormatStyle::SpacesInParensCustom(true, false, false, false)); + Style.SpacesInParens = FormatStyle::SIPO_Never; + Style.SpacesInParensOptions = {}; + CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions, + FormatStyle::SpacesInParensCustom(false, true, false, false)); + Style.SpacesInParens = FormatStyle::SIPO_Never; + Style.SpacesInParensOptions = {}; + CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions, + FormatStyle::SpacesInParensCustom(false, false, true, false)); + Style.SpacesInParens = FormatStyle::SIPO_Never; + Style.SpacesInParensOptions = {}; + Style.ColumnLimit = 123; FormatStyle BaseStyle = getLLVMStyle(); CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 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 @@ -11031,14 +11031,16 @@ verifyFormat("template void operator=(T) && {}", AlignMiddle); FormatStyle Spaces = getLLVMStyle(); - Spaces.SpacesInCStyleCastParentheses = true; + Spaces.SpacesInParens = FormatStyle::SIPO_Custom; + Spaces.SpacesInParensOptions = {}; + Spaces.SpacesInParensOptions.InCStyleCasts = true; verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); - Spaces.SpacesInCStyleCastParentheses = false; - Spaces.SpacesInParentheses = true; + Spaces.SpacesInParensOptions.InCStyleCasts = false; + Spaces.SpacesInParensOptions.Other = true; verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); @@ -13674,7 +13676,8 @@ FormatStyle SpaceBetweenBraces = getLLVMStyle(); SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; - SpaceBetweenBraces.SpacesInParentheses = true; + SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom; + SpaceBetweenBraces.SpacesInParensOptions.Other = true; SpaceBetweenBraces.SpacesInSquareBrackets = true; verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); @@ -13697,7 +13700,8 @@ "};", format("vectorx{1,2,3,4,};", SpaceBetweenBraces)); verifyFormat("vector< int > x{};", SpaceBetweenBraces); - SpaceBetweenBraces.SpaceInEmptyParentheses = true; + SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom; + SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true; verifyFormat("vector< int > x{ };", SpaceBetweenBraces); } @@ -16707,10 +16711,43 @@ verifyFormat("! ! x", Spaces); } -TEST_F(FormatTest, ConfigurableSpacesInParentheses) { +TEST_F(FormatTest, ConfigurableSpacesInParens) { FormatStyle Spaces = getLLVMStyle(); - Spaces.SpacesInParentheses = true; + verifyFormat("do_something(::globalVar);", Spaces); + verifyFormat("call(x, y, z);", Spaces); + verifyFormat("call();", Spaces); + verifyFormat("std::function callback;", Spaces); + verifyFormat("void inFunction() { std::function fct; }", + Spaces); + verifyFormat("while ((bool)1)\n" + " continue;", + Spaces); + verifyFormat("for (;;)\n" + " continue;", + Spaces); + verifyFormat("if (true)\n" + " f();\n" + "else if (true)\n" + " f();", + Spaces); + verifyFormat("do {\n" + " do_something((int)i);\n" + "} while (something());", + Spaces); + verifyFormat("switch (x) {\n" + "default:\n" + " break;\n" + "}", + Spaces); + verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); + verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); + verifyFormat("void f() __attribute__((asdf));", Spaces); + + Spaces.SpacesInParens = FormatStyle::SIPO_Custom; + Spaces.SpacesInParensOptions = {}; + Spaces.SpacesInParensOptions.Other = true; + Spaces.SpacesInParensOptions.InConditionalStatements = true; verifyFormat("do_something( ::globalVar );", Spaces); verifyFormat("call( x, y, z );", Spaces); verifyFormat("call();", Spaces); @@ -16737,9 +16774,13 @@ " break;\n" "}", Spaces); + verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces); + verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces); + verifyFormat("void f() __attribute__( ( asdf ) );", Spaces); - Spaces.SpacesInParentheses = false; - Spaces.SpacesInCStyleCastParentheses = true; + Spaces.SpacesInParens = FormatStyle::SIPO_Custom; + Spaces.SpacesInParensOptions = {}; + Spaces.SpacesInParensOptions.InCStyleCasts = true; verifyFormat("Type *A = ( Type * )P;", Spaces); verifyFormat("Type *A = ( vector )P;", Spaces); verifyFormat("x = ( int32 )y;", Spaces); @@ -16750,9 +16791,10 @@ verifyFormat("#define x (( int )-1)", Spaces); // Run the first set of tests again with: - Spaces.SpacesInParentheses = false; - Spaces.SpaceInEmptyParentheses = true; - Spaces.SpacesInCStyleCastParentheses = true; + Spaces.SpacesInParens = FormatStyle::SIPO_Custom; + Spaces.SpacesInParensOptions = {}; + Spaces.SpacesInParensOptions.InEmptyParentheses = true; + Spaces.SpacesInParensOptions.InCStyleCasts = true; verifyFormat("call(x, y, z);", Spaces); verifyFormat("call( );", Spaces); verifyFormat("std::function callback;", Spaces); @@ -16776,6 +16818,9 @@ " break;\n" "}", Spaces); + verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); + verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); + verifyFormat("void f( ) __attribute__((asdf));", Spaces); // Run the first set of tests again with: Spaces.SpaceAfterCStyleCast = true; @@ -16808,9 +16853,12 @@ verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces); verifyFormat("bool *y = ( bool * ) (x);", Spaces); verifyFormat("throw ( int32 ) x;", Spaces); + verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); + verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); + verifyFormat("void f( ) __attribute__((asdf));", Spaces); // Run subset of tests again with: - Spaces.SpacesInCStyleCastParentheses = false; + Spaces.SpacesInParensOptions.InCStyleCasts = false; Spaces.SpaceAfterCStyleCast = true; verifyFormat("while ((bool) 1)\n" " continue;", @@ -16833,6 +16881,9 @@ verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces); verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces); verifyFormat("throw (int32) x;", Spaces); + verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); + verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); + verifyFormat("void f( ) __attribute__((asdf));", Spaces); Spaces.ColumnLimit = 80; Spaces.IndentWidth = 4; @@ -23522,11 +23573,11 @@ verifyFormat("_Atomic(int*)* a;", Style); verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); - Style.SpacesInCStyleCastParentheses = true; - Style.SpacesInParentheses = false; + Style.SpacesInParens = FormatStyle::SIPO_Custom; + Style.SpacesInParensOptions.InCStyleCasts = true; verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); - Style.SpacesInCStyleCastParentheses = false; - Style.SpacesInParentheses = true; + Style.SpacesInParensOptions.InCStyleCasts = false; + Style.SpacesInParensOptions.Other = true; verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); } @@ -23589,7 +23640,8 @@ FormatStyle Spaces = getLLVMStyle(); Spaces.IfMacros.clear(); Spaces.IfMacros.push_back("MYIF"); - Spaces.SpacesInConditionalStatement = true; + Spaces.SpacesInParens = FormatStyle::SIPO_Custom; + Spaces.SpacesInParensOptions.InConditionalStatements = true; verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); verifyFormat("if ( !a )\n return;", Spaces); verifyFormat("if ( a )\n return;", Spaces); diff --git a/clang/unittests/Format/FormatTestVerilog.cpp b/clang/unittests/Format/FormatTestVerilog.cpp --- a/clang/unittests/Format/FormatTestVerilog.cpp +++ b/clang/unittests/Format/FormatTestVerilog.cpp @@ -807,7 +807,8 @@ " if (x)\n" " x = x;", Style); - Style.SpacesInConditionalStatement = true; + Style.SpacesInParens = FormatStyle::SIPO_Custom; + Style.SpacesInParensOptions.InConditionalStatements = true; verifyFormat("if ( x )\n" " x = x;\n" "else if ( x )\n" @@ -903,7 +904,8 @@ verifyFormat("repeat (x) begin\n" "end"); auto Style = getDefaultStyle(); - Style.SpacesInConditionalStatement = true; + Style.SpacesInParens = FormatStyle::SIPO_Custom; + Style.SpacesInParensOptions.InConditionalStatements = true; verifyFormat("foreach ( x[x] )\n" " x = x;", Style);