Skip to content

Commit 79b4c34

Browse files
committed
Use SIPCS for other sub-options
1 parent 2d4e90d commit 79b4c34

File tree

8 files changed

+244
-87
lines changed

8 files changed

+244
-87
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5679,7 +5679,7 @@ the configuration (without a prefix: ``Auto``).
56795679
SpacesInParens: Custom
56805680
SpacesInParensOptions:
56815681
InAttributeSpecifiers: NonConsecutive
5682-
InConditionalStatements: true
5682+
InConditionalStatements: Always
56835683
InEmptyParentheses: true
56845684
56855685
Nested configuration flags:
@@ -5702,15 +5702,49 @@ the configuration (without a prefix: ``Auto``).
57025702
Always:
57035703
__attribute__( ( noreturn ) )
57045704

5705+
.. code-block:: c++
5706+
5707+
NonConsecutive:
5708+
_attribute__(( noreturn ))
5709+
5710+
.. code-block:: c++
5711+
5712+
Never:
5713+
_attribute__((noreturn))
5714+
5715+
Possible values:
5716+
5717+
* ``SIPCS_Never`` (in configuration: ``Never``)
5718+
Never put spaces in parens.
5719+
5720+
* ``SIPCS_NonConsecutive`` (in configuration: ``NonConsecutive``)
5721+
Only put spaces in parens not followed by the same consecutive parens.
5722+
5723+
* ``SIPCS_Always`` (in configuration: ``Always``)
5724+
Always put spaces in parens.
5725+
5726+
5727+
* ``SpacesInParensCustomStyle InConditionalStatements``
5728+
Put a space in parentheses only inside conditional statements
5729+
(``for/if/while/switch...``).
5730+
5731+
.. code-block:: c++
5732+
5733+
Always:
5734+
if ( ( a ) ) { ... }
5735+
while ( i < 5 ) { ... }
5736+
57055737
.. code-block:: c++
57065738

57075739
NonConsecutive:
5708-
_attribute__(( noreturn ))
5740+
if (( a )) { ... }
5741+
while ( i < 5 ) { ... }
57095742

57105743
.. code-block:: c++
57115744

57125745
Never:
5713-
_attribute__((noreturn))
5746+
if ((a)) { ... }
5747+
while (i < 5) { ... }
57145748

57155749
Possible values:
57165750

@@ -5724,21 +5758,38 @@ the configuration (without a prefix: ``Auto``).
57245758
Always put spaces in parens.
57255759

57265760

5727-
* ``bool InConditionalStatements`` Put a space in parentheses only inside conditional statements
5728-
(``for/if/while/switch...``).
5761+
* ``SpacesInParensCustomStyle InCStyleCasts``
5762+
Put a space in C style casts.
57295763

57305764
.. code-block:: c++
57315765

5732-
true: false:
5733-
if ( a ) { ... } vs. if (a) { ... }
5734-
while ( i < 5 ) { ... } while (i < 5) { ... }
5766+
Always: false:
5767+
x = ( int32 )y vs. x = (int32)y
5768+
y = (( int (*)(int) )foo)(x);
5769+
5770+
.. code-block:: c++
57355771

5736-
* ``bool InCStyleCasts`` Put a space in C style casts.
5772+
NonConsecutive:
5773+
x = ( int32 )y
5774+
y = ((int (*)(int))foo)(x);
57375775

57385776
.. code-block:: c++
57395777

5740-
true: false:
5741-
x = ( int32 )y vs. x = (int32)y
5778+
Never:
5779+
x = (int32)y
5780+
y = ((int (*)(int))foo)(x);
5781+
5782+
Possible values:
5783+
5784+
* ``SIPCS_Never`` (in configuration: ``Never``)
5785+
Never put spaces in parens.
5786+
5787+
* ``SIPCS_NonConsecutive`` (in configuration: ``NonConsecutive``)
5788+
Only put spaces in parens not followed by the same consecutive parens.
5789+
5790+
* ``SIPCS_Always`` (in configuration: ``Always``)
5791+
Always put spaces in parens.
5792+
57425793

57435794
* ``bool InEmptyParentheses`` Put a space in parentheses only if the parentheses are empty i.e. '()'
57445795

@@ -5752,12 +5803,44 @@ the configuration (without a prefix: ``Auto``).
57525803
} }
57535804
} }
57545805

5755-
* ``bool Other`` Put a space in parentheses not covered by preceding options.
5806+
* ``SpacesInParensCustomStyle Other``
5807+
Put a space in parentheses not covered by preceding options.
57565808

