From: Daniel Jasper Date: Mon, 2 Jun 2014 11:54:20 +0000 (+0000) Subject: clang-format: Fix special case of binary operator detection. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7d028298ceecaed3270f947baaea94b58965e642;p=platform%2Fupstream%2Fllvm.git clang-format: Fix special case of binary operator detection. 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 --- diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index d8de4c6..849dfb0 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -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) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 2718a89..4078964 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -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;");