Skip to content

Commit 54cdd97

Browse files
owencac-rhodes
authored andcommitted
[clang-format] Annotate ::operator and Foo::operator correctly (#164048)
This effectively reverts commit b5f6689 and fixes #111011 more narrowly. Fixes #160513 (cherry picked from commit 3bb9d4a)
1 parent ceeb930 commit 54cdd97

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3775,18 +3775,12 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
37753775
if (Current.is(TT_FunctionDeclarationName))
37763776
return true;
37773777

3778-
if (!Current.Tok.getIdentifierInfo())
3778+
if (Current.isNoneOf(tok::identifier, tok::kw_operator))
37793779
return false;
37803780

37813781
const auto *Prev = Current.getPreviousNonComment();
37823782
assert(Prev);
37833783

3784-
if (Prev->is(tok::coloncolon))
3785-
Prev = Prev->Previous;
3786-
3787-
if (!Prev)
3788-
return false;
3789-
37903784
const auto &Previous = *Prev;
37913785

37923786
if (const auto *PrevPrev = Previous.getPreviousNonComment();
@@ -3835,6 +3829,8 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
38353829

38363830
// Find parentheses of parameter list.
38373831
if (Current.is(tok::kw_operator)) {
3832+
if (Line.startsWith(tok::kw_friend))
3833+
return true;
38383834
if (Previous.Tok.getIdentifierInfo() &&
38393835
!Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
38403836
return true;

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsOverloadedOperators) {
11291129
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
11301130
// Not TT_FunctionDeclarationName.
11311131
EXPECT_TOKEN(Tokens[3], tok::kw_operator, TT_Unknown);
1132+
1133+
Tokens = annotate("SomeAPI::operator()();");
1134+
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
1135+
// Not TT_FunctionDeclarationName.
1136+
EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown);
11321137
}
11331138

11341139
TEST_F(TokenAnnotatorTest, OverloadedOperatorInTemplate) {

0 commit comments

Comments
 (0)