Skip to content

Commit 0b41025

Browse files
[clang-format] add an option to insert a space only for empty braces
Co-authored-by: Björn Schäpers <github@hazardy.de>
1 parent 203232f commit 0b41025

File tree

6 files changed

+62
-13
lines changed

6 files changed

+62
-13
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6237,18 +6237,30 @@ the configuration (without a prefix: ``Auto``).
62376237
true: false:
62386238
x = ( int32 )y vs. x = (int32)y
62396239

6240-
* ``bool InEmptyParentheses`` Put a space in parentheses only if the parentheses are empty i.e. '()'
6240+
* ``bool InEmptyParentheses`` Put a space in parentheses and braces only if they are empty i.e. '()' or '{}'
62416241

62426242
.. code-block:: c++
62436243

62446244
true: false:
62456245
void f( ) { vs. void f() {
62466246
int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()};
6247+
T a = { }; T a = {};
62476248
if (true) { if (true) {
62486249
f( ); f();
62496250
} }
62506251
} }
62516252

6253+
* ``bool InEmptyBraces`` Put a space in *only* braces, not for parentheses, only if the braces are empty i.e. '{}'
6254+
6255+
.. code-block:: c++
6256+
6257+
true: false:
6258+
void f() { vs. void f() {
6259+
T x = {}; T x = { };
6260+
g(x, {}); g(x, { });
6261+
} }
6262+
6263+
62526264
* ``bool Other`` Put a space in parentheses not covered by preceding options.
62536265

62546266
.. code-block:: c++

clang/include/clang/Format/Format.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4673,6 +4673,17 @@ struct FormatStyle {
46734673
/// } }
46744674
/// \endcode
46754675
bool InEmptyParentheses;
4676+
/// Put a space in brackets only if the parentheses are empty i.e. '()'
4677+
/// \code
4678+
/// true: false:
4679+
/// void f( ) { vs. void f() {
4680+
/// int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()};
4681+
/// if (true) { if (true) {
4682+
/// f( ); f();
4683+
/// } }
4684+
/// } }
4685+
/// \endcode
4686+
bool InEmptyBraces;
46764687
/// Put a space in parentheses not covered by preceding options.
46774688
/// \code
46784689
/// true: false:
@@ -4682,18 +4693,20 @@ struct FormatStyle {
46824693

46834694
SpacesInParensCustom()
46844695
: InConditionalStatements(false), InCStyleCasts(false),
4685-
InEmptyParentheses(false), Other(false) {}
4696+
InEmptyParentheses(false), InEmptyBraces(false), Other(false) {}
46864697

46874698
SpacesInParensCustom(bool InConditionalStatements, bool InCStyleCasts,
4688-
bool InEmptyParentheses, bool Other)
4699+
bool InEmptyParentheses, bool InEmptyBraces,
4700+
bool Other)
46894701
: InConditionalStatements(InConditionalStatements),
46904702
InCStyleCasts(InCStyleCasts), InEmptyParentheses(InEmptyParentheses),
4691-
Other(Other) {}
4703+
InEmptyBraces(InEmptyBraces), Other(Other) {}
46924704

46934705
bool operator==(const SpacesInParensCustom &R) const {
46944706
return InConditionalStatements == R.InConditionalStatements &&
46954707
InCStyleCasts == R.InCStyleCasts &&
4696-
InEmptyParentheses == R.InEmptyParentheses && Other == R.Other;
4708+
InEmptyParentheses == R.InEmptyParentheses &&
4709+
InEmptyBraces == R.InEmptyBraces && Other == R.Other;
46974710
}
46984711
bool operator!=(const SpacesInParensCustom &R) const {
46994712
return !(*this == R);

clang/lib/Format/Format.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ template <> struct MappingTraits<FormatStyle::SpacesInParensCustom> {
723723
IO.mapOptional("InCStyleCasts", Spaces.InCStyleCasts);
724724
IO.mapOptional("InConditionalStatements", Spaces.InConditionalStatements);
725725
IO.mapOptional("InEmptyParentheses", Spaces.InEmptyParentheses);
726+
IO.mapOptional("InEmptyBraces", Spaces.InEmptyBraces);
726727
IO.mapOptional("Other", Spaces.Other);
727728
}
728729
};
@@ -1864,6 +1865,9 @@ FormatStyle getWebKitStyle() {
18641865
Style.PointerAlignment = FormatStyle::PAS_Left;
18651866
Style.SpaceBeforeCpp11BracedList = true;
18661867
Style.SpaceInEmptyBlock = true;
1868+
Style.SpacesInParensOptions.InEmptyParentheses = false;
1869+
Style.SpacesInParensOptions.Other = false;
1870+
Style.SpacesInParensOptions.InEmptyBraces = true;
18671871
return Style;
18681872
}
18691873

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4341,6 +4341,11 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
43414341
Right.MatchingParen == &Left && Line.Children.empty()) {
43424342
return Style.SpaceInEmptyBlock;
43434343
}
4344+
if (Style.SpacesInParensOptions.InEmptyBraces &&
4345+
(Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
4346+
Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
4347+
return Style.SpacesInParensOptions.InEmptyBraces;
4348+
}
43444349
if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
43454350
(Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
43464351
Right.is(tok::r_brace) && Right.isNot(BK_Block))) {

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
238238
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
239239
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
240240
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
241+
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyBraces);
241242
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, Other);
242243
}
243244

