Skip to content

Commit d5a563b

Browse files
committed
Only filter doubled parens situationally like RemoveParentheses
1 parent dbc3ea4 commit d5a563b

File tree

8 files changed

+271
-286
lines changed

8 files changed

+271
-286
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 25 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -5688,8 +5688,8 @@ the configuration (without a prefix: ``Auto``).
56885688
# Example of usage:
56895689
SpacesInParens: Custom
56905690
SpacesInParensOptions:
5691-
InAttributeSpecifiers: NonConsecutive
5692-
InConditionalStatements: Always
5691+
InAttributeSpecifiers: false
5692+
InConditionalStatements: true
56935693
InEmptyParentheses: true
56945694
56955695
Nested configuration flags:
@@ -5704,103 +5704,60 @@ the configuration (without a prefix: ``Auto``).
57045704
InConditionalStatements: true
57055705
Other: true
57065706

5707-
* ``SpacesInParensCustomStyle InAttributeSpecifiers``
5708-
Put a space in parentheses of attribute specifiers.
5707+
* ``bool ExceptDoubleParentheses`` Override any of the following options to prevent addition of space
5708+
between the first two parentheses in situations where a pair of
5709+
parentheses have been used.
57095710

57105711
.. code-block:: c++
57115712

5712-
Always:
5713-
__attribute__( ( noreturn ) )
5713+
true:
5714+
__attribute__(( noreturn ))
5715+
__decltype__(( x ))
5716+
if (( a = b ))
5717+
false:
5718+
Uses the applicable option.
5719+
5720+
* ``bool InAttributeSpecifiers`` Put a space in parentheses of attribute specifiers.
57145721

57155722
.. code-block:: c++
57165723

5717-
NonConsecutive:
5718-
_attribute__(( noreturn ))
5724+
true:
5725+
__attribute__( ( noreturn ) )
57195726

57205727
.. code-block:: c++
57215728

5722-
Never:
5729+
false:
57235730
_attribute__((noreturn))
57245731

5725-
Possible values:
5726-
5727-
* ``SIPCS_Never`` (in configuration: ``Never``)
5728-
Never put spaces in parens.
5729-
5730-
* ``SIPCS_NonConsecutive`` (in configuration: ``NonConsecutive``)
5731-
Only put spaces in parens not followed by the same consecutive parens.
5732-
5733-
* ``SIPCS_Always`` (in configuration: ``Always``)
5734-
Always put spaces in parens.
5735-
5736-
5737-
* ``SpacesInParensCustomStyle InConditionalStatements``
5738-
Put a space in parentheses only inside conditional statements
5732+
* ``bool InConditionalStatements`` Put a space in parentheses only inside conditional statements
57395733
(``for/if/while/switch...``).
57405734

57415735
.. code-block:: c++
57425736

5743-
Always:
5737+
true:
57445738
if ( ( a ) ) { ... }
57455739
while ( i < 5 ) { ... }
57465740

57475741
.. code-block:: c++
57485742

5749-
NonConsecutive:
5750-
if (( a )) { ... }
5751-
while ( i < 5 ) { ... }
5752-
5753-
.. code-block:: c++
5754-
5755-
Never:
5743+
false:
57565744
if ((a)) { ... }
57575745
while (i < 5) { ... }
57585746

5759-
Possible values:
5760-
5761-
* ``SIPCS_Never`` (in configuration: ``Never``)
5762-
Never put spaces in parens.
5763-
5764-
* ``SIPCS_NonConsecutive`` (in configuration: ``NonConsecutive``)
5765-
Only put spaces in parens not followed by the same consecutive parens.
5766-
5767-
* ``SIPCS_Always`` (in configuration: ``Always``)
5768-
Always put spaces in parens.
5769-
5770-
5771-
* ``SpacesInParensCustomStyle InCStyleCasts``
5772-
Put a space in C style casts.
5747+
* ``bool InCStyleCasts`` Put a space in C style casts.
57735748

57745749
.. code-block:: c++
57755750

5776-
Always:
5751+
true:
57775752
x = ( int32 )y
57785753
y = (( int (*)(int) )foo)(x);
57795754

57805755
.. code-block:: c++
57815756

5782-
NonConsecutive:
5783-
x = ( int32 )y
5784-
y = ((int (*)(int))foo)(x);
5785-
5786-
.. code-block:: c++
5787-
5788-
Never:
5757+
false:
57895758
x = (int32)y
57905759
y = ((int (*)(int))foo)(x);
57915760

5792-
Possible values:
5793-
5794-
* ``SIPCS_Never`` (in configuration: ``Never``)
5795-
Never put spaces in parens.
5796-
5797-
* ``SIPCS_NonConsecutive`` (in configuration: ``NonConsecutive``)
5798-
Only put spaces in parens not followed by the same consecutive parens.
5799-
5800-
* ``SIPCS_Always`` (in configuration: ``Always``)
5801-
Always put spaces in parens.
5802-
5803-
58045761
* ``bool InEmptyParentheses`` Put a space in parentheses only if the parentheses are empty i.e. '()'
58055762

