Skip to content

Commit cc6d1f8

Browse files
[clangd] When finding refs for a renaming alias, do not return refs to underlying decls
Fixes clangd/clangd#515 Differential Revision: https://reviews.llvm.org/D87225
1 parent 9f9f89d commit cc6d1f8

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

clang-tools-extra/clangd/XRefs.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,11 +1140,21 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
11401140
} else {
11411141
// Handle references to Decls.
11421142

1143-
// We also show references to the targets of using-decls, so we include
1144-
// DeclRelation::Underlying.
1145-
DeclRelationSet Relations = DeclRelation::TemplatePattern |
1146-
DeclRelation::Alias | DeclRelation::Underlying;
1147-
auto Decls = getDeclAtPosition(AST, *CurLoc, Relations);
1143+
DeclRelationSet Relations =
1144+
DeclRelation::TemplatePattern | DeclRelation::Alias;
1145+
std::vector<const NamedDecl *> Decls =
1146+
getDeclAtPosition(AST, *CurLoc, Relations);
1147+
std::vector<const NamedDecl *> NonrenamingAliasUnderlyingDecls;
1148+
// If the results include a *non-renaming* alias, get its
1149+
// underlying decls as well. (See similar logic in locateASTReferent()).
1150+
for (const NamedDecl *D : Decls) {
1151+
if (llvm::isa<UsingDecl>(D) || llvm::isa<UnresolvedUsingValueDecl>(D)) {
1152+
for (const NamedDecl *AD :
1153+
getDeclAtPosition(AST, *CurLoc, DeclRelation::Underlying))
1154+
NonrenamingAliasUnderlyingDecls.push_back(AD);
1155+
}
1156+
}
1157+
llvm::copy(NonrenamingAliasUnderlyingDecls, std::back_inserter(Decls));
11481158

11491159
// We traverse the AST to find references in the main file.
11501160
auto MainFileRefs = findRefs(Decls, AST);

clang-tools-extra/clangd/unittests/XRefsTests.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,13 @@ TEST(FindReferences, WithinAST) {
15881588
auto lambda = [x = [[waldo]]](){};
15891589
}
15901590
)cpp",
1591+
R"cpp(// Renaming alias
1592+
template <typename> class Vector {};
1593+
using [[^X]] = Vector<int>;
1594+
[[X]] x1;
1595+
Vector<int> x2;
1596+
Vector<double> y;
1597+
)cpp",
15911598
};
15921599
for (const char *Test : Tests) {
15931600
Annotations T(Test);

0 commit comments

Comments
 (0)