Skip to content

Commit c06e8ba

Browse files
committed
Support function and overloaded operator SpacesInParensOption
This change separates function calls, declarations, definitions, and overloaded operators from `SpacesInParensOptions.Other` to allow control over each independently. Fixes llvm#55428.
1 parent 5d28f33 commit c06e8ba

File tree

7 files changed

+265
-13
lines changed

7 files changed

+265
-13
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5660,13 +5660,42 @@ the configuration (without a prefix: ``Auto``).
56605660
} }
56615661
} }
56625662

5663-
* ``bool Other`` Put a space in parentheses not covered by preceding options.
5663+
* ``bool InFunctionCalls`` Put a space in parentheses of function calls.
56645664

56655665
.. code-block:: c++
56665666

56675667
true: false:
56685668
t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete;
56695669

5670+
* ``bool InFunctionDeclarations`` Put a space in parentheses of function declarations.
5671+
5672+
.. code-block:: c++
5673+
5674+
true: false:
5675+
void foo( int bar ); vs. void foo(int bar);
5676+
5677+
* ``bool InFunctionDefinitions`` Put a space in parentheses of function definitions.
5678+
5679+
.. code-block:: c++
5680+
5681+
true: false:
5682+
void foo( int bar ) { } vs. void foo(int bar) { }
5683+
5684+
* ``bool InOverloadedOperators`` Put a space in parentheses of overloaded operators.
5685+
5686+
.. code-block:: c++
5687+
5688+
true: false:
5689+
void operator++( int a ) vs. void operator++(int a)
5690+
object.operator++( 10 ) vs. object.operator++(10)
5691+
5692+
* ``bool Other`` Put a space in parentheses not covered by preceding options.
5693+
5694+
.. code-block:: c++
5695+
5696+
true: false:
5697+
x = ( y + z ); vs. x = (y+z);
5698+
56705699

56715700
.. _SpacesInParentheses:
56725701

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,9 @@ clang-format
10811081
- Add ``.clang-format-ignore`` files.
10821082
- Add ``InAttributeSpecifiers`` style option to ``SpacesInParensOptions``
10831083
to control addition of spaces after the ``__attribute__`` keyword.
1084+
- Add ``InFunctionCalls``, ``InFunctionDeclarations``,
1085+
``InFunctionDefinitions``, and ``InOverloadedOperators`` style options to
1086+
``SpacesInParensOptions`` to control addition of spaces.
10841087

10851088
libclang
10861089
--------

clang/include/clang/Format/Format.h

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4546,30 +4546,69 @@ struct FormatStyle {
45464546
/// } }
45474547
/// \endcode
45484548
bool InEmptyParentheses;
4549-
/// Put a space in parentheses not covered by preceding options.
4549+
/// Put a space in parentheses of function calls.
45504550
/// \code
45514551
/// true: false:
45524552
/// t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete;
45534553
/// \endcode
4554+
bool InFunctionCalls;
4555+
/// Put a space in parentheses of function declarations.
4556+
/// \code
4557+
/// true: false:
4558+
/// void foo( int bar ); vs. void foo(int bar);
4559+
/// \endcode
4560+
bool InFunctionDeclarations;
4561+
/// Put a space in parentheses of function definitions.
4562+
/// \code
4563+
/// true: false:
4564+
/// void foo( int bar ) { } vs. void foo(int bar) { }
4565+
/// \endcode
4566+
bool InFunctionDefinitions;
4567+
/// Put a space in parentheses of overloaded operators.
4568+
/// \code
4569+
/// true: false:
4570+
/// void operator++( int a ) vs. void operator++(int a)
4571+
/// object.operator++( 10 ) vs. object.operator++(10)
4572+
/// \endcode
4573+
bool InOverloadedOperators;
4574+
/// Put a space in parentheses not covered by preceding options.
4575+
/// \code
4576+
/// true: false:
4577+
/// x = ( y + z ); vs. x = (y+z);
4578+
/// \endcode
45544579
bool Other;
45554580

45564581
SpacesInParensCustom()
45574582
: InAttributeSpecifiers(false), InConditionalStatements(false),
4558-
InCStyleCasts(false), InEmptyParentheses(false), Other(false) {}
4583+
InCStyleCasts(false), InEmptyParentheses(false),
4584+
InFunctionCalls(false), InFunctionDeclarations(false),
4585+
InFunctionDefinitions(false), InOverloadedOperators(false),
4586+
Other(false) {}
45594587

45604588
SpacesInParensCustom(bool InAttributeSpecifiers,
45614589
bool InConditionalStatements, bool InCStyleCasts,
4562-
bool InEmptyParentheses, bool Other)
4590+
bool InEmptyParentheses, bool InFunctionCalls,
4591+
bool InFunctionDeclarations,
4592+
bool InFunctionDefinitions, bool InOverloadedOperators,
4593+
bool Other)
45634594
: InAttributeSpecifiers(InAttributeSpecifiers),
45644595
InConditionalStatements(InConditionalStatements),
45654596
InCStyleCasts(InCStyleCasts), InEmptyParentheses(InEmptyParentheses),
4566-
Other(Other) {}
4597+
InFunctionCalls(InFunctionCalls),
4598+
InFunctionDeclarations(InFunctionDeclarations),
4599+
InFunctionDefinitions(InFunctionDefinitions),
4600+
InOverloadedOperators(InOverloadedOperators), Other(Other) {}
45674601

