Skip to content

Commit d123e08

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 bc74625 commit d123e08

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
@@ -1212,6 +1212,22 @@ struct FormatStyle {
12121212
/// \version 3.7
12131213
bool BinPackArguments;
12141214

1215+
/// If ``BinPackLongBracedLists`` is ``true`` it overrides
1216+
/// ``BinPackArguments`` if there are 20 or more items in a braced
1217+
/// initializer list.
1218+
/// \code
1219+
/// BinPackLongBracedLists: false vs. BinPackLongBracedLists: 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 20
1229+
bool BinPackLongBracedLists;
1230+
12151231
/// Different way to try to fit all parameters on a line.
12161232
enum BinPackParametersStyle : int8_t {
12171233
/// Bin-pack parameters.
@@ -2520,6 +2536,9 @@ struct FormatStyle {
25202536
/// (e.g. a type or variable name), clang-format formats as if the ``{}`` were
25212537
/// the parentheses of a function call with that name. If there is no name,
25222538
/// a zero-length name is assumed.
2539+
///
2540+
/// ``BinPackArguments`` may be forced to true for initializer lists with
2541+
/// more than 20 items if ``BinPackLongBracedLists`` is true.
25232542
/// \code
25242543
/// true: false:
25252544
/// vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 };
@@ -5222,6 +5241,7 @@ struct FormatStyle {
52225241
R.AlwaysBreakBeforeMultilineStrings &&
52235242
AttributeMacros == R.AttributeMacros &&
52245243
BinPackArguments == R.BinPackArguments &&
5244+
BinPackLongBracedLists == R.BinPackLongBracedLists &&
52255245
BinPackParameters == R.BinPackParameters &&
52265246
BitFieldColonSpacing == R.BitFieldColonSpacing &&
52275247
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("BinPackLongBracedLists", Style.BinPackLongBracedLists);
998999
IO.mapOptional("BinPackParameters", Style.BinPackParameters);
9991000
IO.mapOptional("BitFieldColonSpacing", Style.BitFieldColonSpacing);
10001001
IO.mapOptional("BracedInitializerIndentWidth",
@@ -1502,6 +1503,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
15021503
LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
15031504
LLVMStyle.AttributeMacros.push_back("__capability");
15041505
LLVMStyle.BinPackArguments = true;
1506+
LLVMStyle.BinPackLongBracedLists = true;
15051507
LLVMStyle.BinPackParameters = FormatStyle::BPPS_BinPack;
15061508
LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both;
15071509
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
@@ -164,6 +164,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
164164
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
165165
CHECK_PARSE_BOOL(AllowShortNamespacesOnASingleLine);
166166
CHECK_PARSE_BOOL(BinPackArguments);
167+
CHECK_PARSE_BOOL(BinPackLongBracedLists);
167168
CHECK_PARSE_BOOL(BreakAdjacentStringLiterals);
168169
CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
169170
CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);

clang/unittests/Format/FormatTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14081,6 +14081,32 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
1408114081
" ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
1408214082
"};",
1408314083
NoBinPacking);
14084+
NoBinPacking.BinPackLongBracedLists = false;
14085+
verifyFormat("const Aaaaaa aaaaa = {\n"
14086+
" aaaaa,\n"
14087+
" bbbbb,\n"
14088+
" ccccc,\n"
14089+
" ddddd,\n"
14090+
" eeeee,\n"
14091+
" ffffff,\n"
14092+
" ggggg,\n"
14093+
" hhhhhh,\n"
14094+
" iiiiii,\n"
14095+
" jjjjjj,\n"
14096+
" kkkkkk,\n"
14097+
" aaaaa,\n"
14098+
" bbbbb,\n"
14099+
" ccccc,\n"
14100+
" ddddd,\n"
14101+
" eeeee,\n"
14102+
" ffffff,\n"
14103+
" ggggg,\n"
14104+
" hhhhhh,\n"
14105+
" iiiiii,\n"
14106+
" jjjjjj,\n"
14107+
" kkkkkk,\n"
14108+
"};",
14109+
NoBinPacking);
1408414110

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

0 commit comments

Comments
 (0)