From: Haojian Wu Date: Fri, 20 Oct 2017 12:37:16 +0000 (+0000) Subject: [clang-refactor] Add "-Inplace" option to the commandline tool. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d6aede0fad127fa8eedd339e9594bf30cbd98294;p=clang [clang-refactor] Add "-Inplace" option to the commandline tool. Summary: 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. Reviewers: arphaman, ioeric Reviewed By: arphaman Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D39092 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@316212 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/test/Refactor/tool-apply-replacements.cpp b/test/Refactor/tool-apply-replacements.cpp index 9f4595089c..59b0991dd2 100644 --- a/test/Refactor/tool-apply-replacements.cpp +++ b/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 { diff --git a/tools/clang-refactor/ClangRefactor.cpp b/tools/clang-refactor/ClangRefactor.cpp index ed9bf5ed28..523c3a6220 100644 --- a/tools/clang-refactor/ClangRefactor.cpp +++ b/tools/clang-refactor/ClangRefactor.cpp @@ -40,6 +40,11 @@ static cl::OptionCategory CommonRefactorOptions("Refactoring options"); static cl::opt Verbose("v", cl::desc("Use verbose output"), cl::cat(cl::GeneralCategory), cl::sub(*cl::AllSubCommands)); + +static cl::opt Inplace("i", cl::desc("Inplace edit s"), + cl::cat(cl::GeneralCategory), + cl::sub(*cl::AllSubCommands)); + } // end namespace opts namespace { @@ -436,13 +441,18 @@ public: 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; }