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 | |
Eugene Zelenko | a4c2efb | 2016-07-25 20:30:13 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
| 17 | #include "clang/Basic/DiagnosticOptions.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 18 | #include "clang/Basic/FileManager.h" |
Eugene Zelenko | a4c2efb | 2016-07-25 20:30:13 +0000 | [diff] [blame] | 19 | #include "clang/Basic/IdentifierTable.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 20 | #include "clang/Basic/LangOptions.h" |
Eugene Zelenko | a4c2efb | 2016-07-25 20:30:13 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
| 22 | #include "clang/Basic/TokenKinds.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 24 | #include "clang/Rewrite/Core/Rewriter.h" |
| 25 | #include "clang/Tooling/CommonOptionsParser.h" |
| 26 | #include "clang/Tooling/Refactoring.h" |
Alex Lorenz | 4abbd92 | 2017-06-30 16:36:09 +0000 | [diff] [blame] | 27 | #include "clang/Tooling/Refactoring/Rename/RenamingAction.h" |
| 28 | #include "clang/Tooling/Refactoring/Rename/USRFindingAction.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" |
Eugene Zelenko | a4c2efb | 2016-07-25 20:30:13 +0000 | [diff] [blame] | 34 | #include "llvm/Support/YAMLTraits.h" |
Kirill Bobyrev | 8100940 | 2016-08-04 09:23:30 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 36 | #include <string> |
Eugene Zelenko | a4c2efb | 2016-07-25 20:30:13 +0000 | [diff] [blame] | 37 | #include <system_error> |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace llvm; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 40 | using namespace clang; |
| 41 | |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 42 | /// \brief An oldname -> newname rename. |
| 43 | struct RenameAllInfo { |
Miklos Vajna | c29c81a | 2016-08-10 07:13:29 +0000 | [diff] [blame] | 44 | unsigned Offset = 0; |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 45 | std::string QualifiedName; |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 46 | std::string NewName; |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | LLVM_YAML_IS_SEQUENCE_VECTOR(RenameAllInfo) |
| 50 | |
| 51 | namespace llvm { |
| 52 | namespace yaml { |
| 53 | |
Miklos Vajna | c29c81a | 2016-08-10 07:13:29 +0000 | [diff] [blame] | 54 | /// \brief Specialized MappingTraits to describe how a RenameAllInfo is |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 55 | /// (de)serialized. |
| 56 | template <> struct MappingTraits<RenameAllInfo> { |
| 57 | static void mapping(IO &IO, RenameAllInfo &Info) { |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 58 | IO.mapOptional("Offset", Info.Offset); |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 59 | IO.mapOptional("QualifiedName", Info.QualifiedName); |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 60 | IO.mapRequired("NewName", Info.NewName); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | } // end namespace yaml |
| 65 | } // end namespace llvm |
| 66 | |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 67 | static cl::OptionCategory ClangRenameOptions("clang-rename common options"); |
| 68 | |
| 69 | static cl::list<unsigned> SymbolOffsets( |
| 70 | "offset", |
| 71 | cl::desc("Locates the symbol by offset as opposed to <line>:<column>."), |
| 72 | cl::ZeroOrMore, cl::cat(ClangRenameOptions)); |
| 73 | static cl::opt<bool> Inplace("i", cl::desc("Overwrite edited <file>s."), |
| 74 | cl::cat(ClangRenameOptions)); |
| 75 | static cl::list<std::string> |
| 76 | QualifiedNames("qualified-name", |
| 77 | cl::desc("The fully qualified name of the symbol."), |
| 78 | cl::ZeroOrMore, cl::cat(ClangRenameOptions)); |
| 79 | |
| 80 | static cl::list<std::string> |
| 81 | NewNames("new-name", cl::desc("The new name to change the symbol to."), |
| 82 | cl::ZeroOrMore, cl::cat(ClangRenameOptions)); |
| 83 | static cl::opt<bool> PrintName( |
| 84 | "pn", |
| 85 | cl::desc("Print the found symbol's name prior to renaming to stderr."), |
| 86 | cl::cat(ClangRenameOptions)); |
| 87 | static cl::opt<bool> PrintLocations( |
| 88 | "pl", cl::desc("Print the locations affected by renaming to stderr."), |
| 89 | cl::cat(ClangRenameOptions)); |
| 90 | static cl::opt<std::string> |
| 91 | ExportFixes("export-fixes", |
| 92 | cl::desc("YAML file to store suggested fixes in."), |
| 93 | cl::value_desc("filename"), cl::cat(ClangRenameOptions)); |
| 94 | static cl::opt<std::string> |
| 95 | Input("input", cl::desc("YAML file to load oldname-newname pairs from."), |
| 96 | cl::Optional, cl::cat(ClangRenameOptions)); |
Miklos Vajna | a0ce632 | 2017-06-02 09:32:28 +0000 | [diff] [blame] | 97 | static cl::opt<bool> Force("force", |
| 98 | cl::desc("Ignore nonexistent qualified names."), |
| 99 | cl::cat(ClangRenameOptions)); |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 100 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 101 | int main(int argc, const char **argv) { |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 102 | tooling::CommonOptionsParser OP(argc, argv, ClangRenameOptions); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 103 | |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 104 | if (!Input.empty()) { |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 105 | // Populate QualifiedNames and NewNames from a YAML file. |
Miklos Vajna | c29c81a | 2016-08-10 07:13:29 +0000 | [diff] [blame] | 106 | ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = |
| 107 | llvm::MemoryBuffer::getFile(Input); |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 108 | if (!Buffer) { |
| 109 | errs() << "clang-rename: failed to read " << Input << ": " |
| 110 | << Buffer.getError().message() << "\n"; |
Miklos Vajna | c29c81a | 2016-08-10 07:13:29 +0000 | [diff] [blame] | 111 | return 1; |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | std::vector<RenameAllInfo> Infos; |
| 115 | llvm::yaml::Input YAML(Buffer.get()->getBuffer()); |
| 116 | YAML >> Infos; |
| 117 | for (const auto &Info : Infos) { |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 118 | if (!Info.QualifiedName.empty()) |
| 119 | QualifiedNames.push_back(Info.QualifiedName); |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 120 | else |
| 121 | SymbolOffsets.push_back(Info.Offset); |
| 122 | NewNames.push_back(Info.NewName); |
| 123 | } |
| 124 | } |
| 125 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 126 | // Check the arguments for correctness. |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 127 | if (NewNames.empty()) { |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 128 | errs() << "clang-rename: -new-name must be specified.\n\n"; |
Benjamin Kramer | 1fec6cb | 2017-06-30 20:24:32 +0000 | [diff] [blame^] | 129 | return 1; |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if (SymbolOffsets.empty() == QualifiedNames.empty()) { |
| 133 | errs() << "clang-rename: -offset and -qualified-name can't be present at " |
| 134 | "the same time.\n"; |
Benjamin Kramer | 1fec6cb | 2017-06-30 20:24:32 +0000 | [diff] [blame^] | 135 | return 1; |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 138 | // Check if NewNames is a valid identifier in C++17. |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 139 | LangOptions Options; |
| 140 | Options.CPlusPlus = true; |
| 141 | Options.CPlusPlus1z = true; |
| 142 | IdentifierTable Table(Options); |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 143 | for (const auto &NewName : NewNames) { |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 144 | auto NewNameTokKind = Table.get(NewName).getTokenID(); |
| 145 | if (!tok::isAnyIdentifier(NewNameTokKind)) { |
| 146 | errs() << "ERROR: new name is not a valid identifier in C++17.\n\n"; |
Benjamin Kramer | 1fec6cb | 2017-06-30 20:24:32 +0000 | [diff] [blame^] | 147 | return 1; |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 151 | if (SymbolOffsets.size() + QualifiedNames.size() != NewNames.size()) { |
| 152 | errs() << "clang-rename: number of symbol offsets(" << SymbolOffsets.size() |
| 153 | << ") + number of qualified names (" << QualifiedNames.size() |
| 154 | << ") must be equal to number of new names(" << NewNames.size() |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 155 | << ").\n\n"; |
| 156 | cl::PrintHelpMessage(); |
Benjamin Kramer | 1fec6cb | 2017-06-30 20:24:32 +0000 | [diff] [blame^] | 157 | return 1; |
Kirill Bobyrev | 08c588c | 2016-07-21 10:21:31 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Kirill Bobyrev | 8d78af4 | 2016-09-14 13:23:14 +0000 | [diff] [blame] | 160 | auto Files = OP.getSourcePathList(); |
| 161 | tooling::RefactoringTool Tool(OP.getCompilations(), Files); |
Alex Lorenz | 4abbd92 | 2017-06-30 16:36:09 +0000 | [diff] [blame] | 162 | tooling::USRFindingAction FindingAction(SymbolOffsets, QualifiedNames, Force); |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 163 | Tool.run(tooling::newFrontendActionFactory(&FindingAction).get()); |
| 164 | const std::vector<std::vector<std::string>> &USRList = |
| 165 | FindingAction.getUSRList(); |
| 166 | const std::vector<std::string> &PrevNames = FindingAction.getUSRSpellings(); |
| 167 | if (PrintName) { |
| 168 | for (const auto &PrevName : PrevNames) { |
| 169 | outs() << "clang-rename found name: " << PrevName << '\n'; |
Kirill Bobyrev | 8d78af4 | 2016-09-14 13:23:14 +0000 | [diff] [blame] | 170 | } |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 171 | } |
Kirill Bobyrev | 8d78af4 | 2016-09-14 13:23:14 +0000 | [diff] [blame] | 172 | |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 173 | if (FindingAction.errorOccurred()) { |
| 174 | // Diagnostics are already issued at this point. |
Benjamin Kramer | 1fec6cb | 2017-06-30 20:24:32 +0000 | [diff] [blame^] | 175 | return 1; |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 176 | } |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 177 | |
Miklos Vajna | a0ce632 | 2017-06-02 09:32:28 +0000 | [diff] [blame] | 178 | if (Force && PrevNames.size() < NewNames.size()) { |
| 179 | // No matching PrevName for all NewNames. Without Force this is an error |
| 180 | // above already. |
Benjamin Kramer | 1fec6cb | 2017-06-30 20:24:32 +0000 | [diff] [blame^] | 181 | return 0; |
Miklos Vajna | a0ce632 | 2017-06-02 09:32:28 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 184 | // Perform the renaming. |
Alex Lorenz | 4abbd92 | 2017-06-30 16:36:09 +0000 | [diff] [blame] | 185 | tooling::RenamingAction RenameAction(NewNames, PrevNames, USRList, |
| 186 | Tool.getReplacements(), PrintLocations); |
Kirill Bobyrev | e5e7e15 | 2016-09-16 08:45:19 +0000 | [diff] [blame] | 187 | std::unique_ptr<tooling::FrontendActionFactory> Factory = |
| 188 | tooling::newFrontendActionFactory(&RenameAction); |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 189 | int ExitCode; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 190 | |
| 191 | if (Inplace) { |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 192 | ExitCode = Tool.runAndSave(Factory.get()); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 193 | } else { |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 194 | ExitCode = Tool.run(Factory.get()); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 195 | |
Miklos Vajna | a2ca3ed | 2016-06-27 19:34:47 +0000 | [diff] [blame] | 196 | if (!ExportFixes.empty()) { |
| 197 | std::error_code EC; |
| 198 | llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None); |
| 199 | if (EC) { |
| 200 | llvm::errs() << "Error opening output file: " << EC.message() << '\n'; |
Benjamin Kramer | 1fec6cb | 2017-06-30 20:24:32 +0000 | [diff] [blame^] | 201 | return 1; |
Miklos Vajna | a2ca3ed | 2016-06-27 19:34:47 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // Export replacements. |
| 205 | tooling::TranslationUnitReplacements TUR; |
Eric Liu | 267034c | 2016-08-01 10:16:39 +0000 | [diff] [blame] | 206 | const auto &FileToReplacements = Tool.getReplacements(); |
| 207 | for (const auto &Entry : FileToReplacements) |
| 208 | TUR.Replacements.insert(TUR.Replacements.end(), Entry.second.begin(), |
| 209 | Entry.second.end()); |
Miklos Vajna | a2ca3ed | 2016-06-27 19:34:47 +0000 | [diff] [blame] | 210 | |
| 211 | yaml::Output YAML(OS); |
| 212 | YAML << TUR; |
| 213 | OS.close(); |
Benjamin Kramer | 1fec6cb | 2017-06-30 20:24:32 +0000 | [diff] [blame^] | 214 | return 0; |
Miklos Vajna | a2ca3ed | 2016-06-27 19:34:47 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 217 | // Write every file to stdout. Right now we just barf the files without any |
| 218 | // indication of which files start where, other than that we print the files |
| 219 | // in the same order we see them. |
| 220 | LangOptions DefaultLangOptions; |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 221 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 222 | TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts); |
| 223 | DiagnosticsEngine Diagnostics( |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 224 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, |
| 225 | &DiagnosticPrinter, false); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 226 | auto &FileMgr = Tool.getFiles(); |
| 227 | SourceManager Sources(Diagnostics, FileMgr); |
| 228 | Rewriter Rewrite(Sources, DefaultLangOptions); |
| 229 | |
| 230 | Tool.applyAllReplacements(Rewrite); |
| 231 | for (const auto &File : Files) { |
| 232 | const auto *Entry = FileMgr.getFile(File); |
Alexander Shaposhnikov | e4920c6 | 2016-09-17 17:08:47 +0000 | [diff] [blame] | 233 | const auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 234 | Rewrite.getEditBuffer(ID).write(outs()); |
| 235 | } |
| 236 | } |
| 237 | |
Benjamin Kramer | 1fec6cb | 2017-06-30 20:24:32 +0000 | [diff] [blame^] | 238 | return ExitCode; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 239 | } |