[clangd] When finding refs for a renaming alias, do not return refs to underlying...
authorNathan Ridge <zeratul976@hotmail.com>
Mon, 7 Sep 2020 06:28:46 +0000 (02:28 -0400)
committerNathan Ridge <zeratul976@hotmail.com>
Tue, 29 Sep 2020 01:18:31 +0000 (21:18 -0400)
Fixes https://github.com/clangd/clangd/issues/515

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

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

index b448745..9e8791c 100644 (file)
@@ -1140,11 +1140,21 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
   } else {
     // Handle references to Decls.
 
-    // We also show references to the targets of using-decls, so we include
-    // DeclRelation::Underlying.
-    DeclRelationSet Relations = DeclRelation::TemplatePattern |
-                                DeclRelation::Alias | DeclRelation::Underlying;
-    auto Decls = getDeclAtPosition(AST, *CurLoc, Relations);
+    DeclRelationSet Relations =
+        DeclRelation::TemplatePattern | DeclRelation::Alias;
+    std::vector<const NamedDecl *> Decls =
+        getDeclAtPosition(AST, *CurLoc, Relations);
+    std::vector<const NamedDecl *> NonrenamingAliasUnderlyingDecls;
+    // If the results include a *non-renaming* alias, get its
+    // underlying decls as well. (See similar logic in locateASTReferent()).
+    for (const NamedDecl *D : Decls) {
+      if (llvm::isa<UsingDecl>(D) || llvm::isa<UnresolvedUsingValueDecl>(D)) {
+        for (const NamedDecl *AD :
+             getDeclAtPosition(AST, *CurLoc, DeclRelation::Underlying))
+          NonrenamingAliasUnderlyingDecls.push_back(AD);
+      }
+    }
+    llvm::copy(NonrenamingAliasUnderlyingDecls, std::back_inserter(Decls));
 
     // We traverse the AST to find references in the main file.
     auto MainFileRefs = findRefs(Decls, AST);
index 2176b59..a48bb9c 100644 (file)
@@ -1588,6 +1588,13 @@ TEST(FindReferences, WithinAST) {
           auto lambda = [x = [[waldo]]](){};
         }
       )cpp",
+      R"cpp(// Renaming alias
+        template <typename> class Vector {};
+        using [[^X]] = Vector<int>;
+        [[X]] x1;
+        Vector<int> x2;
+        Vector<double> y;
+      )cpp",
   };
   for (const char *Test : Tests) {
     Annotations T(Test);