Skip to content

Commit 9122987

Browse files
committed
Create suboption for spaces in parens custom for non-consecutive parens
1 parent 5e77775 commit 9122987

File tree

5 files changed

+69
-22
lines changed

5 files changed

+69
-22
lines changed

clang/include/clang/Format/Format.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4525,6 +4525,16 @@ struct FormatStyle {
45254525
/// \version 17
45264526
SpacesInParensStyle SpacesInParens;
45274527

4528+
/// Selective control over spaces in parens.
4529+
enum SpacesInParensCustomStyle : int8_t {
4530+
/// Never put spaces in parens.
4531+
SIPCS_Never,
4532+
/// Only put spaces in parens not followed by the same consecutive parens.
4533+
SIPCS_NonConsecutive,
4534+
/// Always put spaces in parens.
4535+
SIPCS_Always
4536+
};
4537+
45284538
/// Precise control over the spacing in parentheses.
45294539
/// \code
45304540
/// # Should be declared this way:
@@ -4536,10 +4546,18 @@ struct FormatStyle {
45364546
struct SpacesInParensCustom {
45374547
/// Put a space in parentheses of attribute specifiers.
45384548
/// \code
4539-
/// true: false:
4540-
/// __attribute__( ( noreturn ) ) vs. __attribute__((noreturn))
4549+
/// Always:
4550+
/// __attribute__( ( noreturn ) )
4551+
/// \endcode
4552+
/// \code
4553+
/// NonConsecutive:
4554+
/// _attribute__(( noreturn ))
4555+
/// \endcode
4556+
/// \code
4557+
/// Never:
4558+
/// _attribute__((noreturn))
45414559
/// \endcode
4542-
bool InAttributeSpecifiers;
4560+
SpacesInParensCustomStyle InAttributeSpecifiers;
45434561
/// Put a space in parentheses only inside conditional statements
45444562
/// (``for/if/while/switch...``).
45454563
/// \code
@@ -4573,10 +4591,10 @@ struct FormatStyle {
45734591
bool Other;
45744592

45754593
SpacesInParensCustom()
4576-
: InAttributeSpecifiers(false), InConditionalStatements(false),
4594+
: InAttributeSpecifiers(SIPCS_Never), InConditionalStatements(false),
45774595
InCStyleCasts(false), InEmptyParentheses(false), Other(false) {}
45784596

4579-
SpacesInParensCustom(bool InAttributeSpecifiers,
4597+
SpacesInParensCustom(SpacesInParensCustomStyle InAttributeSpecifiers,
45804598
bool InConditionalStatements, bool InCStyleCasts,
45814599
bool InEmptyParentheses, bool Other)
45824600
: InAttributeSpecifiers(InAttributeSpecifiers),
@@ -4604,6 +4622,7 @@ struct FormatStyle {
46044622
/// # Example of usage:
46054623
/// SpacesInParens: Custom
46064624
/// SpacesInParensOptions:
4625+
/// InAttributeSpecifiers: NonConsecutive
46074626
/// InConditionalStatements: true
46084627
/// InEmptyParentheses: true
46094628
/// \endcode

clang/lib/Format/Format.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,14 @@ template <> struct MappingTraits<FormatStyle::SpacesInLineComment> {
750750
}
751751
};
752752

753+
template <> struct ScalarEnumerationTraits<FormatStyle::SpacesInParensCustomStyle> {
754+
static void enumeration(IO &IO, FormatStyle::SpacesInParensCustomStyle &Value) {
755+
IO.enumCase(Value, "Never", FormatStyle::SIPCS_Never);
756+
IO.enumCase(Value, "NonConsecutive", FormatStyle::SIPCS_NonConsecutive);
757+
IO.enumCase(Value, "Always", FormatStyle::SIPCS_Always);
758+
}
759+
};
760+
753761
template <> struct MappingTraits<FormatStyle::SpacesInParensCustom> {
754762
static void mapping(IO &IO, FormatStyle::SpacesInParensCustom &Spaces) {
755763
IO.mapOptional("InAttributeSpecifiers", Spaces.InAttributeSpecifiers);
@@ -1191,7 +1199,7 @@ template <> struct MappingTraits<FormatStyle> {
11911199
if (SpacesInParentheses) {
11921200
// set all options except InCStyleCasts and InEmptyParentheses
11931201
// to true for backward compatibility.
1194-
Style.SpacesInParensOptions.InAttributeSpecifiers = true;
1202+
Style.SpacesInParensOptions.InAttributeSpecifiers = FormatStyle::SIPCS_Always;
11951203
Style.SpacesInParensOptions.InConditionalStatements = true;
11961204
Style.SpacesInParensOptions.InCStyleCasts =
11971205
SpacesInCStyleCastParentheses;

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4012,10 +4012,10 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
40124012
const auto isAttributeParen = [](const FormatToken *Paren) {
40134013
return Paren && Paren->isOneOf(TT_AttributeLParen, TT_AttributeRParen);
40144014
};
4015-
if (isAttributeParen(&Left) || isAttributeParen(&Right) ||
4016-
isAttributeParen(Left.Previous) || isAttributeParen(Right.Next)) {
4017-
return Style.SpacesInParensOptions.InAttributeSpecifiers;
4018-
}
4015+
if (isAttributeParen(&Left) || isAttributeParen(&Right))
4016+
return Style.SpacesInParensOptions.InAttributeSpecifiers == FormatStyle::SIPCS_Always;
4017+
if (isAttributeParen(Left.Previous) || isAttributeParen(Right.Next))
4018+
return Style.SpacesInParensOptions.InAttributeSpecifiers != FormatStyle::SIPCS_Never;
40194019
return Style.SpacesInParensOptions.Other;
40204020
}
40214021
if (Right.isOneOf(tok::semi, tok::comma))

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
231231
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
232232
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
233233
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
234-
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InAttributeSpecifiers);
235234
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
236235
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
237236
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
@@ -625,6 +624,20 @@ TEST(ConfigParseTest, ParsesConfiguration) {
625624
SpaceBeforeParensOptions.AfterPlacementOperator,
626625
FormatStyle::SpaceBeforeParensCustom::APO_Leave);
627626

627+
Style.SpacesInParens = FormatStyle::SIPO_Custom;
628+
Style.SpacesInParensOptions.InAttributeSpecifiers = FormatStyle::SIPCS_Never;
629+
CHECK_PARSE("SpacesInParensOptions:\n"
630+
" InAttributeSpecifiers: Always",
631+
SpacesInParensOptions.InAttributeSpecifiers,
632+
FormatStyle::SIPCS_Always);
633+
CHECK_PARSE("SpacesInParensOptions:\n"
634+
" InAttributeSpecifiers: Never",
635+
SpacesInParensOptions.InAttributeSpecifiers,
636+
FormatStyle::SIPCS_Never);
637+
CHECK_PARSE("SpacesInParensOptions:\n"
638+
" InAttributeSpecifiers: NonConsecutive",
639+
SpacesInParensOptions.InAttributeSpecifiers,
640+
FormatStyle::SIPCS_NonConsecutive);
628641
// For backward compatibility:
629642
Style.SpacesInParens = FormatStyle::SIPO_Never;
630643
Style.SpacesInParensOptions = {};
@@ -633,23 +646,23 @@ TEST(ConfigParseTest, ParsesConfiguration) {
633646
Style.SpacesInParens = FormatStyle::SIPO_Never;
634647
Style.SpacesInParensOptions = {};
635648
CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
636-
FormatStyle::SpacesInParensCustom(true, true, false, false,
637-
true));
649+
FormatStyle::SpacesInParensCustom(FormatStyle::SIPCS_Always, true,
650+
false, false, true));
638651
Style.SpacesInParens = FormatStyle::SIPO_Never;
639652
Style.SpacesInParensOptions = {};
640653
CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
641-
FormatStyle::SpacesInParensCustom(false, true, false, false,
642-
false));
654+
FormatStyle::SpacesInParensCustom(FormatStyle::SIPCS_Never, true,
655+
false, false, false));
643656
Style.SpacesInParens = FormatStyle::SIPO_Never;
644657
Style.SpacesInParensOptions = {};
645658
CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
646-
FormatStyle::SpacesInParensCustom(false, false, true, false,
647-
false));
659+
FormatStyle::SpacesInParensCustom(FormatStyle::SIPCS_Never, false,
660+
true, false, false));
648661
Style.SpacesInParens = FormatStyle::SIPO_Never;
649662
Style.SpacesInParensOptions = {};
650663
CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
651-
FormatStyle::SpacesInParensCustom(false, false, false, true,
652-
false));
664+
FormatStyle::SpacesInParensCustom(FormatStyle::SIPCS_Never, false,
665+
false, true, false));
653666
Style.SpacesInParens = FormatStyle::SIPO_Never;
654667
Style.SpacesInParensOptions = {};
655668

