[clang] [clang-format] add BinPackLongBracedLists style option (PR #112482)
Gedare Bloom via cfe-commits
cfe-commits at lists.llvm.org
Sat Dec 7 13:24:11 PST 2024
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/112482
>From 9fb82b16dfea2115431219bd9915d18eff081805 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Tue, 15 Oct 2024 23:55:49 -0600
Subject: [PATCH 1/3] [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.
---
clang/include/clang/Format/Format.h | 19 ++++++++++++++++
clang/lib/Format/Format.cpp | 2 ++
clang/lib/Format/FormatToken.cpp | 2 +-
clang/unittests/Format/ConfigParseTest.cpp | 1 +
clang/unittests/Format/FormatTest.cpp | 26 ++++++++++++++++++++++
5 files changed, 49 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 6383934afa2c40..52eedce8e9d397 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -1208,6 +1208,21 @@ struct FormatStyle {
/// \version 3.7
bool BinPackArguments;
+ /// If ``BinPackArguments`` is ``false`` this option can override it if
+ /// ``true`` when 20 or more items are in a braced initializer list.
+ /// \code
+ /// BinPackLongBracedLists: false vs. BinPackLongBracedLists: true
+ /// vector<int> x{ vector<int> x{1, 2, ...,
+ /// 20, 21};
+ /// 1,
+ /// 2,
+ /// ...,
+ /// 20,
+ /// 21};
+ /// \endcode
+ /// \version 20
+ bool BinPackLongBracedLists;
+
/// Different way to try to fit all parameters on a line.
enum BinPackParametersStyle : int8_t {
/// Bin-pack parameters.
@@ -2515,6 +2530,9 @@ struct FormatStyle {
/// (e.g. a type or variable name), clang-format formats as if the ``{}`` were
/// the parentheses of a function call with that name. If there is no name,
/// a zero-length name is assumed.
+ ///
+ /// ``BinPackArguments`` may be forced to true for initializer lists with
+ /// more than 20 items if ``BinPackLongBracedLists`` is true.
/// \code
/// true: false:
/// vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 };
@@ -5172,6 +5190,7 @@ struct FormatStyle {
R.AlwaysBreakBeforeMultilineStrings &&
AttributeMacros == R.AttributeMacros &&
BinPackArguments == R.BinPackArguments &&
+ BinPackLongBracedLists == R.BinPackLongBracedLists &&
BinPackParameters == R.BinPackParameters &&
BitFieldColonSpacing == R.BitFieldColonSpacing &&
BracedInitializerIndentWidth == R.BracedInitializerIndentWidth &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index ee52972ce66f4a..72106945214207 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -981,6 +981,7 @@ template <> struct MappingTraits<FormatStyle> {
Style.AlwaysBreakBeforeMultilineStrings);
IO.mapOptional("AttributeMacros", Style.AttributeMacros);
IO.mapOptional("BinPackArguments", Style.BinPackArguments);
+ IO.mapOptional("BinPackLongBracedLists", Style.BinPackLongBracedLists);
IO.mapOptional("BinPackParameters", Style.BinPackParameters);
IO.mapOptional("BitFieldColonSpacing", Style.BitFieldColonSpacing);
IO.mapOptional("BracedInitializerIndentWidth",
@@ -1484,6 +1485,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
LLVMStyle.AttributeMacros.push_back("__capability");
LLVMStyle.BinPackArguments = true;
+ LLVMStyle.BinPackLongBracedLists = true;
LLVMStyle.BinPackParameters = FormatStyle::BPPS_BinPack;
LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both;
LLVMStyle.BracedInitializerIndentWidth = std::nullopt;
diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp
index 963e8f87793fa0..a56a13bd58a519 100644
--- a/clang/lib/Format/FormatToken.cpp
+++ b/clang/lib/Format/FormatToken.cpp
@@ -174,7 +174,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.BinPackLongBracedLists)) {
return;
}
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 7fc7492271668b..9c29cd3f57273f 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -160,6 +160,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
CHECK_PARSE_BOOL(BinPackArguments);
+ CHECK_PARSE_BOOL(BinPackLongBracedLists);
CHECK_PARSE_BOOL(BreakAdjacentStringLiterals);
CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 250e51b5421664..fe8193ca2ea124 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -14038,6 +14038,32 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
" ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
"};",
NoBinPacking);
+ NoBinPacking.BinPackLongBracedLists = false;
+ 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"
+ " iiiiii,\n"
+ " jjjjjj,\n"
+ " kkkkkk,\n"
+ "};",
+ NoBinPacking);
NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
verifyFormat("static uint8 CddDp83848Reg[] = {\n"
>From 77cf13b1d97cc13a7b0660b9b4b41a0bc4ae8b40 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Wed, 16 Oct 2024 00:00:16 -0600
Subject: [PATCH 2/3] dump format style
---
clang/docs/ClangFormatStyleOptions.rst | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 4be448171699ca..adcc6e390e7bc9 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -2177,6 +2177,23 @@ the configuration (without a prefix: ``Auto``).
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
}
+.. _BinPackLongBracedLists:
+
+**BinPackLongBracedLists** (``Boolean``) :versionbadge:`clang-format 20` :ref:`¶ <BinPackLongBracedLists>`
+ If ``BinPackArguments`` is ``false`` this option can override it if
+ ``true`` when 20 or more items are in a braced initializer list.
+
+ .. code-block:: c++
+
+ BinPackLongBracedLists: false vs. BinPackLongBracedLists: 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>`
@@ -3769,6 +3786,9 @@ the configuration (without a prefix: ``Auto``).
the parentheses of a function call with that name. If there is no name,
a zero-length name is assumed.
+ ``BinPackArguments`` may be forced to true for initializer lists with
+ more than 20 items if ``BinPackLongBracedLists`` is true.
+
.. code-block:: c++
true: false:
>From 30829662808db582f425a837671446a39429cd92 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Wed, 16 Oct 2024 00:01:54 -0600
Subject: [PATCH 3/3] update release notes
---
clang/docs/ReleaseNotes.rst | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0bb2eb820cd726..aab48d16e06aa8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -978,6 +978,8 @@ clang-format
``Never``, and ``true`` to ``Always``.
- Adds ``RemoveEmptyLinesInUnwrappedLines`` option.
- Adds ``KeepFormFeed`` option and set it to ``true`` for ``GNU`` style.
+- Adds ``BinPackLongBracedLists`` option to override bin packing options in
+ long (20 item or more) braced list initializer lists.
libclang
--------
More information about the cfe-commits
mailing list