Manuel Klimek | 3f00134 | 2012-05-23 16:29:20 +0000 | [diff] [blame] | 1 | //===--- Refactoring.cpp - Framework for clang refactoring tools ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Implements tools to support refactorings. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 14 | #include "clang/Basic/DiagnosticOptions.h" |
Manuel Klimek | 3f00134 | 2012-05-23 16:29:20 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
Manuel Klimek | 3f00134 | 2012-05-23 16:29:20 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 18 | #include "clang/Lex/Lexer.h" |
Ted Kremenek | cdf8149 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 19 | #include "clang/Rewrite/Core/Rewriter.h" |
Manuel Klimek | 3f00134 | 2012-05-23 16:29:20 +0000 | [diff] [blame] | 20 | #include "clang/Tooling/Refactoring.h" |
Ariel J. Bernal | 31c181b | 2013-10-01 14:59:00 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileSystem.h" |
| 22 | #include "llvm/Support/Path.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_os_ostream.h" |
Manuel Klimek | 3f00134 | 2012-05-23 16:29:20 +0000 | [diff] [blame] | 24 | |
| 25 | namespace clang { |
| 26 | namespace tooling { |
| 27 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame^] | 28 | RefactoringTool::RefactoringTool( |
| 29 | const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths, |
| 30 | std::shared_ptr<PCHContainerOperations> PCHContainerOps) |
| 31 | : ClangTool(Compilations, SourcePaths, PCHContainerOps) {} |
Edwin Vane | 5038ac0 | 2013-01-11 17:04:55 +0000 | [diff] [blame] | 32 | |
| 33 | Replacements &RefactoringTool::getReplacements() { return Replace; } |
| 34 | |
| 35 | int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) { |
| 36 | if (int Result = run(ActionFactory)) { |
| 37 | return Result; |
| 38 | } |
| 39 | |
| 40 | LangOptions DefaultLangOptions; |
| 41 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 42 | TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts); |
| 43 | DiagnosticsEngine Diagnostics( |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 44 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), |
Edwin Vane | 5038ac0 | 2013-01-11 17:04:55 +0000 | [diff] [blame] | 45 | &*DiagOpts, &DiagnosticPrinter, false); |
| 46 | SourceManager Sources(Diagnostics, getFiles()); |
| 47 | Rewriter Rewrite(Sources, DefaultLangOptions); |
| 48 | |
| 49 | if (!applyAllReplacements(Rewrite)) { |
| 50 | llvm::errs() << "Skipped some replacements.\n"; |
| 51 | } |
| 52 | |
| 53 | return saveRewrittenFiles(Rewrite); |
| 54 | } |
| 55 | |
| 56 | bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) { |
| 57 | return tooling::applyAllReplacements(Replace, Rewrite); |
| 58 | } |
| 59 | |
| 60 | int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) { |
Alp Toker | a23d266 | 2013-10-29 08:32:41 +0000 | [diff] [blame] | 61 | return Rewrite.overwriteChangedFiles() ? 1 : 0; |
Manuel Klimek | 3f00134 | 2012-05-23 16:29:20 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | } // end namespace tooling |
| 65 | } // end namespace clang |