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: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=804ec3446da1c01af7206c526ef5fecec3e6ae60;p=clang 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(); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@210017 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index d8de4c68f3..849dfb02f8 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/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/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 2718a8992c..40789640e2 100644 --- a/unittests/Format/FormatTest.cpp +++ b/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;");