From: Daniel Jasper Date: Fri, 25 Oct 2013 14:29:37 +0000 (+0000) Subject: clang-format: Adapt line break penalties for LLVM style. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=47066e46b9ce4f830ead3c7b9a4cb5bf0ac2c857;p=clang clang-format: Adapt line break penalties for LLVM style. Specifically make clang-format less eager to break after the opening parenthesis of a function call. Before: aaaaaaaaaaaaaaaaa( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); After: aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); Apparently that is preferable. This penalties are adapted conservatively, we might have to increase them a little bit further. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@193410 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index 6745082546..fda37ad2b4 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -52,6 +52,9 @@ struct FormatStyle { /// \brief The penalty for breaking before the first \c <<. unsigned PenaltyBreakFirstLessLess; + /// \brief The penalty for breaking a function call after "call(". + unsigned PenaltyBreakBeforeFirstCallParameter; + /// \brief Set whether & and * bind to the type as opposed to the variable. bool PointerBindsToType; diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 79244b1993..fd84454d90 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -137,6 +137,8 @@ template <> struct MappingTraits { IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation); IO.mapOptional("ObjCSpaceBeforeProtocolList", Style.ObjCSpaceBeforeProtocolList); + IO.mapOptional("PenaltyBreakBeforeFirstCallParameter", + Style.PenaltyBreakBeforeFirstCallParameter); IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment); IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); IO.mapOptional("PenaltyBreakFirstLessLess", @@ -219,6 +221,7 @@ FormatStyle getLLVMStyle() { setDefaultPenalties(LLVMStyle); LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60; + LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19; return LLVMStyle; } @@ -263,6 +266,7 @@ FormatStyle getGoogleStyle() { setDefaultPenalties(GoogleStyle); GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; + GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1; return GoogleStyle; } diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 7fa96215b1..9240c63481 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1177,7 +1177,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, if (Left.is(tok::l_paren) && Line.MightBeFunctionDecl) return 100; if (Left.opensScope()) - return Left.ParameterCount > 1 ? prec::Comma : 19; + return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter + : 19; if (Right.is(tok::lessless)) { if (Left.is(tok::string_literal)) { diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 431940938d..ac95f27b2f 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -2973,9 +2973,9 @@ TEST_F(FormatTest, BreaksDesireably) { "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); verifyFormat( - "aaaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); // Indent consistently indenpendent of call expression. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" @@ -3122,7 +3122,7 @@ TEST_F(FormatTest, FormatsBuilderPattern) { " ->aaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " ->aaaaaaaaaaaaaaaaa();"); - verifyFormat( + verifyGoogleFormat( "someobj->Add((new util::filetools::Handler(dir))\n" " ->OnEvent1(NewPermanentCallback(\n" " this, &HandlerHolderClass::EventHandlerCBA))\n" @@ -4497,7 +4497,7 @@ TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) { NoSpaces); } -TEST_F(FormatTest, FormatsBracedListsinColumnLayout) { +TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { verifyFormat("vector x = { 1, 22, 333, 4444, 55555, 666666, 7777777,\n" " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" @@ -4538,8 +4538,8 @@ TEST_F(FormatTest, FormatsBracedListsinColumnLayout) { verifyFormat("vector x = { 1, 1, 1, 1,\n" " 1, 1, 1, 1, };", getLLVMStyleWithColumns(39)); - verifyFormat("vector x = {\n" - " 1, 1, 1, 1, 1, 1, 1, 1, //\n" + verifyFormat("vector x = { 1, 1, 1, 1,\n" + " 1, 1, 1, 1, //\n" "};", getLLVMStyleWithColumns(39)); verifyFormat("vector x = { 1, 1, 1, 1,\n" @@ -6597,6 +6597,8 @@ TEST_F(FormatTest, ParsesConfiguration) { ConstructorInitializerIndentWidth, 1234u); CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); + CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", + PenaltyBreakBeforeFirstCallParameter, 1234u); CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", PenaltyReturnTypeOnItsOwnLine, 1234u);