Skip to content

Commit 5f868e9

Browse files
committed
[clang-format] add BinPackLongBracedLists style option
The use of Cpp11BracedListStyle with BinPackParameters=False avoids bin packing until reaching a hard-coded limit of 20 items. This is an arbitrary choice. Introduce a new style option to allow disabling this limit.
1 parent 3d43789 commit 5f868e9

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

clang/include/clang/Format/Format.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,6 +2515,9 @@ struct FormatStyle {
25152515
/// (e.g. a type or variable name), clang-format formats as if the ``{}`` were
25162516
/// the parentheses of a function call with that name. If there is no name,
25172517
/// a zero-length name is assumed.
2518+
///
2519+
/// ``BinPackArguments`` may be forced to true for initializer lists with
2520+
/// more than 20 items if ``BinPackLongBracedLists`` is true.
25182521
/// \code
25192522
/// true: false:
25202523
/// vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 };
@@ -3398,6 +3401,21 @@ struct FormatStyle {
33983401
/// \version 3.7
33993402
unsigned MaxEmptyLinesToKeep;
34003403

3404+
/// If ``BinPackArguments`` is ``false`` this option can override it if
3405+
/// ``true`` when 20 or more items are in a braced initializer list.
3406+
/// \code
3407+
/// BinPackLongBracedLists: false vs. BinPackLongBracedLists: true
3408+
/// vector<int> x{ vector<int> x{1, 2, ...,
3409+
/// 20, 21};
3410+
/// 1,
3411+
/// 2,
3412+
/// ...,
3413+
/// 20,
3414+
/// 21};
3415+
/// \endcode
3416+
/// \version 20
3417+
bool BinPackLongBracedLists;
3418+
34013419
/// Different ways to indent namespace contents.
34023420
enum NamespaceIndentationKind : int8_t {
34033421
/// Don't indent in namespaces.
@@ -5234,6 +5252,7 @@ struct FormatStyle {
52345252
LineEnding == R.LineEnding && MacroBlockBegin == R.MacroBlockBegin &&
52355253
MacroBlockEnd == R.MacroBlockEnd && Macros == R.Macros &&
52365254
MaxEmptyLinesToKeep == R.MaxEmptyLinesToKeep &&
5255+
BinPackLongBracedLists == R.BinPackLongBracedLists &&
52375256
NamespaceIndentation == R.NamespaceIndentation &&
52385257
NamespaceMacros == R.NamespaceMacros &&
52395258
ObjCBinPackProtocolList == R.ObjCBinPackProtocolList &&

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,8 @@ template <> struct MappingTraits<FormatStyle> {
10601060
IO.mapOptional("Macros", Style.Macros);
10611061
IO.mapOptional("MainIncludeChar", Style.IncludeStyle.MainIncludeChar);
10621062
IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
1063+
IO.mapOptional("BinPackLongBracedLists",
1064+
Style.BinPackLongBracedLists);
10631065
IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
10641066
IO.mapOptional("NamespaceMacros", Style.NamespaceMacros);
10651067
IO.mapOptional("ObjCBinPackProtocolList", Style.ObjCBinPackProtocolList);
@@ -1573,6 +1575,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
15731575
LLVMStyle.Language = Language;
15741576
LLVMStyle.LineEnding = FormatStyle::LE_DeriveLF;
15751577
LLVMStyle.MaxEmptyLinesToKeep = 1;
1578+
LLVMStyle.BinPackLongBracedLists = true;
15761579
LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
15771580
LLVMStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
15781581
LLVMStyle.ObjCBlockIndentWidth = 2;

clang/lib/Format/FormatToken.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
174174
// have many items (20 or more) or we allow bin-packing of function call
175175
// arguments.
176176
if (Style.Cpp11BracedListStyle && !Style.BinPackArguments &&
177-
Commas.size() < 19) {
177+
(Commas.size() < 19 || !Style.BinPackLongBracedLists)) {
178178
return;
179179
}
180180

clang/unittests/Format/FormatTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14038,6 +14038,32 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
1403814038
" ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
1403914039
"};",
1404014040
NoBinPacking);
14041+
NoBinPacking.BinPackLongBracedLists = false;
14042+
verifyFormat("const Aaaaaa aaaaa = {\n"
14043+
" aaaaa,\n"
14044+
" bbbbb,\n"
14045+
" ccccc,\n"
14046+
" ddddd,\n"
14047+
" eeeee,\n"
14048+
" ffffff,\n"
14049+
" ggggg,\n"
14050+
" hhhhhh,\n"
14051+
" iiiiii,\n"
14052+
" jjjjjj,\n"
14053+
" kkkkkk,\n"
14054+
" aaaaa,\n"
14055+
" bbbbb,\n"
14056+
" ccccc,\n"
14057+
" ddddd,\n"
14058+
" eeeee,\n"
14059+
" ffffff,\n"
14060+
" ggggg,\n"
14061+
" hhhhhh,\n"
14062+
" iiiiii,\n"
14063+
" jjjjjj,\n"
14064+
" kkkkkk,\n"
14065+
"};",
14066+
NoBinPacking);
1404114067

1404214068
NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
1404314069
verifyFormat("static uint8 CddDp83848Reg[] = {\n"

0 commit comments

Comments
 (0)