clang/unittests/Format/FormatTest.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16821,7 +16821,8 @@ TEST_F(FormatTest, ConfigurableSpacesInParens) {
1682116821
Spaces.SpacesInParensOptions = {};
1682216822
Spaces.SpacesInParensOptions.Other = true;
1682316823
Spaces.SpacesInParensOptions.InConditionalStatements = true;
16824-
Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
16824+
Spaces.SpacesInParensOptions.InAttributeSpecifiers =
16825+
FormatStyle::SIPCS_Always;
1682516826
verifyFormat("do_something( ::globalVar );", Spaces);
1682616827
verifyFormat("call( x, y, z );", Spaces);
1682716828
verifyFormat("call();", Spaces);
@@ -16896,11 +16897,17 @@ TEST_F(FormatTest, ConfigurableSpacesInParens) {
1689616897
verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
1689716898
verifyFormat("void f( ) __attribute__((asdf));", Spaces);
1689816899

16899-
Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
16900+
Spaces.SpacesInParensOptions.InAttributeSpecifiers =
16901+
FormatStyle::SIPCS_Always;
1690016902
verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
1690116903
verifyFormat("void __attribute__( ( naked ) ) foo(int bar)", Spaces);
1690216904
verifyFormat("void f( ) __attribute__( ( asdf ) );", Spaces);
16903-
Spaces.SpacesInParensOptions.InAttributeSpecifiers = false;
16905+
Spaces.SpacesInParensOptions.InAttributeSpecifiers =
16906+
FormatStyle::SIPCS_NonConsecutive;
16907+
verifyFormat("SomeType *__attribute__(( attr )) *a = NULL;", Spaces);
16908+
verifyFormat("void __attribute__(( naked )) foo(int bar)", Spaces);
16909+
verifyFormat("void f( ) __attribute__(( asdf ));", Spaces);
16910+
Spaces.SpacesInParensOptions.InAttributeSpecifiers = FormatStyle::SIPCS_Never;
1690416911

1690516912
// Run the first set of tests again with:
1690616913
Spaces.SpaceAfterCStyleCast = true;

0 commit comments

Comments
 (0)