diff options
Diffstat (limited to 'lib/Tooling/Refactoring.cpp')
-rw-r--r-- | lib/Tooling/Refactoring.cpp | 63 |
1 files changed, 34 insertions, 29 deletions
diff --git a/lib/Tooling/Refactoring.cpp b/lib/Tooling/Refactoring.cpp index c5002ef9fc..9ed0999140 100644 --- a/lib/Tooling/Refactoring.cpp +++ b/lib/Tooling/Refactoring.cpp @@ -135,50 +135,55 @@ bool applyAllReplacements(Replacements &Replaces, Rewriter &Rewrite) { return Result; } -bool saveRewrittenFiles(Rewriter &Rewrite) { - for (Rewriter::buffer_iterator I = Rewrite.buffer_begin(), - E = Rewrite.buffer_end(); - I != E; ++I) { - // FIXME: This code is copied from the FixItRewriter.cpp - I think it should - // go into directly into Rewriter (there we also have the Diagnostics to - // handle the error cases better). - const FileEntry *Entry = - Rewrite.getSourceMgr().getFileEntryForID(I->first); - std::string ErrorInfo; - llvm::raw_fd_ostream FileStream( - Entry->getName(), ErrorInfo, llvm::raw_fd_ostream::F_Binary); - if (!ErrorInfo.empty()) - return false; - I->second.write(FileStream); - FileStream.flush(); - } - return true; -} - RefactoringTool::RefactoringTool(const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths) - : Tool(Compilations, SourcePaths) {} + : ClangTool(Compilations, SourcePaths) {} Replacements &RefactoringTool::getReplacements() { return Replace; } -int RefactoringTool::run(FrontendActionFactory *ActionFactory) { - int Result = Tool.run(ActionFactory); +int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) { + if (int Result = run(ActionFactory)) { + return Result; + } + LangOptions DefaultLangOptions; IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts); DiagnosticsEngine Diagnostics( llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, &DiagnosticPrinter, false); - SourceManager Sources(Diagnostics, Tool.getFiles()); + SourceManager Sources(Diagnostics, getFiles()); Rewriter Rewrite(Sources, DefaultLangOptions); - if (!applyAllReplacements(Replace, Rewrite)) { + + if (!applyAllReplacements(Rewrite)) { llvm::errs() << "Skipped some replacements.\n"; } - if (!saveRewrittenFiles(Rewrite)) { - llvm::errs() << "Could not save rewritten files.\n"; - return 1; + + return saveRewrittenFiles(Rewrite); +} + +bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) { + return tooling::applyAllReplacements(Replace, Rewrite); +} + +int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) { + for (Rewriter::buffer_iterator I = Rewrite.buffer_begin(), + E = Rewrite.buffer_end(); + I != E; ++I) { + // FIXME: This code is copied from the FixItRewriter.cpp - I think it should + // go into directly into Rewriter (there we also have the Diagnostics to + // handle the error cases better). + const FileEntry *Entry = + Rewrite.getSourceMgr().getFileEntryForID(I->first); + std::string ErrorInfo; + llvm::raw_fd_ostream FileStream( + Entry->getName(), ErrorInfo, llvm::raw_fd_ostream::F_Binary); + if (!ErrorInfo.empty()) + return 1; + I->second.write(FileStream); + FileStream.flush(); } - return Result; + return 0; } } // end namespace tooling |