[PATCH] D22237: clang-rename: fix renaming member functions

Miklos Vajna via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 14 12:16:46 PDT 2016


vmiklos updated this revision to Diff 64025.

https://reviews.llvm.org/D22237

Files:
  clang-rename/USRLocFinder.cpp
  test/clang-rename/VirtualFunction.cpp

Index: test/clang-rename/VirtualFunction.cpp
===================================================================
--- /dev/null
+++ test/clang-rename/VirtualFunction.cpp
@@ -0,0 +1,26 @@
+// RUN: cat %s > %t.cpp
+// RUN: clang-rename -offset=163 -new-name=boo %t.cpp -i --
+// RUN: sed 's,//.*,,' %t.cpp | FileCheck %s
+
+class A {
+public:
+  virtual void foo() { } // CHECK: virtual void boo() { }
+};
+
+class B : public A {
+public:
+  void foo() { } // CHECK: void boo() { }
+};
+
+int main() {
+  A a;
+  a.foo();
+
+  B b;
+  b.foo();
+
+  return 0;
+}
+
+// Use grep -FUbo 'foo' <file> to get the correct offset of foo when changing
+// this file.
Index: clang-rename/USRLocFinder.cpp
===================================================================
--- clang-rename/USRLocFinder.cpp
+++ clang-rename/USRLocFinder.cpp
@@ -111,6 +111,11 @@
     return true;
   }
 
+  bool VisitCXXMethodDecl(CXXMethodDecl *Decl) {
+    handleCXXMethodDecl(Decl, Decl->getLocation());
+    return true;
+  }
+
   // Expression visitors:
 
   bool VisitDeclRefExpr(const DeclRefExpr *Expr) {
@@ -163,6 +168,12 @@
     return handleCXXNamedCastExpr(Expr);
   }
 
+  bool VisitCXXMemberCallExpr(const CXXMemberCallExpr *Expr) {
+    CXXMethodDecl *Decl = Expr->getMethodDecl();
+    handleCXXMethodDecl(Decl, Expr->getExprLoc());
+    return true;
+  }
+
   // Non-visitors:
 
   // \brief Returns a list of unique locations. Duplicate or overlapping
@@ -200,6 +211,21 @@
     return true;
   }
 
+  void handleCXXMethodDecl(const CXXMethodDecl* Decl, SourceLocation Location) {
+    if (getUSRForDecl(Decl) == USR) {
+      // This member function was requested to be renamed explicitly.
+      LocationsFound.push_back(Location);
+    }
+    else if (Decl->isVirtual()) {
+      for (auto *OverridenDecl : Decl->overridden_methods()) {
+        if (getUSRForDecl(OverridenDecl) == USR) {
+          // This member function overwrites one that is to be renamed.
+          LocationsFound.push_back(Location);
+        }
+      }
+    }
+  }
+
   // All the locations of the USR were found.
   const std::string USR;
   // Old name that is renamed.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22237.64025.patch
Type: text/x-patch
Size: 2138 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160714/8550e1a2/attachment.bin>


More information about the cfe-commits mailing list