Skip to content

Commit e0a21e2

Browse files
authored
[clang-format] Add BinPackLongBracedList style option (#112482)
The use of Cpp11BracedListStyle with BinPackArguments=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 de12bf5 commit e0a21e2

File tree

7 files changed

+86
-1
lines changed

7 files changed

+86
-1
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,6 +2182,24 @@ the configuration (without a prefix: ``Auto``).
21822182
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
21832183
}
21842184

2185+
.. _BinPackLongBracedList:
2186+
2187+
**BinPackLongBracedList** (``Boolean``) :versionbadge:`clang-format 21` :ref:`<BinPackLongBracedList>`
2188+
If ``BinPackLongBracedList`` is ``true`` it overrides
2189+
``BinPackArguments`` if there are 20 or more items in a braced
2190+
initializer list.
2191+
2192+
.. code-block:: c++
2193+
2194+
BinPackLongBracedList: false vs. BinPackLongBracedList: true
2195+
vector<int> x{ vector<int> x{1, 2, ...,
2196+
20, 21};
2197+
1,
2198+
2,
2199+
...,
2200+
20,
2201+
21};
2202+
21852203
.. _BinPackParameters:
21862204

21872205
**BinPackParameters** (``BinPackParametersStyle``) :versionbadge:`clang-format 3.7` :ref:`<BinPackParameters>`

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ clang-format
237237
------------
238238

239239
- Adds ``BreakBeforeTemplateCloser`` option.
240+
- Adds ``BinPackLongBracedList`` option to override bin packing options in
241+
long (20 item or more) braced list initializer lists.
240242

241243
libclang
242244
--------

clang/include/clang/Format/Format.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,22 @@ struct FormatStyle {
12121212
/// \version 3.7
12131213
bool BinPackArguments;
12141214

1215+
/// If ``BinPackLongBracedList`` is ``true`` it overrides
1216+
/// ``BinPackArguments`` if there are 20 or more items in a braced
1217+
/// initializer list.
1218+
/// \code
1219+
/// BinPackLongBracedList: false vs. BinPackLongBracedList: true
1220+
/// vector<int> x{ vector<int> x{1, 2, ...,
1221+
/// 20, 21};
1222+
/// 1,
1223+
/// 2,
1224+
/// ...,
1225+
/// 20,
1226+
/// 21};
1227+
/// \endcode
1228+
/// \version 21
1229+
bool BinPackLongBracedList;
1230+
12151231
/// Different way to try to fit all parameters on a line.
12161232
enum BinPackParametersStyle : int8_t {
12171233
/// Bin-pack parameters.
@@ -5266,6 +5282,7 @@ struct FormatStyle {
52665282
R.AlwaysBreakBeforeMultilineStrings &&
52675283
AttributeMacros == R.AttributeMacros &&
52685284
BinPackArguments == R.BinPackArguments &&
5285+
BinPackLongBracedList == R.BinPackLongBracedList &&
52695286
BinPackParameters == R.BinPackParameters &&
52705287
BitFieldColonSpacing == R.BitFieldColonSpacing &&
52715288
BracedInitializerIndentWidth == R.BracedInitializerIndentWidth &&

clang/lib/Format/Format.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,7 @@ template <> struct MappingTraits<FormatStyle> {
995995
Style.AlwaysBreakBeforeMultilineStrings);
996996
IO.mapOptional("AttributeMacros", Style.AttributeMacros);
997997
IO.mapOptional("BinPackArguments", Style.BinPackArguments);
998+
IO.mapOptional("BinPackLongBracedList", Style.BinPackLongBracedList);
998999
IO.mapOptional("BinPackParameters", Style.BinPackParameters);
9991000
IO.mapOptional("BitFieldColonSpacing", Style.BitFieldColonSpacing);
10001001
IO.mapOptional("BracedInitializerIndentWidth",
@@ -1507,6 +1508,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
15071508
LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
15081509
LLVMStyle.AttributeMacros.push_back("__capability");
15091510
LLVMStyle.BinPackArguments = true;
1511+
LLVMStyle.BinPackLongBracedList = true;
15101512
LLVMStyle.BinPackParameters = FormatStyle::BPPS_BinPack;
15111513
LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both;
15121514
LLVMStyle.BracedInitializerIndentWidth = std::nullopt;

clang/lib/Format/FormatToken.cpp

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

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
168168
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
169169
CHECK_PARSE_BOOL(AllowShortNamespacesOnASingleLine);
170170
CHECK_PARSE_BOOL(BinPackArguments);
171+
CHECK_PARSE_BOOL(BinPackLongBracedList);
171172
CHECK_PARSE_BOOL(BreakAdjacentStringLiterals);
172173
CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
173174
CHECK_PARSE_BOOL(BreakBeforeTemplateCloser);

clang/unittests/Format/FormatTest.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14420,6 +14420,51 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
1442014420
"};",
1442114421
NoBinPacking);
1442214422

14423+
NoBinPacking.BinPackLongBracedList = false;
14424+
verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
14425+
" bbbbb,\n"
14426+
" ccccc,\n"
14427+
" ddddd,\n"
14428+
" eeeee,\n"
14429+
" ffffff,\n"
14430+
" ggggg,\n"
14431+
" hhhhhh,\n"
14432+
" iiiiii,\n"
14433+
" jjjjjj,\n"
14434+
" kkkkkk,\n"
14435+
" aaaaa,\n"
14436+
" bbbbb,\n"
14437+
" ccccc,\n"
14438+
" ddddd,\n"
14439+
" eeeee,\n"
14440+
" ffffff,\n"
14441+
" ggggg,\n"
14442+
" hhhhhh,\n"
14443+
" iiiiii};",
14444+
NoBinPacking);
14445+
verifyFormat("const Aaaaaa aaaaa = {\n"
14446+
" aaaaa,\n"
14447+
" bbbbb,\n"
14448+
" ccccc,\n"
14449+
" ddddd,\n"
14450+
" eeeee,\n"
14451+
" ffffff,\n"
14452+
" ggggg,\n"
14453+
" hhhhhh,\n"
14454+
" iiiiii,\n"
14455+
" jjjjjj,\n"
14456+
" kkkkkk,\n"
14457+
" aaaaa,\n"
14458+
" bbbbb,\n"
14459+
" ccccc,\n"
14460+
" ddddd,\n"
14461+
" eeeee,\n"
14462+
" ffffff,\n"
14463+
" ggggg,\n"
14464+
" hhhhhh,\n"
14465+
"};",
14466+
NoBinPacking);
14467+
1442314468
NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
1442414469
verifyFormat("static uint8 CddDp83848Reg[] = {\n"
1442514470
" CDDDP83848_BMCR_REGISTER,\n"

0 commit comments

Comments
 (0)