]> granicus.if.org Git - clang/commitdiff
clang-format: Adapt line break penalties for LLVM style.
authorDaniel Jasper <djasper@google.com>
Fri, 25 Oct 2013 14:29:37 +0000 (14:29 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 25 Oct 2013 14:29:37 +0000 (14:29 +0000)
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

include/clang/Format/Format.h
lib/Format/Format.cpp
lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index 6745082546e50bcace56cbd630127d257183bab7..fda37ad2b491cca8d8cc4e1e9741aa54a663d63c 100644 (file)
@@ -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;
 
index 79244b199378c59ed4315900e099aae2b318b787..fd84454d9074200c79a9d3f53894d5bd0739cc7c 100644 (file)
@@ -137,6 +137,8 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
     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;
 }
index 7fa96215b11c1102bd4c3903b53abcd4126d5f2e..9240c6348148e091265f0fa9a44000ac8809697b 100644 (file)
@@ -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)) {
index 431940938dc98376cdef206df15879b92162c047..ac95f27b2f6de74b1d25eda8b45c343a927d8d24 100644 (file)
@@ -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<int> 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<int> x = { 1, 1, 1, 1,\n"
                "                  1, 1, 1, 1, };",
                getLLVMStyleWithColumns(39));
-  verifyFormat("vector<int> x = {\n"
-               "  1, 1, 1, 1, 1, 1, 1, 1, //\n"
+  verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
+               "                  1, 1, 1, 1, //\n"
                "};",
                getLLVMStyleWithColumns(39));
   verifyFormat("vector<int> 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);