From 7a623cd42ce3426b6d1532d40a8069d5f70592d8 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Thu, 7 Aug 2025 00:59:16 -0700 Subject: [PATCH] [clang-format] Handle `&&` in requires clause in requires requires Fixes #152266 --- clang/lib/Format/TokenAnnotator.cpp | 5 ++++- clang/unittests/Format/TokenAnnotatorTest.cpp | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index cb41756c56bf7..19c42c88762fb 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3129,8 +3129,11 @@ class AnnotatingParser { // It is very unlikely that we are going to find a pointer or reference type // definition on the RHS of an assignment. - if (IsExpression && !Contexts.back().CaretFound) + if (IsExpression && !Contexts.back().CaretFound && + Line.getFirstNonComment()->isNot( + TT_RequiresClauseInARequiresExpression)) { return TT_BinaryOperator; + } // Opeartors at class scope are likely pointer or reference members. if (!Scopes.empty() && Scopes.back() == ST_Class) diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 815c79e68dac9..6d769396589ee 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -1391,6 +1391,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) { ASSERT_EQ(Tokens.size(), 38u) << Tokens; EXPECT_TOKEN(Tokens[19], tok::l_brace, TT_RequiresExpressionLBrace); + Tokens = + annotate("template \n" + " requires requires {\n" + " requires std::same_as>;\n" + " }\n" + "void Foo();"); + ASSERT_EQ(Tokens.size(), 34u) << Tokens; + EXPECT_TOKEN(Tokens[21], tok::ampamp, TT_PointerOrReference); + Tokens = annotate("template concept C =" "std::same_as, std::iter_value_t>;");