]> granicus.if.org Git - clang/commitdiff
clang-format: Keep trailing annotations together.
authorDaniel Jasper <djasper@google.com>
Mon, 16 Dec 2013 15:01:54 +0000 (15:01 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 16 Dec 2013 15:01:54 +0000 (15:01 +0000)
Before:
  virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa, aaaaaaaaaaa aaaaa) const
      override;
  virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() const
      override;

After:
  virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,
                     aaaaaaaaaaa aaaaa) const override;
  virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
      const override;

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@197391 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/FormatToken.h
lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index fa676b39736efa951549885bc186393cf45ab662..60265509db9bfbf18d62708866cdc0bb8080a5c5 100644 (file)
@@ -58,6 +58,7 @@ enum TokenType {
   TT_StartOfName,
   TT_TemplateCloser,
   TT_TemplateOpener,
+  TT_TrailingAnnotation,
   TT_TrailingReturnArrow,
   TT_TrailingUnaryOperator,
   TT_UnaryOperator,
index e2615dbb6a275144899687a03349d9d1282fe2a1..db071f101e46d1c4cee2da2348c08b291c232de6 100644 (file)
@@ -367,7 +367,7 @@ private:
       if (!parseParens())
         return false;
       if (Line.MustBeDeclaration && Contexts.size() == 1 &&
-          !Contexts.back().IsExpression)
+          !Contexts.back().IsExpression && Line.First->Type != TT_ObjCProperty)
         Line.MightBeFunctionDecl = true;
       break;
     case tok::l_square:
@@ -715,6 +715,11 @@ private:
         if (PreviousNoComment &&
             PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
           Current.Type = TT_DesignatedInitializerPeriod;
+      } else if (Current.isOneOf(tok::identifier, tok::kw_const) &&
+                 Line.MightBeFunctionDecl && Contexts.size() == 1) {
+        // Line.MightBeFunctionDecl can only be true after the parentheses of a
+        // function declaration have been found.
+        Current.Type = TT_TrailingAnnotation;
       }
     }
   }
@@ -1171,11 +1176,13 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
     return 150;
   }
 
-  // Breaking before a trailing 'const' or not-function-like annotation is bad.
-  if (Left.is(tok::r_paren) && Line.Type != LT_ObjCProperty &&
-      (Right.is(tok::kw_const) || (Right.is(tok::identifier) && Right.Next &&
-                                   Right.Next->isNot(tok::l_paren))))
-    return 100;
+  if (Right.Type == TT_TrailingAnnotation && Right.Next &&
+      Right.Next->isNot(tok::l_paren)) {
+    // Breaking before a trailing annotation is bad unless it is function-like.
+    // Use a slightly higher penalty after ")" so that annotations like
+    // "const override" are kept together.
+    return Left.is(tok::r_paren) ? 100 : 120;
+  }
 
   // In for-loops, prefer breaking at ',' and ';'.
   if (Line.First->is(tok::kw_for) && Left.is(tok::equal))
index 5e38f5974a7abbf5529adde0d138725e1ee22f7d..e9b2a9c9add9fa3d2e494c685d4b621612a81703 100644 (file)
@@ -3066,6 +3066,11 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
+  verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
+               "                   aaaaaaaaaaa aaaaa) const override;");
+  verifyGoogleFormat(
+      "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
+      "    const override;");
 
   // Unless this would lead to the first parameter being broken.
   verifyFormat("void someLongFunction(int someLongParameter)\n"