Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 1 | //===--- tools/extra/clang-rename/ClangRename.cpp - Clang rename tool -----===// |
| 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 | /// \file |
| 11 | /// \brief This file implements a clang-rename tool that automatically finds and |
| 12 | /// renames symbols in C++ code. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Manuel Klimek | 3f840a9 | 2014-10-13 11:30:27 +0000 | [diff] [blame] | 16 | #include "../USRFindingAction.h" |
| 17 | #include "../RenamingAction.h" |
Eugene Zelenko | a4c2efb | 2016-07-25 20:30:13 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
| 19 | #include "clang/Basic/DiagnosticOptions.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 20 | #include "clang/Basic/FileManager.h" |
Eugene Zelenko | a4c2efb | 2016-07-25 20:30:13 +0000 | [diff] [blame] | 21 | #include "clang/Basic/IdentifierTable.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 22 | #include "clang/Basic/LangOptions.h" |
Eugene Zelenko | a4c2efb | 2016-07-25 20:30:13 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
| 24 | #include "clang/Basic/TokenKinds.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 26 | #include "clang/Rewrite/Core/Rewriter.h" |
| 27 | #include "clang/Tooling/CommonOptionsParser.h" |
| 28 | #include "clang/Tooling/Refactoring.h" |
Miklos Vajna | a2ca3ed | 2016-06-27 19:34:47 +0000 | [diff] [blame] | 29 | #include "clang/Tooling/ReplacementsYaml.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 30 | #include "clang/Tooling/Tooling.h" |
| 31 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
Eugene Zelenko | a4c2efb | 2016-07-25 20:30:13 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
| 33 | #include "llvm/Support/FileSystem.h" |
| 34 | #include "llvm/Support/raw_ostream.h" |
| 35 | #include "llvm/Support/YAMLTraits.h" |
| 36 | #include <cstdlib> |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 37 | #include <string> |
Eugene Zelenko | a4c2efb | 2016-07-25 20:30:13 +0000 | [diff] [blame] | 38 | #include <system_error> |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace llvm; |
| 41 | |
| 42 | cl::OptionCategory ClangRenameCategory("Clang-rename options"); |
| 43 | |
| 44 | static cl::opt<std::string> |
| 45 | NewName( |
| 46 | "new-name", |
| 47 | cl::desc("The new name to change the symbol to."), |
| 48 | cl::cat(ClangRenameCategory)); |
| 49 | static cl::opt<unsigned> |
| 50 | SymbolOffset( |
| 51 | "offset", |
| 52 | cl::desc("Locates the symbol by offset as opposed to <line>:<column>."), |
| 53 | cl::cat(ClangRenameCategory)); |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 54 | static cl::opt<std::string> |
| 55 | OldName( |
| 56 | "old-name", |
| 57 | cl::desc("The fully qualified name of the symbol, if -offset is not used."), |
| 58 | cl::cat(ClangRenameCategory)); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 59 | static cl::opt<bool> |
| 60 | Inplace( |
| 61 | "i", |
| 62 | cl::desc("Overwrite edited <file>s."), |
| 63 | cl::cat(ClangRenameCategory)); |
| 64 | static cl::opt<bool> |
| 65 | PrintName( |
| 66 | "pn", |
| 67 | cl::desc("Print the found symbol's name prior to renaming to stderr."), |
| 68 | cl::cat(ClangRenameCategory)); |
| 69 | static cl::opt<bool> |
| 70 | PrintLocations( |
| 71 | "pl", |
| 72 | cl::desc("Print the locations affected by renaming to stderr."), |
| 73 | cl::cat(ClangRenameCategory)); |
Miklos Vajna | a2ca3ed | 2016-06-27 19:34:47 +0000 | [diff] [blame] | 74 | static cl::opt<std::string> |
| 75 | ExportFixes( |
| 76 | "export-fixes", |
| 77 | cl::desc("YAML file to store suggested fixes in."), |
| 78 | cl::value_desc("filename"), |
| 79 | cl::cat(ClangRenameCategory)); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 80 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 81 | using namespace clang; |
| 82 | |
| 83 | const char RenameUsage[] = "A tool to rename symbols in C/C++ code.\n\ |
| 84 | clang-rename renames every occurrence of a symbol found at <offset> in\n\ |
| 85 | <source0>. If -i is specified, the edited files are overwritten to disk.\n\ |
| 86 | Otherwise, the results are written to stdout.\n"; |
| 87 | |
| 88 | int main(int argc, const char **argv) { |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 89 | tooling::CommonOptionsParser OP(argc, argv, ClangRenameCategory, RenameUsage); |
| 90 | |
| 91 | // Check the arguments for correctness. |
| 92 | |
| 93 | if (NewName.empty()) { |
Kirill Bobyrev | 08c588c | 2016-07-21 10:21:31 +0000 | [diff] [blame] | 94 | errs() << "ERROR: no new name provided.\n\n"; |
| 95 | exit(1); |
| 96 | } |
| 97 | |
| 98 | // Check if NewName is a valid identifier in C++17. |
| 99 | LangOptions Options; |
| 100 | Options.CPlusPlus = true; |
| 101 | Options.CPlusPlus1z = true; |
| 102 | IdentifierTable Table(Options); |
| 103 | auto NewNameTokKind = Table.get(NewName).getTokenID(); |
| 104 | if (!tok::isAnyIdentifier(NewNameTokKind)) { |
Eugene Zelenko | a4c2efb | 2016-07-25 20:30:13 +0000 | [diff] [blame] | 105 | errs() << "ERROR: new name is not a valid identifier in C++17.\n\n"; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 106 | exit(1); |
| 107 | } |
| 108 | |
| 109 | // Get the USRs. |
| 110 | auto Files = OP.getSourcePathList(); |
| 111 | tooling::RefactoringTool Tool(OP.getCompilations(), Files); |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 112 | rename::USRFindingAction USRAction(SymbolOffset, OldName); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 113 | |
| 114 | // Find the USRs. |
| 115 | Tool.run(tooling::newFrontendActionFactory(&USRAction).get()); |
| 116 | const auto &USRs = USRAction.getUSRs(); |
| 117 | const auto &PrevName = USRAction.getUSRSpelling(); |
| 118 | |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 119 | if (PrevName.empty()) { |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 120 | // An error should have already been printed. |
| 121 | exit(1); |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 122 | } |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 123 | |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 124 | if (PrintName) { |
| 125 | errs() << "clang-rename: found name: " << PrevName << '\n'; |
| 126 | } |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 127 | |
| 128 | // Perform the renaming. |
| 129 | rename::RenamingAction RenameAction(NewName, PrevName, USRs, |
| 130 | Tool.getReplacements(), PrintLocations); |
| 131 | auto Factory = tooling::newFrontendActionFactory(&RenameAction); |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 132 | int ExitCode; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 133 | |
| 134 | if (Inplace) { |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 135 | ExitCode = Tool.runAndSave(Factory.get()); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 136 | } else { |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 137 | ExitCode = Tool.run(Factory.get()); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 138 | |
Miklos Vajna | a2ca3ed | 2016-06-27 19:34:47 +0000 | [diff] [blame] | 139 | if (!ExportFixes.empty()) { |
| 140 | std::error_code EC; |
| 141 | llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None); |
| 142 | if (EC) { |
| 143 | llvm::errs() << "Error opening output file: " << EC.message() << '\n'; |
| 144 | exit(1); |
| 145 | } |
| 146 | |
| 147 | // Export replacements. |
| 148 | tooling::TranslationUnitReplacements TUR; |
| 149 | const tooling::Replacements &Replacements = Tool.getReplacements(); |
| 150 | TUR.Replacements.insert(TUR.Replacements.end(), Replacements.begin(), |
| 151 | Replacements.end()); |
| 152 | |
| 153 | yaml::Output YAML(OS); |
| 154 | YAML << TUR; |
| 155 | OS.close(); |
| 156 | exit(0); |
| 157 | } |
| 158 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 159 | // Write every file to stdout. Right now we just barf the files without any |
| 160 | // indication of which files start where, other than that we print the files |
| 161 | // in the same order we see them. |
| 162 | LangOptions DefaultLangOptions; |
| 163 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = |
| 164 | new DiagnosticOptions(); |
| 165 | TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts); |
| 166 | DiagnosticsEngine Diagnostics( |
| 167 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), |
| 168 | &*DiagOpts, &DiagnosticPrinter, false); |
| 169 | auto &FileMgr = Tool.getFiles(); |
| 170 | SourceManager Sources(Diagnostics, FileMgr); |
| 171 | Rewriter Rewrite(Sources, DefaultLangOptions); |
| 172 | |
| 173 | Tool.applyAllReplacements(Rewrite); |
| 174 | for (const auto &File : Files) { |
| 175 | const auto *Entry = FileMgr.getFile(File); |
| 176 | auto ID = Sources.translateFile(Entry); |
| 177 | Rewrite.getEditBuffer(ID).write(outs()); |
| 178 | } |
| 179 | } |
| 180 | |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 181 | exit(ExitCode); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 182 | } |