Skip to content

Commit

Permalink
[clang-format] Add SpacesInParens with SpacesInParensOptions
Browse files Browse the repository at this point in the history
This is a refactoring of:
- SpacesInConditionalStatement
- SpacesInCStyleCastParentheses
- SpaceInEmptyParentheses
- SpacesInParentheses

These are now options under the new Style Option: SpacesInParens. The
existing options are maintained for backward compatibility.

Within SpacesInParens, there are currently options for:
- Never
- Custom

The currently available options for Custom are:
- InConditionalStatements
- InCStyleCasts
- InEmptyParentheses
- Other

Setting InConditionalStatements and Other to true enables the same space
additions as SpacesInParentheses.

This refactoring does not add or remove any existing features, but it makes
it possible to more easily extend and maintain the addition of spaces within
parentheses.

Related to #55428.

Differential Revision: https://reviews.llvm.org/D155239
  • Loading branch information
gedare authored and owenca committed Jul 25, 2023
1 parent 2398e26 commit c669541
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 97 deletions.
126 changes: 100 additions & 26 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5215,16 +5215,8 @@ the configuration (without a prefix: ``Auto``).

**SpaceInEmptyParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`<SpaceInEmptyParentheses>`
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:

Expand Down Expand Up @@ -5281,23 +5273,16 @@ the configuration (without a prefix: ``Auto``).

**SpacesInCStyleCastParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`<SpacesInCStyleCastParentheses>`
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:`<SpacesInConditionalStatement>`
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:

Expand Down Expand Up @@ -5358,15 +5343,104 @@ the configuration (without a prefix: ``Auto``).
* ``unsigned Maximum`` The maximum number of spaces at the start of the comment.


.. _SpacesInParentheses:
.. _SpacesInParens:

**SpacesInParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`<SpacesInParentheses>`
If ``true``, spaces will be inserted after ``(`` and before ``)``.
**SpacesInParens** (``SpacesInParensStyle``) :versionbadge:`clang-format 17` :ref:`<SpacesInParens>`
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:`<SpacesInParensOptions>`
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:`<SpacesInParentheses>`
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:

Expand Down
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,9 @@ clang-format
- Add ``TypeNames`` to treat listed non-keyword identifiers as type names.
- Add ``AlignConsecutiveShortCaseStatements`` which can be used to align case
labels in conjunction with ``AllowShortCaseLabelsOnASingleLine``.
- Add ``SpacesInParens`` style with ``SpacesInParensOptions`` to replace
``SpacesInConditionalStatement``, ``SpacesInCStyleCastParentheses``,
``SpaceInEmptyParentheses``, and ``SpacesInParentheses``.

libclang
--------
Expand Down
145 changes: 115 additions & 30 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4151,17 +4151,10 @@ struct FormatStyle {
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).
Expand Down Expand Up @@ -4208,13 +4201,10 @@ struct FormatStyle {

/// 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
Expand All @@ -4228,12 +4218,10 @@ struct FormatStyle {
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 {
Expand Down Expand Up @@ -4277,13 +4265,112 @@ struct FormatStyle {
/// \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
Expand Down Expand Up @@ -4587,17 +4674,15 @@ struct FormatStyle {
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 &&
Expand Down
Loading

0 comments on commit c669541

Please sign in to comment.