[clang] [clang-format] add BinPackLongBracedLists style option (PR #112482)
Gedare Bloom via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 29 15:32:40 PST 2025
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/112482
>From a01d46f6dfe7f383557b7e98df01b73a4f238cdf 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/5] [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 | 20 +++++++++++++++++
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, 50 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 6f432d1d5031542..31761c3311c34e5 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -1212,6 +1212,22 @@ struct FormatStyle {
/// \version 3.7
bool BinPackArguments;
+ /// If ``BinPackLongBracedLists`` is ``true`` it overrides
+ /// ``BinPackArguments`` if there are 20 or more items 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.
@@ -2520,6 +2536,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 };
@@ -5239,6 +5258,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 f02bf95cfeed7ea..4c52e92def43a37 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -995,6 +995,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",
@@ -1505,6 +1506,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 963e8f87793fa09..a56a13bd58a5192 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 5bb1c00ab0bb239..7fec20723089fbb 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -168,6 +168,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
CHECK_PARSE_BOOL(AllowShortNamespacesOnASingleLine);
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 57f12221cdc7e66..73bfbee58dd83b1 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -14185,6 +14185,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 a2bcf54cbb612b60bfa350c40bd656aa30e48861 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/5] dump format style
---
clang/docs/ClangFormatStyleOptions.rst | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index bbb912eb10e94d7..31a476c981ff4ea 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -2182,6 +2182,24 @@ the configuration (without a prefix: ``Auto``).
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
}
+.. _BinPackLongBracedLists:
+
+**BinPackLongBracedLists** (``Boolean``) :versionbadge:`clang-format 20` :ref:`¶ <BinPackLongBracedLists>`
+ If ``BinPackLongBracedLists`` is ``true`` it overrides
+ ``BinPackArguments`` if there are 20 or more items 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>`
@@ -3775,6 +3793,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 da549c9b16816c727caab9e1a28eb9806c973028 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/5] update release notes
---
clang/docs/ReleaseNotes.rst | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2b56ce974289d5e..ae1f7d4fc50978d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -200,6 +200,9 @@ AST Matchers
clang-format
------------
+- Adds ``BinPackLongBracedLists`` option to override bin packing options in
+ long (20 item or more) braced list initializer lists.
+
libclang
--------
>From 14118ee7c4541e1e2adb461575314c63867c50a3 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Wed, 29 Jan 2025 16:32:06 -0700
Subject: [PATCH 4/5] Format.h: bump version to 21
---
clang/include/clang/Format/Format.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 31761c3311c34e5..55c4d4a85be891e 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -1225,7 +1225,7 @@ struct FormatStyle {
/// 20,
/// 21};
/// \endcode
- /// \version 20
+ /// \version 21
bool BinPackLongBracedLists;
/// Different way to try to fit all parameters on a line.
>From 5a1d96321023366fe79765f9e8a7b7ee89904cc4 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Wed, 29 Jan 2025 16:32:18 -0700
Subject: [PATCH 5/5] Run clang-format-style
---
clang/docs/ClangFormatStyleOptions.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 31a476c981ff4ea..c79e121bd36002c 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -2184,7 +2184,7 @@ the configuration (without a prefix: ``Auto``).
.. _BinPackLongBracedLists:
-**BinPackLongBracedLists** (``Boolean``) :versionbadge:`clang-format 20` :ref:`¶ <BinPackLongBracedLists>`
+**BinPackLongBracedLists** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BinPackLongBracedLists>`
If ``BinPackLongBracedLists`` is ``true`` it overrides
``BinPackArguments`` if there are 20 or more items in a braced
initializer list.
More information about the cfe-commits
mailing list