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 | |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 14 | #include "clang/Tooling/Refactoring.h" |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 15 | #include "clang/Basic/DiagnosticOptions.h" |
Manuel Klimek | 3f00134 | 2012-05-23 16:29:20 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
| 17 | #include "clang/Basic/SourceManager.h" |
Eric Liu | 4c1ef97a | 2016-03-29 16:31:53 +0000 | [diff] [blame] | 18 | #include "clang/Format/Format.h" |
Manuel Klimek | 3f00134 | 2012-05-23 16:29:20 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 20 | #include "clang/Lex/Lexer.h" |
Ted Kremenek | cdf8149 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 21 | #include "clang/Rewrite/Core/Rewriter.h" |
Ariel J. Bernal | 31c181b | 2013-10-01 14:59:00 +0000 | [diff] [blame] | 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) |
Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 31 | : ClangTool(Compilations, SourcePaths, std::move(PCHContainerOps)) {} |
Edwin Vane | 5038ac0 | 2013-01-11 17:04:55 +0000 | [diff] [blame] | 32 | |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 33 | std::map<std::string, Replacements> &RefactoringTool::getReplacements() { |
| 34 | return FileToReplaces; |
| 35 | } |
Edwin Vane | 5038ac0 | 2013-01-11 17:04:55 +0000 | [diff] [blame] | 36 | |
| 37 | int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) { |
| 38 | if (int Result = run(ActionFactory)) { |
| 39 | return Result; |
| 40 | } |
| 41 | |
| 42 | LangOptions DefaultLangOptions; |
| 43 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 44 | TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts); |
| 45 | DiagnosticsEngine Diagnostics( |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 46 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), |
Edwin Vane | 5038ac0 | 2013-01-11 17:04:55 +0000 | [diff] [blame] | 47 | &*DiagOpts, &DiagnosticPrinter, false); |
| 48 | SourceManager Sources(Diagnostics, getFiles()); |
| 49 | Rewriter Rewrite(Sources, DefaultLangOptions); |
| 50 | |
| 51 | if (!applyAllReplacements(Rewrite)) { |
| 52 | llvm::errs() << "Skipped some replacements.\n"; |
| 53 | } |
| 54 | |
| 55 | return saveRewrittenFiles(Rewrite); |
| 56 | } |
| 57 | |
| 58 | bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) { |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 59 | bool Result = true; |
Eric Liu | cf2913c | 2016-11-07 06:08:23 +0000 | [diff] [blame] | 60 | for (const auto &Entry : groupReplacementsByFile( |
| 61 | Rewrite.getSourceMgr().getFileManager(), FileToReplaces)) |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 62 | Result = tooling::applyAllReplacements(Entry.second, Rewrite) && Result; |
| 63 | return Result; |
Edwin Vane | 5038ac0 | 2013-01-11 17:04:55 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) { |
Alp Toker | a23d266 | 2013-10-29 08:32:41 +0000 | [diff] [blame] | 67 | return Rewrite.overwriteChangedFiles() ? 1 : 0; |
Manuel Klimek | 3f00134 | 2012-05-23 16:29:20 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 70 | bool formatAndApplyAllReplacements( |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 71 | const std::map<std::string, Replacements> &FileToReplaces, |
| 72 | Rewriter &Rewrite, StringRef Style) { |
Eric Liu | 4c1ef97a | 2016-03-29 16:31:53 +0000 | [diff] [blame] | 73 | SourceManager &SM = Rewrite.getSourceMgr(); |
| 74 | FileManager &Files = SM.getFileManager(); |
| 75 | |
Eric Liu | 4c1ef97a | 2016-03-29 16:31:53 +0000 | [diff] [blame] | 76 | bool Result = true; |
Eric Liu | cf2913c | 2016-11-07 06:08:23 +0000 | [diff] [blame] | 77 | for (const auto &FileAndReplaces : groupReplacementsByFile( |
| 78 | Rewrite.getSourceMgr().getFileManager(), FileToReplaces)) { |
Benjamin Kramer | 442b9a9 | 2016-05-29 11:04:56 +0000 | [diff] [blame] | 79 | const std::string &FilePath = FileAndReplaces.first; |
Eric Liu | 4c1ef97a | 2016-03-29 16:31:53 +0000 | [diff] [blame] | 80 | auto &CurReplaces = FileAndReplaces.second; |
| 81 | |
| 82 | const FileEntry *Entry = Files.getFile(FilePath); |
| 83 | FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User); |
| 84 | StringRef Code = SM.getBufferData(ID); |
| 85 | |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 86 | auto CurStyle = format::getStyle(Style, FilePath, "LLVM"); |
| 87 | if (!CurStyle) { |
| 88 | llvm::errs() << llvm::toString(CurStyle.takeError()) << "\n"; |
| 89 | return false; |
| 90 | } |
| 91 | |
Eric Liu | 4f8d994 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 92 | auto NewReplacements = |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 93 | format::formatReplacements(Code, CurReplaces, *CurStyle); |
Eric Liu | 4f8d994 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 94 | if (!NewReplacements) { |
| 95 | llvm::errs() << llvm::toString(NewReplacements.takeError()) << "\n"; |
| 96 | return false; |
| 97 | } |
| 98 | Result = applyAllReplacements(*NewReplacements, Rewrite) && Result; |
Eric Liu | 4c1ef97a | 2016-03-29 16:31:53 +0000 | [diff] [blame] | 99 | } |
| 100 | return Result; |
| 101 | } |
| 102 | |
Manuel Klimek | 3f00134 | 2012-05-23 16:29:20 +0000 | [diff] [blame] | 103 | } // end namespace tooling |
| 104 | } // end namespace clang |