Skip to content

Commit eb789f3

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 f95f10c commit eb789f3

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 };
@@ -5235,6 +5254,7 @@ struct FormatStyle {
52355254
R.AlwaysBreakBeforeMultilineStrings &&
52365255
AttributeMacros == R.AttributeMacros &&
52375256
BinPackArguments == R.BinPackArguments &&
5257+
BinPackLongBracedLists == R.BinPackLongBracedLists &&
52385258
BinPackParameters == R.BinPackParameters &&
52395259
BitFieldColonSpacing == R.BitFieldColonSpacing &&
52405260
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",
@@ -1503,6 +1504,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
15031504
LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
15041505
LLVMStyle.AttributeMacros.push_back("__capability");
15051506
LLVMStyle.BinPackArguments = true;
1507+
LLVMStyle.BinPackLongBracedLists = true;
15061508
LLVMStyle.BinPackParameters = FormatStyle::BPPS_BinPack;
15071509
LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both;
15081510
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
@@ -14185,6 +14185,32 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
1418514185
" ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
1418614186
"};",
1418714187
NoBinPacking);
14188+
NoBinPacking.BinPackLongBracedLists = false;
14189+
verifyFormat("const Aaaaaa aaaaa = {\n"
14190+
" aaaaa,\n"
14191+
" bbbbb,\n"
14192+
" ccccc,\n"
14193+
" ddddd,\n"
14194+
" eeeee,\n"
14195+
" ffffff,\n"
14196+
" ggggg,\n"
14197+
" hhhhhh,\n"
14198+
" iiiiii,\n"
14199+
" jjjjjj,\n"
14200+
" kkkkkk,\n"
14201+
" aaaaa,\n"
14202+
" bbbbb,\n"
14203+
" ccccc,\n"
14204+
" ddddd,\n"
14205+
" eeeee,\n"
14206+
" ffffff,\n"
14207+
" ggggg,\n"
14208+
" hhhhhh,\n"
14209+
" iiiiii,\n"
14210+
" jjjjjj,\n"
14211+
" kkkkkk,\n"
14212+
"};",
14213+
NoBinPacking);
1418814214

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

0 commit comments

Comments
 (0)