[clang] [clang-format] add MaxSingleLinesInBracedList style option (PR #112482)
Gedare Bloom via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 2 12:04:27 PST 2024
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/112482
>From 5f868e9ce386923052f1b14936f04b129c7d6c00 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 | 3 +++
clang/lib/Format/FormatToken.cpp | 2 +-
clang/unittests/Format/FormatTest.cpp | 26 ++++++++++++++++++++++++++
4 files changed, 49 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 6383934afa2c40..6e5deed2f71d5a 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2515,6 +2515,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 };
@@ -3398,6 +3401,21 @@ struct FormatStyle {
/// \version 3.7
unsigned MaxEmptyLinesToKeep;
+ /// 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 ways to indent namespace contents.
enum NamespaceIndentationKind : int8_t {
/// Don't indent in namespaces.
@@ -5234,6 +5252,7 @@ struct FormatStyle {
LineEnding == R.LineEnding && MacroBlockBegin == R.MacroBlockBegin &&
MacroBlockEnd == R.MacroBlockEnd && Macros == R.Macros &&
MaxEmptyLinesToKeep == R.MaxEmptyLinesToKeep &&
+ BinPackLongBracedLists == R.BinPackLongBracedLists &&
NamespaceIndentation == R.NamespaceIndentation &&
NamespaceMacros == R.NamespaceMacros &&
ObjCBinPackProtocolList == R.ObjCBinPackProtocolList &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index ee52972ce66f4a..fadfbdb51dc5a2 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1060,6 +1060,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("Macros", Style.Macros);
IO.mapOptional("MainIncludeChar", Style.IncludeStyle.MainIncludeChar);
IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
+ IO.mapOptional("BinPackLongBracedLists",
+ Style.BinPackLongBracedLists);
IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
IO.mapOptional("NamespaceMacros", Style.NamespaceMacros);
IO.mapOptional("ObjCBinPackProtocolList", Style.ObjCBinPackProtocolList);
@@ -1573,6 +1575,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.Language = Language;
LLVMStyle.LineEnding = FormatStyle::LE_DeriveLF;
LLVMStyle.MaxEmptyLinesToKeep = 1;
+ LLVMStyle.BinPackLongBracedLists = true;
LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
LLVMStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
LLVMStyle.ObjCBlockIndentWidth = 2;
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/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 d25eb46195b2c1ec3822938650972806903a7918 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 c667d874ccd1e4aa23348c8f5a78fab834ecbbc1 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