Skip to content

Commit

Permalink
[Index] Fix reported references in presence of template type aliases
Browse files Browse the repository at this point in the history
Summary: See the added test for an example.

Reviewers: kadircet

Reviewed By: kadircet

Subscribers: jkorous, arphaman, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D62303

llvm-svn: 361511
  • Loading branch information
ilya-biryukov committed May 23, 2019
1 parent fd11a5f commit 3467584
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
11 changes: 11 additions & 0 deletions clang-tools-extra/clangd/unittests/XRefsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,17 @@ TEST(LocateSymbol, Ambiguous) {
ElementsAre(Sym("Foo"), Sym("Foo")));
}

TEST(LocateSymbol, TemplateTypedefs) {
auto T = Annotations(R"cpp(
template <class T> struct function {};
template <class T> using callback = function<T()>;
c^allback<int> foo;
)cpp");
auto AST = TestTU::withCode(T.code()).build();
EXPECT_THAT(locateSymbolAt(AST, T.point()), ElementsAre(Sym("callback")));
}

TEST(LocateSymbol, RelPathsInCompileCommand) {
// The source is in "/clangd-test/src".
// We build in "/clangd-test/build".
Expand Down
44 changes: 28 additions & 16 deletions clang/lib/Index/IndexTypeSourceInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,29 +133,41 @@ class TypeIndexer : public RecursiveASTVisitor<TypeIndexer> {
return true;
}

template<typename TypeLocType>
bool HandleTemplateSpecializationTypeLoc(TypeLocType TL) {
if (const auto *T = TL.getTypePtr()) {
if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) {
if (!RD->isImplicit() || IndexCtx.shouldIndexImplicitInstantiation()) {
IndexCtx.handleReference(RD, TL.getTemplateNameLoc(), Parent,
ParentDC, SymbolRoleSet(), Relations);
return true;
}
}
if (const TemplateDecl *D = T->getTemplateName().getAsTemplateDecl())
IndexCtx.handleReference(D, TL.getTemplateNameLoc(), Parent, ParentDC,
SymbolRoleSet(), Relations);
void HandleTemplateSpecializationTypeLoc(TemplateName TemplName,
SourceLocation TemplNameLoc,
CXXRecordDecl *ResolvedClass,
bool IsTypeAlias) {
// In presence of type aliases, the resolved class was never written in
// the code so don't report it.
if (!IsTypeAlias && ResolvedClass &&
(!ResolvedClass->isImplicit() ||
IndexCtx.shouldIndexImplicitInstantiation())) {
IndexCtx.handleReference(ResolvedClass, TemplNameLoc, Parent, ParentDC,
SymbolRoleSet(), Relations);
} else if (const TemplateDecl *D = TemplName.getAsTemplateDecl()) {
IndexCtx.handleReference(D, TemplNameLoc, Parent, ParentDC,
SymbolRoleSet(), Relations);
}
return true;
}

bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
return HandleTemplateSpecializationTypeLoc(TL);
auto *T = TL.getTypePtr();
if (!T)
return true;
HandleTemplateSpecializationTypeLoc(
T->getTemplateName(), TL.getTemplateNameLoc(), T->getAsCXXRecordDecl(),
T->isTypeAlias());
return true;
}

bool VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc TL) {
return HandleTemplateSpecializationTypeLoc(TL);
auto *T = TL.getTypePtr();
if (!T)
return true;
HandleTemplateSpecializationTypeLoc(
T->getTemplateName(), TL.getTemplateNameLoc(), T->getAsCXXRecordDecl(),
/*IsTypeAlias=*/false);
return true;
}

bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Expand Down

0 comments on commit 3467584

Please sign in to comment.