diff options
| author | Hans Wennborg <hans@hanshq.net> | 2018-06-14 08:01:09 +0000 |
|---|---|---|
| committer | Hans Wennborg <hans@hanshq.net> | 2018-06-14 08:01:09 +0000 |
| commit | bfc340653022b00383c97db117719d15d20bdfa3 (patch) | |
| tree | f6145004922987f12a6f02dbe3a96908f41ac575 | |
| parent | 49fad1cbf2bba88a5c0d596e3aa46d142f9d73ad (diff) | |
| download | bcm5719-llvm-bfc340653022b00383c97db117719d15d20bdfa3.tar.gz bcm5719-llvm-bfc340653022b00383c97db117719d15d20bdfa3.zip | |
[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
| -rw-r--r-- | clang/docs/ClangFormatStyleOptions.rst | 12 | ||||
| -rw-r--r-- | clang/include/clang/Format/Format.h | 12 | ||||
| -rw-r--r-- | clang/lib/Format/Format.cpp | 4 | ||||
| -rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 3 | ||||
| -rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 6 |
5 files changed, 37 insertions, 0 deletions
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 24ce4fba775..976362f1ee2 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -1791,6 +1791,18 @@ the configuration (without a prefix: ``Auto``). 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<int> { 1, 2, 3 }; vector<int>{ 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. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 66b77628932..efc7cf643b2 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1495,6 +1495,17 @@ struct FormatStyle { /// \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<int> { 1, 2, 3 }; vector<int>{ 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 @@ -1738,6 +1749,7 @@ struct FormatStyle { SpaceAfterCStyleCast == R.SpaceAfterCStyleCast && SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword && SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators && + SpaceBeforeCpp11BracedList == R.SpaceBeforeCpp11BracedList && SpaceBeforeCtorInitializerColon == R.SpaceBeforeCtorInitializerColon && SpaceBeforeInheritanceColon == R.SpaceBeforeInheritanceColon && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 7e62d1596c8..71fc81ad503 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -449,6 +449,8 @@ template <> struct MappingTraits<FormatStyle> { Style.SpaceAfterTemplateKeyword); IO.mapOptional("SpaceBeforeAssignmentOperators", Style.SpaceBeforeAssignmentOperators); + IO.mapOptional("SpaceBeforeCpp11BracedList", + Style.SpaceBeforeCpp11BracedList); IO.mapOptional("SpaceBeforeCtorInitializerColon", Style.SpaceBeforeCtorInitializerColon); IO.mapOptional("SpaceBeforeInheritanceColon", @@ -697,6 +699,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; LLVMStyle.SpaceBeforeRangeBasedForLoopColon = true; LLVMStyle.SpaceBeforeAssignmentOperators = true; + LLVMStyle.SpaceBeforeCpp11BracedList = false; LLVMStyle.SpacesInAngles = false; LLVMStyle.PenaltyBreakAssignment = prec::Assignment; @@ -892,6 +895,7 @@ FormatStyle getWebKitStyle() { Style.ObjCBlockIndentWidth = 4; Style.ObjCSpaceAfterProperty = true; Style.PointerAlignment = FormatStyle::PAS_Left; + Style.SpaceBeforeCpp11BracedList = true; return Style; } diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index ea957690edb..3280f25fef0 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2548,6 +2548,9 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, 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) && diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 399503a528f..f234e287cf1 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -7019,6 +7019,11 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) { " { \"ccccccccccccccccccccc\", 2 }\n" "};", ExtraSpaces); + + FormatStyle SpaceBeforeBrace = getLLVMStyle(); + SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; + verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); + verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); } TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { @@ -10622,6 +10627,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) { 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); |

