]> granicus.if.org Git - clang/commitdiff
clang-format: Keep more trailing annotations on the same line.
authorDaniel Jasper <djasper@google.com>
Wed, 9 Apr 2014 10:01:49 +0000 (10:01 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 9 Apr 2014 10:01:49 +0000 (10:01 +0000)
More precisely keep all short annotations (<10 characters) on the same
line if possible. Previously, clang-format would only prefer to do so
for "const", "override" and "final". However, it seems to be generally
preferable, especially because some codebases have to wrap those in
macros for backwards compatibility.

Before:
  void someLongFunction(int someLongParameter)
      OVERRIDE {}

After:
  void someLongFunction(
      int someLongParameter) OVERRIDE {}

This fixes llvm.org/PR19363.

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

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

index 03921b04b1e6913035f2acf54e9682cd68f7e0e7..cd05a6807e61b6dc551630d1fd227a30f13ce8fb 100644 (file)
@@ -1279,11 +1279,9 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
     // annotations (i.e. "const", "final" and "override") on the same line.
     // Use a slightly higher penalty after ")" so that annotations like
     // "const override" are kept together.
-    bool is_standard_annotation = Right.is(tok::kw_const) ||
-                                  Right.TokenText == "override" ||
-                                  Right.TokenText == "final";
+    bool is_short_annotation = Right.TokenText.size() < 10;
     return (Left.is(tok::r_paren) ? 100 : 120) +
-           (is_standard_annotation ? 50 : 0);
+           (is_short_annotation ? 50 : 0);
   }
 
   // In for-loops, prefer breaking at ',' and ';'.
index 5adc0d5725b4440c12453a1dc21edd414672957e..e3da016663c7899c186c9be17b64563a97c39e6d 100644 (file)
@@ -3269,9 +3269,15 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
   verifyFormat("void someLongFunction(\n"
                "    int someLongParameter) override {}",
                Style);
+  verifyFormat("void someLongFunction(\n"
+               "    int someLongParameter) OVERRIDE {}",
+               Style);
   verifyFormat("void someLongFunction(\n"
                "    int someLongParameter) final {}",
                Style);
+  verifyFormat("void someLongFunction(\n"
+               "    int someLongParameter) FINAL {}",
+               Style);
   verifyFormat("void someLongFunction(\n"
                "    int parameter) const override {}",
                Style);