[clang] [clang-format] Harden annotation of operator keywords (PR #196768)
via cfe-commits
cfe-commits at lists.llvm.org
Sat May 9 15:55:22 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Björn Schäpers (HazardyKnusperkeks)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/196768.diff
2 Files Affected:
- (modified) clang/lib/Format/TokenAnnotator.cpp (+1-3)
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+3)
``````````diff
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) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/196768
More information about the cfe-commits
mailing list