ClangRename.cpp 8.1 KB

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