58065763
.. code-block:: c++
@@ -5813,45 +5770,24 @@ the configuration (without a prefix: ``Auto``).
58135770
} }
58145771
} }
58155772

5816-
* ``SpacesInParensCustomStyle Other``
5817-
Put a space in parentheses not covered by preceding options.
5773+
* ``bool Other`` Put a space in parentheses not covered by preceding options.
58185774

58195775
.. code-block:: c++
58205776

5821-
Always:
5777+
true:
58225778
t f( Deleted & ) & = delete;
58235779
decltype( ( x ) )
58245780
x = ( (int32)y )
58255781
y = ( (int ( * )( int ))foo )( x );
58265782

58275783
.. code-block:: c++
58285784

5829-
NonConsecutive:
5830-
t f( Deleted & ) & = delete;
5831-
decltype(( x ))
5832-
x = ((int32))y
5833-
y = ((int ( * )( int ))foo)( x );
5834-
5835-
.. code-block:: c++
5836-
5837-
Never:
5785+
false:
58385786
t f(Deleted &) & = delete;
58395787
decltype((x))
58405788
x = ((int32))y
58415789
y = ((int (*)(int))foo)(x);
58425790

5843-
Possible values:
5844-
5845-
* ``SIPCS_Never`` (in configuration: ``Never``)
5846-
Never put spaces in parens.
5847-
5848-
* ``SIPCS_NonConsecutive`` (in configuration: ``NonConsecutive``)
5849-
Only put spaces in parens not followed by the same consecutive parens.
5850-
5851-
* ``SIPCS_Always`` (in configuration: ``Always``)
5852-
Always put spaces in parens.
5853-
5854-
58555791

58565792
.. _SpacesInParentheses:
58575793

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,10 +1284,9 @@ clang-format
12841284
- Add ``SkipMacroDefinitionBody`` option.
12851285
- Add ``InAttributeSpecifiers`` style option to ``SpacesInParensOptions``
12861286
to control addition of spaces after the ``__attribute__`` keyword.
1287-
- Add ``NonConsecutive`` sub-option for ``InAttributeSpecifiers``,
1288-
``InConditionalStatements``, ``InCStyleCasts``, and ``Other`` options of
1289-
``SpacesInParensOptions`` to control addition of spaces between consecutive
1290-
parentheses.
1287+
- Add ``ExceptDoubleParentheses`` sub-option for ``SpacesInParensOptions``
1288+
to override addition of spaces between multiple, non-redundant parentheses
1289+
similar to the rules used for ``RemoveParentheses``.
12911290

12921291
libclang
12931292
--------