45684602
bool operator==(const SpacesInParensCustom &R) const {
45694603
return InAttributeSpecifiers == R.InAttributeSpecifiers &&
45704604
InConditionalStatements == R.InConditionalStatements &&
45714605
InCStyleCasts == R.InCStyleCasts &&
4572-
InEmptyParentheses == R.InEmptyParentheses && Other == R.Other;
4606+
InEmptyParentheses == R.InEmptyParentheses &&
4607+
InFunctionCalls == R.InFunctionCalls &&
4608+
InFunctionDeclarations == R.InFunctionDeclarations &&
4609+
InFunctionDefinitions == R.InFunctionDefinitions &&
4610+
InOverloadedOperators == R.InOverloadedOperators &&
4611+
Other == R.Other;
45734612
}
45744613
bool operator!=(const SpacesInParensCustom &R) const {
45754614
return !(*this == R);

clang/lib/Format/Format.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,10 @@ template <> struct MappingTraits<FormatStyle::SpacesInParensCustom> {
757757
IO.mapOptional("InCStyleCasts", Spaces.InCStyleCasts);
758758
IO.mapOptional("InConditionalStatements", Spaces.InConditionalStatements);
759759
IO.mapOptional("InEmptyParentheses", Spaces.InEmptyParentheses);
760+
IO.mapOptional("InFunctionCalls", Spaces.InFunctionCalls);
761+
IO.mapOptional("InFunctionDeclarations", Spaces.InFunctionDeclarations);
762+
IO.mapOptional("InFunctionDefinitions", Spaces.InFunctionDefinitions);
763+
IO.mapOptional("InOverloadedOperators", Spaces.InOverloadedOperators);
760764
IO.mapOptional("Other", Spaces.Other);
761765
}
762766
};
@@ -1198,6 +1202,10 @@ template <> struct MappingTraits<FormatStyle> {
11981202
SpacesInCStyleCastParentheses;
11991203
Style.SpacesInParensOptions.InEmptyParentheses =
12001204
SpaceInEmptyParentheses;
1205+
Style.SpacesInParensOptions.InFunctionCalls = true;
1206+
Style.SpacesInParensOptions.InFunctionDeclarations = true;
1207+
Style.SpacesInParensOptions.InFunctionDefinitions = true;
1208+
Style.SpacesInParensOptions.InOverloadedOperators = true;
12011209
Style.SpacesInParensOptions.Other = true;
12021210
} else {
12031211
Style.SpacesInParensOptions = {};

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4010,6 +4010,34 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
40104010
isAttributeParen(Left.Previous) || isAttributeParen(Right.Next)) {
40114011
return Style.SpacesInParensOptions.InAttributeSpecifiers;
40124012
}
4013+
// Function declaration or definition
4014+
const auto isFunctionDeclParen = [](const FormatToken *Paren) {
4015+
return Paren && Paren->is(TT_FunctionDeclarationName);
4016+
};
4017+
if (isFunctionDeclParen(Left.Previous) ||
4018+
(Right.MatchingParen &&
4019+
isFunctionDeclParen(Right.MatchingParen->Previous))) {
4020+
if (Line.MightBeFunctionDecl) {
4021+
if (Line.mightBeFunctionDefinition())
4022+
return Style.SpacesInParensOptions.InFunctionDefinitions;
4023+
return Style.SpacesInParensOptions.InFunctionDeclarations;
4024+
}
4025+
return Style.SpacesInParensOptions.Other;
4026+
}
4027+
if (Left.is(TT_OverloadedOperatorLParen) ||
4028+
(Right.MatchingParen &&
4029+
Right.MatchingParen->is(TT_OverloadedOperatorLParen))) {
4030+
return Style.SpacesInParensOptions.InOverloadedOperators;
4031+
}
4032+
const auto isFunctionCallParen = [](const FormatToken *Paren) {
4033+
return Paren && Paren->ParameterCount > 0 && Paren->Previous &&
4034+
Paren->Previous->is(tok::identifier);
4035+
};
4036+
if ((isFunctionCallParen(&Left) ||
4037+
isFunctionCallParen(Right.MatchingParen)) &&
4038+
(Line.Type != LT_PreprocessorDirective)) {
4039+
return Style.SpacesInParensOptions.InFunctionCalls;
4040+
}
40134041
return Style.SpacesInParensOptions.Other;
40144042
}
40154043
if (Right.isOneOf(tok::semi, tok::comma))

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
235235
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
236236
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
237237
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
238+
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InFunctionCalls);
239+
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InFunctionDeclarations);
240+
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InFunctionDefinitions);
241+
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InOverloadedOperators);
238242
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, Other);
239243
}
240244

@@ -627,23 +631,23 @@ TEST(ConfigParseTest, ParsesConfiguration) {
627631
Style.SpacesInParens = FormatStyle::SIPO_Never;
628632
Style.SpacesInParensOptions = {};
629633
CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
630-
FormatStyle::SpacesInParensCustom(true, true, false, false,
631-
true));
634+
FormatStyle::SpacesInParensCustom(true, true, false, false, true,
635+
true, true, true, true));
632636
Style.SpacesInParens = FormatStyle::SIPO_Never;
633637
Style.SpacesInParensOptions = {};
634638
CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
635639
FormatStyle::SpacesInParensCustom(false, true, false, false,
636-
false));
640+
false, false, false, false, false));
637641
Style.SpacesInParens = FormatStyle::SIPO_Never;
638642
Style.SpacesInParensOptions = {};
639643
CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
640644
FormatStyle::SpacesInParensCustom(false, false, true, false,
641-
false));
645+
false, false, false, false, false));
642646
Style.SpacesInParens = FormatStyle::SIPO_Never;
643647
Style.SpacesInParensOptions = {};
644648
CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
645649
FormatStyle::SpacesInParensCustom(false, false, false, true,
646-
false));
650+
false, false, false, false, false));
647651
Style.SpacesInParens = FormatStyle::SIPO_Never;
648652
Style.SpacesInParensOptions = {};
649653

0 commit comments

Comments
 (0)