Skip to content

Commit d625f81

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 aebd338 commit d625f81

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

clang/include/clang/Format/Format.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,22 @@ struct FormatStyle {
12081208
/// \version 3.7
12091209
bool BinPackArguments;
12101210

1211+
/// If ``BinPackLongBracedLists`` is ``true`` it overrides
1212+
/// ``BinPackArguments`` if there are 20 or more items in a braced
1213+
/// initializer list.
1214+
/// \code
1215+
/// BinPackLongBracedLists: false vs. BinPackLongBracedLists: true
1216+
/// vector<int> x{ vector<int> x{1, 2, ...,
1217+
/// 20, 21};
1218+
/// 1,
1219+
/// 2,
1220+
/// ...,
1221+
/// 20,
1222+
/// 21};
1223+
/// \endcode
1224+
/// \version 20
1225+
bool BinPackLongBracedLists;
1226+
12111227
/// Different way to try to fit all parameters on a line.
12121228
enum BinPackParametersStyle : int8_t {
12131229
/// Bin-pack parameters.
@@ -2515,6 +2531,9 @@ struct FormatStyle {
25152531
/// (e.g. a type or variable name), clang-format formats as if the ``{}`` were
25162532
/// the parentheses of a function call with that name. If there is no name,
25172533
/// a zero-length name is assumed.
2534+
///
2535+
/// ``BinPackArguments`` may be forced to true for initializer lists with
2536+
/// more than 20 items if ``BinPackLongBracedLists`` is true.
25182537
/// \code
25192538
/// true: false:
25202539
/// vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 };
@@ -5172,6 +5191,7 @@ struct FormatStyle {
51725191
R.AlwaysBreakBeforeMultilineStrings &&
51735192
AttributeMacros == R.AttributeMacros &&
51745193
BinPackArguments == R.BinPackArguments &&
5194+
BinPackLongBracedLists == R.BinPackLongBracedLists &&
51755195
BinPackParameters == R.BinPackParameters &&
51765196
BitFieldColonSpacing == R.BitFieldColonSpacing &&
51775197
BracedInitializerIndentWidth == R.BracedInitializerIndentWidth &&

clang/lib/Format/Format.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,7 @@ template <> struct MappingTraits<FormatStyle> {
981981
Style.AlwaysBreakBeforeMultilineStrings);
982982
IO.mapOptional("AttributeMacros", Style.AttributeMacros);
983983
IO.mapOptional("BinPackArguments", Style.BinPackArguments);
984+
IO.mapOptional("BinPackLongBracedLists", Style.BinPackLongBracedLists);
984985
IO.mapOptional("BinPackParameters", Style.BinPackParameters);
985986
IO.mapOptional("BitFieldColonSpacing", Style.BitFieldColonSpacing);
986987
IO.mapOptional("BracedInitializerIndentWidth",
@@ -1484,6 +1485,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
14841485
LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
14851486
LLVMStyle.AttributeMacros.push_back("__capability");
14861487
LLVMStyle.BinPackArguments = true;
1488+
LLVMStyle.BinPackLongBracedLists = true;
14871489
LLVMStyle.BinPackParameters = FormatStyle::BPPS_BinPack;
14881490
LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both;
14891491
LLVMStyle.BracedInitializerIndentWidth = std::nullopt;

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/ConfigParseTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
160160
CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
161161
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
162162
CHECK_PARSE_BOOL(BinPackArguments);
163+
CHECK_PARSE_BOOL(BinPackLongBracedLists);
163164
CHECK_PARSE_BOOL(BreakAdjacentStringLiterals);
164165
CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
165166
CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);

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)