Skip to content

Commit bfc3406

Browse files
committed
[clang-format] Add SpaceBeforeCpp11BracedList option.
WebKit C++ style for object initialization is as follows: Foo foo { bar }; Yet using clang-format -style=webkit changes this to: Foo foo{ bar }; As there is no existing combination of rules that will ensure a space before a braced list in this fashion, this patch adds a new SpaceBeforeCpp11BracedList rule. Patch by Ross Kirsling! Differential Revision: https://reviews.llvm.org/D46024 llvm-svn: 334692
1 parent 49fad1c commit bfc3406

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,18 @@ the configuration (without a prefix: ``Auto``).
17911791
int a = 5; vs. int a=5;
17921792
a += 42 a+=42;
17931793

1794+
**SpaceBeforeCpp11BracedList** (``bool``)
1795+
If ``true``, a space will be inserted before a C++11 braced list
1796+
used to initialize an object (after the preceding identifier or type).
1797+
1798+
.. code-block:: c++
1799+
1800+
true: false:
1801+
Foo foo { bar }; vs. Foo foo{ bar };
1802+
Foo {}; Foo{};
1803+
vector<int> { 1, 2, 3 }; vector<int>{ 1, 2, 3 };
1804+
new int[3] { 1, 2, 3 }; new int[3]{ 1, 2, 3 };
1805+
17941806
**SpaceBeforeCtorInitializerColon** (``bool``)
17951807
If ``false``, spaces will be removed before constructor initializer
17961808
colon.

clang/include/clang/Format/Format.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,17 @@ struct FormatStyle {
14951495
/// \endcode
14961496
bool SpaceBeforeAssignmentOperators;
14971497

1498+
/// If ``true``, a space will be inserted before a C++11 braced list
1499+
/// used to initialize an object (after the preceding identifier or type).
1500+
/// \code
1501+
/// true: false:
1502+
/// Foo foo { bar }; vs. Foo foo{ bar };
1503+
/// Foo {}; Foo{};
1504+
/// vector<int> { 1, 2, 3 }; vector<int>{ 1, 2, 3 };
1505+
/// new int[3] { 1, 2, 3 }; new int[3]{ 1, 2, 3 };
1506+
/// \endcode
1507+
bool SpaceBeforeCpp11BracedList;
1508+
14981509
/// If ``false``, spaces will be removed before constructor initializer
14991510
/// colon.
15001511
/// \code
@@ -1738,6 +1749,7 @@ struct FormatStyle {
17381749
SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
17391750
SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword &&
17401751
SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
1752+
SpaceBeforeCpp11BracedList == R.SpaceBeforeCpp11BracedList &&
17411753
SpaceBeforeCtorInitializerColon ==
17421754
R.SpaceBeforeCtorInitializerColon &&
17431755
SpaceBeforeInheritanceColon == R.SpaceBeforeInheritanceColon &&

clang/lib/Format/Format.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,8 @@ template <> struct MappingTraits<FormatStyle> {
449449
Style.SpaceAfterTemplateKeyword);
450450
IO.mapOptional("SpaceBeforeAssignmentOperators",
451451
Style.SpaceBeforeAssignmentOperators);
452+
IO.mapOptional("SpaceBeforeCpp11BracedList",
453+
Style.SpaceBeforeCpp11BracedList);
452454
IO.mapOptional("SpaceBeforeCtorInitializerColon",
453455
Style.SpaceBeforeCtorInitializerColon);
454456
IO.mapOptional("SpaceBeforeInheritanceColon",
@@ -697,6 +699,7 @@ FormatStyle getLLVMStyle() {
697699
LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
698700
LLVMStyle.SpaceBeforeRangeBasedForLoopColon = true;
699701
LLVMStyle.SpaceBeforeAssignmentOperators = true;
702+
LLVMStyle.SpaceBeforeCpp11BracedList = false;
700703
LLVMStyle.SpacesInAngles = false;
701704

702705
LLVMStyle.PenaltyBreakAssignment = prec::Assignment;
@@ -892,6 +895,7 @@ FormatStyle getWebKitStyle() {
892895
Style.ObjCBlockIndentWidth = 4;
893896
Style.ObjCSpaceAfterProperty = true;
894897
Style.PointerAlignment = FormatStyle::PAS_Left;
898+
Style.SpaceBeforeCpp11BracedList = true;
895899
return Style;
896900
}
897901

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,6 +2548,9 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
25482548
if (Style.isCpp()) {
25492549
if (Left.is(tok::kw_operator))
25502550
return Right.is(tok::coloncolon);
2551+
if (Right.is(tok::l_brace) && Right.BlockKind == BK_BracedInit &&
2552+
!Left.opensScope() && Style.SpaceBeforeCpp11BracedList)
2553+
return true;
25512554
} else if (Style.Language == FormatStyle::LK_Proto ||
25522555
Style.Language == FormatStyle::LK_TextProto) {
25532556
if (Right.is(tok::period) &&

clang/unittests/Format/FormatTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7019,6 +7019,11 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
70197019
" { \"ccccccccccccccccccccc\", 2 }\n"
70207020
"};",
70217021
ExtraSpaces);
7022+
7023+
FormatStyle SpaceBeforeBrace = getLLVMStyle();
7024+
SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
7025+
verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
7026+
verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
70227027
}
70237028

70247029
TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
@@ -10622,6 +10627,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) {
1062210627
CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
1062310628
CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
1062410629
CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
10630+
CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
1062510631
CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
1062610632
CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
1062710633
CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);

0 commit comments

Comments
 (0)