kbobyrev updated this revision to Diff 304464.
kbobyrev added a comment.

Actually reset RenameTests.cpp


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D71880/new/

https://reviews.llvm.org/D71880

Files:
  clang-tools-extra/clangd/refactor/Rename.cpp

Index: clang-tools-extra/clangd/refactor/Rename.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/Rename.cpp
+++ clang-tools-extra/clangd/refactor/Rename.cpp
@@ -92,6 +92,7 @@
        targetDecl(SelectedNode->ASTNode,
                   DeclRelation::Alias | DeclRelation::TemplatePattern)) {
     // Get to CXXRecordDecl from constructor or destructor.
+    // FIXME(kirillbobyrev): Just use getRenameRoot?
     D = tooling::getCanonicalSymbolDeclaration(D);
     Result.insert(D);
   }
@@ -219,23 +220,80 @@
   return error("Cannot rename symbol: {0}", Message(Reason));
 }
 
+const Decl *getRenameRootDecl(const ClassTemplateSpecializationDecl *D) {
+  return D->getSpecializedTemplate()->getTemplatedDecl();
+}
+
+const Decl *getRenameRootDecl(const TemplateDecl *D) {
+  return D->getTemplatedDecl();
+}
+
+const Decl *getRenameRootDecl(const CXXMethodDecl *D) {
+  const auto *Result = D;
+  if (const auto *InstantiatedMethod = D->getInstantiatedFromMemberFunction())
+    Result = cast<CXXMethodDecl>(InstantiatedMethod);
+  while (Result->isVirtual() && Result->size_overridden_methods())
+    Result = *Result->overridden_methods().begin();
+  return Result;
+}
+
+const Decl *getRenameRootDecl(const FunctionDecl *D) {
+  const auto *Definition = D->getDefinition();
+  const auto *Candidate = Definition ? Definition : D->getMostRecentDecl();
+  return Candidate->isTemplateInstantiation()
+             ? Candidate->getPrimaryTemplate()->getTemplatedDecl()
+             : Candidate;
+}
+
+const Decl *getRenameRootDecl(const FieldDecl *D) {
+  // This is a hacky way to do something like
+  // CXXMethodDecl::getINstantiatedFromMemberFunction for the field because
+  // Clang AST does not store relevant information about the field that is
+  // instantiated.
+  const auto *TemplateSpec =
+      dyn_cast<ClassTemplateSpecializationDecl>(D->getParent());
+  if (!TemplateSpec)
+    return D;
+  const auto *FieldParent = TemplateSpec->getTemplateInstantiationPattern();
+  if (!FieldParent)
+    return D;
+  for (const auto *Field : FieldParent->fields())
+    if (Field->getFieldIndex() == D->getFieldIndex() &&
+        Field->getLocation() == D->getLocation())
+      return Field;
+  return D;
+}
+
+// FIXME(kirillbobyrev): Write documentation.
+const Decl *getRenameRootDecl(const Decl *D) {
+  const auto *Candidate = D;
+  if (const auto *RD = dyn_cast<RecordDecl>(D)) {
+    const auto *Definition = RD->getDefinition();
+    Candidate = Definition ? Definition : RD->getMostRecentDecl();
+  }
+  if (const auto *Template = dyn_cast<TemplateDecl>(Candidate))
+    return getRenameRootDecl(Template);
+  if (const auto *ClassTemplateSpecialization =
+          dyn_cast<ClassTemplateSpecializationDecl>(Candidate))
+    return getRenameRootDecl(ClassTemplateSpecialization);
+  if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(Candidate))
+    return getRenameRootDecl(Constructor->getParent());
+  if (const auto *Destructor = dyn_cast<CXXDestructorDecl>(Candidate))
+    return getRenameRootDecl(Destructor->getParent());
+  if (const auto *Method = dyn_cast<CXXMethodDecl>(Candidate))
+    return getRenameRootDecl(Method);
+  if (const auto *Function = dyn_cast<FunctionDecl>(Candidate))
+    return getRenameRootDecl(Function);
+  if (const auto *Field = dyn_cast<FieldDecl>(Candidate))
+    return getRenameRootDecl(Field);
+  return Candidate;
+}
+
 // Return all rename occurrences in the main file.
 std::vector<SourceLocation> findOccurrencesWithinFile(ParsedAST &AST,
                                                       const NamedDecl &ND) {
   trace::Span Tracer("FindOccurrenceeWithinFile");
-  // If the cursor is at the underlying CXXRecordDecl of the
-  // ClassTemplateDecl, ND will be the CXXRecordDecl. In this case, we need to
-  // get the primary template manually.
-  // getUSRsForDeclaration will find other related symbols, e.g. virtual and its
-  // overriddens, primary template and all explicit specializations.
-  // FIXME: Get rid of the remaining tooling APIs.
-  const auto *RenameDecl =
-      ND.getDescribedTemplate() ? ND.getDescribedTemplate() : &ND;
-  std::vector<std::string> RenameUSRs =
-      tooling::getUSRsForDeclaration(RenameDecl, AST.getASTContext());
-  llvm::DenseSet<SymbolID> TargetIDs;
-  for (auto &USR : RenameUSRs)
-    TargetIDs.insert(SymbolID(USR));
+  const auto *RenameDeclRoot = getRenameRootDecl(&ND);
 
   std::vector<SourceLocation> Results;
   for (Decl *TopLevelDecl : AST.getLocalTopLevelDecls()) {
@@ -243,11 +301,11 @@
       if (Ref.Targets.empty())
         return;
       for (const auto *Target : Ref.Targets) {
-        auto ID = getSymbolID(Target);
-        if (!ID || TargetIDs.find(ID) == TargetIDs.end())
+        if (getRenameRootDecl(Target) == RenameDeclRoot) {
+          Results.push_back(Ref.NameLoc);
           return;
+        }
       }
-      Results.push_back(Ref.NameLoc);
     });
   }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to