[cfe-commits] r171007 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp

Daniel Jasper djasper at google.com
Sun Dec 23 16:13:23 PST 2012


Author: djasper
Date: Sun Dec 23 18:13:23 2012
New Revision: 171007

URL: http://llvm.org/viewvc/llvm-project?rev=171007&view=rev
Log:
Take operator precedence into account when splitting lines.

With this patch, splitting after binary operators has a panelty corresponding
to the operator's precedence. We used to ignore this and eagerly format like:

  if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb &&
      ccccccccccccccccccccccccc) { .. }

With this patch, this becomes:

  if (aaaaaaaaaaaaaaaaaaaaaaaaa ||
      bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) { .. }

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

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=171007&r1=171006&r2=171007&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Sun Dec 23 18:13:23 2012
@@ -90,7 +90,7 @@
       : Style(Style), SourceMgr(SourceMgr), Line(Line),
         Annotations(Annotations), Replaces(Replaces),
         StructuralError(StructuralError) {
-    Parameters.PenaltyIndentLevel = 5;
+    Parameters.PenaltyIndentLevel = 15;
   }
 
   void format() {
@@ -325,10 +325,14 @@
 
     if (Left.Tok.is(tok::semi) || Left.Tok.is(tok::comma))
       return 0;
-    if (Left.Tok.is(tok::equal) || Left.Tok.is(tok::l_paren) ||
-        Left.Tok.is(tok::pipepipe) || Left.Tok.is(tok::ampamp))
+    if (Left.Tok.is(tok::l_paren))
       return 2;
 
+    prec::Level Level =
+        getBinOpPrecedence(Line.Tokens[Index].Tok.getKind(), true, true);
+    if (Level != prec::Unknown)
+      return Level;
+
     if (Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period))
       return 200;
 

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=171007&r1=171006&r2=171007&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sun Dec 23 18:13:23 2012
@@ -456,6 +456,21 @@
       "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
 }
 
+TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
+  verifyFormat(
+      "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
+      "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
+  verifyFormat(
+      "if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
+      "    ccccccccccccccccccccccccc) {\n}");
+  verifyFormat(
+      "if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
+      "    ccccccccccccccccccccccccc) {\n}");
+  verifyFormat(
+      "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
+      "    ccccccccccccccccccccccccc) {\n}");
+}
+
 TEST_F(FormatTest, AlignsStringLiterals) {
   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
                "                                      \"short literal\");");





More information about the cfe-commits mailing list