[clang] [clang-format] Fix a regression of annotating PointerOrReference (PR #149039)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 16 19:46:14 PDT 2025
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/149039
>From 2418846a33683555f2e14de91aa42c1ec38b3fdb Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Wed, 16 Jul 2025 01:59:40 -0700
Subject: [PATCH 1/2] [clang-format] Fix a regression of annotating
PointerOrReference
Fixes 149010
---
clang/lib/Format/TokenAnnotator.cpp | 5 ++++-
clang/unittests/Format/TokenAnnotatorTest.cpp | 4 ++++
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 739209a5681f8..176c9c278b18c 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3112,7 +3112,7 @@ class AnnotatingParser {
// It's more likely that & represents operator& than an uninitialized
// reference.
- if (Tok.is(tok::amp) && PrevToken && PrevToken->Tok.isAnyIdentifier() &&
+ if (Tok.is(tok::amp) && PrevToken->Tok.isAnyIdentifier() &&
IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
NextToken && NextToken->Tok.isAnyIdentifier()) {
if (auto NextNext = NextToken->getNextNonComment();
@@ -3122,6 +3122,9 @@ class AnnotatingParser {
}
}
+ if (PrevToken->isTypeName(LangOpts))
+ return TT_PointerOrReference;
+
if (Line.Type == LT_SimpleRequirement ||
(!Scopes.empty() && Scopes.back() == ST_CompoundRequirement)) {
return TT_BinaryOperator;
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index e281a4945a862..30942c461427a 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -390,6 +390,10 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
EXPECT_TOKEN(Tokens[20], tok::l_brace, TT_CompoundRequirementLBrace);
EXPECT_TOKEN(Tokens[22], tok::star, TT_BinaryOperator);
+ Tokens = annotate("bool foo = requires { static_cast<int &&>(1); };");
+ ASSERT_EQ(Tokens.size(), 17u) << Tokens;
+ EXPECT_TOKEN(Tokens[8], tok::ampamp, TT_PointerOrReference);
+
Tokens = annotate("return s.operator int *();");
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
// Not TT_FunctionDeclarationName.
>From de73e796ae5369f68e434c7b119d25f916058e76 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Wed, 16 Jul 2025 19:05:49 -0700
Subject: [PATCH 2/2] Add a heuristic to cover user-defined types
---
clang/lib/Format/TokenAnnotator.cpp | 11 ++++++-----
clang/unittests/Format/TokenAnnotatorTest.cpp | 2 +-
2 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 176c9c278b18c..581bfbab0972d 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2996,14 +2996,18 @@ class AnnotatingParser {
const FormatToken *PrevToken = Tok.getPreviousNonComment();
if (!PrevToken)
return TT_UnaryOperator;
- if (PrevToken->is(TT_TypeName))
+ if (PrevToken->isTypeName(LangOpts))
return TT_PointerOrReference;
if (PrevToken->isPlacementOperator() && Tok.is(tok::ampamp))
return TT_BinaryOperator;
- const FormatToken *NextToken = Tok.getNextNonComment();
+ auto *NextToken = Tok.getNextNonComment();
if (!NextToken)
return TT_PointerOrReference;
+ if (NextToken->is(tok::greater)) {
+ NextToken->setFinalizedType(TT_TemplateCloser);
+ return TT_PointerOrReference;
+ }
if (InTemplateArgument && NextToken->is(tok::kw_noexcept))
return TT_BinaryOperator;
@@ -3122,9 +3126,6 @@ class AnnotatingParser {
}
}
- if (PrevToken->isTypeName(LangOpts))
- return TT_PointerOrReference;
-
if (Line.Type == LT_SimpleRequirement ||
(!Scopes.empty() && Scopes.back() == ST_CompoundRequirement)) {
return TT_BinaryOperator;
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 30942c461427a..ce7787ede0f5c 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -390,7 +390,7 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
EXPECT_TOKEN(Tokens[20], tok::l_brace, TT_CompoundRequirementLBrace);
EXPECT_TOKEN(Tokens[22], tok::star, TT_BinaryOperator);
- Tokens = annotate("bool foo = requires { static_cast<int &&>(1); };");
+ Tokens = annotate("bool foo = requires { static_cast<Foo &&>(1); };");
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
EXPECT_TOKEN(Tokens[8], tok::ampamp, TT_PointerOrReference);
More information about the cfe-commits
mailing list