Skip to content

Commit 839922e

Browse files
committed
clang-format: Format long lists in columns if without bin-packing.
After (even with BinPacking = false): const Aaaaaa aaaaa = { aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk, }; Before: <each element on its own line> This fixes http://llvm.org/PR20623. llvm-svn: 215529
1 parent 5b1a044 commit 839922e

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

clang/lib/Format/FormatToken.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
131131
if (!Token->MatchingParen || Token->isNot(tok::l_brace))
132132
return;
133133

134-
// In C++11 braced list style, we should not format in columns unless we allow
135-
// bin-packing of function parameters.
136-
if (Style.Cpp11BracedListStyle && !Style.BinPackParameters)
134+
// In C++11 braced list style, we should not format in columns unless they
135+
// have many items (20 or more) or we allow bin-packing of function
136+
// parameters.
137+
if (Style.Cpp11BracedListStyle && !Style.BinPackParameters &&
138+
Commas.size() < 19)
137139
return;
138140

139141
FormatToken *ItemBegin = Token->Next;

clang/unittests/Format/FormatTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5441,6 +5441,13 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
54415441
" kkkkkk,\n"
54425442
"};",
54435443
NoBinPacking);
5444+
verifyFormat(
5445+
"const Aaaaaa aaaaa = {\n"
5446+
" aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
5447+
" iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
5448+
" ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
5449+
"};",
5450+
NoBinPacking);
54445451

54455452
// FIXME: The alignment of these trailing comments might be bad. Then again,
54465453
// this might be utterly useless in real code.

0 commit comments

Comments
 (0)