[PATCH] D21676: clang-rename: add a -s (suffix) option

Miklos Vajna via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 24 00:15:57 PDT 2016


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

Use case: a class is declared in a header, and defined in two 
translation units. clang-rename is asked to rename a class member that's
referenced in both translation units.

Using -i is not possible, as in case the first clang-rename invocation
touches the header, the second invocation will result in compilation
errors. Using -s handles this situation, each invocation can work on the 
original source, and at the end the user can move the .new-rename files
in place with something like

for i in $(find . -name "*.new-rename"); do mv -f $i ${i%%.new-rename}; done

http://reviews.llvm.org/D21676

Files:
  clang-rename/tool/ClangRename.cpp
  test/clang-rename/ClassTestSuffix.cpp

Index: test/clang-rename/ClassTestSuffix.cpp
===================================================================
--- /dev/null
+++ test/clang-rename/ClassTestSuffix.cpp
@@ -0,0 +1,15 @@
+// RUN: cat %s > %T/test.cpp
+// RUN: clang-rename -offset=164 -new-name=Hector %T/test.cpp -s --
+// RUN: sed 's,//.*,,' %T/test.cpp.new-rename | FileCheck %s
+class Cla  // CHECK: class Hector
+{
+};
+
+int main()
+{
+    Cla *Pointer = 0; // CHECK: Hector *Pointer = 0;
+    return 0;
+}
+
+// Use grep -FUbo 'Cla' <file> to get the correct offset of Cla when changing
+// this file.
Index: clang-rename/tool/ClangRename.cpp
===================================================================
--- clang-rename/tool/ClangRename.cpp
+++ clang-rename/tool/ClangRename.cpp
@@ -63,6 +63,11 @@
     cl::desc("Overwrite edited <file>s."),
     cl::cat(ClangRenameCategory));
 static cl::opt<bool>
+Suffix(
+    "s",
+    cl::desc("Similar to -i, but write out edited <file>s with a '.new-rename' suffix."),
+    cl::cat(ClangRenameCategory));
+static cl::opt<bool>
 PrintName(
     "pn",
     cl::desc("Print the found symbol's name prior to renaming to stderr."),
@@ -144,7 +149,18 @@
     for (const auto &File : Files) {
       const auto *Entry = FileMgr.getFile(File);
       auto ID = Sources.translateFile(Entry);
-      Rewrite.getEditBuffer(ID).write(outs());
+      if (Suffix) {
+        // Write files with a '.new-rename' suffix instead of overwriting.
+        std::string FileName = std::string(Entry->getName()) + ".new-rename";
+        std::error_code Error;
+        llvm::raw_fd_ostream Stream(FileName, Error, llvm::sys::fs::F_None);
+        if (!Error) {
+          Rewrite.getEditBuffer(ID).write(Stream);
+        }
+      } else {
+        // Write to stdout.
+        Rewrite.getEditBuffer(ID).write(outs());
+      }
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21676.61759.patch
Type: text/x-patch
Size: 1842 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160624/99628ad0/attachment-0001.bin>


More information about the cfe-commits mailing list