From: Daniel Jasper Date: Mon, 30 Jun 2014 13:44:47 +0000 (+0000) Subject: clang-format: Improve expression heuristics. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b1a7280eb33f3feeae7133ff4f641a3f71ac5648;p=clang clang-format: Improve expression heuristics. Upon encountering a binary operator inside parentheses, assume that the parentheses contain an expression. Before: MACRO('0' <= c&& c <= '9'); After: MACRO('0' <= c && c <= '9'); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@212040 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 0760f35bbb..c1fbf81e11 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -164,6 +164,8 @@ private: CurrentToken->Previous->Previous->isOneOf(tok::l_paren, tok::coloncolon)) MightBeFunctionType = true; + if (CurrentToken->Previous->Type == TT_BinaryOperator) + Contexts.back().IsExpression = true; if (CurrentToken->is(tok::r_paren)) { if (MightBeFunctionType && CurrentToken->Next && (CurrentToken->Next->is(tok::l_paren) || diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 11a42a51fa..75eea98275 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -4841,6 +4841,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyIndependentOfContext("MACRO(int *i);"); verifyIndependentOfContext("MACRO(auto *a);"); verifyIndependentOfContext("MACRO(const A *a);"); + verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); // FIXME: Is there a way to make this work? // verifyIndependentOfContext("MACRO(A *a);");