Skip to content

Commit 6cfedea

Browse files
authored
[clang-format] Add SpaceInEmptyBraces option (#153765)
Also set it to SIEB_Always for WebKit style. Closes #85525. Closes #93635.
1 parent a21d17f commit 6cfedea

File tree

8 files changed

+155
-30
lines changed

8 files changed

+155
-30
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6486,13 +6486,51 @@ the configuration (without a prefix: ``Auto``).
64866486
.. _SpaceInEmptyBlock:
64876487

64886488
**SpaceInEmptyBlock** (``Boolean``) :versionbadge:`clang-format 10` :ref:`<SpaceInEmptyBlock>`
6489-
If ``true``, spaces will be inserted into ``{}``.
6489+
This option is **deprecated**. See ``Block`` of ``SpaceInEmptyBraces``.
6490+
6491+
.. _SpaceInEmptyBraces:
6492+
6493+
**SpaceInEmptyBraces** (``SpaceInEmptyBracesStyle``) :versionbadge:`clang-format 22` :ref:`<SpaceInEmptyBraces>`
6494+
Specifies when to insert a space in empty braces.
6495+
6496+
.. note::
6497+
6498+
This option doesn't apply to initializer braces if
6499+
``Cpp11BracedListStyle`` is set to ``true``.
6500+
6501+
Possible values:
6502+
6503+
* ``SIEB_Always`` (in configuration: ``Always``)
6504+
Always insert a space in empty braces.
6505+
6506+
.. code-block:: c++
6507+
6508+
void f() { }
6509+
class Unit { };
6510+
auto a = [] { };
6511+
int x{ };
6512+
6513+
* ``SIEB_Block`` (in configuration: ``Block``)
6514+
Only insert a space in empty blocks.
6515+
6516+
.. code-block:: c++
6517+
6518+
void f() { }
6519+
class Unit { };
6520+
auto a = [] { };
6521+
int x{};
6522+
6523+
* ``SIEB_Never`` (in configuration: ``Never``)
6524+
Never insert a space in empty braces.
6525+
6526+
.. code-block:: c++
6527+
6528+
void f() {}
6529+
class Unit {};
6530+
auto a = [] {};
6531+
int x{};
64906532

6491-
.. code-block:: c++
64926533

6493-
true: false:
6494-
void f() { } vs. void f() {}
6495-
while (true) { } while (true) {}
64966534

64976535
.. _SpaceInEmptyParentheses:
64986536

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ AST Matchers
314314

315315
clang-format
316316
------------
317+
- Add ``SpaceInEmptyBraces`` option and set it to ``Always`` for WebKit style.
317318

318319
libclang
319320
--------

clang/include/clang/Format/Format.h

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4813,14 +4813,45 @@ struct FormatStyle {
48134813
/// \version 7
48144814
bool SpaceBeforeRangeBasedForLoopColon;
48154815

4816-
/// If ``true``, spaces will be inserted into ``{}``.
4817-
/// \code
4818-
/// true: false:
4819-
/// void f() { } vs. void f() {}
4820-
/// while (true) { } while (true) {}
4821-
/// \endcode
4816+
/// This option is **deprecated**. See ``Block`` of ``SpaceInEmptyBraces``.
48224817
/// \version 10
4823-
bool SpaceInEmptyBlock;
4818+
// bool SpaceInEmptyBlock;
4819+
4820+
/// Style of when to insert a space in empty braces.
4821+
enum SpaceInEmptyBracesStyle : int8_t {
4822+
/// Always insert a space in empty braces.
4823+
/// \code
4824+
/// void f() { }
4825+
/// class Unit { };
4826+
/// auto a = [] { };
4827+
/// int x{ };
4828+
/// \endcode
4829+
SIEB_Always,
4830+
/// Only insert a space in empty blocks.
4831+
/// \code
4832+
/// void f() { }
4833+
/// class Unit { };
4834+
/// auto a = [] { };
4835+
/// int x{};
4836+
/// \endcode
4837+
SIEB_Block,
4838+
/// Never insert a space in empty braces.
4839+
/// \code
4840+
/// void f() {}
4841+
/// class Unit {};
4842+
/// auto a = [] {};
4843+
/// int x{};
4844+
/// \endcode
4845+
SIEB_Never
4846+
};
4847+
4848+
/// Specifies when to insert a space in empty braces.
4849+
/// \note
4850+
/// This option doesn't apply to initializer braces if
4851+
/// ``Cpp11BracedListStyle`` is set to ``true``.
4852+
/// \endnote
4853+
/// \version 22
4854+
SpaceInEmptyBracesStyle SpaceInEmptyBraces;
48244855

48254856
/// If ``true``, spaces may be inserted into ``()``.
48264857
/// This option is **deprecated**. See ``InEmptyParentheses`` of
@@ -5494,7 +5525,7 @@ struct FormatStyle {
54945525
SpaceBeforeRangeBasedForLoopColon ==
54955526
R.SpaceBeforeRangeBasedForLoopColon &&
54965527
SpaceBeforeSquareBrackets == R.SpaceBeforeSquareBrackets &&
5497-
SpaceInEmptyBlock == R.SpaceInEmptyBlock &&
5528+
SpaceInEmptyBraces == R.SpaceInEmptyBraces &&
54985529
SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments &&
54995530
SpacesInAngles == R.SpacesInAngles &&
55005531
SpacesInContainerLiterals == R.SpacesInContainerLiterals &&

clang/lib/Format/Format.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,15 @@ struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensStyle> {
763763
}
764764
};
765765

766+
template <>
767+
struct ScalarEnumerationTraits<FormatStyle::SpaceInEmptyBracesStyle> {
768+
static void enumeration(IO &IO, FormatStyle::SpaceInEmptyBracesStyle &Value) {
769+
IO.enumCase(Value, "Always", FormatStyle::SIEB_Always);
770+
IO.enumCase(Value, "Block", FormatStyle::SIEB_Block);
771+
IO.enumCase(Value, "Never", FormatStyle::SIEB_Never);
772+
}
773+
};
774+
766775
template <> struct ScalarEnumerationTraits<FormatStyle::SpacesInAnglesStyle> {
767776
static void enumeration(IO &IO, FormatStyle::SpacesInAnglesStyle &Value) {
768777
IO.enumCase(Value, "Never", FormatStyle::SIAS_Never);
@@ -931,6 +940,7 @@ template <> struct MappingTraits<FormatStyle> {
931940
bool DeriveLineEnding = true;
932941
bool UseCRLF = false;
933942

943+
bool SpaceInEmptyBlock = false;
934944
bool SpaceInEmptyParentheses = false;
935945
bool SpacesInConditionalStatement = false;
936946
bool SpacesInCStyleCastParentheses = false;
@@ -960,6 +970,7 @@ template <> struct MappingTraits<FormatStyle> {
960970
IO.mapOptional("PointerBindsToType", Style.PointerAlignment);
961971
IO.mapOptional("SpaceAfterControlStatementKeyword",
962972
Style.SpaceBeforeParens);
973+
IO.mapOptional("SpaceInEmptyBlock", SpaceInEmptyBlock);
963974
IO.mapOptional("SpaceInEmptyParentheses", SpaceInEmptyParentheses);
964975
IO.mapOptional("SpacesInConditionalStatement",
965976
SpacesInConditionalStatement);
@@ -1193,7 +1204,7 @@ template <> struct MappingTraits<FormatStyle> {
11931204
Style.SpaceBeforeRangeBasedForLoopColon);
11941205
IO.mapOptional("SpaceBeforeSquareBrackets",
11951206
Style.SpaceBeforeSquareBrackets);
1196-
IO.mapOptional("SpaceInEmptyBlock", Style.SpaceInEmptyBlock);
1207+
IO.mapOptional("SpaceInEmptyBraces", Style.SpaceInEmptyBraces);
11971208
IO.mapOptional("SpacesBeforeTrailingComments",
11981209
Style.SpacesBeforeTrailingComments);
11991210
IO.mapOptional("SpacesInAngles", Style.SpacesInAngles);
@@ -1276,6 +1287,13 @@ template <> struct MappingTraits<FormatStyle> {
12761287
Style.LineEnding = FormatStyle::LE_DeriveCRLF;
12771288
}
12781289

1290+
// If SpaceInEmptyBlock was specified but SpaceInEmptyBraces was not,
1291+
// initialize the latter from the former for backward compatibility.
1292+
if (SpaceInEmptyBlock &&
1293+
Style.SpaceInEmptyBraces == FormatStyle::SIEB_Never) {
1294+
Style.SpaceInEmptyBraces = FormatStyle::SIEB_Block;
1295+
}
1296+
12791297
if (Style.SpacesInParens != FormatStyle::SIPO_Custom &&
12801298
(SpacesInParentheses || SpaceInEmptyParentheses ||
12811299
SpacesInConditionalStatement || SpacesInCStyleCastParentheses)) {
@@ -1677,7 +1695,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
16771695
LLVMStyle.SpaceBeforeParensOptions.AfterIfMacros = true;
16781696
LLVMStyle.SpaceBeforeRangeBasedForLoopColon = true;
16791697
LLVMStyle.SpaceBeforeSquareBrackets = false;
1680-
LLVMStyle.SpaceInEmptyBlock = false;
1698+
LLVMStyle.SpaceInEmptyBraces = FormatStyle::SIEB_Never;
16811699
LLVMStyle.SpacesBeforeTrailingComments = 1;
16821700
LLVMStyle.SpacesInAngles = FormatStyle::SIAS_Never;
16831701
LLVMStyle.SpacesInContainerLiterals = true;
@@ -1984,7 +2002,7 @@ FormatStyle getWebKitStyle() {
19842002
Style.ObjCSpaceAfterProperty = true;
19852003
Style.PointerAlignment = FormatStyle::PAS_Left;
19862004
Style.SpaceBeforeCpp11BracedList = true;
1987-
Style.SpaceInEmptyBlock = true;
2005+
Style.SpaceInEmptyBraces = FormatStyle::SIEB_Always;
19882006
return Style;
19892007
}
19902008

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4516,16 +4516,9 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
45164516
return Left.is(tok::hash);
45174517
if (Left.isOneOf(tok::hashhash, tok::hash))
45184518
return Right.is(tok::hash);
4519-
if (Left.is(BK_Block) && Right.is(tok::r_brace) &&
4520-
Right.MatchingParen == &Left && Line.Children.empty()) {
4521-
return Style.SpaceInEmptyBlock;
4522-
}
45234519
if (Style.SpacesInParens == FormatStyle::SIPO_Custom) {
4524-
if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
4525-
(Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
4526-
Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
4520+
if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
45274521
return Style.SpacesInParensOptions.InEmptyParentheses;
4528-
}
45294522
if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
45304523
Left.is(tok::r_paren) && Right.is(tok::r_paren)) {
45314524
auto *InnerLParen = Left.MatchingParen;
@@ -4803,8 +4796,6 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
48034796
Right.is(TT_ArraySubscriptLSquare))) {
48044797
return false;
48054798
}
4806-
if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
4807-
return !Left.Children.empty(); // No spaces in "{}".
48084799
if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) ||
48094800
(Right.is(tok::r_brace) && Right.MatchingParen &&
48104801
Right.MatchingParen->isNot(BK_Block))) {
@@ -4986,6 +4977,17 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
49864977
if (Left.is(tok::star) && Right.is(tok::comment))
49874978
return true;
49884979

4980+
if (Left.is(tok::l_brace) && Right.is(tok::r_brace) &&
4981+
Left.Children.empty()) {
4982+
if (Left.is(BK_Block))
4983+
return Style.SpaceInEmptyBraces != FormatStyle::SIEB_Never;
4984+
if (Style.Cpp11BracedListStyle) {
4985+
return Style.SpacesInParens == FormatStyle::SIPO_Custom &&
4986+
Style.SpacesInParensOptions.InEmptyParentheses;
4987+
}
4988+
return Style.SpaceInEmptyBraces == FormatStyle::SIEB_Always;
4989+
}
4990+
49894991
const auto *BeforeLeft = Left.Previous;
49904992

49914993
if (IsCpp) {

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,8 @@ class LineJoiner {
864864
if (ShouldMerge()) {
865865
// We merge empty blocks even if the line exceeds the column limit.
866866
Tok->SpacesRequiredBefore =
867-
(Style.SpaceInEmptyBlock || Line.Last->is(tok::comment)) ? 1 : 0;
867+
Style.SpaceInEmptyBraces != FormatStyle::SIEB_Never ||
868+
Line.Last->is(tok::comment);
868869
Tok->CanBreakBefore = true;
869870
return 1;
870871
} else if (Limit != 0 && !Line.startsWithNamespace() &&

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
200200
CHECK_PARSE_BOOL(RemoveSemicolon);
201201
CHECK_PARSE_BOOL(SkipMacroDefinitionBody);
202202
CHECK_PARSE_BOOL(SpacesInSquareBrackets);
203-
CHECK_PARSE_BOOL(SpaceInEmptyBlock);
204203
CHECK_PARSE_BOOL(SpacesInContainerLiterals);
205204
CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
206205
CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
@@ -688,6 +687,17 @@ TEST(ConfigParseTest, ParsesConfiguration) {
688687
SpaceBeforeParens,
689688
FormatStyle::SBPO_ControlStatementsExceptControlMacros);
690689

690+
Style.SpaceInEmptyBraces = FormatStyle::SIEB_Never;
691+
CHECK_PARSE("SpaceInEmptyBraces: Always", SpaceInEmptyBraces,
692+
FormatStyle::SIEB_Always);
693+
CHECK_PARSE("SpaceInEmptyBraces: Block", SpaceInEmptyBraces,
694+
FormatStyle::SIEB_Block);
695+
CHECK_PARSE("SpaceInEmptyBraces: Never", SpaceInEmptyBraces,
696+
FormatStyle::SIEB_Never);
697+
// For backward compatibility:
698+
CHECK_PARSE("SpaceInEmptyBlock: true", SpaceInEmptyBraces,
699+
FormatStyle::SIEB_Block);
700+
691701
// For backward compatibility:
692702
Style.SpacesInParens = FormatStyle::SIPO_Never;
693703
Style.SpacesInParensOptions = {};

clang/unittests/Format/FormatTest.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7059,7 +7059,7 @@ TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
70597059
verifyFormat("enum E {};");
70607060
verifyFormat("enum E {}");
70617061
FormatStyle Style = getLLVMStyle();
7062-
Style.SpaceInEmptyBlock = true;
7062+
Style.SpaceInEmptyBraces = FormatStyle::SIEB_Block;
70637063
verifyFormat("void f() { }", "void f() {}", Style);
70647064
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
70657065
verifyFormat("{ }", Style);
@@ -7087,7 +7087,7 @@ TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
70877087
Style);
70887088

70897089
Style = getLLVMStyle(FormatStyle::LK_CSharp);
7090-
Style.SpaceInEmptyBlock = true;
7090+
Style.SpaceInEmptyBraces = FormatStyle::SIEB_Block;
70917091
verifyFormat("Event += () => { };", Style);
70927092
}
70937093

@@ -25596,6 +25596,30 @@ TEST_F(FormatTest, SpacesInConditionalStatement) {
2559625596
verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces);
2559725597
}
2559825598

25599+
TEST_F(FormatTest, SpaceInEmptyBraces) {
25600+
constexpr StringRef Code("void f() {}\n"
25601+
"class Unit {};\n"
25602+
"auto a = [] {};\n"
25603+
"int x{};");
25604+
verifyFormat(Code);
25605+
25606+
auto Style = getWebKitStyle();
25607+
EXPECT_EQ(Style.SpaceInEmptyBraces, FormatStyle::SIEB_Always);
25608+
25609+
verifyFormat("void f() { }\n"
25610+
"class Unit { };\n"
25611+
"auto a = [] { };\n"
25612+
"int x { };",
25613+
Code, Style);
25614+
25615+
Style.SpaceInEmptyBraces = FormatStyle::SIEB_Block;
25616+
verifyFormat("void f() { }\n"
25617+
"class Unit { };\n"
25618+
"auto a = [] { };\n"
25619+
"int x {};",
25620+
Code, Style);
25621+
}
25622+
2559925623
TEST_F(FormatTest, AlternativeOperators) {
2560025624
// Test case for ensuring alternate operators are not
2560125625
// combined with their right most neighbour.

0 commit comments

Comments
 (0)