Skip to content

Commit 3bb9d4a

Browse files
authored
[clang-format] Annotate ::operator and Foo::operator correctly (#164048)
This effectively reverts commit b5f6689 and fixes #111011 more narrowly. Fixes #160513
1 parent 49180d8 commit 3bb9d4a

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
@@ -3794,18 +3794,12 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
37943794
if (Current.is(TT_FunctionDeclarationName))
37953795
return true;
37963796

3797-
if (!Current.Tok.getIdentifierInfo())
3797+
if (Current.isNoneOf(tok::identifier, tok::kw_operator))
37983798
return false;
37993799

38003800
const auto *Prev = Current.getPreviousNonComment();
38013801
assert(Prev);
38023802

3803-
if (Prev->is(tok::coloncolon))
3804-
Prev = Prev->Previous;
3805-
3806-
if (!Prev)
3807-
return false;
3808-
38093803
const auto &Previous = *Prev;
38103804

38113805
if (const auto *PrevPrev = Previous.getPreviousNonComment();
@@ -3854,6 +3848,8 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
38543848

38553849
// Find parentheses of parameter list.
38563850
if (Current.is(tok::kw_operator)) {
3851+
if (Line.startsWith(tok::kw_friend))
3852+
return true;
38573853
if (Previous.Tok.getIdentifierInfo() &&
38583854
Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) {
38593855
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)