[PATCH] D21364: clang-rename: implement renaming of classes with a dtor

Miklos Vajna via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 15 00:21:51 PDT 2016


vmiklos created this revision.
vmiklos added a reviewer: klimek.
vmiklos added a subscriber: cfe-commits.

Work in progress, not complete yet.

Current result:

clang-rename -offset 135 -new-name=Bar DtorDefTest.cpp --

  - handles "Foo" from "Foo::~Foo" correctly
  - handles "~Foo" from "Foo::~Foo", but currently replaces "~Foo" with "Baro"

Where the problem is obviously that we want to replace the string "Foo", but the dtor is called "~Foo". I see two way around this:

1) Extend LocationsFound, so that it can store not only locations, but also prefix strings along with locations, so when attempting to replace "Foo" with something else, we attempt to replace "~Foo". This sounds a bit ugly, but possible to do.

2) Instead of adding DestructorDecl->getLocation() to the location list, add a location that's "getLocation()+1", so points to the byte after the "~" character. I think this would be a better way, but I did not find so far what is the API to do something like this (i.e. if I have a SourceLocation that points to byte X, how do I produce a SourceLocation that points to byte X+1 in the same source file?).

Manuel, do you have an opinion on this? If 2) is doable, I would rather do that; but 1) is the only option, then sure, I can do that.


http://reviews.llvm.org/D21364

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

Index: test/clang-rename/DtorDefTest.cpp
===================================================================
--- /dev/null
+++ test/clang-rename/DtorDefTest.cpp
@@ -0,0 +1,17 @@
+// RUN: cat %s > %t.cpp
+// RUN: clang-rename -offset=135 -new-name=Bar %t.cpp -i --
+// RUN: sed 's,//.*,,' %t.cpp | FileCheck %s
+class Foo {
+public:
+  Foo();
+  ~Foo(); // CHECK: ~Bar();
+};
+
+Foo::Foo() {
+}
+
+Foo::~Foo() { // CHECK: Bar::~Bar();
+}
+
+// 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
@@ -87,6 +87,19 @@
     return true;
   }
 
+  bool VisitCXXDestructorDecl(clang::CXXDestructorDecl *DestructorDecl) {
+    if (getUSRForDecl(DestructorDecl->getParent()) == USR) {
+      // Handles "~Foo" from "Foo::~Foo", but currently replaces "~Foo" with "Baro".
+      LocationsFound.push_back(DestructorDecl->getLocation());
+      if (DestructorDecl->isThisDeclarationADefinition()) {
+        // Handles "Foo" from "Foo::~Foo".
+        LocationsFound.push_back(DestructorDecl->getLocStart());
+      }
+    }
+
+    return true;
+  }
+
   // Expression visitors:
 
   bool VisitDeclRefExpr(const DeclRefExpr *Expr) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21364.60793.patch
Type: text/x-patch
Size: 1332 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160615/a92f27c5/attachment.bin>


More information about the cfe-commits mailing list