]> granicus.if.org Git - clang/commitdiff
[rename] NFC, extract symbol canonicalization logic into function
authorAlex Lorenz <arphaman@gmail.com>
Wed, 2 Aug 2017 14:15:27 +0000 (14:15 +0000)
committerAlex Lorenz <arphaman@gmail.com>
Wed, 2 Aug 2017 14:15:27 +0000 (14:15 +0000)
This function will be used by the clang-refactor's rename actions

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@309813 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Tooling/Refactoring/Rename/USRFindingAction.h
lib/Tooling/Refactoring/Rename/USRFindingAction.cpp

index 8aafee95bc097078689cc59d75fa5d468bae3ac9..644a3988e44b49f639eb3e82f3eb4b9e22dc3630 100644 (file)
@@ -28,6 +28,15 @@ class NamedDecl;
 
 namespace tooling {
 
+/// Returns the canonical declaration that best represents a symbol that can be
+/// renamed.
+///
+/// The following canonicalization rules are currently used:
+///
+/// - A constructor is canonicalized to its class.
+/// - A destructor is canonicalized to its class.
+const NamedDecl *getCanonicalSymbolDeclaration(const NamedDecl *FoundDecl);
+
 struct USRFindingAction {
   USRFindingAction(ArrayRef<unsigned> SymbolOffsets,
                    ArrayRef<std::string> QualifiedNames, bool Force)
index 2769802ad2bc6c24b66a05ee915315f22ff6e6f7..bac3963e1bd9b5a3360cddf0c500fd9d4a3ed8ad 100644 (file)
@@ -39,6 +39,21 @@ using namespace llvm;
 namespace clang {
 namespace tooling {
 
+const NamedDecl *getCanonicalSymbolDeclaration(const NamedDecl *FoundDecl) {
+  // If FoundDecl is a constructor or destructor, we want to instead take
+  // the Decl of the corresponding class.
+  if (const auto *CtorDecl = dyn_cast<CXXConstructorDecl>(FoundDecl))
+    FoundDecl = CtorDecl->getParent();
+  else if (const auto *DtorDecl = dyn_cast<CXXDestructorDecl>(FoundDecl))
+    FoundDecl = DtorDecl->getParent();
+  // FIXME: (Alex L): Canonicalize implicit template instantions, just like
+  // the indexer does it.
+
+  // Note: please update the declaration's doc comment every time the
+  // canonicalization rules are changed.
+  return FoundDecl;
+}
+
 namespace {
 // \brief NamedDeclFindingConsumer should delegate finding USRs of given Decl to
 // AdditionalUSRFinder. AdditionalUSRFinder adds USRs of ctor and dtor if given
@@ -193,13 +208,7 @@ private:
       return false;
     }
 
-    // If FoundDecl is a constructor or destructor, we want to instead take
-    // the Decl of the corresponding class.
-    if (const auto *CtorDecl = dyn_cast<CXXConstructorDecl>(FoundDecl))
-      FoundDecl = CtorDecl->getParent();
-    else if (const auto *DtorDecl = dyn_cast<CXXDestructorDecl>(FoundDecl))
-      FoundDecl = DtorDecl->getParent();
-
+    FoundDecl = getCanonicalSymbolDeclaration(FoundDecl);
     SpellingNames.push_back(FoundDecl->getNameAsString());
     AdditionalUSRFinder Finder(FoundDecl, Context);
     USRList.push_back(Finder.Find());