r221727 - clang-format: Improve handling of comments in binary expressions.

Daniel Jasper djasper at google.com
Tue Nov 11 15:04:51 PST 2014


Author: djasper
Date: Tue Nov 11 17:04:51 2014
New Revision: 221727

URL: http://llvm.org/viewvc/llvm-project?rev=221727&view=rev
Log:
clang-format: Improve handling of comments in binary expressions.

Before:
  b = a &&
      // Comment
      b.c &&
      d;

After:
  b = a &&
      // Comment
      b.c && d;

This fixes llvm.org/PR21535.

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=221727&r1=221726&r2=221727&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue Nov 11 17:04:51 2014
@@ -1149,8 +1149,7 @@ public:
           Current->OperatorIndex = OperatorIndex;
           ++OperatorIndex;
         }
-
-        next(/*SkipPastLeadingComments=*/false);
+        next(/*SkipPastLeadingComments=*/Precedence > 0);
       }
     }
   }

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=221727&r1=221726&r2=221727&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Nov 11 17:04:51 2014
@@ -3118,6 +3118,9 @@ TEST_F(FormatTest, LineBreakingInBinaryE
   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
                "    cccccc) {\n}");
+  verifyFormat("b = a &&\n"
+               "    // Comment\n"
+               "    b.c && d;");
 
   // If the LHS of a comparison is not a binary expression itself, the
   // additional linebreak confuses many people.





More information about the cfe-commits mailing list