[llvm-branch-commits] [clang] release/20.x: [clang-format] Correctly annotate kw_operator in using decls (#136545) (PR #136808)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Apr 22 21:17:47 PDT 2025
https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/136808
Backport 037657de7e5ccd4a37054829874a209b82fb8be7
Requested by: @owenca
>From 38c4df051750c677518f61ae9b072a0b015be2b9 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Tue, 22 Apr 2025 21:08:56 -0700
Subject: [PATCH] [clang-format] Correctly annotate kw_operator in using decls
(#136545)
Fix #136541
(cherry picked from commit 037657de7e5ccd4a37054829874a209b82fb8be7)
---
clang/lib/Format/TokenAnnotator.cpp | 6 ++++--
clang/unittests/Format/TokenAnnotatorTest.cpp | 5 +++++
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 44580d8624684..11b941c5a0411 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3961,8 +3961,10 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
FormatToken *AfterLastAttribute = nullptr;
FormatToken *ClosingParen = nullptr;
- for (auto *Tok = FirstNonComment ? FirstNonComment->Next : nullptr; Tok;
- Tok = Tok->Next) {
+ for (auto *Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
+ ? FirstNonComment->Next
+ : nullptr;
+ Tok; Tok = Tok->Next) {
if (Tok->is(TT_StartOfName))
SeenName = true;
if (Tok->Previous->EndsCppAttributeGroup)
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index b7b8a21b726b6..757db66c3e298 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1073,6 +1073,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsOverloadedOperators) {
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_FunctionDeclarationName);
EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_OverloadedOperatorLParen);
+
+ Tokens = annotate("using std::operator==;");
+ ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ // Not TT_FunctionDeclarationName.
+ EXPECT_TOKEN(Tokens[3], tok::kw_operator, TT_Unknown);
}
TEST_F(TokenAnnotatorTest, OverloadedOperatorInTemplate) {
More information about the llvm-branch-commits
mailing list