Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,24 @@ the configuration (without a prefix: ``Auto``).
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
}

.. _BinPackLongBracedList:

**BinPackLongBracedList** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BinPackLongBracedList>`
If ``BinPackLongBracedList`` is ``true`` it overrides
``BinPackArguments`` if there are 20 or more items in a braced
initializer list.

.. code-block:: c++

BinPackLongBracedList: false vs. BinPackLongBracedList: true
vector<int> x{ vector<int> x{1, 2, ...,
20, 21};
1,
2,
...,
20,
21};

.. _BinPackParameters:

**BinPackParameters** (``BinPackParametersStyle``) :versionbadge:`clang-format 3.7` :ref:`¶ <BinPackParameters>`
Expand Down
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ clang-format
------------

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

libclang
--------
Expand Down
17 changes: 17 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,22 @@ struct FormatStyle {
/// \version 3.7
bool BinPackArguments;

/// If ``BinPackLongBracedList`` is ``true`` it overrides
/// ``BinPackArguments`` if there are 20 or more items in a braced
/// initializer list.
/// \code
/// BinPackLongBracedList: false vs. BinPackLongBracedList: true
/// vector<int> x{ vector<int> x{1, 2, ...,
/// 20, 21};
/// 1,
/// 2,
/// ...,
/// 20,
/// 21};
/// \endcode
/// \version 21
bool BinPackLongBracedList;

/// Different way to try to fit all parameters on a line.
enum BinPackParametersStyle : int8_t {
/// Bin-pack parameters.
Expand Down Expand Up @@ -5266,6 +5282,7 @@ struct FormatStyle {
R.AlwaysBreakBeforeMultilineStrings &&
AttributeMacros == R.AttributeMacros &&
BinPackArguments == R.BinPackArguments &&
BinPackLongBracedList == R.BinPackLongBracedList &&
BinPackParameters == R.BinPackParameters &&
BitFieldColonSpacing == R.BitFieldColonSpacing &&
BracedInitializerIndentWidth == R.BracedInitializerIndentWidth &&
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ template <> struct MappingTraits<FormatStyle> {
Style.AlwaysBreakBeforeMultilineStrings);
IO.mapOptional("AttributeMacros", Style.AttributeMacros);
IO.mapOptional("BinPackArguments", Style.BinPackArguments);
IO.mapOptional("BinPackLongBracedList", Style.BinPackLongBracedList);
IO.mapOptional("BinPackParameters", Style.BinPackParameters);
IO.mapOptional("BitFieldColonSpacing", Style.BitFieldColonSpacing);
IO.mapOptional("BracedInitializerIndentWidth",
Expand Down Expand Up @@ -1507,6 +1508,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
LLVMStyle.AttributeMacros.push_back("__capability");
LLVMStyle.BinPackArguments = true;
LLVMStyle.BinPackLongBracedList = true;
LLVMStyle.BinPackParameters = FormatStyle::BPPS_BinPack;
LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both;
LLVMStyle.BracedInitializerIndentWidth = std::nullopt;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/FormatToken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
// have many items (20 or more) or we allow bin-packing of function call
// arguments.
if (Style.Cpp11BracedListStyle && !Style.BinPackArguments &&
Commas.size() < 19) {
(Commas.size() < 19 || !Style.BinPackLongBracedList)) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
CHECK_PARSE_BOOL(AllowShortNamespacesOnASingleLine);
CHECK_PARSE_BOOL(BinPackArguments);
CHECK_PARSE_BOOL(BinPackLongBracedList);
CHECK_PARSE_BOOL(BreakAdjacentStringLiterals);
CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
CHECK_PARSE_BOOL(BreakBeforeTemplateCloser);
Expand Down
45 changes: 45 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14420,6 +14420,51 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
"};",
NoBinPacking);

NoBinPacking.BinPackLongBracedList = false;
verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
" bbbbb,\n"
" ccccc,\n"
" ddddd,\n"
" eeeee,\n"
" ffffff,\n"
" ggggg,\n"
" hhhhhh,\n"
" iiiiii,\n"
" jjjjjj,\n"
" kkkkkk,\n"
" aaaaa,\n"
" bbbbb,\n"
" ccccc,\n"
" ddddd,\n"
" eeeee,\n"
" ffffff,\n"
" ggggg,\n"
" hhhhhh,\n"
" iiiiii};",
NoBinPacking);
verifyFormat("const Aaaaaa aaaaa = {\n"
" aaaaa,\n"
" bbbbb,\n"
" ccccc,\n"
" ddddd,\n"
" eeeee,\n"
" ffffff,\n"
" ggggg,\n"
" hhhhhh,\n"
" iiiiii,\n"
" jjjjjj,\n"
" kkkkkk,\n"
" aaaaa,\n"
" bbbbb,\n"
" ccccc,\n"
" ddddd,\n"
" eeeee,\n"
" ffffff,\n"
" ggggg,\n"
" hhhhhh,\n"
"};",
NoBinPacking);

NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
verifyFormat("static uint8 CddDp83848Reg[] = {\n"
" CDDDP83848_BMCR_REGISTER,\n"
Expand Down