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