https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/112482
>From 8282f754ae40b64a88e2d22a6cb21e7028da8371 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/4] [clang-format] add BinPackLongBracedList 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 fbc9291ae950d41..476d9d4cf4ba1cb 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 21 + bool BinPackLongBracedLists; + /// Different way to try to fit all parameters on a line. enum BinPackParametersStyle : int8_t { /// Bin-pack parameters. @@ -2547,6 +2563,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 }; @@ -5266,6 +5285,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 387daad934f67ac..b9dd541a3dda2a6 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", @@ -1507,6 +1508,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 99bce1f5f09851e..239781129d7b96f 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -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.BinPackLongBracedLists)) { return; } diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index 0cb2a1288bfd7ec..1ebc1ac2faa817a 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(BreakBeforeTemplateCloser); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index a9fddc3275aed99..c0072ab93d07b1a 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -14419,6 +14419,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 0439c0031dae001cb5179f70a9739dc668b01970 Mon Sep 17 00:00:00 2001 From: Gedare Bloom <ged...@rtems.org> Date: Wed, 16 Oct 2024 00:01:54 -0600 Subject: [PATCH 2/4] 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 92f63c15030898f..03997395f56d84c 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -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 -------- >From bd6f9640401732548f60ffe328b310519bd4fabb Mon Sep 17 00:00:00 2001 From: Gedare Bloom <ged...@rtems.org> Date: Fri, 7 Feb 2025 21:23:36 -0700 Subject: [PATCH 3/4] updates from review --- clang/include/clang/Format/Format.h | 10 +++----- clang/lib/Format/Format.cpp | 4 +-- clang/lib/Format/FormatToken.cpp | 2 +- clang/unittests/Format/ConfigParseTest.cpp | 2 +- clang/unittests/Format/FormatTest.cpp | 29 ++++++++++++++++++---- 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 476d9d4cf4ba1cb..4a398ad97077a22 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1212,11 +1212,11 @@ struct FormatStyle { /// \version 3.7 bool BinPackArguments; - /// If ``BinPackLongBracedLists`` is ``true`` it overrides + /// If ``BinPackLongBracedList`` is ``true`` it overrides /// ``BinPackArguments`` if there are 20 or more items in a braced /// initializer list. /// \code - /// BinPackLongBracedLists: false vs. BinPackLongBracedLists: true + /// BinPackLongBracedList: false vs. BinPackLongBracedList: true /// vector<int> x{ vector<int> x{1, 2, ..., /// 20, 21}; /// 1, @@ -1226,7 +1226,7 @@ struct FormatStyle { /// 21}; /// \endcode /// \version 21 - bool BinPackLongBracedLists; + bool BinPackLongBracedList; /// Different way to try to fit all parameters on a line. enum BinPackParametersStyle : int8_t { @@ -2564,8 +2564,6 @@ struct FormatStyle { /// 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 }; @@ -5285,7 +5283,7 @@ struct FormatStyle { R.AlwaysBreakBeforeMultilineStrings && AttributeMacros == R.AttributeMacros && BinPackArguments == R.BinPackArguments && - BinPackLongBracedLists == R.BinPackLongBracedLists && + BinPackLongBracedList == R.BinPackLongBracedList && BinPackParameters == R.BinPackParameters && BitFieldColonSpacing == R.BitFieldColonSpacing && BracedInitializerIndentWidth == R.BracedInitializerIndentWidth && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index b9dd541a3dda2a6..0898b69528ebcde 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -995,7 +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("BinPackLongBracedList", Style.BinPackLongBracedList); IO.mapOptional("BinPackParameters", Style.BinPackParameters); IO.mapOptional("BitFieldColonSpacing", Style.BitFieldColonSpacing); IO.mapOptional("BracedInitializerIndentWidth", @@ -1508,7 +1508,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; LLVMStyle.AttributeMacros.push_back("__capability"); LLVMStyle.BinPackArguments = true; - LLVMStyle.BinPackLongBracedLists = true; + LLVMStyle.BinPackLongBracedList = 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 239781129d7b96f..fb040a004360200 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -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 || !Style.BinPackLongBracedLists)) { + (Commas.size() < 19 || !Style.BinPackLongBracedList)) { return; } diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index 1ebc1ac2faa817a..9cd262960b72468 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -168,7 +168,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) { CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); CHECK_PARSE_BOOL(AllowShortNamespacesOnASingleLine); CHECK_PARSE_BOOL(BinPackArguments); - CHECK_PARSE_BOOL(BinPackLongBracedLists); + CHECK_PARSE_BOOL(BinPackLongBracedList); CHECK_PARSE_BOOL(BreakAdjacentStringLiterals); CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); CHECK_PARSE_BOOL(BreakBeforeTemplateCloser); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index c0072ab93d07b1a..cea0dbd14b324c5 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -14405,7 +14405,7 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) { " ddddd,\n" " eeeee,\n" " ffffff,\n" - " ggggg,\n" + " gggggg,\n" " hhhhhh,\n" " iiiiii,\n" " jjjjjj,\n" @@ -14419,7 +14419,8 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) { " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" "};", NoBinPacking); - NoBinPacking.BinPackLongBracedLists = false; + + NoBinPacking.BinPackLongBracedList = false; verifyFormat("const Aaaaaa aaaaa = {\n" " aaaaa,\n" " bbbbb,\n" @@ -14440,11 +14441,29 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) { " ffffff,\n" " ggggg,\n" " hhhhhh,\n" - " iiiiii,\n" - " jjjjjj,\n" - " kkkkkk,\n" "};", NoBinPacking); + 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); NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; verifyFormat("static uint8 CddDp83848Reg[] = {\n" >From 0e49075997d4a495d43aa5daf0814086cef6e344 Mon Sep 17 00:00:00 2001 From: Gedare Bloom <ged...@rtems.org> Date: Wed, 16 Oct 2024 00:00:16 -0600 Subject: [PATCH 4/4] dump format style --- clang/docs/ClangFormatStyleOptions.rst | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index ce38a3a9ba1f739..bf48caed26bd961 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -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>` @@ -3804,6 +3822,7 @@ 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. + .. code-block:: c++ true: false: _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits