Skip to content

Commit 85036b2

Browse files
committed
[clang-format] Fix a bug in annotating operator functions
Fixes #164866
1 parent 44601d1 commit 85036b2

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,9 +2646,9 @@ class AnnotatingParser {
26462646
return false;
26472647
}
26482648

2649-
bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2650-
PreviousNotConst->Previous &&
2651-
PreviousNotConst->Previous->is(tok::hash);
2649+
const auto *PrevPrev = PreviousNotConst->Previous;
2650+
const bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2651+
PrevPrev && PrevPrev->is(tok::hash);
26522652

26532653
if (PreviousNotConst->is(TT_TemplateCloser)) {
26542654
return PreviousNotConst && PreviousNotConst->MatchingParen &&
@@ -2674,8 +2674,11 @@ class AnnotatingParser {
26742674
}
26752675

26762676
// *a or &a or &&a.
2677-
if (PreviousNotConst->is(TT_PointerOrReference))
2677+
if (PreviousNotConst->is(TT_PointerOrReference) ||
2678+
(PreviousNotConst->is(tok::coloncolon) && PrevPrev &&
2679+
PrevPrev->is(TT_PointerOrReference))) {
26782680
return true;
2681+
}
26792682

26802683
// MyClass a;
26812684
if (PreviousNotConst->isTypeName(LangOpts))

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,6 +2344,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
23442344
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_FunctionDeclarationName);
23452345
EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionDeclarationLParen);
23462346

2347+
Tokens = annotate("::foo::bar& ::foo::bar::operator=(::foo::bar& other);");
2348+
ASSERT_EQ(Tokens.size(), 22u) << Tokens;
2349+
EXPECT_TOKEN(Tokens[6], tok::identifier, TT_FunctionDeclarationName);
2350+
EXPECT_TOKEN(Tokens[17], tok::amp, TT_PointerOrReference);
2351+
23472352
Tokens = annotate("int iso_time(time_t);");
23482353
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
23492354
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);

0 commit comments

Comments
 (0)