57575809
.. code-block:: c++
57585810

5759-
true: false:
5760-
t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete;
5811+
Always:
5812+
t f( Deleted & ) & = delete;
5813+
decltype( ( x ) )
5814+
x = ( (int32)y )
5815+
y = ( (int ( * )( int ))foo )( x );
5816+
5817+
.. code-block:: c++
5818+
5819+
NonConsecutive:
5820+
t f( Deleted & ) & = delete;
5821+
decltype(( x ))
5822+
x = ((int32))y
5823+
y = ((int ( * )( int ))foo)( x );
5824+
5825+
.. code-block:: c++
5826+
5827+
Never:
5828+
t f(Deleted &) & = delete;
5829+
decltype((x))
5830+
x = ((int32))y
5831+
y = ((int (*)(int))foo)(x);
5832+
5833+
Possible values:
5834+
5835+
* ``SIPCS_Never`` (in configuration: ``Never``)
5836+
Never put spaces in parens.
5837+
5838+
* ``SIPCS_NonConsecutive`` (in configuration: ``NonConsecutive``)
5839+
Only put spaces in parens not followed by the same consecutive parens.
5840+
5841+
* ``SIPCS_Always`` (in configuration: ``Always``)
5842+
Always put spaces in parens.
5843+
57615844

57625845

57635846
.. _SpacesInParentheses:

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,10 @@ clang-format
11041104
- Add ``AlignFunctionPointers`` sub-option for ``AlignConsecutiveDeclarations``.
11051105
- Add ``InAttributeSpecifiers`` style option to ``SpacesInParensOptions``
11061106
to control addition of spaces after the ``__attribute__`` keyword.
1107+
- Add ``NonConsecutive`` sub-option for ``InAttributeSpecifiers``,
1108+
``InConditionalStatements``, ``InCStyleCasts``, and ``Other`` options of
1109+
``SpacesInParensOptions`` to control addition of spaces between consecutive
1110+
parentheses.
11071111

11081112
libclang
11091113
--------

clang/include/clang/Format/Format.h

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4550,28 +4550,49 @@ struct FormatStyle {
45504550
/// __attribute__( ( noreturn ) )
45514551
/// \endcode
45524552
/// \code
4553-
/// NonConsecutive:
4554-
/// _attribute__(( noreturn ))
4553+
/// NonConsecutive:
4554+
/// _attribute__(( noreturn ))
45554555
/// \endcode
45564556
/// \code
4557-
/// Never:
4558-
/// _attribute__((noreturn))
4557+
/// Never:
4558+
/// _attribute__((noreturn))
45594559
/// \endcode
45604560
SpacesInParensCustomStyle InAttributeSpecifiers;
45614561
/// Put a space in parentheses only inside conditional statements
45624562
/// (``for/if/while/switch...``).
45634563
/// \code
4564-
/// true: false:
4565-
/// if ( a ) { ... } vs. if (a) { ... }
4566-
/// while ( i < 5 ) { ... } while (i < 5) { ... }
4564+
/// Always:
4565+
/// if ( ( a ) ) { ... }
4566+
/// while ( i < 5 ) { ... }
4567+
/// \endcode
4568+
/// \code
4569+
/// NonConsecutive:
4570+
/// if (( a )) { ... }
4571+
/// while ( i < 5 ) { ... }
45674572
/// \endcode
4568-
bool InConditionalStatements;
4573+
/// \code
4574+
/// Never:
4575+
/// if ((a)) { ... }
4576+
/// while (i < 5) { ... }
4577+
/// \endcode
4578+
SpacesInParensCustomStyle InConditionalStatements;
45694579
/// Put a space in C style casts.
45704580
/// \code
4571-
/// true: false:
4572-
/// x = ( int32 )y vs. x = (int32)y
4581+
/// Always: false:
4582+
/// x = ( int32 )y vs. x = (int32)y
4583+
/// y = (( int (*)(int) )foo)(x);
45734584
/// \endcode
4574-
bool InCStyleCasts;
4585+
/// \code
4586+
/// NonConsecutive:
4587+
/// x = ( int32 )y
4588+
/// y = ((int (*)(int))foo)(x);
4589+
/// \endcode
4590+
/// \code
4591+
/// Never:
4592+
/// x = (int32)y
4593+
/// y = ((int (*)(int))foo)(x);
4594+
/// \endcode
4595+
SpacesInParensCustomStyle InCStyleCasts;
45754596
/// Put a space in parentheses only if the parentheses are empty i.e. '()'
45764597
/// \code
45774598
/// true: false:
@@ -4585,18 +4606,39 @@ struct FormatStyle {
45854606
bool InEmptyParentheses;
45864607
/// Put a space in parentheses not covered by preceding options.
45874608
/// \code
4588-
/// true: false:
4589-
/// t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete;
4609+
/// Always:
4610+
/// t f( Deleted & ) & = delete;
4611+
/// decltype( ( x ) )
4612+
/// x = ( (int32)y )
4613+
/// y = ( (int ( * )( int ))foo )( x );
4614+
/// \endcode
4615+
/// \code
4616+
/// NonConsecutive:
4617+
/// t f( Deleted & ) & = delete;
4618+
/// decltype(( x ))
4619+
/// x = ((int32))y
4620+
/// y = ((int ( * )( int ))foo)( x );
4621+
/// \endcode
4622+
/// \code
4623+
/// Never:
4624+
/// t f(Deleted &) & = delete;
4625+
/// decltype((x))
4626+
/// x = ((int32))y
4627+
/// y = ((int (*)(int))foo)(x);
45904628
/// \endcode
4591-
bool Other;
4629+
SpacesInParensCustomStyle Other;
45924630

45934631
SpacesInParensCustom()
4594-
: InAttributeSpecifiers(SIPCS_Never), InConditionalStatements(false),
4595-
InCStyleCasts(false), InEmptyParentheses(false), Other(false) {}
4632+
: InAttributeSpecifiers(SIPCS_Never),
4633+
InConditionalStatements(SIPCS_Never),
4634+
InCStyleCasts(SIPCS_Never), InEmptyParentheses(false),
4635+
Other(SIPCS_Never) {}
45964636

45974637
SpacesInParensCustom(SpacesInParensCustomStyle InAttributeSpecifiers,
4598-
bool InConditionalStatements, bool InCStyleCasts,
4599-
bool InEmptyParentheses, bool Other)
4638+
SpacesInParensCustomStyle InConditionalStatements,
4639+
SpacesInParensCustomStyle InCStyleCasts,
4640+
bool InEmptyParentheses,
4641+
SpacesInParensCustomStyle Other)
46004642
: InAttributeSpecifiers(InAttributeSpecifiers),
46014643
InConditionalStatements(InConditionalStatements),
46024644
InCStyleCasts(InCStyleCasts), InEmptyParentheses(InEmptyParentheses),
@@ -4623,7 +4665,7 @@ struct FormatStyle {
46234665
/// SpacesInParens: Custom
46244666
/// SpacesInParensOptions:
46254667
/// InAttributeSpecifiers: NonConsecutive
4626-
/// InConditionalStatements: true
4668+
/// InConditionalStatements: Always
46274669
/// InEmptyParentheses: true
46284670
/// \endcode
46294671
/// \version 17

clang/lib/Format/Format.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,10 @@ struct ScalarEnumerationTraits<FormatStyle::SpacesInParensCustomStyle> {
757757
IO.enumCase(Value, "Never", FormatStyle::SIPCS_Never);
758758
IO.enumCase(Value, "NonConsecutive", FormatStyle::SIPCS_NonConsecutive);
759759
IO.enumCase(Value, "Always", FormatStyle::SIPCS_Always);
760+
761+
// For backward compatibility.
762+
IO.enumCase(Value, "true", FormatStyle::SIPCS_Always);
763+
IO.enumCase(Value, "false", FormatStyle::SIPCS_Never);
760764
}
761765
};
762766

@@ -1198,23 +1202,27 @@ template <> struct MappingTraits<FormatStyle> {
11981202
if (Style.SpacesInParens != FormatStyle::SIPO_Custom &&
11991203
(SpacesInParentheses || SpaceInEmptyParentheses ||
12001204
SpacesInConditionalStatement || SpacesInCStyleCastParentheses)) {
1205+
const auto CoerceBooleanToSIPCS = [](const bool enabled) {
1206+
return enabled ? FormatStyle::SIPCS_Always : FormatStyle::SIPCS_Never;
1207+
};
12011208
if (SpacesInParentheses) {
12021209
// set all options except InCStyleCasts and InEmptyParentheses
1203-
// to true for backward compatibility.
1210+
// to true/Always for backward compatibility.
12041211
Style.SpacesInParensOptions.InAttributeSpecifiers =
12051212
FormatStyle::SIPCS_Always;
1206-
Style.SpacesInParensOptions.InConditionalStatements = true;
1213+
Style.SpacesInParensOptions.InConditionalStatements =
1214+
FormatStyle::SIPCS_Always;
12071215
Style.SpacesInParensOptions.InCStyleCasts =
1208-
SpacesInCStyleCastParentheses;
1216+
CoerceBooleanToSIPCS(SpacesInCStyleCastParentheses);
12091217
Style.SpacesInParensOptions.InEmptyParentheses =
12101218
SpaceInEmptyParentheses;
1211-
Style.SpacesInParensOptions.Other = true;
1219+
Style.SpacesInParensOptions.Other = FormatStyle::SIPCS_Always;
12121220
} else {
12131221
Style.SpacesInParensOptions = {};
12141222
Style.SpacesInParensOptions.InConditionalStatements =
1215-
SpacesInConditionalStatement;
1223+
CoerceBooleanToSIPCS(SpacesInConditionalStatement);
12161224
Style.SpacesInParensOptions.InCStyleCasts =
1217-
SpacesInCStyleCastParentheses;
1225+
CoerceBooleanToSIPCS(SpacesInCStyleCastParentheses);
12181226
Style.SpacesInParensOptions.InEmptyParentheses =
12191227
SpaceInEmptyParentheses;
12201228
}

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,8 +3549,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
35493549
const FormatToken *Prev = Current->Previous;
35503550
if (Current->is(TT_LineComment)) {
35513551
if (Prev->is(BK_BracedInit) && Prev->opensScope()) {
3552-
Current->SpacesRequiredBefore =
3553-
(Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
3552+
Current->SpacesRequiredBefore = (Style.Cpp11BracedListStyle && Style.SpacesInParensOptions.Other == FormatStyle::SIPCS_Never)
35543553
? 0
35553554
: 1;
35563555
} else if (Prev->is(TT_VerilogMultiLineListLParen)) {
@@ -3968,7 +3967,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
39683967
Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
39693968
return Style.SpacesInParensOptions.InEmptyParentheses;
39703969
}
3971-
if (Style.SpacesInParensOptions.InConditionalStatements) {
3970+
if (Style.SpacesInParensOptions.InConditionalStatements != FormatStyle::SIPCS_Never) {
3971+
// TODO: check consecutive parens
39723972
const FormatToken *LeftParen = nullptr;
39733973
if (Left.is(tok::l_paren))
39743974
LeftParen = &Left;
@@ -4004,10 +4004,12 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
40044004
return true;
40054005
}
40064006

4007+
// TODO: check consecutive parens
40074008
if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
40084009
if (Right.is(TT_CastRParen) ||
40094010
(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) {
4010-
return Style.SpacesInParensOptions.InCStyleCasts;
4011+
return Style.SpacesInParensOptions.InCStyleCasts !=
4012+
FormatStyle::SIPCS_Never;
40114013
}
40124014
const auto isAttributeParen = [](const FormatToken *Paren) {
40134015
return Paren && Paren->isOneOf(TT_AttributeLParen, TT_AttributeRParen);
@@ -4020,7 +4022,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
40204022
return Style.SpacesInParensOptions.InAttributeSpecifiers !=
40214023
FormatStyle::SIPCS_Never;
40224024
}
4023-
return Style.SpacesInParensOptions.Other;
4025+
return Style.SpacesInParensOptions.Other != FormatStyle::SIPCS_Never;
40244026
}
40254027
if (Right.isOneOf(tok::semi, tok::comma))
40264028
return false;
@@ -4243,7 +4245,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
42434245
if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) ||
42444246
(Right.is(tok::r_brace) && Right.MatchingParen &&
42454247
Right.MatchingParen->isNot(BK_Block))) {
4246-
return !Style.Cpp11BracedListStyle || Style.SpacesInParensOptions.Other;
4248+
return !Style.Cpp11BracedListStyle || (Style.SpacesInParensOptions.Other !=
4249+
FormatStyle::SIPCS_Never);
42474250
}
42484251
if (Left.is(TT_BlockComment)) {
42494252
// No whitespace in x(/*foo=*/1), except for JavaScript.
@@ -4929,7 +4932,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
49294932
!(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
49304933
tok::kw___super, TT_TemplateOpener,
49314934
TT_TemplateCloser)) ||
4932-
(Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
4935+
(Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other !=
4936+
FormatStyle::SIPCS_Always);
49334937
}
49344938
if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
49354939
return ShouldAddSpacesInAngles();

0 commit comments

Comments
 (0)