[clang] [clang-format] Harden annotation of operator keywords (PR #196768)
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Sat May 9 15:54:47 PDT 2026
https://github.com/HazardyKnusperkeks created https://github.com/llvm/llvm-project/pull/196768
The star was already annotated as TT_PointerOrReference, just overwrite it for the sake of not crashing. Also remove the annotation above, since that would always be overwritten (or at least I don't see when not, and there's no failed test).
Fixes #196054.
>From 962705a5f6829fccb92992502d7becfa56463c95 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern at hazardy.de>
Date: Sun, 10 May 2026 00:29:19 +0200
Subject: [PATCH] [clang-format] Harden annotation of operator keywords
The star was already annotated as TT_PointerOrReference, just overwrite
it for the sake of not crashing. Also remove the annotation above, since
that would always be overwritten (or at least I don't see when not, and
there's no failed test).
Fixes #196054.
---
clang/lib/Format/TokenAnnotator.cpp | 4 +---
clang/unittests/Format/TokenAnnotatorTest.cpp | 3 +++
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 640f03a4ac130..b2c5b6f2b5df9 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1684,8 +1684,6 @@ class AnnotatingParser {
}
while (CurrentToken &&
CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren)) {
- if (CurrentToken->isOneOf(tok::star, tok::amp))
- CurrentToken->setType(TT_PointerOrReference);
auto Next = CurrentToken->getNextNonComment();
if (!Next)
break;
@@ -1704,7 +1702,7 @@ class AnnotatingParser {
Previous->isPointerOrReference() ||
// User defined literal.
Previous->TokenText.starts_with("\"\"")) {
- Previous->setType(TT_OverloadedOperator);
+ Previous->overwriteFixedType(TT_OverloadedOperator);
if (CurrentToken->isOneOf(tok::less, tok::greater))
break;
}
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 48ae9e144cc2a..2ad5881d64031 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1208,6 +1208,9 @@ TEST_F(TokenAnnotatorTest, UnderstandsOverloadedOperators) {
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
// Not TT_FunctionDeclarationName.
EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown);
+
+ // Verify we don't crash.
+ annotate("operator foo*;");
}
TEST_F(TokenAnnotatorTest, OverloadedOperatorInTemplate) {
More information about the cfe-commits
mailing list