https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/112482
>From eb789f3dd6aab070865fc92270aa20f872f30dab Mon Sep 17 00:00:00 2001 From: Gedare Bloom <ged...@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 | 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 fd526f189ec833..00b4d1977f6d45 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 }; @@ -5235,6 +5254,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 c25d9bf7c22519..603ad9979e8770 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", @@ -1503,6 +1504,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 9746aa35478465..bba64d549db623 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -164,6 +164,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 265461561d2012..e23aa841de7257 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 298bcef2e75c9b99ff25c25259e64bd272ed212a Mon Sep 17 00:00:00 2001 From: Gedare Bloom <ged...@rtems.org> Date: Wed, 16 Oct 2024 00:00:16 -0600 Subject: [PATCH 2/3] 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 30a2325949f48a..763c8a9c3f0b27 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 0b06c824a188507e135d3545899c32f3a76369b4 Mon Sep 17 00:00:00 2001 From: Gedare Bloom <ged...@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 031c5d84e49f97..bbc0bf57258816 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -1292,6 +1292,8 @@ clang-format - Adds support for bash globstar in ``.clang-format-ignore``. - Adds ``WrapNamespaceBodyWithEmptyLines`` option. - Adds the ``IndentExportBlock`` option. +- Adds ``BinPackLongBracedLists`` option to override bin packing options in + long (20 item or more) braced list initializer lists. libclang -------- _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits