Index: docs/ClangFormatStyleOptions.rst =================================================================== --- docs/ClangFormatStyleOptions.rst +++ docs/ClangFormatStyleOptions.rst @@ -1729,6 +1729,18 @@ int a = 5; vs. int a=5; a += 42 a+=42; +**SpaceBeforeCpp11BracedList** (``bool``) + If ``true``, a space will be inserted before a C++11 braced list + used to initialize an object (after the preceding identifier or type). + + .. code-block:: c++ + + true: false: + Foo foo { bar }; vs. Foo foo{ bar }; + Foo {}; Foo{}; + vector { 1, 2, 3 }; vector{ 1, 2, 3 }; + new int[3] { 1, 2, 3 }; new int[3]{ 1, 2, 3 }; + **SpaceBeforeCtorInitializerColon** (``bool``) If ``false``, spaces will be removed before constructor initializer colon. Index: include/clang/Format/Format.h =================================================================== --- include/clang/Format/Format.h +++ include/clang/Format/Format.h @@ -1525,6 +1525,17 @@ /// \endcode bool SpaceBeforeAssignmentOperators; + /// If ``true``, a space will be inserted before a C++11 braced list + /// used to initialize an object (after the preceding identifier or type). + /// \code + /// true: false: + /// Foo foo { bar }; vs. Foo foo{ bar }; + /// Foo {}; Foo{}; + /// vector { 1, 2, 3 }; vector{ 1, 2, 3 }; + /// new int[3] { 1, 2, 3 }; new int[3]{ 1, 2, 3 }; + /// \endcode + bool SpaceBeforeCpp11BracedList; + /// If ``false``, spaces will be removed before constructor initializer /// colon. /// \code @@ -1767,6 +1778,7 @@ SpaceAfterCStyleCast == R.SpaceAfterCStyleCast && SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword && SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators && + SpaceBeforeCpp11BracedList == R.SpaceBeforeCpp11BracedList && SpaceBeforeCtorInitializerColon == R.SpaceBeforeCtorInitializerColon && SpaceBeforeInheritanceColon == R.SpaceBeforeInheritanceColon && Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -413,6 +413,8 @@ Style.SpaceAfterTemplateKeyword); IO.mapOptional("SpaceBeforeAssignmentOperators", Style.SpaceBeforeAssignmentOperators); + IO.mapOptional("SpaceBeforeCpp11BracedList", + Style.SpaceBeforeCpp11BracedList); IO.mapOptional("SpaceBeforeCtorInitializerColon", Style.SpaceBeforeCtorInitializerColon); IO.mapOptional("SpaceBeforeInheritanceColon", @@ -675,6 +677,7 @@ LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; LLVMStyle.SpaceBeforeRangeBasedForLoopColon = true; LLVMStyle.SpaceBeforeAssignmentOperators = true; + LLVMStyle.SpaceBeforeCpp11BracedList = false; LLVMStyle.SpacesInAngles = false; LLVMStyle.PenaltyBreakAssignment = prec::Assignment; @@ -869,6 +872,7 @@ Style.ObjCBlockIndentWidth = 4; Style.ObjCSpaceAfterProperty = true; Style.PointerAlignment = FormatStyle::PAS_Left; + Style.SpaceBeforeCpp11BracedList = true; return Style; } Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -2538,6 +2538,9 @@ if (Style.isCpp()) { if (Left.is(tok::kw_operator)) return Right.is(tok::coloncolon); + if (Right.is(tok::l_brace) && Right.BlockKind == BK_BracedInit && + !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) + return true; } else if (Style.Language == FormatStyle::LK_Proto || Style.Language == FormatStyle::LK_TextProto) { if (Right.is(tok::period) && Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -6864,6 +6864,67 @@ verifyFormat("vector foo = { ::SomeGlobalFunction() };", ExtraSpaces); verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); + + FormatStyle SpaceBeforeBrace = getLLVMStyle(); + SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; + verifyFormat("vector x {1, 2, 3, 4};", SpaceBeforeBrace); + verifyFormat("vector x {\n" + " 1,\n" + " 2,\n" + " 3,\n" + " 4,\n" + "};", + SpaceBeforeBrace); + verifyFormat("vector x {{}, {}, {}, {}};", SpaceBeforeBrace); + verifyFormat("f({1, 2});", SpaceBeforeBrace); + verifyFormat("auto v = Foo {-1};", SpaceBeforeBrace); + verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});", SpaceBeforeBrace); + verifyFormat("Class::Class : member {1, 2, 3} {}", SpaceBeforeBrace); + verifyFormat("new vector {1, 2, 3};", SpaceBeforeBrace); + verifyFormat("new int[3] {1, 2, 3};", SpaceBeforeBrace); + verifyFormat("new int {1};", SpaceBeforeBrace); + verifyFormat("return {arg1, arg2};", SpaceBeforeBrace); + verifyFormat("return {arg1, SomeType {parameter}};", SpaceBeforeBrace); + verifyFormat("int count = set {f(), g(), h()}.size();", + SpaceBeforeBrace); + verifyFormat("new T {arg1, arg2};", SpaceBeforeBrace); + verifyFormat("f(MyMap[{composite, key}]);", SpaceBeforeBrace); + verifyFormat("class Class {\n" + " T member = {arg1, arg2};\n" + "};", + SpaceBeforeBrace); + verifyFormat("vector foo = {::SomeGlobalFunction()};", + SpaceBeforeBrace); + verifyFormat("const struct A a = {.a = 1, .b = 2};", SpaceBeforeBrace); + verifyFormat("const struct A a = {[0] = 1, [1] = 2};", SpaceBeforeBrace); + verifyFormat("static_assert(std::is_integral {} + 0, \"\");", + SpaceBeforeBrace); + verifyFormat("int a = std::is_integral {} + 0;", SpaceBeforeBrace); + verifyFormat("int foo(int i) { return fo1 {}(i); }", SpaceBeforeBrace); + verifyFormat("int foo(int i) { return fo1 {}(i); }", SpaceBeforeBrace); + verifyFormat("auto i = decltype(x) {};", SpaceBeforeBrace); + verifyFormat("std::vector v = {1, 0 /* comment */};", + SpaceBeforeBrace); + verifyFormat("Node n {1, Node {1000}, //\n" + " 2};", + SpaceBeforeBrace); + verifyFormat("Aaaa aaaaaaa {\n" + " {\n" + " aaaa,\n" + " },\n" + "};", + SpaceBeforeBrace); + verifyFormat("class C : public D {\n" + " SomeClass SC {2};\n" + "};", + SpaceBeforeBrace); + verifyFormat("class C : public A {\n" + " class D : public B {\n" + " void f() { int i {2}; }\n" + " };\n" + "};", + SpaceBeforeBrace); + verifyFormat("#define A {a, a},", SpaceBeforeBrace); } TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { @@ -10446,6 +10507,7 @@ CHECK_PARSE_BOOL(SpaceAfterCStyleCast); CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); + CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);