[PATCH] D62303: [Index] Fix reported references in presence of template type aliases

Ilya Biryukov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu May 23 05:50:31 PDT 2019


ilya-biryukov created this revision.
ilya-biryukov added a reviewer: kadircet.
Herald added subscribers: arphaman, jkorous.
Herald added a project: clang.

See the added test for an example.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D62303

Files:
  clang-tools-extra/clangd/unittests/XRefsTests.cpp
  clang/lib/Index/IndexTypeSourceInfo.cpp


Index: clang/lib/Index/IndexTypeSourceInfo.cpp
===================================================================
--- clang/lib/Index/IndexTypeSourceInfo.cpp
+++ clang/lib/Index/IndexTypeSourceInfo.cpp
@@ -133,29 +133,43 @@
     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,
+  void HandleTemplateSpecializationTypeLoc(TemplateName TemplName,
+                                           SourceLocation TemplNameLoc,
+                                           CXXRecordDecl *ResolvedClass,
+                                           bool IsTypeAlias) {
+    if (ResolvedClass) {
+      // In presence of type aliases, the resolved class was never written in
+      // the code so don't report it.
+      if (!IsTypeAlias && (!ResolvedClass->isImplicit() ||
+                           IndexCtx.shouldIndexImplicitInstantiation())) {
+        IndexCtx.handleReference(ResolvedClass, TemplNameLoc, Parent, ParentDC,
                                  SymbolRoleSet(), Relations);
+        return;
+      }
     }
-    return true;
+    if (const TemplateDecl *D = TemplName.getAsTemplateDecl())
+      IndexCtx.handleReference(D, TemplNameLoc, Parent, ParentDC,
+                               SymbolRoleSet(), Relations);
   }
 
   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) {
Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -497,6 +497,17 @@
               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".


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D62303.200938.patch
Type: text/x-patch
Size: 3442 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190523/b90f075d/attachment.bin>


More information about the cfe-commits mailing list