clang-format: Fix special case of binary operator detection.
authorDaniel Jasper <djasper@google.com>
Mon, 2 Jun 2014 11:54:20 +0000 (11:54 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 2 Jun 2014 11:54:20 +0000 (11:54 +0000)
There is a pattern where evaluation order is used as control flow.
This patch special-cases a commonly occuring version of this pattern.

Before:
  Aaaaa *aaa = nullptr;
  // ...
  aaa &&aaa->f();

After:
  Aaaaa *aaa = nullptr;
  // ...
  aaa && aaa->f();

llvm-svn: 210017

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

index d8de4c6..849dfb0 100644 (file)
@@ -927,6 +927,12 @@ private:
         (InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
       return TT_BinaryOperator;
 
+    // This catches some cases where evaluation order is used as control flow:
+    //   aaa && aaa->f();
+    const FormatToken *NextNextToken = NextToken->getNextNonComment();
+    if (NextNextToken && NextNextToken->is(tok::arrow))
+      return TT_BinaryOperator;
+
     // It is very unlikely that we are going to find a pointer or reference type
     // definition on the RHS of an assignment.
     if (IsExpression)
index 2718a89..4078964 100644 (file)
@@ -4714,6 +4714,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
   verifyIndependentOfContext("typedef void (*f)(int *a);");
   verifyIndependentOfContext("int i{a * b};");
+  verifyIndependentOfContext("aaa && aaa->f();");
 
   verifyIndependentOfContext("InvalidRegions[*R] = 0;");