https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/132885

Annotate the angles in `A<B != A>B` as template opener/closer as it's unlikely 
that they are less/greater-than operators in this context.

Fix #132248

>From 2f376c819aed998ee30fb007d3c151efb7762bc0 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpi...@gmail.com>
Date: Mon, 24 Mar 2025 22:28:45 -0700
Subject: [PATCH] [clang-format] Fix a regression on annotating template angles

Annotate the angles in `A<B != A>B` as template opener/closer as it's
unlikely that they are less/greater-than operators in this context.

Fix #132248
---
 clang/lib/Format/TokenAnnotator.cpp           |  3 +--
 clang/unittests/Format/TokenAnnotatorTest.cpp | 15 +++++++++++++--
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 01bf8c3778928..d87b3a6088bd8 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -253,8 +253,7 @@ class AnnotatingParser {
       // FIXME: This is getting out of hand, write a decent parser.
       if (MaybeAngles && InExpr && !Line.startsWith(tok::kw_template) &&
           Prev.is(TT_BinaryOperator) &&
-          (Prev.isOneOf(tok::pipepipe, tok::ampamp) ||
-           Prev.getPrecedence() == prec::Equality)) {
+          Prev.isOneOf(tok::pipepipe, tok::ampamp)) {
         MaybeAngles = false;
       }
       if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 1ff785110fc34..7bdae1666a90a 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -720,8 +720,9 @@ TEST_F(TokenAnnotatorTest, 
UnderstandsNonTemplateAngleBrackets) {
 
   Tokens = annotate("return A < B != A > B;");
   ASSERT_EQ(Tokens.size(), 10u) << Tokens;
-  EXPECT_TOKEN(Tokens[2], tok::less, TT_BinaryOperator);
-  EXPECT_TOKEN(Tokens[6], tok::greater, TT_BinaryOperator);
+  // FIXME:
+  // EXPECT_TOKEN(Tokens[2], tok::less, TT_BinaryOperator);
+  // EXPECT_TOKEN(Tokens[6], tok::greater, TT_BinaryOperator);
 
   Tokens = annotate("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
   ASSERT_EQ(Tokens.size(), 27u) << Tokens;
@@ -3827,6 +3828,16 @@ TEST_F(TokenAnnotatorTest, TemplateInstantiation) {
   ASSERT_EQ(Tokens.size(), 24u) << Tokens;
   EXPECT_TOKEN(Tokens[6], tok::less, TT_TemplateOpener);
   EXPECT_TOKEN(Tokens[18], tok::greater, TT_TemplateCloser);
+
+  Tokens = annotate(
+      "std::uint16_t kMTU = std::conditional<\n"
+      "    kTypeKind == KindA,\n"
+      "    std::integral_constant<std::uint16_t, kIoSockMtu>>::type::value;");
+  ASSERT_EQ(Tokens.size(), 30u) << Tokens;
+  EXPECT_TOKEN(Tokens[8], tok::less, TT_TemplateOpener);
+  EXPECT_TOKEN(Tokens[16], tok::less, TT_TemplateOpener);
+  EXPECT_TOKEN(Tokens[22], tok::greater, TT_TemplateCloser);
+  EXPECT_TOKEN(Tokens[23], tok::greater, TT_TemplateCloser);
 }
 
 TEST_F(TokenAnnotatorTest, VariableTemplate) {

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to