Skip to content

Commit c669541

Browse files
gedareowenca
authored andcommitted
[clang-format] Add SpacesInParens with SpacesInParensOptions
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
1 parent 2398e26 commit c669541

File tree

8 files changed

+394
-97
lines changed

8 files changed

+394
-97
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 100 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5215,16 +5215,8 @@ the configuration (without a prefix: ``Auto``).
52155215

52165216
**SpaceInEmptyParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`<SpaceInEmptyParentheses>`
52175217
If ``true``, spaces may be inserted into ``()``.
5218-
5219-
.. code-block:: c++
5220-
5221-
true: false:
5222-
void f( ) { vs. void f() {
5223-
int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()};
5224-
if (true) { if (true) {
5225-
f( ); f();
5226-
} }
5227-
} }
5218+
This option is **deprecated**. See ``InEmptyParentheses`` of
5219+
``SpacesInParensOptions``.
52285220

52295221
.. _SpacesBeforeTrailingComments:
52305222

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

52825274
**SpacesInCStyleCastParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`<SpacesInCStyleCastParentheses>`
52835275
If ``true``, spaces may be inserted into C style casts.
5284-
5285-
.. code-block:: c++
5286-
5287-
true: false:
5288-
x = ( int32 )y vs. x = (int32)y
5276+
This option is **deprecated**. See ``InCStyleCasts`` of
5277+
``SpacesInParensOptions``.
52895278

52905279
.. _SpacesInConditionalStatement:
52915280

52925281
**SpacesInConditionalStatement** (``Boolean``) :versionbadge:`clang-format 10` :ref:`<SpacesInConditionalStatement>`
52935282
If ``true``, spaces will be inserted around if/for/switch/while
52945283
conditions.
5295-
5296-
.. code-block:: c++
5297-
5298-
true: false:
5299-
if ( a ) { ... } vs. if (a) { ... }
5300-
while ( i < 5 ) { ... } while (i < 5) { ... }
5284+
This option is **deprecated**. See ``InConditionalStatements`` of
5285+
``SpacesInParensOptions``.
53015286

53025287
.. _SpacesInContainerLiterals:
53035288

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

53605345

5361-
.. _SpacesInParentheses:
5346+
.. _SpacesInParens:
53625347

5363-
**SpacesInParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`<SpacesInParentheses>`
5364-
If ``true``, spaces will be inserted after ``(`` and before ``)``.
5348+
**SpacesInParens** (``SpacesInParensStyle``) :versionbadge:`clang-format 17` :ref:`<SpacesInParens>`
5349+
Defines in which cases spaces will be inserted after ``(`` and before
5350+
``)``.
5351+
5352+
Possible values:
5353+
5354+
* ``SIPO_Never`` (in configuration: ``Never``)
5355+
Never put a space in parentheses.
5356+
5357+
.. code-block:: c++
5358+
5359+
void f() {
5360+
if(true) {
5361+
f();
5362+
}
5363+
}
5364+
5365+
* ``SIPO_Custom`` (in configuration: ``Custom``)
5366+
Configure each individual space in parentheses in
5367+
`SpacesInParensOptions`.
5368+
5369+
5370+
5371+
.. _SpacesInParensOptions:
5372+
5373+
**SpacesInParensOptions** (``SpacesInParensCustom``) :versionbadge:`clang-format 17` :ref:`<SpacesInParensOptions>`
5374+
Control of individual spaces in parentheses.
5375+
5376+
If ``SpacesInParens`` is set to ``Custom``, use this to specify
5377+
how each individual space in parentheses case should be handled.
5378+
Otherwise, this is ignored.
5379+
5380+
.. code-block:: yaml
5381+
5382+
# Example of usage:
5383+
SpacesInParens: Custom
5384+
SpacesInParensOptions:
5385+
InConditionalStatements: true
5386+
InEmptyParentheses: true
5387+
5388+
Nested configuration flags:
5389+
5390+
Precise control over the spacing in parentheses.
53655391