clang/include/clang/Format/Format.h

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4533,16 +4533,6 @@ struct FormatStyle {
45334533
/// \version 17
45344534
SpacesInParensStyle SpacesInParens;
45354535

4536-
/// Selective control over spaces in parens.
4537-
enum SpacesInParensCustomStyle : int8_t {
4538-
/// Never put spaces in parens.
4539-
SIPCS_Never,
4540-
/// Only put spaces in parens not followed by the same consecutive parens.
4541-
SIPCS_NonConsecutive,
4542-
/// Always put spaces in parens.
4543-
SIPCS_Always
4544-
};
4545-
45464536
/// Precise control over the spacing in parentheses.
45474537
/// \code
45484538
/// # Should be declared this way:
@@ -4552,55 +4542,53 @@ struct FormatStyle {
45524542
/// Other: true
45534543
/// \endcode
45544544
struct SpacesInParensCustom {
4555-
/// Put a space in parentheses of attribute specifiers.
4545+
/// Override any of the following options to prevent addition of space
4546+
/// between the first two parentheses in situations where a pair of
4547+
/// parentheses have been used.
45564548
/// \code
4557-
/// Always:
4558-
/// __attribute__( ( noreturn ) )
4549+
/// true:
4550+
/// __attribute__(( noreturn ))
4551+
/// __decltype__(( x ))
4552+
/// if (( a = b ))
45594553
/// \endcode
4554+
/// false:
4555+
/// Uses the applicable option.
4556+
bool ExceptDoubleParentheses;
4557+
/// Put a space in parentheses of attribute specifiers.
45604558
/// \code
4561-
/// NonConsecutive:
4562-
/// _attribute__(( noreturn ))
4559+
/// true:
4560+
/// __attribute__( ( noreturn ) )
45634561
/// \endcode
45644562
/// \code
4565-
/// Never:
4563+
/// false:
45664564
/// _attribute__((noreturn))
45674565
/// \endcode
4568-
SpacesInParensCustomStyle InAttributeSpecifiers;
4566+
bool InAttributeSpecifiers;
45694567
/// Put a space in parentheses only inside conditional statements
45704568
/// (``for/if/while/switch...``).
45714569
/// \code
4572-
/// Always:
4570+
/// true:
45734571
/// if ( ( a ) ) { ... }
45744572
/// while ( i < 5 ) { ... }
45754573
/// \endcode
45764574
/// \code
4577-
/// NonConsecutive:
4578-
/// if (( a )) { ... }
4579-
/// while ( i < 5 ) { ... }
4580-
/// \endcode
4581-
/// \code
4582-
/// Never:
4575+
/// false:
45834576
/// if ((a)) { ... }
45844577
/// while (i < 5) { ... }
45854578
/// \endcode
4586-
SpacesInParensCustomStyle InConditionalStatements;
4579+
bool InConditionalStatements;
45874580
/// Put a space in C style casts.
45884581
/// \code
4589-
/// Always:
4582+
/// true:
45904583
/// x = ( int32 )y
45914584
/// y = (( int (*)(int) )foo)(x);
45924585
/// \endcode
45934586
/// \code
4594-
/// NonConsecutive:
4595-
/// x = ( int32 )y
4596-
/// y = ((int (*)(int))foo)(x);
4597-
/// \endcode
4598-
/// \code
4599-
/// Never:
4587+
/// false:
46004588
/// x = (int32)y
46014589
/// y = ((int (*)(int))foo)(x);
46024590
/// \endcode
4603-
SpacesInParensCustomStyle InCStyleCasts;
4591+
bool InCStyleCasts;
46044592
/// Put a space in parentheses only if the parentheses are empty i.e. '()'
46054593
/// \code
46064594
/// true: false:
@@ -4614,45 +4602,39 @@ struct FormatStyle {
46144602
bool InEmptyParentheses;
46154603
/// Put a space in parentheses not covered by preceding options.
46164604
/// \code
4617-
/// Always:
4605+
/// true:
46184606
/// t f( Deleted & ) & = delete;
46194607
/// decltype( ( x ) )
46204608
/// x = ( (int32)y )
46214609
/// y = ( (int ( * )( int ))foo )( x );
46224610
/// \endcode
46234611
/// \code
4624-
/// NonConsecutive:
4625-
/// t f( Deleted & ) & = delete;
4626-
/// decltype(( x ))
4627-
/// x = ((int32))y
4628-
/// y = ((int ( * )( int ))foo)( x );
4629-
/// \endcode
4630-
/// \code
4631-
/// Never:
4612+
/// false:
46324613
/// t f(Deleted &) & = delete;
46334614
/// decltype((x))
46344615
/// x = ((int32))y
46354616
/// y = ((int (*)(int))foo)(x);
46364617
/// \endcode
4637-
SpacesInParensCustomStyle Other;
4618+
bool Other;
46384619

46394620
SpacesInParensCustom()
4640-
: InAttributeSpecifiers(SIPCS_Never),
4641-
InConditionalStatements(SIPCS_Never), InCStyleCasts(SIPCS_Never),
4642-
InEmptyParentheses(false), Other(SIPCS_Never) {}
4643-
4644-
SpacesInParensCustom(SpacesInParensCustomStyle InAttributeSpecifiers,
4645-
SpacesInParensCustomStyle InConditionalStatements,
4646-
SpacesInParensCustomStyle InCStyleCasts,
4647-
bool InEmptyParentheses,
4648-
SpacesInParensCustomStyle Other)
4649-
: InAttributeSpecifiers(InAttributeSpecifiers),
4621+
: ExceptDoubleParentheses(false), InAttributeSpecifiers(false),
4622+
InConditionalStatements(false), InCStyleCasts(false),
4623+
InEmptyParentheses(false), Other(false) {}
4624+
4625+
SpacesInParensCustom(bool ExceptDoubleParentheses,
4626+
bool InAttributeSpecifiers,
4627+
bool InConditionalStatements, bool InCStyleCasts,
4628+
bool InEmptyParentheses, bool Other)
4629+
: ExceptDoubleParentheses(ExceptDoubleParentheses),
4630+
InAttributeSpecifiers(InAttributeSpecifiers),
46504631
InConditionalStatements(InConditionalStatements),
46514632
InCStyleCasts(InCStyleCasts), InEmptyParentheses(InEmptyParentheses),
46524633
Other(Other) {}
46534634

46544635
bool operator==(const SpacesInParensCustom &R) const {
4655-
return InAttributeSpecifiers == R.InAttributeSpecifiers &&
4636+
return ExceptDoubleParentheses == R.ExceptDoubleParentheses &&
4637+
InAttributeSpecifiers == R.InAttributeSpecifiers &&
46564638
InConditionalStatements == R.InConditionalStatements &&
46574639
InCStyleCasts == R.InCStyleCasts &&
46584640
InEmptyParentheses == R.InEmptyParentheses && Other == R.Other;
@@ -4671,8 +4653,8 @@ struct FormatStyle {
46714653
/// # Example of usage:
46724654
/// SpacesInParens: Custom
46734655
/// SpacesInParensOptions:
4674-
/// InAttributeSpecifiers: NonConsecutive
4675-
/// InConditionalStatements: Always
4656+
/// InAttributeSpecifiers: false
4657+
/// InConditionalStatements: true
46764658
/// InEmptyParentheses: true
46774659
/// \endcode
46784660
/// \version 17

0 commit comments

Comments
 (0)