@@ -619,20 +620,24 @@ TEST(ConfigParseTest, ParsesConfiguration) {
619620
FormatStyle::SIPO_Custom);
620621
Style.SpacesInParens = FormatStyle::SIPO_Never;
621622
Style.SpacesInParensOptions = {};
622-
CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
623-
FormatStyle::SpacesInParensCustom(true, false, false, true));
623+
CHECK_PARSE(
624+
"SpacesInParentheses: true", SpacesInParensOptions,
625+
FormatStyle::SpacesInParensCustom(true, false, false, false, true));
624626
Style.SpacesInParens = FormatStyle::SIPO_Never;
625627
Style.SpacesInParensOptions = {};
626-
CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
627-
FormatStyle::SpacesInParensCustom(true, false, false, false));
628+
CHECK_PARSE(
629+
"SpacesInConditionalStatement: true", SpacesInParensOptions,
630+
FormatStyle::SpacesInParensCustom(true, false, false, false, false));
628631
Style.SpacesInParens = FormatStyle::SIPO_Never;
629632
Style.SpacesInParensOptions = {};
630-
CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
631-
FormatStyle::SpacesInParensCustom(false, true, false, false));
633+
CHECK_PARSE(
634+
"SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
635+
FormatStyle::SpacesInParensCustom(false, true, false, false, false));
632636
Style.SpacesInParens = FormatStyle::SIPO_Never;
633637
Style.SpacesInParensOptions = {};
634-
CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
635-
FormatStyle::SpacesInParensCustom(false, false, true, false));
638+
CHECK_PARSE(
639+
"SpaceInEmptyParentheses: true", SpacesInParensOptions,
640+
FormatStyle::SpacesInParensCustom(false, false, true, false, false));
636641
Style.SpacesInParens = FormatStyle::SIPO_Never;
637642
Style.SpacesInParensOptions = {};
638643

clang/unittests/Format/FormatTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14027,6 +14027,16 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
1402714027
SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
1402814028
SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
1402914029
verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
14030+
14031+
SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = false;
14032+
SpaceBetweenBraces.SpacesInParensOptions.Other = false;
14033+
SpaceBetweenBraces.SpacesInParensOptions.InEmptyBraces = true;
14034+
// This achieves braces-only spacing required by WebKit style.
14035+
verifyFormat("T x = { };\n"
14036+
"toImpl(listenerRef)\n"
14037+
" ->use({ });\n"
14038+
"g();",
14039+
SpaceBetweenBraces);
1403014040
}
1403114041

1403214042
TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {

0 commit comments

Comments
 (0)