53665392
.. code-block:: c++
53675393

5368-
true: false:
5369-
t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete;
5394+
# Should be declared this way:
5395+
SpacesInParens: Custom
5396+
SpacesInParensOptions:
5397+
InConditionalStatements: true
5398+
Other: true
5399+
5400+
* ``bool InConditionalStatements`` Put a space in parentheses only inside conditional statements
5401+
(``for/if/while/switch...``).
5402+
5403+
.. code-block:: c++
5404+
5405+
true: false:
5406+
if ( a ) { ... } vs. if (a) { ... }
5407+
while ( i < 5 ) { ... } while (i < 5) { ... }
5408+
5409+
* ``bool InCStyleCasts`` Put a space in C style casts.
5410+
5411+
.. code-block:: c++
5412+
5413+
true: false:
5414+
x = ( int32 )y vs. x = (int32)y
5415+
5416+
* ``bool InEmptyParentheses`` Put a space in parentheses only if the parentheses are empty i.e. '()'
5417+
5418+
.. code-block:: c++
5419+
5420+
true: false:
5421+
void f( ) { vs. void f() {
5422+
int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()};
5423+
if (true) { if (true) {
5424+
f( ); f();
5425+
} }
5426+
} }
5427+
5428+
* ``bool Other`` Put a space in parentheses not covered by preceding options.
5429+
5430+
.. code-block:: c++
5431+
5432+
true: false:
5433+
t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete;
5434+
5435+
5436+
.. _SpacesInParentheses:
5437+
5438+
**SpacesInParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`<SpacesInParentheses>`
5439+
If ``true'', spaces will be inserted after ``(`` and before ``)``.
5440+
This option is **deprecated**. The previous behavior is preserved by using
5441+
``SpacesInParens`` with ``Custom`` and by setting all
5442+
``SpacesInParensOptions`` to ``true`` except for ``InCStyleCasts`` and
5443+
``InEmptyParentheses``.
53705444

53715445
.. _SpacesInSquareBrackets:
53725446

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,9 @@ clang-format
990990
- Add ``TypeNames`` to treat listed non-keyword identifiers as type names.
991991
- Add ``AlignConsecutiveShortCaseStatements`` which can be used to align case
992992
labels in conjunction with ``AllowShortCaseLabelsOnASingleLine``.
993+
- Add ``SpacesInParens`` style with ``SpacesInParensOptions`` to replace
994+
``SpacesInConditionalStatement``, ``SpacesInCStyleCastParentheses``,
995+
``SpaceInEmptyParentheses``, and ``SpacesInParentheses``.
993996

994997
libclang
995998
--------

clang/include/clang/Format/Format.h

