r332434 - clang-format: tweak formatting of variable initialization blocks
Francois Ferrand via cfe-commits
cfe-commits at lists.llvm.org
Wed May 16 01:03:52 PDT 2018
Author: typz
Date: Wed May 16 01:03:52 2018
New Revision: 332434
URL: http://llvm.org/viewvc/llvm-project?rev=332434&view=rev
Log:
clang-format: tweak formatting of variable initialization blocks
Summary:
This patch changes the behavior of PenaltyBreakBeforeFirstCallParameter
so that is does not apply after a brace, when Cpp11BracedListStyle is
false.
This way, variable initialization is wrapped more like an initializer
than like a function call, which is more consistent with user
expectations for this braced list style.
With PenaltyBreakBeforeFirstCallParameter=200, this gives the following
code: (with Cpp11BracedListStyle=false)
Before :
const std::unordered_map<std::string, int> Something::MyHashTable =
{ { "aaaaaaaaaaaaaaaaaaaaa", 0 },
{ "bbbbbbbbbbbbbbbbbbbbb", 1 },
{ "ccccccccccccccccccccc", 2 } };
After :
const std::unordered_set<std::string> Something::MyUnorderedSet = {
{ "aaaaaaaaaaaaaaaaaaaaa", 0 },
{ "bbbbbbbbbbbbbbbbbbbbb", 1 },
{ "ccccccccccccccccccccc", 2 }
};
Reviewers: krasimir, djasper, klimek
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D43290
Modified:
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=332434&r1=332433&r2=332434&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed May 16 01:03:52 2018
@@ -2311,6 +2311,8 @@ unsigned TokenAnnotator::splitPenalty(co
if (Left.opensScope()) {
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign)
return 0;
+ if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
+ return 19;
return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
: 19;
}
@@ -3048,6 +3050,9 @@ bool TokenAnnotator::canBreakBefore(cons
if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0)
return false;
+ if (Left.is(tok::equal) && Right.is(tok::l_brace) &&
+ !Style.Cpp11BracedListStyle)
+ return false;
if (Left.is(tok::l_paren) && Left.is(TT_AttributeParen))
return false;
if (Left.is(tok::l_paren) && Left.Previous &&
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=332434&r1=332433&r2=332434&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed May 16 01:03:52 2018
@@ -6708,6 +6708,15 @@ TEST_F(FormatTest, LayoutCxx11BraceIniti
"};");
verifyFormat("#define A {a, a},");
+ // Avoid breaking between equal sign and opening brace
+ FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
+ AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
+ verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
+ " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
+ " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
+ " {\"ccccccccccccccccccccc\", 2}};",
+ AvoidBreakingFirstArgument);
+
// Binpacking only if there is no trailing comma
verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
" cccccccccc, dddddddddd};",
@@ -6864,6 +6873,21 @@ TEST_F(FormatTest, LayoutCxx11BraceIniti
verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
+
+ // Avoid breaking between initializer/equal sign and opening brace
+ ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
+ verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
+ " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
+ " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
+ " { \"ccccccccccccccccccccc\", 2 }\n"
+ "};",
+ ExtraSpaces);
+ verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
+ " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
+ " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
+ " { \"ccccccccccccccccccccc\", 2 }\n"
+ "};",
+ ExtraSpaces);
}
TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
More information about the cfe-commits
mailing list