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