Lines changed: 115 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4151,17 +4151,10 @@ struct FormatStyle {
41514151
bool SpaceInEmptyBlock;
41524152

41534153
/// If ``true``, spaces may be inserted into ``()``.
4154-
/// \code
4155-
/// true: false:
4156-
/// void f( ) { vs. void f() {
4157-
/// int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()};
4158-
/// if (true) { if (true) {
4159-
/// f( ); f();
4160-
/// } }
4161-
/// } }
4162-
/// \endcode
4154+
/// This option is **deprecated**. See ``InEmptyParentheses`` of
4155+
/// ``SpacesInParensOptions``.
41634156
/// \version 3.7
4164-
bool SpaceInEmptyParentheses;
4157+
// bool SpaceInEmptyParentheses;
41654158

41664159
/// The number of spaces before trailing line comments
41674160
/// (``//`` - comments).
@@ -4208,13 +4201,10 @@ struct FormatStyle {
42084201

42094202
/// If ``true``, spaces will be inserted around if/for/switch/while
42104203
/// conditions.
4211-
/// \code
4212-
/// true: false:
4213-
/// if ( a ) { ... } vs. if (a) { ... }
4214-
/// while ( i < 5 ) { ... } while (i < 5) { ... }
4215-
/// \endcode
4204+
/// This option is **deprecated**. See ``InConditionalStatements`` of
4205+
/// ``SpacesInParensOptions``.
42164206
/// \version 10
4217-
bool SpacesInConditionalStatement;
4207+
// bool SpacesInConditionalStatement;
42184208

42194209
/// If ``true``, spaces are inserted inside container literals (e.g. ObjC and
42204210
/// Javascript array and dict literals). For JSON, use
@@ -4228,12 +4218,10 @@ struct FormatStyle {
42284218
bool SpacesInContainerLiterals;
42294219

42304220
/// If ``true``, spaces may be inserted into C style casts.
4231-
/// \code
4232-
/// true: false:
4233-
/// x = ( int32 )y vs. x = (int32)y
4234-
/// \endcode
4221+
/// This option is **deprecated**. See ``InCStyleCasts`` of
4222+
/// ``SpacesInParensOptions``.
42354223
/// \version 3.7
4236-
bool SpacesInCStyleCastParentheses;
4224+
// bool SpacesInCStyleCastParentheses;
42374225

42384226
/// Control of spaces within a single line comment.
42394227
struct SpacesInLineComment {
@@ -4277,13 +4265,112 @@ struct FormatStyle {
42774265
/// \version 13
42784266
SpacesInLineComment SpacesInLineCommentPrefix;
42794267

4280-
/// If ``true``, spaces will be inserted after ``(`` and before ``)``.
4268+
/// Different ways to put a space before opening and closing parentheses.
4269+
enum SpacesInParensStyle : int8_t {
4270+
/// Never put a space in parentheses.
4271+
/// \code
4272+
/// void f() {
4273+
/// if(true) {
4274+
/// f();
4275+
/// }
4276+
/// }
4277+
/// \endcode
4278+
SIPO_Never,
4279+
/// Configure each individual space in parentheses in
4280+
/// `SpacesInParensOptions`.
4281+
SIPO_Custom,
4282+
};
4283+
4284+
/// If ``true'', spaces will be inserted after ``(`` and before ``)``.
4285+
/// This option is **deprecated**. The previous behavior is preserved by using
4286+
/// ``SpacesInParens`` with ``Custom`` and by setting all
4287+
/// ``SpacesInParensOptions`` to ``true`` except for ``InCStyleCasts`` and
4288+
/// ``InEmptyParentheses``.
4289+
/// \version 3.7
4290+
// bool SpacesInParentheses;
4291+
4292+
/// Defines in which cases spaces will be inserted after ``(`` and before
4293+
/// ``)``.
4294+
/// \version 17
4295+
SpacesInParensStyle SpacesInParens;
4296+
4297+
/// Precise control over the spacing in parentheses.
42814298
/// \code
4282-
/// true: false:
4283-
/// t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete;
4299+
/// # Should be declared this way:
4300+
/// SpacesInParens: Custom
4301+
/// SpacesInParensOptions:
4302+
/// InConditionalStatements: true
4303+
/// Other: true
42844304
/// \endcode
4285-
/// \version 3.7
4286-
bool SpacesInParentheses;
4305+
struct SpacesInParensCustom {
4306+
/// Put a space in parentheses only inside conditional statements
4307+
/// (``for/if/while/switch...``).
4308+
/// \code
4309+
/// true: false:
4310+
/// if ( a ) { ... } vs. if (a) { ... }
4311+
/// while ( i < 5 ) { ... } while (i < 5) { ... }
4312+
/// \endcode
4313+
bool InConditionalStatements;
4314+
/// Put a space in C style casts.
4315+
/// \code
4316+
/// true: false:
4317+
/// x = ( int32 )y vs. x = (int32)y
4318+
/// \endcode
4319+
bool InCStyleCasts;
4320+
/// Put a space in parentheses only if the parentheses are empty i.e. '()'
4321+
/// \code
4322+
/// true: false:
4323+
/// void f( ) { vs. void f() {
4324+
/// int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()};
4325+
/// if (true) { if (true) {
4326+
/// f( ); f();
4327+
/// } }
4328+
/// } }
4329+
/// \endcode
4330+
bool InEmptyParentheses;
4331+
/// Put a space in parentheses not covered by preceding options.
4332+
/// \code
4333+
/// true: false:
4334+
/// t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete;
4335+
/// \endcode
4336+
bool Other;
4337+
4338+
SpacesInParensCustom()
4339+
: InConditionalStatements(false), InCStyleCasts(false),
4340+
InEmptyParentheses(false), Other(false) {}
4341+
4342+
SpacesInParensCustom(bool InConditionalStatements, bool InCStyleCasts,
4343+
bool InEmptyParentheses, bool Other)
4344+
: InConditionalStatements(InConditionalStatements),
4345+
InCStyleCasts(InCStyleCasts),
4346+
InEmptyParentheses(InEmptyParentheses),
4347+
Other(Other) {}
4348+
4349+
bool operator==(const SpacesInParensCustom &R) const {
4350+
return InConditionalStatements == R.InConditionalStatements &&
4351+
InCStyleCasts == R.InCStyleCasts &&
4352+
InEmptyParentheses == R.InEmptyParentheses &&
4353+
Other == R.Other;
4354+
}
4355+
bool operator!=(const SpacesInParensCustom &R) const {
4356+
return !(*this == R);
4357+
}
4358+
};
4359+
4360+
/// Control of individual spaces in parentheses.
4361+
///
4362+
/// If ``SpacesInParens`` is set to ``Custom``, use this to specify
4363+
/// how each individual space in parentheses case should be handled.
4364+
/// Otherwise, this is ignored.
4365+
/// \code{.yaml}
4366+
/// # Example of usage:
4367+
/// SpacesInParens: Custom
4368+
/// SpacesInParensOptions:
4369+
/// InConditionalStatements: true
4370+
/// InEmptyParentheses: true
4371+
/// \endcode
4372+
/// \version 17
4373+
SpacesInParensCustom SpacesInParensOptions;
42874374

42884375
/// If ``true``, spaces will be inserted after ``[`` and before ``]``.
42894376
/// Lambdas without arguments or unspecified size array declarations will not
@@ -4587,17 +4674,15 @@ struct FormatStyle {
45874674
R.SpaceBeforeRangeBasedForLoopColon &&
45884675
SpaceBeforeSquareBrackets == R.SpaceBeforeSquareBrackets &&
45894676
SpaceInEmptyBlock == R.SpaceInEmptyBlock &&
4590-
SpaceInEmptyParentheses == R.SpaceInEmptyParentheses &&
45914677
SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments &&
45924678
SpacesInAngles == R.SpacesInAngles &&
4593-
SpacesInConditionalStatement == R.SpacesInConditionalStatement &&
45944679
SpacesInContainerLiterals == R.SpacesInContainerLiterals &&
4595-
SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses &&
45964680
SpacesInLineCommentPrefix.Minimum ==
45974681
R.SpacesInLineCommentPrefix.Minimum &&
45984682
SpacesInLineCommentPrefix.Maximum ==
45994683
R.SpacesInLineCommentPrefix.Maximum &&
4600-
SpacesInParentheses == R.SpacesInParentheses &&
4684+
SpacesInParens == R.SpacesInParens &&
4685+
SpacesInParensOptions == R.SpacesInParensOptions &&
46014686
SpacesInSquareBrackets == R.SpacesInSquareBrackets &&
46024687
Standard == R.Standard &&
46034688
StatementAttributeLikeMacros == R.StatementAttributeLikeMacros &&

0 commit comments

Comments
 (0)