https://github.com/owenca created https://github.com/llvm/llvm-project/pull/138633
Fix #138485 >From e72f081f3a1f06ec9e868042cbe7db0a3c18796d Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Mon, 5 May 2025 22:35:40 -0700 Subject: [PATCH] [clang-format] Fix a bug in annotating binary operator && Fix #138485 --- clang/lib/Format/TokenAnnotator.cpp | 8 +++++--- clang/unittests/Format/TokenAnnotatorTest.cpp | 7 +++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index caf386cffd25b..65c651809e7d0 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3093,10 +3093,12 @@ class AnnotatingParser { if (InTemplateArgument && NextToken->Tok.isAnyIdentifier()) return TT_BinaryOperator; - // "&&" followed by "*" or "&" is quite unlikely to be two successive unary - // "&". - if (Tok.is(tok::ampamp) && NextToken->isOneOf(tok::star, tok::amp)) + // "&&" followed by "(", "*", or "&" is quite unlikely to be two successive + // unary "&". + if (Tok.is(tok::ampamp) && + NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) { return TT_BinaryOperator; + } // This catches some cases where evaluation order is used as control flow: // aaa && aaa->f(); diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 0fb64ceec5c97..03a366469558a 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -754,6 +754,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsNonTemplateAngleBrackets) { ASSERT_EQ(Tokens.size(), 27u) << Tokens; EXPECT_TOKEN(Tokens[7], tok::less, TT_BinaryOperator); EXPECT_TOKEN(Tokens[20], tok::greater, TT_BinaryOperator); + + Tokens = annotate("bool foo = a < b && (c * d) > e;"); + ASSERT_EQ(Tokens.size(), 16u) << Tokens; + EXPECT_TOKEN(Tokens[4], tok::less, TT_BinaryOperator); + EXPECT_TOKEN(Tokens[6], tok::ampamp, TT_BinaryOperator); + EXPECT_TOKEN(Tokens[9], tok::star, TT_BinaryOperator); + EXPECT_TOKEN(Tokens[12], tok::greater, TT_BinaryOperator); } TEST_F(TokenAnnotatorTest, UnderstandsTemplateTemplateParameters) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits