[PATCH] D23009: [clang-rename] handle overridden functions correctly

Kirill Bobyrev via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 1 04:38:19 PDT 2016


omtcyfz created this revision.
omtcyfz added reviewers: alexfh, klimek.
omtcyfz added a subscriber: cfe-commits.

1. Renaming overridden functions only works for two levels of "overriding hierarchy". `clang-rename` should recursively add overridden methods.
2. Make use of `forEachOverridden` AST Matcher.

https://reviews.llvm.org/D23009

Files:
  clang-rename/USRFindingAction.cpp
  test/clang-rename/FunctionOverride.cpp

Index: test/clang-rename/FunctionOverride.cpp
===================================================================
--- /dev/null
+++ test/clang-rename/FunctionOverride.cpp
@@ -0,0 +1,8 @@
+// RUN: clang-rename -offset=93 -new-name=boo %s -- | FileCheck %s
+
+class A { virtual void foo(); };    // CHECK: class A { virtual void boo(); };
+class B : public A { void foo(); }; // CHECK: class B : public A { void boo(); };
+class C : public B { void foo(); }; // CHECK: class C : public B { void boo(); };
+
+// Use grep -FUbo 'Foo' <file> to get the correct offset of Foo when changing
+// this file.
Index: clang-rename/USRFindingAction.cpp
===================================================================
--- clang-rename/USRFindingAction.cpp
+++ clang-rename/USRFindingAction.cpp
@@ -54,21 +54,25 @@
 
   void Find() {
     USRSet.insert(getUSRForDecl(FoundDecl));
-    addUSRsFromOverrideSetsAndCtorDtors();
+    if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FoundDecl)) {
+      addUSRsFromOverrideSets(MethodDecl);
+    }
+    if (const auto *RecordDecl = dyn_cast<CXXRecordDecl>(FoundDecl)) {
+      addUSRsOfCtorDtors(RecordDecl);
+    }
     addMatchers();
     Finder.matchAST(Context);
     USRs->insert(USRs->end(), USRSet.begin(), USRSet.end());
   }
 
 private:
   void addMatchers() {
     const auto CXXMethodDeclMatcher =
-        cxxMethodDecl(isVirtual()).bind("cxxMethodDecl");
+        cxxMethodDecl(forEachOverridden(cxxMethodDecl().bind("cxxMethodDecl")));
     Finder.addMatcher(CXXMethodDeclMatcher, this);
   }
 
-  // FIXME: Implement hasOverriddenMethod and matchesUSR matchers to make
-  // lookups more efficient.
+  // FIXME: Implement matchesUSR matchers to make lookups more efficient.
   virtual void run(const MatchFinder::MatchResult &Result) {
     const auto *VirtualMethod =
         Result.Nodes.getNodeAs<CXXMethodDecl>("cxxMethodDecl");
@@ -83,20 +87,19 @@
     }
   }
 
-  void addUSRsFromOverrideSetsAndCtorDtors() {
-    // If D is CXXRecordDecl we should add all USRs of its constructors.
-    if (const auto *RecordDecl = dyn_cast<CXXRecordDecl>(FoundDecl)) {
-      RecordDecl = RecordDecl->getDefinition();
-      for (const auto *CtorDecl : RecordDecl->ctors()) {
-        USRSet.insert(getUSRForDecl(CtorDecl));
-      }
-      USRSet.insert(getUSRForDecl(RecordDecl->getDestructor()));
+  void addUSRsOfCtorDtors(const CXXRecordDecl *RecordDecl) {
+    RecordDecl = RecordDecl->getDefinition();
+    for (const auto *CtorDecl : RecordDecl->ctors()) {
+      USRSet.insert(getUSRForDecl(CtorDecl));
     }
-    // If D is CXXMethodDecl we should add all USRs of its overriden methods.
-    if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FoundDecl)) {
-      for (auto &OverriddenMethod : MethodDecl->overridden_methods()) {
-        USRSet.insert(getUSRForDecl(OverriddenMethod));
-      }
+    USRSet.insert(getUSRForDecl(RecordDecl->getDestructor()));
+  }
+
+  void addUSRsFromOverrideSets(const CXXMethodDecl *MethodDecl) {
+    USRSet.insert(getUSRForDecl(MethodDecl));
+    for (auto &OverriddenMethod : MethodDecl->overridden_methods()) {
+      // Recursively visit each OverridenMethod.
+      addUSRsFromOverrideSets(OverriddenMethod);
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23009.66302.patch
Type: text/x-patch
Size: 3225 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160801/f7e1393c/attachment.bin>


More information about the cfe-commits mailing list