https://github.com/timon-ul updated 
https://github.com/llvm/llvm-project/pull/177273

>From ff644a0ff41d1521436567b10501e112bda93759 Mon Sep 17 00:00:00 2001
From: Timon Ulrich <[email protected]>
Date: Sat, 17 Jan 2026 03:16:06 +0100
Subject: [PATCH 1/6] First step towards indexing template instantiations

---
 clang-tools-extra/clangd/XRefs.cpp            | 42 +++++++++++--------
 .../clangd/index/SymbolCollector.cpp          | 13 +++++-
 .../clangd/unittests/XRefsTests.cpp           | 20 +++++++++
 clang/include/clang/Index/IndexingOptions.h   |  2 +-
 clang/lib/Index/IndexDecl.cpp                 | 33 ++++++++++++++-
 clang/lib/Index/IndexTypeSourceInfo.cpp       |  9 ++++
 clang/lib/Index/IndexingContext.cpp           |  4 +-
 7 files changed, 98 insertions(+), 25 deletions(-)

diff --git a/clang-tools-extra/clangd/XRefs.cpp 
b/clang-tools-extra/clangd/XRefs.cpp
index 8a24d19a7d129..a517abba7762a 100644
--- a/clang-tools-extra/clangd/XRefs.cpp
+++ b/clang-tools-extra/clangd/XRefs.cpp
@@ -306,25 +306,31 @@ std::vector<LocatedSymbol> 
findImplementors(llvm::DenseSet<SymbolID> IDs,
 
   RelationsRequest Req;
   Req.Predicate = Predicate;
-  Req.Subjects = std::move(IDs);
+  llvm::DenseSet<SymbolID> RecursiveSearch = std::move(IDs);
   std::vector<LocatedSymbol> Results;
-  Index->relations(Req, [&](const SymbolID &Subject, const Symbol &Object) {
-    auto DeclLoc =
-        indexToLSPLocation(Object.CanonicalDeclaration, MainFilePath);
-    if (!DeclLoc) {
-      elog("Find overrides: {0}", DeclLoc.takeError());
-      return;
-    }
-    Results.emplace_back();
-    Results.back().Name = Object.Name.str();
-    Results.back().PreferredDeclaration = *DeclLoc;
-    auto DefLoc = indexToLSPLocation(Object.Definition, MainFilePath);
-    if (!DefLoc) {
-      elog("Failed to convert location: {0}", DefLoc.takeError());
-      return;
-    }
-    Results.back().Definition = *DefLoc;
-  });
+
+  while (!RecursiveSearch.empty()) {
+    Req.Subjects = std::move(RecursiveSearch);
+    RecursiveSearch = {};
+    Index->relations(Req, [&](const SymbolID &Subject, const Symbol &Object) {
+      auto DeclLoc =
+          indexToLSPLocation(Object.CanonicalDeclaration, MainFilePath);
+      if (!DeclLoc) {
+        elog("Find overrides: {0}", DeclLoc.takeError());
+        return;
+      }
+      Results.emplace_back();
+      Results.back().Name = Object.Name.str();
+      Results.back().PreferredDeclaration = *DeclLoc;
+      auto DefLoc = indexToLSPLocation(Object.Definition, MainFilePath);
+      if (!DefLoc) {
+        elog("Failed to convert location: {0}", DefLoc.takeError());
+        return;
+      }
+      Results.back().Definition = *DefLoc;
+      RecursiveSearch.insert(Object.ID);
+    });
+  }
   return Results;
 }
 
diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp 
b/clang-tools-extra/clangd/index/SymbolCollector.cpp
index bd974e4c18818..8d905ad1fea10 100644
--- a/clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -603,6 +603,12 @@ bool SymbolCollector::handleDeclOccurrence(
   assert(ASTCtx && PP && HeaderFileURIs);
   assert(CompletionAllocator && CompletionTUInfo);
   assert(ASTNode.OrigD);
+  const NamedDecl *NOD = dyn_cast<NamedDecl>(ASTNode.OrigD);
+  std::string NameOD = "";
+  if (NOD){
+    NameOD = printName(*ASTCtx, *NOD);
+    NameOD += printTemplateSpecializationArgs(*NOD);
+  }
   // Indexing API puts canonical decl into D, which might not have a valid
   // source location for implicit/built-in decls. Fallback to original decl in
   // such cases.
@@ -896,9 +902,12 @@ void SymbolCollector::processRelations(
     //       in the index and find nothing, but that's a situation they
     //       probably need to handle for other reasons anyways.
     // We currently do (B) because it's simpler.
-    if (*RKind == RelationKind::BaseOf)
+    if (*RKind == RelationKind::BaseOf) {
+      std::string SubjectName = printName(*ASTCtx, ND) + 
printTemplateSpecializationArgs(ND);
+      const auto *Sym = Symbols.find(ObjectID);
+      std::string ObjectName = (Sym->Scope + Sym->Name + 
Sym->TemplateSpecializationArgs).str();
       this->Relations.insert({ID, *RKind, ObjectID});
-    else if (*RKind == RelationKind::OverriddenBy)
+    } else if (*RKind == RelationKind::OverriddenBy)
       this->Relations.insert({ObjectID, *RKind, ID});
   }
 }
diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp 
b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
index 4106c6cf7b2d0..5cefbeb78071a 100644
--- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -1958,6 +1958,26 @@ TEST(FindImplementations, InheritanceObjC) {
                                        Code.range("protocolDef"))));
 }
 
+TEST(FindImplementations, InheritanceRecursive) {
+  Annotations Main(R"cpp(
+    template <typename... T>
+    struct Inherit : T... {};
+
+    struct Al$Alpha^pha {};
+
+    struct $Impl1[[impl]]: Inherit<Alpha> {};
+  )cpp");
+
+  TestTU TU;
+  TU.Code = std::string(Main.code());
+  auto AST = TU.build();
+  auto Index = TU.index();
+
+  EXPECT_THAT(
+      findImplementations(AST, Main.point("Alpha"), Index.get()),
+      ElementsAre(sym("impl", Main.range("Impl1"), Main.range("Impl1"))));
+}
+
 TEST(FindImplementations, CaptureDefinition) {
   llvm::StringRef Test = R"cpp(
     struct Base {
diff --git a/clang/include/clang/Index/IndexingOptions.h 
b/clang/include/clang/Index/IndexingOptions.h
index c670797e9fa60..1094ec46dc9af 100644
--- a/clang/include/clang/Index/IndexingOptions.h
+++ b/clang/include/clang/Index/IndexingOptions.h
@@ -27,7 +27,7 @@ struct IndexingOptions {
   SystemSymbolFilterKind SystemSymbolFilter =
       SystemSymbolFilterKind::DeclarationsOnly;
   bool IndexFunctionLocals = false;
-  bool IndexImplicitInstantiation = false;
+  bool IndexImplicitInstantiation = true;
   bool IndexMacros = true;
   // Whether to index macro definitions in the Preprocessor when preprocessor
   // callback is not available (e.g. after parsing has finished). Note that
diff --git a/clang/lib/Index/IndexDecl.cpp b/clang/lib/Index/IndexDecl.cpp
index df875e0b40079..51b400f915bd3 100644
--- a/clang/lib/Index/IndexDecl.cpp
+++ b/clang/lib/Index/IndexDecl.cpp
@@ -14,6 +14,7 @@
 #include "clang/AST/DeclVisitor.h"
 #include "clang/Index/IndexDataConsumer.h"
 #include "clang/Index/IndexSymbol.h"
+#include "llvm/Support/Casting.h"
 
 using namespace clang;
 using namespace index;
@@ -734,7 +735,37 @@ class IndexingDeclVisitor : public 
ConstDeclVisitor<IndexingDeclVisitor, bool> {
       indexTemplateParameters(Params, Parent);
     }
 
-    return Visit(Parent);
+    bool shouldContinue = Visit(Parent);
+    if (!shouldContinue)
+      return false;
+
+    // TODO: cleanup maybe, so far copy paste from
+    // RecursiveASTVisitor::TraverseTemplateInstantiation, we have technically
+    // `shouldIndexImplicitInstantiation()` available here, but the logic is
+    // different and I am confused.
+    if (const auto *CTD = llvm::dyn_cast<ClassTemplateDecl>(D))
+      for (auto *SD : CTD->specializations())
+        for (auto *RD : SD->redecls()) {
+          assert(!cast<CXXRecordDecl>(RD)->isInjectedClassName());
+          switch (cast<ClassTemplateSpecializationDecl>(RD)
+                      ->getSpecializationKind()) {
+          // Visit the implicit instantiations with the requested pattern.
+          case TSK_Undeclared:
+          case TSK_ImplicitInstantiation:
+            Visit(RD);
+            break;
+
+          // We don't need to do anything on an explicit instantiation
+          // or explicit specialization because there will be an explicit
+          // node for it elsewhere.
+          case TSK_ExplicitInstantiationDeclaration:
+          case TSK_ExplicitInstantiationDefinition:
+          case TSK_ExplicitSpecialization:
+            break;
+          }
+        }
+
+    return true;
   }
 
   bool VisitConceptDecl(const ConceptDecl *D) {
diff --git a/clang/lib/Index/IndexTypeSourceInfo.cpp 
b/clang/lib/Index/IndexTypeSourceInfo.cpp
index c9ad36b5406c5..be3d75e84c351 100644
--- a/clang/lib/Index/IndexTypeSourceInfo.cpp
+++ b/clang/lib/Index/IndexTypeSourceInfo.cpp
@@ -10,6 +10,7 @@
 #include "clang/AST/ASTConcept.h"
 #include "clang/AST/PrettyPrinter.h"
 #include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/AST/TypeBase.h"
 #include "clang/AST/TypeLoc.h"
 #include "clang/Sema/HeuristicResolver.h"
 #include "llvm/ADT/ScopeExit.h"
@@ -193,6 +194,14 @@ class TypeIndexer : public 
RecursiveASTVisitor<TypeIndexer> {
     return true;
   }
 
+  // bool TraverseSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc TL,
+  //                                           bool TraverseQualifier) {
+  //   auto Type = TL.getAs<TemplateSpecializationTypeLoc>();
+  //   if (!Type.isNull())
+  //     TraverseTemplateSpecializationTypeLoc(Type, TraverseQualifier);
+  //   return true;
+  // }
+
   bool 
VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc 
TL) {
     auto *T = TL.getTypePtr();
     if (!T)
diff --git a/clang/lib/Index/IndexingContext.cpp 
b/clang/lib/Index/IndexingContext.cpp
index bdd6c5acf1d34..c6843cf46cf8e 100644
--- a/clang/lib/Index/IndexingContext.cpp
+++ b/clang/lib/Index/IndexingContext.cpp
@@ -402,9 +402,7 @@ bool IndexingContext::handleDeclOccurrence(const Decl *D, 
SourceLocation Loc,
   if (!OrigD)
     OrigD = D;
 
-  if (isTemplateImplicitInstantiation(D)) {
-    if (!IsRef)
-      return true;
+  if (isTemplateImplicitInstantiation(D) && IsRef) {
     D = adjustTemplateImplicitInstantiation(D);
     if (!D)
       return true;

>From 511ba41d9153e13247fd5fb9eb61ec330dfb4e21 Mon Sep 17 00:00:00 2001
From: Timon Ulrich <[email protected]>
Date: Sat, 17 Jan 2026 03:20:22 +0100
Subject: [PATCH 2/6] New methods for visiting SubstTemplateType

---
 clang/lib/Index/IndexTypeSourceInfo.cpp | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Index/IndexTypeSourceInfo.cpp 
b/clang/lib/Index/IndexTypeSourceInfo.cpp
index be3d75e84c351..e16d8ac791898 100644
--- a/clang/lib/Index/IndexTypeSourceInfo.cpp
+++ b/clang/lib/Index/IndexTypeSourceInfo.cpp
@@ -194,13 +194,21 @@ class TypeIndexer : public 
RecursiveASTVisitor<TypeIndexer> {
     return true;
   }
 
-  // bool TraverseSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc TL,
-  //                                           bool TraverseQualifier) {
-  //   auto Type = TL.getAs<TemplateSpecializationTypeLoc>();
-  //   if (!Type.isNull())
-  //     TraverseTemplateSpecializationTypeLoc(Type, TraverseQualifier);
-  //   return true;
-  // }
+  bool VisitSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc TL) {
+    auto QT = TL.getTypePtr()->getReplacementType();
+    auto *T = QT->getAsNonAliasTemplateSpecializationType();
+    if (!T)
+      return true;
+    HandleTemplateSpecializationTypeLoc(
+        T->getTemplateName(), TL.getTemplateNameLoc(), T->getAsCXXRecordDecl(),
+        T->isTypeAlias());
+    return true;
+  }
+
+  bool TraverseSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc TL,
+                                            bool TraverseQualifier) {
+    return true;
+  }
 
   bool 
VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc 
TL) {
     auto *T = TL.getTypePtr();

>From b5ffe8fb17140df17999a61693064bec25248ec2 Mon Sep 17 00:00:00 2001
From: Timon Ulrich <[email protected]>
Date: Wed, 21 Jan 2026 23:20:57 +0100
Subject: [PATCH 3/6] Implemented template inheritance handling

---
 .../clangd/index/SymbolCollector.cpp          | 22 ++++++------
 .../clangd/index/SymbolCollector.h            |  2 +-
 .../clangd/unittests/XRefsTests.cpp           | 36 ++++++++++++++-----
 clang/include/clang/Index/IndexingOptions.h   |  2 +-
 clang/lib/Index/IndexDecl.cpp                 | 33 +++++++----------
 clang/lib/Index/IndexTypeSourceInfo.cpp       | 19 +++++-----
 6 files changed, 61 insertions(+), 53 deletions(-)

diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp 
b/clang-tools-extra/clangd/index/SymbolCollector.cpp
index 8d905ad1fea10..f8ff9bf277ceb 100644
--- a/clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -603,12 +603,7 @@ bool SymbolCollector::handleDeclOccurrence(
   assert(ASTCtx && PP && HeaderFileURIs);
   assert(CompletionAllocator && CompletionTUInfo);
   assert(ASTNode.OrigD);
-  const NamedDecl *NOD = dyn_cast<NamedDecl>(ASTNode.OrigD);
-  std::string NameOD = "";
-  if (NOD){
-    NameOD = printName(*ASTCtx, *NOD);
-    NameOD += printTemplateSpecializationArgs(*NOD);
-  }
+
   // Indexing API puts canonical decl into D, which might not have a valid
   // source location for implicit/built-in decls. Fallback to original decl in
   // such cases.
@@ -679,7 +674,7 @@ bool SymbolCollector::handleDeclOccurrence(
   // refs, because the indexing code only populates relations for specific
   // occurrences. For example, RelationBaseOf is only populated for the
   // occurrence inside the base-specifier.
-  processRelations(*ND, ID, Relations);
+  processRelations(ID, *ASTNode.OrigD, Relations);
 
   bool CollectRef = static_cast<bool>(Opts.RefFilter & toRefKind(Roles));
   // Unlike other fields, e.g. Symbols (which use spelling locations), we use
@@ -881,7 +876,7 @@ bool SymbolCollector::handleMacroOccurrence(const 
IdentifierInfo *Name,
 }
 
 void SymbolCollector::processRelations(
-    const NamedDecl &ND, const SymbolID &ID,
+    const SymbolID &ID, const Decl &OrigD,
     ArrayRef<index::SymbolRelation> Relations) {
   for (const auto &R : Relations) {
     auto RKind = indexableRelation(R);
@@ -903,10 +898,15 @@ void SymbolCollector::processRelations(
     //       probably need to handle for other reasons anyways.
     // We currently do (B) because it's simpler.
     if (*RKind == RelationKind::BaseOf) {
-      std::string SubjectName = printName(*ASTCtx, ND) + 
printTemplateSpecializationArgs(ND);
-      const auto *Sym = Symbols.find(ObjectID);
-      std::string ObjectName = (Sym->Scope + Sym->Name + 
Sym->TemplateSpecializationArgs).str();
       this->Relations.insert({ID, *RKind, ObjectID});
+      // If the Subject is a template, we also want a relation to the
+      // template instantiation (OrigD) to record inheritance chains.
+      if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(&OrigD);
+          CTSD && !CTSD->isExplicitSpecialization()) {
+        auto OrigID = getSymbolIDCached(&OrigD);
+        if (OrigID)
+          this->Relations.insert({OrigID, *RKind, ObjectID});
+      }
     } else if (*RKind == RelationKind::OverriddenBy)
       this->Relations.insert({ObjectID, *RKind, ID});
   }
diff --git a/clang-tools-extra/clangd/index/SymbolCollector.h 
b/clang-tools-extra/clangd/index/SymbolCollector.h
index 4d51d747639b1..54a12ba122240 100644
--- a/clang-tools-extra/clangd/index/SymbolCollector.h
+++ b/clang-tools-extra/clangd/index/SymbolCollector.h
@@ -169,7 +169,7 @@ class SymbolCollector : public index::IndexDataConsumer {
                                bool IsMainFileSymbol);
   void addDefinition(const NamedDecl &, const Symbol &DeclSymbol,
                      bool SkipDocCheck);
-  void processRelations(const NamedDecl &ND, const SymbolID &ID,
+  void processRelations(const SymbolID &ID, const Decl &OrigD,
                         ArrayRef<index::SymbolRelation> Relations);
 
   std::optional<SymbolLocation> getTokenLocation(SourceLocation TokLoc);
diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp 
b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
index 5cefbeb78071a..9a382f4a8c257 100644
--- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -1878,8 +1878,8 @@ TEST(FindImplementations, Inheritance) {
       virtual void B$2^ar();
       void Concrete();  // No implementations for concrete methods.
     };
-    struct Child2 : Child1 {
-      void $3[[Foo]]() override;
+    struct $0[[Child2]] : Child1 {
+      void $1[[$3[[Foo]]]]() override;
       void $2[[Bar]]() override;
     };
     void FromReference() {
@@ -1958,14 +1958,27 @@ TEST(FindImplementations, InheritanceObjC) {
                                        Code.range("protocolDef"))));
 }
 
-TEST(FindImplementations, InheritanceRecursive) {
+TEST(FindImplementations, InheritanceTemplate) {
   Annotations Main(R"cpp(
+    class Fi$First^rst {};
+
+    class Sec$Second^ond {};
+
+    class Th$Third^ird {};
+
     template <typename... T>
-    struct Inherit : T... {};
+    struct $Third[[Inherit]] : T... {};
+
+    template struct $First[[Inherit]]<First>;
+
+    template<>
+    struct $Second[[Inherit]]<Second> : Second {};
 
-    struct Al$Alpha^pha {};
+    class $First[[Battler]] : Inherit<First> {};
 
-    struct $Impl1[[impl]]: Inherit<Alpha> {};
+    class $Second[[Beatrice]] : Inherit<Second> {};
+
+    class $Third[[Maria]] : Inherit<Third> {};
   )cpp");
 
   TestTU TU;
@@ -1973,9 +1986,14 @@ TEST(FindImplementations, InheritanceRecursive) {
   auto AST = TU.build();
   auto Index = TU.index();
 
-  EXPECT_THAT(
-      findImplementations(AST, Main.point("Alpha"), Index.get()),
-      ElementsAre(sym("impl", Main.range("Impl1"), Main.range("Impl1"))));
+  EXPECT_THAT(findImplementations(AST, Main.point("First"), Index.get()),
+              UnorderedPointwise(declRange(), Main.ranges("First")));
+
+  EXPECT_THAT(findImplementations(AST, Main.point("Second"), Index.get()),
+              UnorderedPointwise(declRange(), Main.ranges("Second")));
+
+  EXPECT_THAT(findImplementations(AST, Main.point("Third"), Index.get()),
+              UnorderedPointwise(declRange(), Main.ranges("Third")));
 }
 
 TEST(FindImplementations, CaptureDefinition) {
diff --git a/clang/include/clang/Index/IndexingOptions.h 
b/clang/include/clang/Index/IndexingOptions.h
index 1094ec46dc9af..c670797e9fa60 100644
--- a/clang/include/clang/Index/IndexingOptions.h
+++ b/clang/include/clang/Index/IndexingOptions.h
@@ -27,7 +27,7 @@ struct IndexingOptions {
   SystemSymbolFilterKind SystemSymbolFilter =
       SystemSymbolFilterKind::DeclarationsOnly;
   bool IndexFunctionLocals = false;
-  bool IndexImplicitInstantiation = true;
+  bool IndexImplicitInstantiation = false;
   bool IndexMacros = true;
   // Whether to index macro definitions in the Preprocessor when preprocessor
   // callback is not available (e.g. after parsing has finished). Note that
diff --git a/clang/lib/Index/IndexDecl.cpp b/clang/lib/Index/IndexDecl.cpp
index 51b400f915bd3..f0fe1d0af7791 100644
--- a/clang/lib/Index/IndexDecl.cpp
+++ b/clang/lib/Index/IndexDecl.cpp
@@ -739,30 +739,21 @@ class IndexingDeclVisitor : public 
ConstDeclVisitor<IndexingDeclVisitor, bool> {
     if (!shouldContinue)
       return false;
 
-    // TODO: cleanup maybe, so far copy paste from
-    // RecursiveASTVisitor::TraverseTemplateInstantiation, we have technically
-    // `shouldIndexImplicitInstantiation()` available here, but the logic is
-    // different and I am confused.
+    // Only check instantiation if D is canonical to prevent infinite cycling
+    if (D != D->getCanonicalDecl())
+      return true;
+
     if (const auto *CTD = llvm::dyn_cast<ClassTemplateDecl>(D))
       for (auto *SD : CTD->specializations())
         for (auto *RD : SD->redecls()) {
-          assert(!cast<CXXRecordDecl>(RD)->isInjectedClassName());
-          switch (cast<ClassTemplateSpecializationDecl>(RD)
-                      ->getSpecializationKind()) {
-          // Visit the implicit instantiations with the requested pattern.
-          case TSK_Undeclared:
-          case TSK_ImplicitInstantiation:
-            Visit(RD);
-            break;
-
-          // We don't need to do anything on an explicit instantiation
-          // or explicit specialization because there will be an explicit
-          // node for it elsewhere.
-          case TSK_ExplicitInstantiationDeclaration:
-          case TSK_ExplicitInstantiationDefinition:
-          case TSK_ExplicitSpecialization:
-            break;
-          }
+          auto *CTSD = cast<ClassTemplateSpecializationDecl>(RD);
+          // For now we are only interested in instantiations with inheritance.
+          if (!CTSD->hasDefinition() || CTSD->bases().empty())
+            continue;
+          // Explicit specialization is handled elsewhere
+          if (CTSD->isExplicitSpecialization())
+            continue;
+          Visit(RD);
         }
 
     return true;
diff --git a/clang/lib/Index/IndexTypeSourceInfo.cpp 
b/clang/lib/Index/IndexTypeSourceInfo.cpp
index e16d8ac791898..bb62b7d6f8d87 100644
--- a/clang/lib/Index/IndexTypeSourceInfo.cpp
+++ b/clang/lib/Index/IndexTypeSourceInfo.cpp
@@ -194,19 +194,18 @@ class TypeIndexer : public 
RecursiveASTVisitor<TypeIndexer> {
     return true;
   }
 
-  bool VisitSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc TL) {
-    auto QT = TL.getTypePtr()->getReplacementType();
-    auto *T = QT->getAsNonAliasTemplateSpecializationType();
+  bool TraverseSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc TL,
+                                            bool TraverseQualifier) {
+    const auto *T = TL.getTypePtr();
     if (!T)
       return true;
-    HandleTemplateSpecializationTypeLoc(
-        T->getTemplateName(), TL.getTemplateNameLoc(), T->getAsCXXRecordDecl(),
-        T->isTypeAlias());
-    return true;
-  }
+    auto QT = T->getReplacementType();
+    if (QT.isNull())
+      return true;
+
+    IndexCtx.handleReference(QT->getAsCXXRecordDecl(), TL.getNameLoc(), Parent,
+                             ParentDC, SymbolRoleSet(), Relations);
 
-  bool TraverseSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc TL,
-                                            bool TraverseQualifier) {
     return true;
   }
 

>From 53c04f63a1a5583d4200a86e7d065e5ec11b4cd2 Mon Sep 17 00:00:00 2001
From: Timon Ulrich <[email protected]>
Date: Thu, 22 Jan 2026 00:05:17 +0100
Subject: [PATCH 4/6] Errors do not meant we shouldn't keep recursing

---
 clang-tools-extra/clangd/XRefs.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/clangd/XRefs.cpp 
b/clang-tools-extra/clangd/XRefs.cpp
index a517abba7762a..79decd35baadd 100644
--- a/clang-tools-extra/clangd/XRefs.cpp
+++ b/clang-tools-extra/clangd/XRefs.cpp
@@ -313,6 +313,7 @@ std::vector<LocatedSymbol> 
findImplementors(llvm::DenseSet<SymbolID> IDs,
     Req.Subjects = std::move(RecursiveSearch);
     RecursiveSearch = {};
     Index->relations(Req, [&](const SymbolID &Subject, const Symbol &Object) {
+      RecursiveSearch.insert(Object.ID);
       auto DeclLoc =
           indexToLSPLocation(Object.CanonicalDeclaration, MainFilePath);
       if (!DeclLoc) {
@@ -328,7 +329,6 @@ std::vector<LocatedSymbol> 
findImplementors(llvm::DenseSet<SymbolID> IDs,
         return;
       }
       Results.back().Definition = *DefLoc;
-      RecursiveSearch.insert(Object.ID);
     });
   }
   return Results;

>From 6502814383b0bc95a0f69c05bf0aa126e4a9fa51 Mon Sep 17 00:00:00 2001
From: Timon Ulrich <[email protected]>
Date: Thu, 22 Jan 2026 00:28:59 +0100
Subject: [PATCH 5/6] Preventing refs for implicit instantiations

---
 clang-tools-extra/clangd/index/SymbolCollector.cpp | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp 
b/clang-tools-extra/clangd/index/SymbolCollector.cpp
index f8ff9bf277ceb..30f34324f5e58 100644
--- a/clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -677,6 +677,13 @@ bool SymbolCollector::handleDeclOccurrence(
   processRelations(ID, *ASTNode.OrigD, Relations);
 
   bool CollectRef = static_cast<bool>(Opts.RefFilter & toRefKind(Roles));
+  // For now we only want the bare minimum of information for a class
+  // instantiation such that we have symbols for the `BaseOf` relation.
+  if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D);
+      CTSD && CTSD->hasDefinition() && !CTSD->bases().empty() &&
+      !CTSD->isExplicitSpecialization()) {
+    CollectRef = false;
+  }
   // Unlike other fields, e.g. Symbols (which use spelling locations), we use
   // file locations for references (as it aligns the behavior of clangd's
   // AST-based xref).

>From 195fd77fca387bff1a4594437b0fb433ef243331 Mon Sep 17 00:00:00 2001
From: Timon Ulrich <[email protected]>
Date: Thu, 22 Jan 2026 11:05:50 +0100
Subject: [PATCH 6/6] More careful handling of casts

---
 clang/lib/Index/IndexDecl.cpp           | 5 ++---
 clang/lib/Index/IndexTypeSourceInfo.cpp | 9 +++++----
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Index/IndexDecl.cpp b/clang/lib/Index/IndexDecl.cpp
index f0fe1d0af7791..35f8f7e43ed7d 100644
--- a/clang/lib/Index/IndexDecl.cpp
+++ b/clang/lib/Index/IndexDecl.cpp
@@ -14,7 +14,6 @@
 #include "clang/AST/DeclVisitor.h"
 #include "clang/Index/IndexDataConsumer.h"
 #include "clang/Index/IndexSymbol.h"
-#include "llvm/Support/Casting.h"
 
 using namespace clang;
 using namespace index;
@@ -746,9 +745,9 @@ class IndexingDeclVisitor : public 
ConstDeclVisitor<IndexingDeclVisitor, bool> {
     if (const auto *CTD = llvm::dyn_cast<ClassTemplateDecl>(D))
       for (auto *SD : CTD->specializations())
         for (auto *RD : SD->redecls()) {
-          auto *CTSD = cast<ClassTemplateSpecializationDecl>(RD);
+          auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD);
           // For now we are only interested in instantiations with inheritance.
-          if (!CTSD->hasDefinition() || CTSD->bases().empty())
+          if (!CTSD || !CTSD->hasDefinition() || CTSD->bases().empty())
             continue;
           // Explicit specialization is handled elsewhere
           if (CTSD->isExplicitSpecialization())
diff --git a/clang/lib/Index/IndexTypeSourceInfo.cpp 
b/clang/lib/Index/IndexTypeSourceInfo.cpp
index bb62b7d6f8d87..ee70bc05914ad 100644
--- a/clang/lib/Index/IndexTypeSourceInfo.cpp
+++ b/clang/lib/Index/IndexTypeSourceInfo.cpp
@@ -202,11 +202,12 @@ class TypeIndexer : public 
RecursiveASTVisitor<TypeIndexer> {
     auto QT = T->getReplacementType();
     if (QT.isNull())
       return true;
+    auto *CXXRD = QT->getAsCXXRecordDecl();
+    if (!CXXRD)
+      return true;
 
-    IndexCtx.handleReference(QT->getAsCXXRecordDecl(), TL.getNameLoc(), Parent,
-                             ParentDC, SymbolRoleSet(), Relations);
-
-    return true;
+    return IndexCtx.handleReference(CXXRD, TL.getNameLoc(), Parent, ParentDC,
+                                    SymbolRoleSet(), Relations);
   }
 
   bool 
VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc 
TL) {

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to