r205845 - clang-format: Keep more trailing annotations on the same line.

Daniel Jasper djasper at google.com
Wed Apr 9 03:01:49 PDT 2014


Author: djasper
Date: Wed Apr  9 05:01:49 2014
New Revision: 205845

URL: http://llvm.org/viewvc/llvm-project?rev=205845&view=rev
Log:
clang-format: Keep more trailing annotations on the same line.

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.

Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=205845&r1=205844&r2=205845&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed Apr  9 05:01:49 2014
@@ -1279,11 +1279,9 @@ unsigned TokenAnnotator::splitPenalty(co
     // 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 ';'.

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=205845&r1=205844&r2=205845&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Apr  9 05:01:49 2014
@@ -3270,9 +3270,15 @@ TEST_F(FormatTest, BreaksFunctionDeclara
                "    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);
 





More information about the cfe-commits mailing list