[PATCH] D39092: [clang-refactor] Add "-Inplace" option to the commandline tool.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 19 06:03:08 PDT 2017


hokein created this revision.

Change clang-refactor default behavior to print the new code after refactoring
(instead of editing the source files), which would make it easier to use
and debug the refactoring action.


https://reviews.llvm.org/D39092

Files:
  test/Refactor/tool-apply-replacements.cpp
  tools/clang-refactor/ClangRefactor.cpp


Index: tools/clang-refactor/ClangRefactor.cpp
===================================================================
--- tools/clang-refactor/ClangRefactor.cpp
+++ tools/clang-refactor/ClangRefactor.cpp
@@ -40,6 +40,11 @@
 static cl::opt<bool> Verbose("v", cl::desc("Use verbose output"),
                              cl::cat(cl::GeneralCategory),
                              cl::sub(*cl::AllSubCommands));
+
+static cl::opt<bool> Inplace("i", cl::desc("Inplace edit <file>s"),
+                             cl::cat(cl::GeneralCategory),
+                             cl::sub(*cl::AllSubCommands));
+
 } // end namespace opts
 
 namespace {
@@ -436,13 +441,18 @@
         return true;
       }
 
-      std::error_code EC;
-      llvm::raw_fd_ostream OS(File, EC, llvm::sys::fs::F_Text);
-      if (EC) {
-        llvm::errs() << EC.message() << "\n";
-        return true;
+      if (opts::Inplace) {
+        std::error_code EC;
+        llvm::raw_fd_ostream OS(File, EC, llvm::sys::fs::F_Text);
+        if (EC) {
+          llvm::errs() << EC.message() << "\n";
+          return true;
+        }
+        OS << *Result;
+        continue;
       }
-      OS << *Result;
+
+      llvm::outs() << *Result;
     }
     return false;
   }
Index: test/Refactor/tool-apply-replacements.cpp
===================================================================
--- test/Refactor/tool-apply-replacements.cpp
+++ test/Refactor/tool-apply-replacements.cpp
@@ -1,10 +1,8 @@
-// RUN: rm -f %t.cp.cpp
-// RUN: cp %s %t.cp.cpp
-// RUN: clang-refactor local-rename -selection=%t.cp.cpp:9:7 -new-name=test %t.cp.cpp --
-// RUN: grep -v CHECK %t.cp.cpp | FileCheck %t.cp.cpp
-// RUN: cp %s %t.cp.cpp
-// RUN: clang-refactor local-rename -selection=%t.cp.cpp:9:7-9:15 -new-name=test %t.cp.cpp --
-// RUN: grep -v CHECK %t.cp.cpp | FileCheck %t.cp.cpp
+// RUN: sed -e 's#//.*$##' %s > %t.cpp
+// RUN: clang-refactor local-rename -selection=%t.cpp:7:7 -new-name=test %t.cpp -- | FileCheck %s
+// RUN: clang-refactor local-rename -selection=%t.cpp:7:7-7:15 -new-name=test %t.cpp -- | FileCheck %s
+// RUN: clang-refactor local-rename -i -selection=%t.cpp:7:7 -new-name=test %t.cpp --
+// RUN: FileCheck -input-file=%t.cpp %s
 
 class RenameMe {
 // CHECK: class test {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39092.119578.patch
Type: text/x-patch
Size: 2251 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171019/1afb1f3d/attachment.bin>


More information about the cfe-commits mailing list