Skip to content

Commit 3fc24a2

Browse files
authored
[clang-format] Fix a bug in annotating class member names (llvm#165351)
For declarations like `Type* ::Class::member...`, `Class` was not annotated as `TT_StartOfName`as it should be. This prevented `member` from being updated to `TT_FunctionDeclarationName` if `member` was a function name. Fixes llvm#164866
1 parent e8a1162 commit 3fc24a2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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->endsSequence(tok::coloncolon,
2679+
TT_PointerOrReference)) {
26782680
return true;
2681+
}
26792682

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

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsOverloadedOperators) {
11191119
EXPECT_TOKEN(Tokens[8], tok::amp, TT_PointerOrReference);
11201120
EXPECT_TOKEN(Tokens[12], tok::amp, TT_PointerOrReference);
11211121

1122+
Tokens = annotate("::foo::bar& ::foo::bar::operator=(::foo::bar& other);");
1123+
ASSERT_EQ(Tokens.size(), 22u) << Tokens;
1124+
EXPECT_TOKEN(Tokens[6], tok::identifier, TT_FunctionDeclarationName);
1125+
EXPECT_TOKEN(Tokens[17], tok::amp, TT_PointerOrReference);
1126+
11221127
Tokens = annotate("SomeLoooooooooooooooooType::Awaitable\n"
11231128
"SomeLoooooooooooooooooType::operator co_await();");
11241129
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
@@ -3484,6 +3489,10 @@ TEST_F(TokenAnnotatorTest, StartOfName) {
34843489
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
34853490
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown); // Not StartOfName
34863491

3492+
Tokens = annotate("int* ::foo::bar;");
3493+
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
3494+
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_StartOfName);
3495+
34873496
auto Style = getLLVMStyle();
34883497
Style.StatementAttributeLikeMacros.push_back("emit");
34893498
Tokens = annotate("emit foo = 0;", Style);

0 commit comments

Comments
 (0)