[clang] 7cd6b0c - [clang] Do not consider the template arguments of bases to be bases themselves

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 1 16:18:31 PDT 2020


Author: Nathan Ridge
Date: 2020-09-01T19:18:03-04:00
New Revision: 7cd6b0c3b5db61be94ed7f494d5036b2c30aaf3f

URL: https://github.com/llvm/llvm-project/commit/7cd6b0c3b5db61be94ed7f494d5036b2c30aaf3f
DIFF: https://github.com/llvm/llvm-project/commit/7cd6b0c3b5db61be94ed7f494d5036b2c30aaf3f.diff

LOG: [clang] Do not consider the template arguments of bases to be bases themselves

Fixes https://github.com/clangd/clangd/issues/504

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

Added: 
    

Modified: 
    clang/lib/Index/IndexTypeSourceInfo.cpp
    clang/test/Index/Core/index-source.cpp
    clang/unittests/Index/IndexTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Index/IndexTypeSourceInfo.cpp b/clang/lib/Index/IndexTypeSourceInfo.cpp
index b9fc90040cfc..ec4ca23942ca 100644
--- a/clang/lib/Index/IndexTypeSourceInfo.cpp
+++ b/clang/lib/Index/IndexTypeSourceInfo.cpp
@@ -8,6 +8,7 @@
 
 #include "IndexingContext.h"
 #include "clang/AST/RecursiveASTVisitor.h"
+#include "llvm/ADT/ScopeExit.h"
 
 using namespace clang;
 using namespace index;
@@ -160,6 +161,26 @@ class TypeIndexer : public RecursiveASTVisitor<TypeIndexer> {
     return true;
   }
 
+  bool TraverseTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
+    if (!WalkUpFromTemplateSpecializationTypeLoc(TL))
+      return false;
+    if (!TraverseTemplateName(TL.getTypePtr()->getTemplateName()))
+      return false;
+
+    // The relations we have to `Parent` do not apply to our template arguments,
+    // so clear them while visiting the args.
+    SmallVector<SymbolRelation, 3> SavedRelations = Relations;
+    Relations.clear();
+    auto ResetSavedRelations =
+        llvm::make_scope_exit([&] { this->Relations = SavedRelations; });
+    for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
+      if (!TraverseTemplateArgumentLoc(TL.getArgLoc(I)))
+        return false;
+    }
+
+    return true;
+  }
+
   bool VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc TL) {
     auto *T = TL.getTypePtr();
     if (!T)

diff  --git a/clang/test/Index/Core/index-source.cpp b/clang/test/Index/Core/index-source.cpp
index 371265b115b2..781343e008fa 100644
--- a/clang/test/Index/Core/index-source.cpp
+++ b/clang/test/Index/Core/index-source.cpp
@@ -560,3 +560,11 @@ class SubclassOffsetof : public Struct {
 };
 
 }
+
+namespace clangd_issue_504 {
+class A {};
+template <typename> class B {};
+class C : B<A> {};
+// CHECK: [[@LINE-1]]:13 | class/C++ | A | c:@N at clangd_issue_504@S at A | <no-cgname> | Ref,RelCont | rel: 1
+// CHECK-NEXT: RelCont | C | c:@N at clangd_issue_504@S at C
+} // namespace clangd_issue_504

diff  --git a/clang/unittests/Index/IndexTests.cpp b/clang/unittests/Index/IndexTests.cpp
index 52744e101f92..0ea78f10f72d 100644
--- a/clang/unittests/Index/IndexTests.cpp
+++ b/clang/unittests/Index/IndexTests.cpp
@@ -334,6 +334,20 @@ TEST(IndexTest, VisitDefaultArgs) {
                              WrittenAt(Position(3, 20)))));
 }
 
+TEST(IndexTest, RelationBaseOf) {
+  std::string Code = R"cpp(
+    class A {};
+    template <typename> class B {};
+    class C : B<A> {};
+  )cpp";
+  auto Index = std::make_shared<Indexer>();
+  tooling::runToolOnCode(std::make_unique<IndexAction>(Index), Code);
+  // A should not be the base of anything.
+  EXPECT_THAT(Index->Symbols,
+              Contains(AllOf(QName("A"), HasRole(SymbolRole::Reference),
+                             Not(HasRole(SymbolRole::RelationBaseOf)))));
+}
+
 } // namespace
 } // namespace index
 } // namespace clang


        


More information about the cfe-commits mailing list