Skip to content

Commit 49fb737

Browse files
committed
[clang-format] add MaxSingleLinesInBracedList 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 setting a configurable limit of items.
1 parent 4c2c177 commit 49fb737

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 ignored for initializer lists with more than
2520+
/// ``MaxSingleLinesInBracedList`` items.
25182521
/// \code
25192522
/// true: false:
25202523
/// vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 };
@@ -3391,6 +3394,21 @@ struct FormatStyle {
33913394
/// \version 3.7
33923395
unsigned MaxEmptyLinesToKeep;
33933396

3397+
/// The maximum number of single item lines to keep in a braced list when
3398+
/// ``BinPackArguments`` is ``false`` before formatting items in columns.
3399+
/// Defaults to 20.
3400+
/// \code
3401+
/// MaxSingleLinesInBracedList: 5 vs. MaxSingleLinesInBracedList: 4
3402+
/// vector<int> x{ vector<int> x{1, 2, 3, 4, 5};
3403+
/// 1,
3404+
/// 2,
3405+
/// 3,
3406+
/// 4,
3407+
/// 5};
3408+
/// \endcode
3409+
/// \version 20
3410+
unsigned MaxSingleLinesInBracedList;
3411+
33943412
/// Different ways to indent namespace contents.
33953413
enum NamespaceIndentationKind : int8_t {
33963414
/// Don't indent in namespaces.
@@ -5204,6 +5222,7 @@ struct FormatStyle {
52045222
LineEnding == R.LineEnding && MacroBlockBegin == R.MacroBlockBegin &&
52055223
MacroBlockEnd == R.MacroBlockEnd && Macros == R.Macros &&
52065224
MaxEmptyLinesToKeep == R.MaxEmptyLinesToKeep &&
5225+
MaxSingleLinesInBracedList == R.MaxSingleLinesInBracedList &&
52075226
NamespaceIndentation == R.NamespaceIndentation &&
52085227
NamespaceMacros == R.NamespaceMacros &&
52095228
ObjCBinPackProtocolList == R.ObjCBinPackProtocolList &&

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,8 @@ template <> struct MappingTraits<FormatStyle> {
10591059
IO.mapOptional("Macros", Style.Macros);
10601060
IO.mapOptional("MainIncludeChar", Style.IncludeStyle.MainIncludeChar);
10611061
IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
1062+
IO.mapOptional("MaxSingleLinesInBracedList",
1063+
Style.MaxSingleLinesInBracedList);
10621064
IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
10631065
IO.mapOptional("NamespaceMacros", Style.NamespaceMacros);
10641066
IO.mapOptional("ObjCBinPackProtocolList", Style.ObjCBinPackProtocolList);
@@ -1569,6 +1571,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
15691571
LLVMStyle.Language = Language;
15701572
LLVMStyle.LineEnding = FormatStyle::LE_DeriveLF;
15711573
LLVMStyle.MaxEmptyLinesToKeep = 1;
1574+
LLVMStyle.MaxSingleLinesInBracedList = 20;
15721575
LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
15731576
LLVMStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
15741577
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() <= Style.MaxSingleLinesInBracedList) {
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.MaxSingleLinesInBracedList = 22;
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)