diff --git a/docs/ClangFormatStyleOptions.rst b/docs/ClangFormatStyleOptions.rst index 95f85ba3dfd12d2278924e167385903d2891b1ae..530f7c5bf8371887875926419a4fe658e7823189 100644 --- a/docs/ClangFormatStyleOptions.rst +++ b/docs/ClangFormatStyleOptions.rst @@ -205,9 +205,13 @@ the configuration (without a prefix: ``Auto``). If ``true``, always break after the ``template<...>`` of a template declaration. +**BinPackArguments** (``bool``) + If ``false``, a function call's arguments will either be all on the + same line or will have one line each. + **BinPackParameters** (``bool``) - If ``false``, a function call's or function definition's parameters - will either all be on the same line or will have one line each. + 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. **BreakBeforeBinaryOperators** (``BinaryOperatorStyle``) The way to wrap binary operators. @@ -288,7 +292,7 @@ the configuration (without a prefix: ``Auto``). **DerivePointerAlignment** (``bool``) If ``true``, analyze the formatted file for the most common - alignment of & and ``*``. ``PointerAlignment`` is then used only as fallback. + alignment of & and *. ``PointerAlignment`` is then used only as fallback. **DisableFormat** (``bool``) Disables formatting at all. @@ -342,6 +346,8 @@ the configuration (without a prefix: ``Auto``). Do not use. * ``LK_Cpp`` (in configuration: ``Cpp``) Should be used for C, C++, ObjectiveC, ObjectiveC++. + * ``LK_Java`` (in configuration: ``Java``) + Should be used for Java. * ``LK_JavaScript`` (in configuration: ``JavaScript``) Should be used for JavaScript. * ``LK_Proto`` (in configuration: ``Proto``) diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index a5a69441c22a6c331bb38a15691c464da75cc840..a979886add41de72b773b49afed9f10239380d2a 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -152,10 +152,14 @@ struct FormatStyle { /// commonly have different usage patterns and a number of special cases. unsigned SpacesBeforeTrailingComments; - /// \brief If \c false, a function call's or function definition's parameters - /// will either all be on the same line or will have one line each. + /// \brief If \c false, a function declaration's or function definition's + /// parameters will either all be on the same line or will have one line each. bool BinPackParameters; + /// \brief If \c false, a function call's arguments will either be all on the + /// same line or will have one line each. + bool BinPackArguments; + /// \brief If \c true, clang-format detects whether function calls and /// definitions are formatted with one parameter per line. /// @@ -405,6 +409,7 @@ struct FormatStyle { AlwaysBreakBeforeMultilineStrings == R.AlwaysBreakBeforeMultilineStrings && BinPackParameters == R.BinPackParameters && + BinPackArguments == R.BinPackArguments && BreakBeforeBinaryOperators == R.BreakBeforeBinaryOperators && BreakBeforeTernaryOperators == R.BreakBeforeTernaryOperators && BreakBeforeBraces == R.BreakBeforeBraces && diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 9d72994c1bfbdbb9d88eb234434e3ca896ca2849..ead1da99c2ddba23c5ca4788378a1827d599046e 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -859,11 +859,13 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, NewIndent = Style.ContinuationIndentWidth + std::max(State.Stack.back().LastSpace, State.Stack.back().StartOfFunctionCall); - AvoidBinPacking = !Style.BinPackParameters || - (Style.ExperimentalAutoDetectBinPacking && - (Current.PackingKind == PPK_OnePerLine || - (!BinPackInconclusiveFunctions && - Current.PackingKind == PPK_Inconclusive))); + AvoidBinPacking = + (State.Line->MustBeDeclaration && !Style.BinPackParameters) || + (!State.Line->MustBeDeclaration && !Style.BinPackArguments) || + (Style.ExperimentalAutoDetectBinPacking && + (Current.PackingKind == PPK_OnePerLine || + (!BinPackInconclusiveFunctions && + Current.PackingKind == PPK_Inconclusive))); // If this '[' opens an ObjC call, determine whether all parameters fit // into one line and put one per line if they don't. if (Current.Type == TT_ObjCMethodExpr && Style.ColumnLimit != 0 && diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 72de9ab7c1c8559ae206c3f5230c985741a1e288..b9d13ab691e749912f2800376e3a4ef55544717f 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -199,6 +199,7 @@ template <> struct MappingTraits<FormatStyle> { IO.mapOptional("BreakConstructorInitializersBeforeComma", Style.BreakConstructorInitializersBeforeComma); IO.mapOptional("BinPackParameters", Style.BinPackParameters); + IO.mapOptional("BinPackArguments", Style.BinPackArguments); IO.mapOptional("ColumnLimit", Style.ColumnLimit); IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", Style.ConstructorInitializerAllOnOneLineOrOnePerLine); @@ -333,6 +334,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; LLVMStyle.AlwaysBreakTemplateDeclarations = false; LLVMStyle.BinPackParameters = true; + LLVMStyle.BinPackArguments = true; LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None; LLVMStyle.BreakBeforeTernaryOperators = true; LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 29f494603c654d517c18dbbcb99119db508c6b09..4091cd5c10f3e7aaae223e40fb8acba3485f269a 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -3685,9 +3685,25 @@ TEST_F(FormatTest, BreaksDesireably) { " NSTrackingActiveAlways;"); } +TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { + FormatStyle NoBinPacking = getGoogleStyle(); + NoBinPacking.BinPackParameters = false; + NoBinPacking.BinPackArguments = true; + verifyFormat("void f() {\n" + " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" + "}", + NoBinPacking); + verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" + " int aaaaaaaaaaaaaaaaaaaa,\n" + " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", + NoBinPacking); +} + TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { FormatStyle NoBinPacking = getGoogleStyle(); NoBinPacking.BinPackParameters = false; + NoBinPacking.BinPackArguments = false; verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", @@ -4026,7 +4042,7 @@ TEST_F(FormatTest, BreaksConditionalExpressions) { " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); FormatStyle NoBinPacking = getLLVMStyle(); - NoBinPacking.BinPackParameters = false; + NoBinPacking.BinPackArguments = false; verifyFormat( "void f() {\n" " g(aaa,\n" @@ -6553,17 +6569,16 @@ TEST_F(FormatTest, FormatObjCMethodExpr) { " der:NO]);\n" "}", getLLVMStyleWithColumns(70)); - verifyFormat("{\n" - " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" - " initWithContentRect:NSMakeRect(origin_global.x,\n" - " origin_global.y,\n" - " pos.width(),\n" - " pos.height())\n" - " styleMask:NSBorderlessWindowMask\n" - " backing:NSBackingStoreBuffered\n" - " defer:NO]);\n" - "}", - getChromiumStyle(FormatStyle::LK_Cpp)); + verifyFormat( + "void f() {\n" + " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" + " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" + " pos.width(), pos.height())\n" + " styleMask:NSBorderlessWindowMask\n" + " backing:NSBackingStoreBuffered\n" + " defer:NO]);\n" + "}", + getChromiumStyle(FormatStyle::LK_Cpp)); verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" " with:contentsNativeView];"); @@ -8387,6 +8402,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) { CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType); CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); CHECK_PARSE_BOOL(BinPackParameters); + CHECK_PARSE_BOOL(BinPackArguments); CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);