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 "../RenamingAction.h" |
Kirill Bobyrev | 8100940 | 2016-08-04 09:23:30 +0000 | [diff] [blame] | 17 | #include "../USRFindingAction.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" |
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" |
Eugene Zelenko | a4c2efb | 2016-07-25 20:30:13 +0000 | [diff] [blame] | 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 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 42 | using namespace clang; |
| 43 | |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 44 | cl::OptionCategory ClangRenameAtCategory("clang-rename rename-at options"); |
| 45 | cl::OptionCategory ClangRenameAllCategory("clang-rename rename-all options"); |
| 46 | |
| 47 | const char RenameAtUsage[] = "A tool to rename symbols in C/C++ code.\n\ |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 48 | clang-rename renames every occurrence of a symbol found at <offset> in\n\ |
| 49 | <source0>. If -i is specified, the edited files are overwritten to disk.\n\ |
| 50 | Otherwise, the results are written to stdout.\n"; |
| 51 | |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 52 | const char RenameAllUsage[] = "A tool to rename symbols in C/C++ code.\n\ |
Kirill Bobyrev | 405699e | 2016-08-19 09:36:14 +0000 | [diff] [blame] | 53 | clang-rename performs renaming given pairs {offset | old-name} -> new-name.\n"; |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 54 | |
| 55 | static int renameAtMain(int argc, const char *argv[]); |
| 56 | static int renameAllMain(int argc, const char *argv[]); |
| 57 | static int helpMain(int argc, const char *argv[]); |
| 58 | |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 59 | /// \brief An oldname -> newname rename. |
| 60 | struct RenameAllInfo { |
| 61 | std::string OldName; |
Miklos Vajna | c29c81a | 2016-08-10 07:13:29 +0000 | [diff] [blame] | 62 | unsigned Offset = 0; |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 63 | std::string NewName; |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | LLVM_YAML_IS_SEQUENCE_VECTOR(RenameAllInfo) |
| 67 | |
| 68 | namespace llvm { |
| 69 | namespace yaml { |
| 70 | |
Miklos Vajna | c29c81a | 2016-08-10 07:13:29 +0000 | [diff] [blame] | 71 | /// \brief Specialized MappingTraits to describe how a RenameAllInfo is |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 72 | /// (de)serialized. |
| 73 | template <> struct MappingTraits<RenameAllInfo> { |
| 74 | static void mapping(IO &IO, RenameAllInfo &Info) { |
| 75 | IO.mapOptional("OldName", Info.OldName); |
| 76 | IO.mapOptional("Offset", Info.Offset); |
| 77 | IO.mapRequired("NewName", Info.NewName); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | } // end namespace yaml |
| 82 | } // end namespace llvm |
| 83 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 84 | int main(int argc, const char **argv) { |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 85 | if (argc > 1) { |
| 86 | using MainFunction = std::function<int(int, const char *[])>; |
| 87 | MainFunction Func = StringSwitch<MainFunction>(argv[1]) |
| 88 | .Case("rename-at", renameAtMain) |
| 89 | .Case("rename-all", renameAllMain) |
| 90 | .Cases("-help", "--help", helpMain) |
| 91 | .Default(nullptr); |
| 92 | |
| 93 | if (Func) { |
| 94 | std::string Invocation = std::string(argv[0]) + " " + argv[1]; |
| 95 | argv[1] = Invocation.c_str(); |
| 96 | return Func(argc - 1, argv + 1); |
| 97 | } else { |
| 98 | return renameAtMain(argc, argv); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | helpMain(argc, argv); |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | int subcommandMain(bool isRenameAll, int argc, const char **argv) { |
| 107 | cl::OptionCategory *Category = nullptr; |
| 108 | const char *Usage = nullptr; |
| 109 | if (isRenameAll) { |
| 110 | Category = &ClangRenameAllCategory; |
| 111 | Usage = RenameAllUsage; |
| 112 | } else { |
| 113 | Category = &ClangRenameAtCategory; |
| 114 | Usage = RenameAtUsage; |
| 115 | } |
| 116 | |
| 117 | cl::list<std::string> NewNames( |
| 118 | "new-name", cl::desc("The new name to change the symbol to."), |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 119 | (isRenameAll ? cl::ZeroOrMore : cl::Required), cl::cat(*Category)); |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 120 | cl::list<unsigned> SymbolOffsets( |
| 121 | "offset", |
| 122 | cl::desc("Locates the symbol by offset as opposed to <line>:<column>."), |
| 123 | (isRenameAll ? cl::ZeroOrMore : cl::Required), cl::cat(*Category)); |
| 124 | cl::list<std::string> OldNames( |
| 125 | "old-name", |
| 126 | cl::desc( |
| 127 | "The fully qualified name of the symbol, if -offset is not used."), |
| 128 | (isRenameAll ? cl::ZeroOrMore : cl::Optional), |
| 129 | cl::cat(ClangRenameAllCategory)); |
| 130 | cl::opt<bool> Inplace("i", cl::desc("Overwrite edited <file>s."), |
| 131 | cl::cat(*Category)); |
| 132 | cl::opt<bool> PrintName( |
| 133 | "pn", |
| 134 | cl::desc("Print the found symbol's name prior to renaming to stderr."), |
| 135 | cl::cat(ClangRenameAtCategory)); |
| 136 | cl::opt<bool> PrintLocations( |
| 137 | "pl", cl::desc("Print the locations affected by renaming to stderr."), |
| 138 | cl::cat(ClangRenameAtCategory)); |
| 139 | cl::opt<std::string> ExportFixes( |
| 140 | "export-fixes", cl::desc("YAML file to store suggested fixes in."), |
| 141 | cl::value_desc("filename"), cl::cat(*Category)); |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 142 | cl::opt<std::string> Input( |
| 143 | "input", cl::desc("YAML file to load oldname-newname pairs from."), |
| 144 | cl::Optional, cl::cat(ClangRenameAllCategory)); |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 145 | |
| 146 | tooling::CommonOptionsParser OP(argc, argv, *Category, Usage); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 147 | |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 148 | if (!Input.empty()) { |
| 149 | // Populate OldNames and NewNames from a YAML file. |
Miklos Vajna | c29c81a | 2016-08-10 07:13:29 +0000 | [diff] [blame] | 150 | ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = |
| 151 | llvm::MemoryBuffer::getFile(Input); |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 152 | if (!Buffer) { |
| 153 | errs() << "clang-rename: failed to read " << Input << ": " |
| 154 | << Buffer.getError().message() << "\n"; |
Miklos Vajna | c29c81a | 2016-08-10 07:13:29 +0000 | [diff] [blame] | 155 | return 1; |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | std::vector<RenameAllInfo> Infos; |
| 159 | llvm::yaml::Input YAML(Buffer.get()->getBuffer()); |
| 160 | YAML >> Infos; |
| 161 | for (const auto &Info : Infos) { |
| 162 | if (!Info.OldName.empty()) |
| 163 | OldNames.push_back(Info.OldName); |
| 164 | else |
| 165 | SymbolOffsets.push_back(Info.Offset); |
| 166 | NewNames.push_back(Info.NewName); |
| 167 | } |
| 168 | } |
| 169 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 170 | // Check the arguments for correctness. |
| 171 | |
Miklos Vajna | 3d71b51 | 2016-08-09 18:20:41 +0000 | [diff] [blame] | 172 | if (NewNames.empty()) { |
| 173 | errs() << "clang-rename: either -new-name or -input is required.\n\n"; |
| 174 | exit(1); |
| 175 | } |
| 176 | |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 177 | // Check if NewNames is a valid identifier in C++17. |
| 178 | for (const auto &NewName : NewNames) { |
| 179 | LangOptions Options; |
| 180 | Options.CPlusPlus = true; |
| 181 | Options.CPlusPlus1z = true; |
| 182 | IdentifierTable Table(Options); |
| 183 | auto NewNameTokKind = Table.get(NewName).getTokenID(); |
| 184 | if (!tok::isAnyIdentifier(NewNameTokKind)) { |
| 185 | errs() << "ERROR: new name is not a valid identifier in C++17.\n\n"; |
| 186 | exit(1); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (!OldNames.empty() && OldNames.size() != NewNames.size()) { |
| 191 | errs() << "clang-rename: number of old names (" << OldNames.size() |
| 192 | << ") do not equal to number of new names (" << NewNames.size() |
| 193 | << ").\n\n"; |
| 194 | cl::PrintHelpMessage(); |
Kirill Bobyrev | 08c588c | 2016-07-21 10:21:31 +0000 | [diff] [blame] | 195 | exit(1); |
| 196 | } |
| 197 | |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 198 | if (!SymbolOffsets.empty() && SymbolOffsets.size() != NewNames.size()) { |
| 199 | errs() << "clang-rename: number of symbol offsets (" << SymbolOffsets.size() |
| 200 | << ") do not equal to number of new names (" << NewNames.size() |
| 201 | << ").\n\n"; |
| 202 | cl::PrintHelpMessage(); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 203 | exit(1); |
| 204 | } |
| 205 | |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 206 | std::vector<std::vector<std::string>> USRList; |
| 207 | std::vector<std::string> PrevNames; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 208 | auto Files = OP.getSourcePathList(); |
| 209 | tooling::RefactoringTool Tool(OP.getCompilations(), Files); |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 210 | unsigned Count = OldNames.size() ? OldNames.size() : SymbolOffsets.size(); |
| 211 | for (unsigned I = 0; I < Count; ++I) { |
| 212 | unsigned SymbolOffset = SymbolOffsets.empty() ? 0 : SymbolOffsets[I]; |
| 213 | const std::string &OldName = OldNames.empty() ? std::string() : OldNames[I]; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 214 | |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 215 | // Get the USRs. |
| 216 | rename::USRFindingAction USRAction(SymbolOffset, OldName); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 217 | |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 218 | // Find the USRs. |
| 219 | Tool.run(tooling::newFrontendActionFactory(&USRAction).get()); |
| 220 | const auto &USRs = USRAction.getUSRs(); |
| 221 | USRList.push_back(USRs); |
| 222 | const auto &PrevName = USRAction.getUSRSpelling(); |
| 223 | PrevNames.push_back(PrevName); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 224 | |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 225 | if (PrevName.empty()) { |
| 226 | // An error should have already been printed. |
| 227 | exit(1); |
| 228 | } |
| 229 | |
| 230 | if (PrintName) { |
| 231 | errs() << "clang-rename: found name: " << PrevName << '\n'; |
| 232 | } |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 233 | } |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 234 | |
| 235 | // Perform the renaming. |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 236 | rename::RenamingAction RenameAction(NewNames, PrevNames, USRList, |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 237 | Tool.getReplacements(), PrintLocations); |
| 238 | auto Factory = tooling::newFrontendActionFactory(&RenameAction); |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 239 | int ExitCode; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 240 | |
| 241 | if (Inplace) { |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 242 | ExitCode = Tool.runAndSave(Factory.get()); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 243 | } else { |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 244 | ExitCode = Tool.run(Factory.get()); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 245 | |
Miklos Vajna | a2ca3ed | 2016-06-27 19:34:47 +0000 | [diff] [blame] | 246 | if (!ExportFixes.empty()) { |
| 247 | std::error_code EC; |
| 248 | llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None); |
| 249 | if (EC) { |
| 250 | llvm::errs() << "Error opening output file: " << EC.message() << '\n'; |
| 251 | exit(1); |
| 252 | } |
| 253 | |
| 254 | // Export replacements. |
| 255 | tooling::TranslationUnitReplacements TUR; |
Eric Liu | 267034c | 2016-08-01 10:16:39 +0000 | [diff] [blame] | 256 | const auto &FileToReplacements = Tool.getReplacements(); |
| 257 | for (const auto &Entry : FileToReplacements) |
| 258 | TUR.Replacements.insert(TUR.Replacements.end(), Entry.second.begin(), |
| 259 | Entry.second.end()); |
Miklos Vajna | a2ca3ed | 2016-06-27 19:34:47 +0000 | [diff] [blame] | 260 | |
| 261 | yaml::Output YAML(OS); |
| 262 | YAML << TUR; |
| 263 | OS.close(); |
| 264 | exit(0); |
| 265 | } |
| 266 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 267 | // Write every file to stdout. Right now we just barf the files without any |
| 268 | // indication of which files start where, other than that we print the files |
| 269 | // in the same order we see them. |
| 270 | LangOptions DefaultLangOptions; |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 271 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 272 | TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts); |
| 273 | DiagnosticsEngine Diagnostics( |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 274 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, |
| 275 | &DiagnosticPrinter, false); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 276 | auto &FileMgr = Tool.getFiles(); |
| 277 | SourceManager Sources(Diagnostics, FileMgr); |
| 278 | Rewriter Rewrite(Sources, DefaultLangOptions); |
| 279 | |
| 280 | Tool.applyAllReplacements(Rewrite); |
| 281 | for (const auto &File : Files) { |
| 282 | const auto *Entry = FileMgr.getFile(File); |
| 283 | auto ID = Sources.translateFile(Entry); |
| 284 | Rewrite.getEditBuffer(ID).write(outs()); |
| 285 | } |
| 286 | } |
| 287 | |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 288 | exit(ExitCode); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 289 | } |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame] | 290 | |
| 291 | /// \brief Top level help. |
| 292 | /// FIXME It would be better if this could be auto-generated. |
| 293 | static int helpMain(int argc, const char *argv[]) { |
| 294 | errs() << "Usage: clang-rename {rename-at|rename-all} [OPTION]...\n\n" |
| 295 | "A tool to rename symbols in C/C++ code.\n\n" |
| 296 | "Subcommands:\n" |
| 297 | " rename-at: Perform rename off of a location in a file. (This " |
| 298 | "is the default.)\n" |
| 299 | " rename-all: Perform rename of all symbols matching one or more " |
| 300 | "fully qualified names.\n"; |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static int renameAtMain(int argc, const char *argv[]) { |
| 305 | return subcommandMain(false, argc, argv); |
| 306 | } |
| 307 | |
| 308 | static int renameAllMain(int argc, const char *argv[]) { |
| 309 | return subcommandMain(true, argc, argv); |
| 310 | } |