Browse Source

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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@221727 91177308-0d34-0410-b5e6-96231b3b80d8
Daniel Jasper 10 years ago
parent
commit
411d4e186c
2 changed files with 4 additions and 2 deletions
  1. 1 2
      lib/Format/TokenAnnotator.cpp
  2. 3 0
      unittests/Format/FormatTest.cpp

+ 1 - 2
lib/Format/TokenAnnotator.cpp

@@ -1149,8 +1149,7 @@ public:
           Current->OperatorIndex = OperatorIndex;
           ++OperatorIndex;
         }
-
-        next(/*SkipPastLeadingComments=*/false);
+        next(/*SkipPastLeadingComments=*/Precedence > 0);
       }
     }
   }

+ 3 - 0
unittests/Format/FormatTest.cpp

@@ -3118,6 +3118,9 @@ TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
   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.