https://github.com/VolatileAcorn updated https://github.com/llvm/llvm-project/pull/101882
>From ddb5bad1cd1bed8781b9156cccce1058a9cc1347 Mon Sep 17 00:00:00 2001 From: Tom Pottage <pottage...@gmail.com> Date: Fri, 2 Aug 2024 20:26:47 +0100 Subject: [PATCH 1/5] [clang-format] Change BinPackParameters to an enum and introduce a OnePerLine value --- clang/docs/ClangFormatStyleOptions.rst | 51 +++++-- clang/include/clang/Format/Format.h | 50 ++++--- clang/lib/Format/ContinuationIndenter.cpp | 27 +--- clang/lib/Format/Format.cpp | 19 ++- clang/lib/Format/FormatToken.h | 19 +++ clang/lib/Format/TokenAnnotator.cpp | 8 ++ clang/unittests/Format/ConfigParseTest.cpp | 14 +- clang/unittests/Format/FormatTest.cpp | 131 +++++++++++++++--- clang/unittests/Format/FormatTestComments.cpp | 30 +++- clang/unittests/Format/FormatTestObjC.cpp | 4 +- 10 files changed, 271 insertions(+), 82 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 72e1bd19b6b520..7e3ee5da6c4ab2 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -1617,7 +1617,7 @@ the configuration (without a prefix: ``Auto``). **AllowAllParametersOfDeclarationOnNextLine** (``Boolean``) :versionbadge:`clang-format 3.3` :ref:`¶ <AllowAllParametersOfDeclarationOnNextLine>` If the function declaration doesn't fit on a line, allow putting all parameters of a function declaration onto - the next line even if ``BinPackParameters`` is ``false``. + the next line even if ``BinPackParameters`` is ``Never``. .. code-block:: c++ @@ -2067,20 +2067,41 @@ the configuration (without a prefix: ``Auto``). .. _BinPackParameters: -**BinPackParameters** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ <BinPackParameters>` - If ``false``, a function declaration's or function definition's - parameters will either all be on the same line or will have one line each. +**BinPackParameters** (``BinPackParametersStyle``) :versionbadge:`clang-format 3.7` :ref:`¶ <BinPackParameters>` + The bin pack parameters style to use. - .. code-block:: c++ + Possible values: + + * ``BPPS_Never`` (in configuration: ``Never``) + Put all parameters on the current line if they fit. + Otherwise, put each one on its own line. + + .. code-block:: c++ + + void f(int a, int b, int c); + + void f(int a, + int b, + int ccccccccccccccccccccccccccccccccccccc); + + * ``BPPS_Always`` (in configuration: ``Always``) + Bin-pack parameters. + + .. code-block:: c++ + + void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + int ccccccccccccccccccccccccccccccccccccccccccc); + + * ``BPPS_OnePerLine`` (in configuration: ``OnePerLine``) + Always put each parameter on its own line. + + .. code-block:: c++ + + void f(int a, + int b, + int c); - true: - void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa, - int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} - false: - void f(int aaaaaaaaaaaaaaaaaaaa, - int aaaaaaaaaaaaaaaaaaaa, - int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} .. _BitFieldColonSpacing: @@ -4816,7 +4837,7 @@ the configuration (without a prefix: ``Auto``). items into as few lines as possible when they go over ``ColumnLimit``. If ``Auto`` (the default), delegates to the value in - ``BinPackParameters``. If that is ``true``, bin-packs Objective-C + ``BinPackParameters``. If that is ``Always``, bin-packs Objective-C protocol conformance list items into as few lines as possible whenever they go over ``ColumnLimit``. @@ -4830,13 +4851,13 @@ the configuration (without a prefix: ``Auto``). .. code-block:: objc - Always (or Auto, if BinPackParameters=true): + Always (or Auto, if BinPackParameters==Always): @interface ccccccccccccc () < ccccccccccccc, ccccccccccccc, ccccccccccccc, ccccccccccccc> { } - Never (or Auto, if BinPackParameters=false): + Never (or Auto, if BinPackParameters!=Always): @interface ddddddddddddd () < ddddddddddddd, ddddddddddddd, diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index ef6c76a070bfaa..16ae74ec3aca65 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -659,7 +659,7 @@ struct FormatStyle { /// If the function declaration doesn't fit on a line, /// allow putting all parameters of a function declaration onto - /// the next line even if ``BinPackParameters`` is ``false``. + /// the next line even if ``BinPackParameters`` is ``Never``. /// \code /// true: /// void myFunction( @@ -1192,20 +1192,36 @@ struct FormatStyle { /// \version 3.7 bool BinPackArguments; - /// If ``false``, a function declaration's or function definition's - /// parameters will either all be on the same line or will have one line each. - /// \code - /// true: - /// void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa, - /// int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} - /// - /// false: - /// void f(int aaaaaaaaaaaaaaaaaaaa, - /// int aaaaaaaaaaaaaaaaaaaa, - /// int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} - /// \endcode + /// Different way to try to fit all parameters on a line. + enum BinPackParametersStyle : int8_t { + /// Put all parameters on the current line if they fit. + /// Otherwise, put each one on its own line. + /// \code + /// void f(int a, int b, int c); + /// + /// void f(int a, + /// int b, + /// int ccccccccccccccccccccccccccccccccccccc); + /// \endcode + BPPS_Never, + /// Bin-pack parameters. + /// \code + /// void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + /// int ccccccccccccccccccccccccccccccccccccccccccc); + /// \endcode + BPPS_Always, + /// Always put each parameter on its own line. + /// \code + /// void f(int a, + /// int b, + /// int c); + /// \endcode + BPPS_OnePerLine, + }; + + /// The bin pack parameters style to use. /// \version 3.7 - bool BinPackParameters; + BinPackParametersStyle BinPackParameters; /// Styles for adding spacing around ``:`` in bitfield definitions. enum BitFieldColonSpacingStyle : int8_t { @@ -3413,7 +3429,7 @@ struct FormatStyle { /// items into as few lines as possible when they go over ``ColumnLimit``. /// /// If ``Auto`` (the default), delegates to the value in - /// ``BinPackParameters``. If that is ``true``, bin-packs Objective-C + /// ``BinPackParameters``. If that is ``Always``, bin-packs Objective-C /// protocol conformance list items into as few lines as possible /// whenever they go over ``ColumnLimit``. /// @@ -3425,13 +3441,13 @@ struct FormatStyle { /// onto individual lines whenever they go over ``ColumnLimit``. /// /// \code{.objc} - /// Always (or Auto, if BinPackParameters=true): + /// Always (or Auto, if BinPackParameters==Always): /// @interface ccccccccccccc () < /// ccccccccccccc, ccccccccccccc, /// ccccccccccccc, ccccccccccccc> { /// } /// - /// Never (or Auto, if BinPackParameters=false): + /// Never (or Auto, if BinPackParameters!=Always): /// @interface ddddddddddddd () < /// ddddddddddddd, /// ddddddddddddd, diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 43d246b7f82419..60172f2a8b6cbb 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -128,25 +128,6 @@ static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) { return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope(); } -// Returns \c true if \c Current starts a new parameter. -static bool startsNextParameter(const FormatToken &Current, - const FormatStyle &Style) { - assert(Current.Previous); - const auto &Previous = *Current.Previous; - if (Current.is(TT_CtorInitializerComma) && - Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) { - return true; - } - if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName)) - return true; - return Previous.is(tok::comma) && !Current.isTrailingComment() && - ((Previous.isNot(TT_CtorInitializerComma) || - Style.BreakConstructorInitializers != - FormatStyle::BCIS_BeforeComma) && - (Previous.isNot(TT_InheritanceComma) || - Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma)); -} - // Returns \c true if \c Token in an alignable binary operator static bool isAlignableBinaryOperator(const FormatToken &Token) { // No need to align binary operators that only have two operands. @@ -437,7 +418,8 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { // sets BreakBeforeParameter to avoid bin packing and this creates a // completely unnecessary line break after a template type that isn't // line-wrapped. - (Previous.NestingLevel == 1 || Style.BinPackParameters)) || + (Previous.NestingLevel == 1 || + Style.BinPackParameters == FormatStyle::BPPS_Always)) || (Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) && Previous.isNot(tok::question)) || (!Style.BreakBeforeTernaryOperators && @@ -1950,11 +1932,12 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, // for backwards compatibility. bool ObjCBinPackProtocolList = (Style.ObjCBinPackProtocolList == FormatStyle::BPS_Auto && - Style.BinPackParameters) || + Style.BinPackParameters == FormatStyle::BPPS_Always) || Style.ObjCBinPackProtocolList == FormatStyle::BPS_Always; bool BinPackDeclaration = - (State.Line->Type != LT_ObjCDecl && Style.BinPackParameters) || + (State.Line->Type != LT_ObjCDecl && + Style.BinPackParameters == FormatStyle::BPPS_Always) || (State.Line->Type == LT_ObjCDecl && ObjCBinPackProtocolList); bool GenericSelection = diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 5358b35c19de25..4a25abab9aa9f4 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -134,6 +134,19 @@ template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> { } }; +template <> +struct ScalarEnumerationTraits<FormatStyle::BinPackParametersStyle> { + static void enumeration(IO &IO, FormatStyle::BinPackParametersStyle &Value) { + IO.enumCase(Value, "Never", FormatStyle::BPPS_Never); + IO.enumCase(Value, "Always", FormatStyle::BPPS_Always); + IO.enumCase(Value, "OnePerLine", FormatStyle::BPPS_OnePerLine); + + // For backward compatibility. + IO.enumCase(Value, "true", FormatStyle::BPPS_Always); + IO.enumCase(Value, "false", FormatStyle::BPPS_Never); + } +}; + template <> struct ScalarEnumerationTraits<FormatStyle::BinPackStyle> { static void enumeration(IO &IO, FormatStyle::BinPackStyle &Value) { IO.enumCase(Value, "Auto", FormatStyle::BPS_Auto); @@ -1460,7 +1473,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; LLVMStyle.AttributeMacros.push_back("__capability"); LLVMStyle.BinPackArguments = true; - LLVMStyle.BinPackParameters = true; + LLVMStyle.BinPackParameters = FormatStyle::BPPS_Always; LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both; LLVMStyle.BracedInitializerIndentWidth = std::nullopt; LLVMStyle.BraceWrapping = {/*AfterCaseLabel=*/false, @@ -1835,7 +1848,7 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; ChromiumStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never; ChromiumStyle.AllowShortLoopsOnASingleLine = false; - ChromiumStyle.BinPackParameters = false; + ChromiumStyle.BinPackParameters = FormatStyle::BPPS_Never; ChromiumStyle.DerivePointerAlignment = false; if (Language == FormatStyle::LK_ObjC) ChromiumStyle.ColumnLimit = 80; @@ -1850,7 +1863,7 @@ FormatStyle getMozillaStyle() { MozillaStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_TopLevel; MozillaStyle.BinPackArguments = false; - MozillaStyle.BinPackParameters = false; + MozillaStyle.BinPackParameters = FormatStyle::BPPS_Never; MozillaStyle.BreakAfterReturnType = FormatStyle::RTBS_TopLevel; MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; MozillaStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index abcedb66b57cc6..06d0cedb2cd1c9 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -1978,6 +1978,25 @@ inline bool continuesLineComment(const FormatToken &FormatTok, FormatTok.OriginalColumn >= MinContinueColumn; } +// Returns \c true if \c Current starts a new parameter. +static bool startsNextParameter(const FormatToken &Current, + const FormatStyle &Style) { + assert(Current.Previous); + const auto &Previous = *Current.Previous; + if (Current.is(TT_CtorInitializerComma) && + Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) { + return true; + } + if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName)) + return true; + return Previous.is(tok::comma) && !Current.isTrailingComment() && + ((Previous.isNot(TT_CtorInitializerComma) || + Style.BreakConstructorInitializers != + FormatStyle::BCIS_BeforeComma) && + (Previous.isNot(TT_InheritanceComma) || + Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma)); +} + } // namespace format } // namespace clang diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 9f79fa9fc516ca..0788bd385cea6a 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -5475,6 +5475,14 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, return true; } + // Ignores the first parameter as this will be handled separately by + // BreakFunctionDefinitionParameters or AlignAfterOpenBracket. + if (FormatStyle::BPPS_OnePerLine == Style.BinPackParameters && + Line.MightBeFunctionDecl && !Left.opensScope() && + startsNextParameter(Right, Style)) { + return true; + } + const auto *BeforeLeft = Left.Previous; const auto *AfterRight = Right.Next; diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index 2ee0df99353ff5..7d3b4cd43dd26c 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -160,7 +160,6 @@ TEST(ConfigParseTest, ParsesConfigurationBools) { CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); CHECK_PARSE_BOOL(BinPackArguments); - CHECK_PARSE_BOOL(BinPackParameters); CHECK_PARSE_BOOL(BreakAdjacentStringLiterals); CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); @@ -436,6 +435,19 @@ TEST(ConfigParseTest, ParsesConfiguration) { CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, FormatStyle::BILS_BeforeComma); + Style.BinPackParameters = FormatStyle::BPPS_Always; + CHECK_PARSE("BinPackParameters: Never", BinPackParameters, + FormatStyle::BPPS_Never); + CHECK_PARSE("BinPackParameters: Always", BinPackParameters, + FormatStyle::BPPS_Always); + CHECK_PARSE("BinPackParameters: OnePerLine", BinPackParameters, + FormatStyle::BPPS_OnePerLine); + // For backward compatibility. + CHECK_PARSE("BinPackParameters: true", BinPackParameters, + FormatStyle::BPPS_Always); + CHECK_PARSE("BinPackParameters: false", BinPackParameters, + FormatStyle::BPPS_Never); + Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers, FormatStyle::PCIS_Never); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index bad1b1d662d133..5ed69d9a0de03c 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2338,7 +2338,7 @@ TEST_F(FormatTest, FormatsForLoop) { "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); FormatStyle NoBinPacking = getLLVMStyle(); - NoBinPacking.BinPackParameters = false; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; verifyFormat("for (int aaaaaaaaaaa = 1;\n" " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaa,\n" @@ -7165,7 +7165,7 @@ TEST_F(FormatTest, LineBreakingInBinaryExpressions) { "}"); FormatStyle OnePerLine = getLLVMStyle(); - OnePerLine.BinPackParameters = false; + OnePerLine.BinPackParameters = FormatStyle::BPPS_Never; verifyFormat( "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" @@ -7319,7 +7319,7 @@ TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { Style = getLLVMStyleWithColumns(20); Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; - Style.BinPackParameters = false; + Style.BinPackParameters = FormatStyle::BPPS_Never; Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; Style.ContinuationIndentWidth = 2; verifyFormat("struct Foo {\n" @@ -7694,7 +7694,7 @@ TEST_F(FormatTest, ConstructorInitializers) { " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaa) {}", OnePerLine); - OnePerLine.BinPackParameters = false; + OnePerLine.BinPackParameters = FormatStyle::BPPS_Never; verifyFormat( "Constructor()\n" " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" @@ -7718,7 +7718,7 @@ TEST_F(FormatTest, ConstructorInitializers) { TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { FormatStyle Style = getLLVMStyleWithColumns(60); Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; - Style.BinPackParameters = false; + Style.BinPackParameters = FormatStyle::BPPS_Never; for (int i = 0; i < 4; ++i) { // Test all combinations of parameters that should not have an effect. @@ -7954,7 +7954,7 @@ TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { } // This parameter should not affect declarations. - Style.BinPackParameters = false; + Style.BinPackParameters = FormatStyle::BPPS_Never; Style.AllowAllArgumentsOnNextLine = false; Style.AllowAllParametersOfDeclarationOnNextLine = true; verifyFormat("void FunctionCallWithReallyLongName(\n" @@ -8049,7 +8049,7 @@ TEST_F(FormatTest, BreakFunctionDefinitionParameters) { // Test the style where all parameters are on their own lines. Style.AllowAllParametersOfDeclarationOnNextLine = false; - Style.BinPackParameters = false; + Style.BinPackParameters = FormatStyle::BPPS_Never; verifyFormat("void functionDecl(paramA, paramB, paramC);\n" "void emptyFunctionDefinition() {}\n" "void functionDefinition(\n" @@ -8244,7 +8244,7 @@ TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaa) {}", OnePerLine); - OnePerLine.BinPackParameters = false; + OnePerLine.BinPackParameters = FormatStyle::BPPS_Never; verifyFormat("Constructor() :\n" " aaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaa().aaa(),\n" @@ -8409,7 +8409,7 @@ TEST_F(FormatTest, MemoizationTests) { // This test takes VERY long when memoization is broken. FormatStyle OnePerLine = getLLVMStyle(); OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; - OnePerLine.BinPackParameters = false; + OnePerLine.BinPackParameters = FormatStyle::BPPS_Never; std::string input = "Constructor()\n" " : aaaa(a,\n"; for (unsigned i = 0, e = 80; i != e; ++i) @@ -8830,7 +8830,7 @@ TEST_F(FormatTest, BreaksDesireably) { TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { FormatStyle NoBinPacking = getGoogleStyle(); - NoBinPacking.BinPackParameters = false; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; NoBinPacking.BinPackArguments = true; verifyFormat("void f() {\n" " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" @@ -8862,7 +8862,7 @@ TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { FormatStyle NoBinPacking = getGoogleStyle(); - NoBinPacking.BinPackParameters = false; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; NoBinPacking.BinPackArguments = false; verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaa,\n" @@ -8925,6 +8925,97 @@ TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { NoBinPacking); } +TEST_F(FormatTest, FormatsDeclarationBreakAlways) { + FormatStyle BreakAlways = getGoogleStyle(); + BreakAlways.BinPackParameters = FormatStyle::BPPS_OnePerLine; + verifyFormat("void f(int a,\n" + " int b);", + BreakAlways); + verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n" + " int cccccccccccccccccccccccc);", + BreakAlways); + + // Ensure AlignAFterOpenBracket interacts correctly with + // PackParameters set to BreakAlways. + BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; + verifyFormat( + "void someLongFunctionName(\n" + " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " int b);", + BreakAlways); + BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; + verifyFormat( + "void someLongFunctionName(\n" + " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " int b\n" + ");", + BreakAlways); +} + +TEST_F(FormatTest, FormatsDefinitionBreakAlways) { + FormatStyle BreakAlways = getGoogleStyle(); + BreakAlways.BinPackParameters = FormatStyle::BPPS_OnePerLine; + verifyFormat("void f(int a,\n" + " int b) {\n" + " f(a, b);\n" + "}", + BreakAlways); + + // Ensure BinPackArguments interact correctly when BinPackParameters is set to + // BreakAlways. + verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n" + " int cccccccccccccccccccccccc) {\n" + " f(aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbb,\n" + " cccccccccccccccccccccccc);\n" + "}", + BreakAlways); + BreakAlways.BinPackArguments = false; + verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n" + " int cccccccccccccccccccccccc) {\n" + " f(aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " bbbbbbbbbbbbbbbbbbbbbbbbb,\n" + " cccccccccccccccccccccccc);\n" + "}", + BreakAlways); + + // Ensure BreakFunctionDefinitionParameters interacts correctly when + // BinPackParameters is set to BreakAlways + BreakAlways.BreakFunctionDefinitionParameters = true; + verifyFormat("void f(\n" + " int a,\n" + " int b) {\n" + " f(a, b);\n" + "}", + BreakAlways); + BreakAlways.BreakFunctionDefinitionParameters = false; + + // Ensure AlignAFterOpenBracket interacts correctly with + // BinPackParameters set to BreakAlways. + BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; + verifyFormat( + "void someLongFunctionName(\n" + " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " int b) {\n" + " someLongFunctionName(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b);\n" + "}", + BreakAlways); + BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; + verifyFormat( + "void someLongFunctionName(\n" + " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " int b\n" + ") {\n" + " someLongFunctionName(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b\n" + " );\n" + "}", + BreakAlways); +} + TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { FormatStyle Style = getLLVMStyleWithColumns(15); Style.ExperimentalAutoDetectBinPacking = true; @@ -9256,7 +9347,7 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) { Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; Style.BinPackArguments = false; - Style.BinPackParameters = false; + Style.BinPackParameters = FormatStyle::BPPS_Never; verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaa aaaaaaaa,\n" " aaaaaaaaa aaaaaaa,\n" @@ -9295,7 +9386,7 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) { Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; Style.BinPackArguments = false; - Style.BinPackParameters = false; + Style.BinPackParameters = FormatStyle::BPPS_Never; verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaa aaaaaaaa,\n" " aaaaaaaaa aaaaaaa,\n" @@ -10706,7 +10797,7 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { " .a();"); FormatStyle NoBinPacking = getLLVMStyle(); - NoBinPacking.BinPackParameters = false; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" @@ -13618,7 +13709,7 @@ TEST_F(FormatTest, HandlesIncludeDirectives) { TEST_F(FormatTest, IncompleteParameterLists) { FormatStyle NoBinPacking = getLLVMStyle(); - NoBinPacking.BinPackParameters = false; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" " double *min_x,\n" " double *max_x,\n" @@ -14284,7 +14375,7 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { " [](const Input &i) -> Output { return " "Output{1, 2}; });"); FormatStyle NoBinPacking = getLLVMStyle(); - NoBinPacking.BinPackParameters = false; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; verifyFormat("waarudo::unit desk = {\n" " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, " "1, 1} * w::m; }};", @@ -19789,7 +19880,7 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { Alignment.AlignConsecutiveAssignments.Enabled = false; Alignment.ColumnLimit = 30; - Alignment.BinPackParameters = false; + Alignment.BinPackParameters = FormatStyle::BPPS_Never; verifyFormat("void foo(float a,\n" " float b,\n" " int c,\n" @@ -19803,7 +19894,7 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { " uint32_t *c,\n" " bool d) {}", Alignment); - Alignment.BinPackParameters = true; + Alignment.BinPackParameters = FormatStyle::BPPS_Always; Alignment.ColumnLimit = 80; // Bug 33507 @@ -23229,7 +23320,7 @@ TEST_F(FormatTest, FormatsLambdas) { " LambdaBodyMustBeBreak);\n" "};", LLVMWithBeforeLambdaBody); - LLVMWithBeforeLambdaBody.BinPackParameters = false; + LLVMWithBeforeLambdaBody.BinPackParameters = FormatStyle::BPPS_Never; verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", LLVMWithBeforeLambdaBody); verifyFormat( @@ -26626,7 +26717,7 @@ TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) { Medium, Style); Style.BinPackArguments = false; - Style.BinPackParameters = false; + Style.BinPackParameters = FormatStyle::BPPS_Never; verifyFormat(Short, Style); diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp index 8f84d59cbb2e2c..4dc3535339f335 100644 --- a/clang/unittests/Format/FormatTestComments.cpp +++ b/clang/unittests/Format/FormatTestComments.cpp @@ -362,6 +362,26 @@ TEST_F(FormatTestComments, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { format("aaaaaaaaaa(aaaa(aaaa,\n" "aaaa), //\n" "aaaa, bbbbb);")); + + FormatStyle BreakAlways = getLLVMStyle(); + BreakAlways.BinPackParameters = FormatStyle::BPPS_OnePerLine; + EXPECT_EQ("SomeFunction(a,\n" + " b, // comment\n" + " c,\n" + " d);", + format("SomeFunction(a,\n" + " b, // comment\n" + " c, d);", + BreakAlways)); + EXPECT_EQ("SomeFunction(a,\n" + " b,\n" + " // comment\n" + " c);", + format("SomeFunction(a,\n" + " b,\n" + " // comment\n" + " c);", + BreakAlways)); } TEST_F(FormatTestComments, RemovesTrailingWhitespaceOfComments) { @@ -404,7 +424,13 @@ TEST_F(FormatTestComments, UnderstandsBlockComments) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); FormatStyle NoBinPacking = getLLVMStyle(); - NoBinPacking.BinPackParameters = false; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; + verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" + " /* parameter 2 */ aaaaaa,\n" + " /* parameter 3 */ aaaaaa,\n" + " /* parameter 4 */ aaaaaa);", + NoBinPacking); + NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" " /* parameter 2 */ aaaaaa,\n" " /* parameter 3 */ aaaaaa,\n" @@ -2449,7 +2475,7 @@ TEST_F(FormatTestComments, BlockComments) { getLLVMStyleWithColumns(50))); FormatStyle NoBinPacking = getLLVMStyle(); - NoBinPacking.BinPackParameters = false; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; EXPECT_EQ("someFunction(1, /* comment 1 */\n" " 2, /* comment 2 */\n" " 3, /* comment 3 */\n" diff --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp index d2c3459e0f846d..ce9258a2d35b13 100644 --- a/clang/unittests/Format/FormatTestObjC.cpp +++ b/clang/unittests/Format/FormatTestObjC.cpp @@ -377,7 +377,7 @@ TEST_F(FormatTestObjC, FormatObjCInterface) { " ddddddddddddd> {\n" "}"); - Style.BinPackParameters = false; + Style.BinPackParameters = FormatStyle::BPPS_Never; Style.ObjCBinPackProtocolList = FormatStyle::BPS_Auto; verifyFormat("@interface eeeeeeeeeeeee () <\n" " eeeeeeeeeeeee,\n" @@ -411,7 +411,7 @@ TEST_F(FormatTestObjC, FormatObjCInterface) { "+ (id)init;\n" "@end"); Style.ColumnLimit = 40; - // BinPackParameters should be true by default. + // BinPackParameters should be BPPS_Always by default. verifyFormat("void eeeeeeee(int eeeee, int eeeee,\n" " int eeeee, int eeeee);"); // ObjCBinPackProtocolList should be BPS_Never by default. >From 73d9666eab62057be3f16163bb94ebf1a440b05d Mon Sep 17 00:00:00 2001 From: Tom Pottage <pottage...@gmail.com> Date: Mon, 19 Aug 2024 18:40:03 +0100 Subject: [PATCH 2/5] Fix warning from static function defintion in header --- clang/lib/Format/FormatToken.cpp | 17 +++++++++++++++++ clang/lib/Format/FormatToken.h | 18 +----------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index 85bec71ffbbc8a..963e8f87793fa0 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -322,5 +322,22 @@ CommaSeparatedList::getColumnFormat(unsigned RemainingCharacters) const { return BestFormat; } +bool startsNextParameter(const FormatToken &Current, const FormatStyle &Style) { + assert(Current.Previous); + const auto &Previous = *Current.Previous; + if (Current.is(TT_CtorInitializerComma) && + Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) { + return true; + } + if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName)) + return true; + return Previous.is(tok::comma) && !Current.isTrailingComment() && + ((Previous.isNot(TT_CtorInitializerComma) || + Style.BreakConstructorInitializers != + FormatStyle::BCIS_BeforeComma) && + (Previous.isNot(TT_InheritanceComma) || + Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma)); +} + } // namespace format } // namespace clang diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index 06d0cedb2cd1c9..2d386d99aa841a 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -1979,23 +1979,7 @@ inline bool continuesLineComment(const FormatToken &FormatTok, } // Returns \c true if \c Current starts a new parameter. -static bool startsNextParameter(const FormatToken &Current, - const FormatStyle &Style) { - assert(Current.Previous); - const auto &Previous = *Current.Previous; - if (Current.is(TT_CtorInitializerComma) && - Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) { - return true; - } - if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName)) - return true; - return Previous.is(tok::comma) && !Current.isTrailingComment() && - ((Previous.isNot(TT_CtorInitializerComma) || - Style.BreakConstructorInitializers != - FormatStyle::BCIS_BeforeComma) && - (Previous.isNot(TT_InheritanceComma) || - Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma)); -} +bool startsNextParameter(const FormatToken &Current, const FormatStyle &Style); } // namespace format } // namespace clang >From abf48552a8381f85829ce8776f17867803d8daae Mon Sep 17 00:00:00 2001 From: Tom Pottage <pottage...@gmail.com> Date: Mon, 19 Aug 2024 19:54:57 +0100 Subject: [PATCH 3/5] Rename the BinPackParameterStyle enum options and improve comment related tests to actually test parameters and not arguments. --- clang/docs/ClangFormatStyleOptions.rst | 14 ++--- clang/include/clang/Format/Format.h | 14 ++--- clang/lib/Format/ContinuationIndenter.cpp | 6 +- clang/lib/Format/Format.cpp | 14 ++--- clang/lib/Format/TokenAnnotator.cpp | 2 +- clang/unittests/Format/ConfigParseTest.cpp | 14 ++--- clang/unittests/Format/FormatTest.cpp | 56 ++++++++--------- clang/unittests/Format/FormatTestComments.cpp | 63 +++++++++---------- clang/unittests/Format/FormatTestObjC.cpp | 4 +- 9 files changed, 93 insertions(+), 94 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 7e3ee5da6c4ab2..b7bff21ebbe88a 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -1617,7 +1617,7 @@ the configuration (without a prefix: ``Auto``). **AllowAllParametersOfDeclarationOnNextLine** (``Boolean``) :versionbadge:`clang-format 3.3` :ref:`¶ <AllowAllParametersOfDeclarationOnNextLine>` If the function declaration doesn't fit on a line, allow putting all parameters of a function declaration onto - the next line even if ``BinPackParameters`` is ``Never``. + the next line even if ``BinPackParameters`` is ``OnePerLine``. .. code-block:: c++ @@ -2072,7 +2072,7 @@ the configuration (without a prefix: ``Auto``). Possible values: - * ``BPPS_Never`` (in configuration: ``Never``) + * ``BPPS_OnePerLine`` (in configuration: ``OnePerLine``) Put all parameters on the current line if they fit. Otherwise, put each one on its own line. @@ -2084,7 +2084,7 @@ the configuration (without a prefix: ``Auto``). int b, int ccccccccccccccccccccccccccccccccccccc); - * ``BPPS_Always`` (in configuration: ``Always``) + * ``BPPS_BinPack`` (in configuration: ``BinPack``) Bin-pack parameters. .. code-block:: c++ @@ -2092,7 +2092,7 @@ the configuration (without a prefix: ``Auto``). void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, int ccccccccccccccccccccccccccccccccccccccccccc); - * ``BPPS_OnePerLine`` (in configuration: ``OnePerLine``) + * ``BPPS_AlwaysOnePerLine`` (in configuration: ``AlwaysOnePerLine``) Always put each parameter on its own line. .. code-block:: c++ @@ -4837,7 +4837,7 @@ the configuration (without a prefix: ``Auto``). items into as few lines as possible when they go over ``ColumnLimit``. If ``Auto`` (the default), delegates to the value in - ``BinPackParameters``. If that is ``Always``, bin-packs Objective-C + ``BinPackParameters``. If that is ``BinPack``, bin-packs Objective-C protocol conformance list items into as few lines as possible whenever they go over ``ColumnLimit``. @@ -4851,13 +4851,13 @@ the configuration (without a prefix: ``Auto``). .. code-block:: objc - Always (or Auto, if BinPackParameters==Always): + Always (or Auto, if BinPackParameters==BinPack): @interface ccccccccccccc () < ccccccccccccc, ccccccccccccc, ccccccccccccc, ccccccccccccc> { } - Never (or Auto, if BinPackParameters!=Always): + Never (or Auto, if BinPackParameters!=BinPack): @interface ddddddddddddd () < ddddddddddddd, ddddddddddddd, diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 16ae74ec3aca65..1fe6b13e77727c 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -659,7 +659,7 @@ struct FormatStyle { /// If the function declaration doesn't fit on a line, /// allow putting all parameters of a function declaration onto - /// the next line even if ``BinPackParameters`` is ``Never``. + /// the next line even if ``BinPackParameters`` is ``OnePerLine``. /// \code /// true: /// void myFunction( @@ -1203,20 +1203,20 @@ struct FormatStyle { /// int b, /// int ccccccccccccccccccccccccccccccccccccc); /// \endcode - BPPS_Never, + BPPS_OnePerLine, /// Bin-pack parameters. /// \code /// void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, /// int ccccccccccccccccccccccccccccccccccccccccccc); /// \endcode - BPPS_Always, + BPPS_BinPack, /// Always put each parameter on its own line. /// \code /// void f(int a, /// int b, /// int c); /// \endcode - BPPS_OnePerLine, + BPPS_AlwaysOnePerLine, }; /// The bin pack parameters style to use. @@ -3429,7 +3429,7 @@ struct FormatStyle { /// items into as few lines as possible when they go over ``ColumnLimit``. /// /// If ``Auto`` (the default), delegates to the value in - /// ``BinPackParameters``. If that is ``Always``, bin-packs Objective-C + /// ``BinPackParameters``. If that is ``BinPack``, bin-packs Objective-C /// protocol conformance list items into as few lines as possible /// whenever they go over ``ColumnLimit``. /// @@ -3441,13 +3441,13 @@ struct FormatStyle { /// onto individual lines whenever they go over ``ColumnLimit``. /// /// \code{.objc} - /// Always (or Auto, if BinPackParameters==Always): + /// Always (or Auto, if BinPackParameters==BinPack): /// @interface ccccccccccccc () < /// ccccccccccccc, ccccccccccccc, /// ccccccccccccc, ccccccccccccc> { /// } /// - /// Never (or Auto, if BinPackParameters!=Always): + /// Never (or Auto, if BinPackParameters!=BinPack): /// @interface ddddddddddddd () < /// ddddddddddddd, /// ddddddddddddd, diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 60172f2a8b6cbb..cb31fad43d8f06 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -419,7 +419,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { // completely unnecessary line break after a template type that isn't // line-wrapped. (Previous.NestingLevel == 1 || - Style.BinPackParameters == FormatStyle::BPPS_Always)) || + Style.BinPackParameters == FormatStyle::BPPS_BinPack)) || (Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) && Previous.isNot(tok::question)) || (!Style.BreakBeforeTernaryOperators && @@ -1932,12 +1932,12 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, // for backwards compatibility. bool ObjCBinPackProtocolList = (Style.ObjCBinPackProtocolList == FormatStyle::BPS_Auto && - Style.BinPackParameters == FormatStyle::BPPS_Always) || + Style.BinPackParameters == FormatStyle::BPPS_BinPack) || Style.ObjCBinPackProtocolList == FormatStyle::BPS_Always; bool BinPackDeclaration = (State.Line->Type != LT_ObjCDecl && - Style.BinPackParameters == FormatStyle::BPPS_Always) || + Style.BinPackParameters == FormatStyle::BPPS_BinPack) || (State.Line->Type == LT_ObjCDecl && ObjCBinPackProtocolList); bool GenericSelection = diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 4a25abab9aa9f4..e1aaf634187b17 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -137,13 +137,13 @@ template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> { template <> struct ScalarEnumerationTraits<FormatStyle::BinPackParametersStyle> { static void enumeration(IO &IO, FormatStyle::BinPackParametersStyle &Value) { - IO.enumCase(Value, "Never", FormatStyle::BPPS_Never); - IO.enumCase(Value, "Always", FormatStyle::BPPS_Always); IO.enumCase(Value, "OnePerLine", FormatStyle::BPPS_OnePerLine); + IO.enumCase(Value, "BinPack", FormatStyle::BPPS_BinPack); + IO.enumCase(Value, "AlwaysOnePerLine", FormatStyle::BPPS_AlwaysOnePerLine); // For backward compatibility. - IO.enumCase(Value, "true", FormatStyle::BPPS_Always); - IO.enumCase(Value, "false", FormatStyle::BPPS_Never); + IO.enumCase(Value, "true", FormatStyle::BPPS_BinPack); + IO.enumCase(Value, "false", FormatStyle::BPPS_OnePerLine); } }; @@ -1473,7 +1473,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; LLVMStyle.AttributeMacros.push_back("__capability"); LLVMStyle.BinPackArguments = true; - LLVMStyle.BinPackParameters = FormatStyle::BPPS_Always; + LLVMStyle.BinPackParameters = FormatStyle::BPPS_BinPack; LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both; LLVMStyle.BracedInitializerIndentWidth = std::nullopt; LLVMStyle.BraceWrapping = {/*AfterCaseLabel=*/false, @@ -1848,7 +1848,7 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; ChromiumStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never; ChromiumStyle.AllowShortLoopsOnASingleLine = false; - ChromiumStyle.BinPackParameters = FormatStyle::BPPS_Never; + ChromiumStyle.BinPackParameters = FormatStyle::BPPS_OnePerLine; ChromiumStyle.DerivePointerAlignment = false; if (Language == FormatStyle::LK_ObjC) ChromiumStyle.ColumnLimit = 80; @@ -1863,7 +1863,7 @@ FormatStyle getMozillaStyle() { MozillaStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_TopLevel; MozillaStyle.BinPackArguments = false; - MozillaStyle.BinPackParameters = FormatStyle::BPPS_Never; + MozillaStyle.BinPackParameters = FormatStyle::BPPS_OnePerLine; MozillaStyle.BreakAfterReturnType = FormatStyle::RTBS_TopLevel; MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; MozillaStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 0788bd385cea6a..2a092d89f1bab6 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -5477,7 +5477,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, // Ignores the first parameter as this will be handled separately by // BreakFunctionDefinitionParameters or AlignAfterOpenBracket. - if (FormatStyle::BPPS_OnePerLine == Style.BinPackParameters && + if (FormatStyle::BPPS_AlwaysOnePerLine == Style.BinPackParameters && Line.MightBeFunctionDecl && !Left.opensScope() && startsNextParameter(Right, Style)) { return true; diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index 7d3b4cd43dd26c..7218885b7dbb42 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -435,18 +435,18 @@ TEST(ConfigParseTest, ParsesConfiguration) { CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, FormatStyle::BILS_BeforeComma); - Style.BinPackParameters = FormatStyle::BPPS_Always; - CHECK_PARSE("BinPackParameters: Never", BinPackParameters, - FormatStyle::BPPS_Never); - CHECK_PARSE("BinPackParameters: Always", BinPackParameters, - FormatStyle::BPPS_Always); + Style.BinPackParameters = FormatStyle::BPPS_BinPack; CHECK_PARSE("BinPackParameters: OnePerLine", BinPackParameters, FormatStyle::BPPS_OnePerLine); + CHECK_PARSE("BinPackParameters: BinPack", BinPackParameters, + FormatStyle::BPPS_BinPack); + CHECK_PARSE("BinPackParameters: AlwaysOnePerLine", BinPackParameters, + FormatStyle::BPPS_AlwaysOnePerLine); // For backward compatibility. CHECK_PARSE("BinPackParameters: true", BinPackParameters, - FormatStyle::BPPS_Always); + FormatStyle::BPPS_BinPack); CHECK_PARSE("BinPackParameters: false", BinPackParameters, - FormatStyle::BPPS_Never); + FormatStyle::BPPS_OnePerLine); Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers, diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 5ed69d9a0de03c..591b6a8bba9300 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2338,7 +2338,7 @@ TEST_F(FormatTest, FormatsForLoop) { "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); FormatStyle NoBinPacking = getLLVMStyle(); - NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat("for (int aaaaaaaaaaa = 1;\n" " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaa,\n" @@ -7165,7 +7165,7 @@ TEST_F(FormatTest, LineBreakingInBinaryExpressions) { "}"); FormatStyle OnePerLine = getLLVMStyle(); - OnePerLine.BinPackParameters = FormatStyle::BPPS_Never; + OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat( "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" @@ -7319,7 +7319,7 @@ TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { Style = getLLVMStyleWithColumns(20); Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; - Style.BinPackParameters = FormatStyle::BPPS_Never; + Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; Style.ContinuationIndentWidth = 2; verifyFormat("struct Foo {\n" @@ -7694,7 +7694,7 @@ TEST_F(FormatTest, ConstructorInitializers) { " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaa) {}", OnePerLine); - OnePerLine.BinPackParameters = FormatStyle::BPPS_Never; + OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat( "Constructor()\n" " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" @@ -7718,7 +7718,7 @@ TEST_F(FormatTest, ConstructorInitializers) { TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { FormatStyle Style = getLLVMStyleWithColumns(60); Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; - Style.BinPackParameters = FormatStyle::BPPS_Never; + Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; for (int i = 0; i < 4; ++i) { // Test all combinations of parameters that should not have an effect. @@ -7954,7 +7954,7 @@ TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { } // This parameter should not affect declarations. - Style.BinPackParameters = FormatStyle::BPPS_Never; + Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; Style.AllowAllArgumentsOnNextLine = false; Style.AllowAllParametersOfDeclarationOnNextLine = true; verifyFormat("void FunctionCallWithReallyLongName(\n" @@ -8049,7 +8049,7 @@ TEST_F(FormatTest, BreakFunctionDefinitionParameters) { // Test the style where all parameters are on their own lines. Style.AllowAllParametersOfDeclarationOnNextLine = false; - Style.BinPackParameters = FormatStyle::BPPS_Never; + Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat("void functionDecl(paramA, paramB, paramC);\n" "void emptyFunctionDefinition() {}\n" "void functionDefinition(\n" @@ -8244,7 +8244,7 @@ TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaa) {}", OnePerLine); - OnePerLine.BinPackParameters = FormatStyle::BPPS_Never; + OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat("Constructor() :\n" " aaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaa().aaa(),\n" @@ -8409,7 +8409,7 @@ TEST_F(FormatTest, MemoizationTests) { // This test takes VERY long when memoization is broken. FormatStyle OnePerLine = getLLVMStyle(); OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; - OnePerLine.BinPackParameters = FormatStyle::BPPS_Never; + OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; std::string input = "Constructor()\n" " : aaaa(a,\n"; for (unsigned i = 0, e = 80; i != e; ++i) @@ -8830,7 +8830,7 @@ TEST_F(FormatTest, BreaksDesireably) { TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { FormatStyle NoBinPacking = getGoogleStyle(); - NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; NoBinPacking.BinPackArguments = true; verifyFormat("void f() {\n" " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" @@ -8862,7 +8862,7 @@ TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { FormatStyle NoBinPacking = getGoogleStyle(); - NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; NoBinPacking.BinPackArguments = false; verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaa,\n" @@ -8927,7 +8927,7 @@ TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { TEST_F(FormatTest, FormatsDeclarationBreakAlways) { FormatStyle BreakAlways = getGoogleStyle(); - BreakAlways.BinPackParameters = FormatStyle::BPPS_OnePerLine; + BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine; verifyFormat("void f(int a,\n" " int b);", BreakAlways); @@ -8936,8 +8936,8 @@ TEST_F(FormatTest, FormatsDeclarationBreakAlways) { " int cccccccccccccccccccccccc);", BreakAlways); - // Ensure AlignAFterOpenBracket interacts correctly with - // PackParameters set to BreakAlways. + // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set + // to BPPS_AlwaysOnePerLine. BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; verifyFormat( "void someLongFunctionName(\n" @@ -8955,7 +8955,7 @@ TEST_F(FormatTest, FormatsDeclarationBreakAlways) { TEST_F(FormatTest, FormatsDefinitionBreakAlways) { FormatStyle BreakAlways = getGoogleStyle(); - BreakAlways.BinPackParameters = FormatStyle::BPPS_OnePerLine; + BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine; verifyFormat("void f(int a,\n" " int b) {\n" " f(a, b);\n" @@ -8963,7 +8963,7 @@ TEST_F(FormatTest, FormatsDefinitionBreakAlways) { BreakAlways); // Ensure BinPackArguments interact correctly when BinPackParameters is set to - // BreakAlways. + // BPPS_AlwaysOnePerLine. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n" " int cccccccccccccccccccccccc) {\n" @@ -8982,7 +8982,7 @@ TEST_F(FormatTest, FormatsDefinitionBreakAlways) { BreakAlways); // Ensure BreakFunctionDefinitionParameters interacts correctly when - // BinPackParameters is set to BreakAlways + // BinPackParameters is set to BPPS_AlwaysOnePerLine BreakAlways.BreakFunctionDefinitionParameters = true; verifyFormat("void f(\n" " int a,\n" @@ -8992,8 +8992,8 @@ TEST_F(FormatTest, FormatsDefinitionBreakAlways) { BreakAlways); BreakAlways.BreakFunctionDefinitionParameters = false; - // Ensure AlignAFterOpenBracket interacts correctly with - // BinPackParameters set to BreakAlways. + // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set + // to BPPS_AlwaysOnePerLine. BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; verifyFormat( "void someLongFunctionName(\n" @@ -9347,7 +9347,7 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) { Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; Style.BinPackArguments = false; - Style.BinPackParameters = FormatStyle::BPPS_Never; + Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaa aaaaaaaa,\n" " aaaaaaaaa aaaaaaa,\n" @@ -9386,7 +9386,7 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) { Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; Style.BinPackArguments = false; - Style.BinPackParameters = FormatStyle::BPPS_Never; + Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaa aaaaaaaa,\n" " aaaaaaaaa aaaaaaa,\n" @@ -10797,7 +10797,7 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { " .a();"); FormatStyle NoBinPacking = getLLVMStyle(); - NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" @@ -13709,7 +13709,7 @@ TEST_F(FormatTest, HandlesIncludeDirectives) { TEST_F(FormatTest, IncompleteParameterLists) { FormatStyle NoBinPacking = getLLVMStyle(); - NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" " double *min_x,\n" " double *max_x,\n" @@ -14375,7 +14375,7 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { " [](const Input &i) -> Output { return " "Output{1, 2}; });"); FormatStyle NoBinPacking = getLLVMStyle(); - NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat("waarudo::unit desk = {\n" " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, " "1, 1} * w::m; }};", @@ -19880,7 +19880,7 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { Alignment.AlignConsecutiveAssignments.Enabled = false; Alignment.ColumnLimit = 30; - Alignment.BinPackParameters = FormatStyle::BPPS_Never; + Alignment.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat("void foo(float a,\n" " float b,\n" " int c,\n" @@ -19894,7 +19894,7 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { " uint32_t *c,\n" " bool d) {}", Alignment); - Alignment.BinPackParameters = FormatStyle::BPPS_Always; + Alignment.BinPackParameters = FormatStyle::BPPS_BinPack; Alignment.ColumnLimit = 80; // Bug 33507 @@ -23320,7 +23320,7 @@ TEST_F(FormatTest, FormatsLambdas) { " LambdaBodyMustBeBreak);\n" "};", LLVMWithBeforeLambdaBody); - LLVMWithBeforeLambdaBody.BinPackParameters = FormatStyle::BPPS_Never; + LLVMWithBeforeLambdaBody.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", LLVMWithBeforeLambdaBody); verifyFormat( @@ -26717,7 +26717,7 @@ TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) { Medium, Style); Style.BinPackArguments = false; - Style.BinPackParameters = FormatStyle::BPPS_Never; + Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat(Short, Style); diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp index 4dc3535339f335..e70e10f13db375 100644 --- a/clang/unittests/Format/FormatTestComments.cpp +++ b/clang/unittests/Format/FormatTestComments.cpp @@ -364,24 +364,17 @@ TEST_F(FormatTestComments, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { "aaaa, bbbbb);")); FormatStyle BreakAlways = getLLVMStyle(); - BreakAlways.BinPackParameters = FormatStyle::BPPS_OnePerLine; - EXPECT_EQ("SomeFunction(a,\n" - " b, // comment\n" - " c,\n" - " d);", - format("SomeFunction(a,\n" - " b, // comment\n" - " c, d);", - BreakAlways)); - EXPECT_EQ("SomeFunction(a,\n" - " b,\n" - " // comment\n" - " c);", - format("SomeFunction(a,\n" - " b,\n" - " // comment\n" - " c);", - BreakAlways)); + BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine; + verifyFormat("int SomeFunction(a,\n" + " b, // comment\n" + " c,\n" + " d);", + BreakAlways); + verifyFormat("int SomeFunction(a,\n" + " b,\n" + " // comment\n" + " c);", + BreakAlways); } TEST_F(FormatTestComments, RemovesTrailingWhitespaceOfComments) { @@ -423,19 +416,25 @@ TEST_F(FormatTestComments, UnderstandsBlockComments) { verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); - FormatStyle NoBinPacking = getLLVMStyle(); - NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; - verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" - " /* parameter 2 */ aaaaaa,\n" - " /* parameter 3 */ aaaaaa,\n" - " /* parameter 4 */ aaaaaa);", - NoBinPacking); - NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; - verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" - " /* parameter 2 */ aaaaaa,\n" - " /* parameter 3 */ aaaaaa,\n" - " /* parameter 4 */ aaaaaa);", - NoBinPacking); + FormatStyle BinPack = getLLVMStyle(); + verifyFormat( + "int aaaaaaaaaaaaa(/* 1st */ int bbbbbbbbbb, /* 2nd */ int ccccccccccc,\n" + " /* 3rd */ int dddddddddddd);", + BinPack); + + FormatStyle OnePerLine = getLLVMStyle(); + OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; + verifyFormat("int a(/* 1st */ int b, /* 2nd */ int c);", OnePerLine); + verifyFormat("int aaaaaaaaaaaaa(/* 1st */ int bbbbbbbbbb,\n" + " /* 2nd */ int ccccccccccc,\n" + " /* 3rd */ int dddddddddddd);", + OnePerLine); + + FormatStyle AlwaysOnePerLine = getLLVMStyle(); + AlwaysOnePerLine.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine; + verifyFormat("int a(/* 1st */ int b,\n" + " /* 2nd */ int c);", + AlwaysOnePerLine); // Aligning block comments in macros. verifyGoogleFormat("#define A \\\n" @@ -2475,7 +2474,7 @@ TEST_F(FormatTestComments, BlockComments) { getLLVMStyleWithColumns(50))); FormatStyle NoBinPacking = getLLVMStyle(); - NoBinPacking.BinPackParameters = FormatStyle::BPPS_Never; + NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; EXPECT_EQ("someFunction(1, /* comment 1 */\n" " 2, /* comment 2 */\n" " 3, /* comment 3 */\n" diff --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp index ce9258a2d35b13..9b6f0c396d4dbf 100644 --- a/clang/unittests/Format/FormatTestObjC.cpp +++ b/clang/unittests/Format/FormatTestObjC.cpp @@ -377,7 +377,7 @@ TEST_F(FormatTestObjC, FormatObjCInterface) { " ddddddddddddd> {\n" "}"); - Style.BinPackParameters = FormatStyle::BPPS_Never; + Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; Style.ObjCBinPackProtocolList = FormatStyle::BPS_Auto; verifyFormat("@interface eeeeeeeeeeeee () <\n" " eeeeeeeeeeeee,\n" @@ -411,7 +411,7 @@ TEST_F(FormatTestObjC, FormatObjCInterface) { "+ (id)init;\n" "@end"); Style.ColumnLimit = 40; - // BinPackParameters should be BPPS_Always by default. + // BinPackParameters should be BPPS_BinPack by default. verifyFormat("void eeeeeeee(int eeeee, int eeeee,\n" " int eeeee, int eeeee);"); // ObjCBinPackProtocolList should be BPS_Never by default. >From 6dad829b5f0034511827f901b42600c8a6e286f9 Mon Sep 17 00:00:00 2001 From: Tom Pottage <pottage...@gmail.com> Date: Tue, 20 Aug 2024 21:12:50 +0100 Subject: [PATCH 4/5] Reorder BinPackParameters enum values and fix style / comments. --- clang/docs/ClangFormatStyleOptions.rst | 16 ++++++------- clang/include/clang/Format/Format.h | 12 +++++----- clang/lib/Format/Format.cpp | 2 +- clang/lib/Format/TokenAnnotator.cpp | 2 +- clang/unittests/Format/ConfigParseTest.cpp | 6 ++--- clang/unittests/Format/FormatTest.cpp | 2 +- clang/unittests/Format/FormatTestComments.cpp | 24 +++++++++++-------- 7 files changed, 34 insertions(+), 30 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index b7bff21ebbe88a..5d5a541d94bc9e 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -2072,6 +2072,14 @@ the configuration (without a prefix: ``Auto``). Possible values: + * ``BPPS_BinPack`` (in configuration: ``BinPack``) + Bin-pack parameters. + + .. code-block:: c++ + + void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + int ccccccccccccccccccccccccccccccccccccccccccc); + * ``BPPS_OnePerLine`` (in configuration: ``OnePerLine``) Put all parameters on the current line if they fit. Otherwise, put each one on its own line. @@ -2084,14 +2092,6 @@ the configuration (without a prefix: ``Auto``). int b, int ccccccccccccccccccccccccccccccccccccc); - * ``BPPS_BinPack`` (in configuration: ``BinPack``) - Bin-pack parameters. - - .. code-block:: c++ - - void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, - int ccccccccccccccccccccccccccccccccccccccccccc); - * ``BPPS_AlwaysOnePerLine`` (in configuration: ``AlwaysOnePerLine``) Always put each parameter on its own line. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 1fe6b13e77727c..154717125f28b1 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1194,6 +1194,12 @@ struct FormatStyle { /// Different way to try to fit all parameters on a line. enum BinPackParametersStyle : int8_t { + /// Bin-pack parameters. + /// \code + /// void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + /// int ccccccccccccccccccccccccccccccccccccccccccc); + /// \endcode + BPPS_BinPack, /// Put all parameters on the current line if they fit. /// Otherwise, put each one on its own line. /// \code @@ -1204,12 +1210,6 @@ struct FormatStyle { /// int ccccccccccccccccccccccccccccccccccccc); /// \endcode BPPS_OnePerLine, - /// Bin-pack parameters. - /// \code - /// void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, - /// int ccccccccccccccccccccccccccccccccccccccccccc); - /// \endcode - BPPS_BinPack, /// Always put each parameter on its own line. /// \code /// void f(int a, diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index e1aaf634187b17..97ff4d813c0535 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -137,8 +137,8 @@ template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> { template <> struct ScalarEnumerationTraits<FormatStyle::BinPackParametersStyle> { static void enumeration(IO &IO, FormatStyle::BinPackParametersStyle &Value) { - IO.enumCase(Value, "OnePerLine", FormatStyle::BPPS_OnePerLine); IO.enumCase(Value, "BinPack", FormatStyle::BPPS_BinPack); + IO.enumCase(Value, "OnePerLine", FormatStyle::BPPS_OnePerLine); IO.enumCase(Value, "AlwaysOnePerLine", FormatStyle::BPPS_AlwaysOnePerLine); // For backward compatibility. diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 2a092d89f1bab6..5196c12a25d626 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -5477,7 +5477,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, // Ignores the first parameter as this will be handled separately by // BreakFunctionDefinitionParameters or AlignAfterOpenBracket. - if (FormatStyle::BPPS_AlwaysOnePerLine == Style.BinPackParameters && + if (Style.BinPackParameters == FormatStyle::BPPS_AlwaysOnePerLine && Line.MightBeFunctionDecl && !Left.opensScope() && startsNextParameter(Right, Style)) { return true; diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index 7218885b7dbb42..b8bdfaaa74e10e 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -435,11 +435,11 @@ TEST(ConfigParseTest, ParsesConfiguration) { CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, FormatStyle::BILS_BeforeComma); - Style.BinPackParameters = FormatStyle::BPPS_BinPack; - CHECK_PARSE("BinPackParameters: OnePerLine", BinPackParameters, - FormatStyle::BPPS_OnePerLine); + Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; CHECK_PARSE("BinPackParameters: BinPack", BinPackParameters, FormatStyle::BPPS_BinPack); + CHECK_PARSE("BinPackParameters: OnePerLine", BinPackParameters, + FormatStyle::BPPS_OnePerLine); CHECK_PARSE("BinPackParameters: AlwaysOnePerLine", BinPackParameters, FormatStyle::BPPS_AlwaysOnePerLine); // For backward compatibility. diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 591b6a8bba9300..a803cf24739b63 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -8982,7 +8982,7 @@ TEST_F(FormatTest, FormatsDefinitionBreakAlways) { BreakAlways); // Ensure BreakFunctionDefinitionParameters interacts correctly when - // BinPackParameters is set to BPPS_AlwaysOnePerLine + // BinPackParameters is set to BPPS_AlwaysOnePerLine. BreakAlways.BreakFunctionDefinitionParameters = true; verifyFormat("void f(\n" " int a,\n" diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp index e70e10f13db375..ad7b6d82d91134 100644 --- a/clang/unittests/Format/FormatTestComments.cpp +++ b/clang/unittests/Format/FormatTestComments.cpp @@ -416,25 +416,29 @@ TEST_F(FormatTestComments, UnderstandsBlockComments) { verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); - FormatStyle BinPack = getLLVMStyle(); verifyFormat( "int aaaaaaaaaaaaa(/* 1st */ int bbbbbbbbbb, /* 2nd */ int ccccccccccc,\n" - " /* 3rd */ int dddddddddddd);", - BinPack); + " /* 3rd */ int dddddddddddd);"); - FormatStyle OnePerLine = getLLVMStyle(); - OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; - verifyFormat("int a(/* 1st */ int b, /* 2nd */ int c);", OnePerLine); + auto Style = getLLVMStyle(); + Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; + verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" + " /* parameter 2 */ aaaaaa,\n" + " /* parameter 3 */ aaaaaa,\n" + " /* parameter 4 */ aaaaaa);", + Style); + + Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; + verifyFormat("int a(/* 1st */ int b, /* 2nd */ int c);", Style); verifyFormat("int aaaaaaaaaaaaa(/* 1st */ int bbbbbbbbbb,\n" " /* 2nd */ int ccccccccccc,\n" " /* 3rd */ int dddddddddddd);", - OnePerLine); + Style); - FormatStyle AlwaysOnePerLine = getLLVMStyle(); - AlwaysOnePerLine.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine; + Style.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine; verifyFormat("int a(/* 1st */ int b,\n" " /* 2nd */ int c);", - AlwaysOnePerLine); + Style); // Aligning block comments in macros. verifyGoogleFormat("#define A \\\n" >From 20dfaa39194fe0a53ee1e1f4e4233136add8db1b Mon Sep 17 00:00:00 2001 From: Tom Pottage <pottage...@gmail.com> Date: Thu, 22 Aug 2024 19:52:45 +0100 Subject: [PATCH 5/5] Remove redundant re-assigning of style in block comment tests --- clang/unittests/Format/FormatTestComments.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp index ad7b6d82d91134..4eea14282c3256 100644 --- a/clang/unittests/Format/FormatTestComments.cpp +++ b/clang/unittests/Format/FormatTestComments.cpp @@ -427,8 +427,6 @@ TEST_F(FormatTestComments, UnderstandsBlockComments) { " /* parameter 3 */ aaaaaa,\n" " /* parameter 4 */ aaaaaa);", Style); - - Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; verifyFormat("int a(/* 1st */ int b, /* 2nd */ int c);", Style); verifyFormat("int aaaaaaaaaaaaa(/* 1st */ int bbbbbbbbbb,\n" " /* 2nd */ int ccccccccccc,\n" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits