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" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTConsumer.h" |
| 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/Basic/FileManager.h" |
| 21 | #include "clang/Basic/LangOptions.h" |
| 22 | #include "clang/Basic/TargetInfo.h" |
| 23 | #include "clang/Basic/TargetOptions.h" |
| 24 | #include "clang/Frontend/CommandLineSourceLoc.h" |
| 25 | #include "clang/Frontend/CompilerInstance.h" |
| 26 | #include "clang/Frontend/FrontendAction.h" |
| 27 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Lexer.h" |
Chandler Carruth | 3cbd71c | 2015-01-14 11:24:38 +0000 | [diff] [blame] | 29 | #include "clang/Lex/Preprocessor.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 30 | #include "clang/Parse/ParseAST.h" |
Chandler Carruth | 3cbd71c | 2015-01-14 11:24:38 +0000 | [diff] [blame] | 31 | #include "clang/Parse/Parser.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 32 | #include "clang/Rewrite/Core/Rewriter.h" |
| 33 | #include "clang/Tooling/CommonOptionsParser.h" |
| 34 | #include "clang/Tooling/Refactoring.h" |
| 35 | #include "clang/Tooling/Tooling.h" |
| 36 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 37 | #include "llvm/Support/Host.h" |
Eugene Zelenko | 05f7e6a | 2016-03-17 17:02:25 +0000 | [diff] [blame] | 38 | #include <cstdlib> |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 39 | #include <string> |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 40 | |
| 41 | using namespace llvm; |
| 42 | |
| 43 | cl::OptionCategory ClangRenameCategory("Clang-rename options"); |
| 44 | |
| 45 | static cl::opt<std::string> |
| 46 | NewName( |
| 47 | "new-name", |
| 48 | cl::desc("The new name to change the symbol to."), |
| 49 | cl::cat(ClangRenameCategory)); |
| 50 | static cl::opt<unsigned> |
| 51 | SymbolOffset( |
| 52 | "offset", |
| 53 | cl::desc("Locates the symbol by offset as opposed to <line>:<column>."), |
| 54 | cl::cat(ClangRenameCategory)); |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 55 | static cl::opt<std::string> |
| 56 | OldName( |
| 57 | "old-name", |
| 58 | cl::desc("The fully qualified name of the symbol, if -offset is not used."), |
| 59 | cl::cat(ClangRenameCategory)); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 60 | static cl::opt<bool> |
| 61 | Inplace( |
| 62 | "i", |
| 63 | cl::desc("Overwrite edited <file>s."), |
| 64 | cl::cat(ClangRenameCategory)); |
| 65 | static cl::opt<bool> |
| 66 | PrintName( |
| 67 | "pn", |
| 68 | cl::desc("Print the found symbol's name prior to renaming to stderr."), |
| 69 | cl::cat(ClangRenameCategory)); |
| 70 | static cl::opt<bool> |
| 71 | PrintLocations( |
| 72 | "pl", |
| 73 | cl::desc("Print the locations affected by renaming to stderr."), |
| 74 | cl::cat(ClangRenameCategory)); |
| 75 | |
| 76 | #define CLANG_RENAME_VERSION "0.0.1" |
| 77 | |
| 78 | static void PrintVersion() { |
| 79 | outs() << "clang-rename version " << CLANG_RENAME_VERSION << "\n"; |
| 80 | } |
| 81 | |
| 82 | using namespace clang; |
| 83 | |
| 84 | const char RenameUsage[] = "A tool to rename symbols in C/C++ code.\n\ |
| 85 | clang-rename renames every occurrence of a symbol found at <offset> in\n\ |
| 86 | <source0>. If -i is specified, the edited files are overwritten to disk.\n\ |
| 87 | Otherwise, the results are written to stdout.\n"; |
| 88 | |
| 89 | int main(int argc, const char **argv) { |
| 90 | cl::SetVersionPrinter(PrintVersion); |
| 91 | tooling::CommonOptionsParser OP(argc, argv, ClangRenameCategory, RenameUsage); |
| 92 | |
| 93 | // Check the arguments for correctness. |
| 94 | |
| 95 | if (NewName.empty()) { |
| 96 | errs() << "clang-rename: no new name provided.\n\n"; |
| 97 | cl::PrintHelpMessage(); |
| 98 | exit(1); |
| 99 | } |
| 100 | |
| 101 | // Get the USRs. |
| 102 | auto Files = OP.getSourcePathList(); |
| 103 | tooling::RefactoringTool Tool(OP.getCompilations(), Files); |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 104 | rename::USRFindingAction USRAction(SymbolOffset, OldName); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 105 | |
| 106 | // Find the USRs. |
| 107 | Tool.run(tooling::newFrontendActionFactory(&USRAction).get()); |
| 108 | const auto &USRs = USRAction.getUSRs(); |
| 109 | const auto &PrevName = USRAction.getUSRSpelling(); |
| 110 | |
| 111 | if (PrevName.empty()) |
| 112 | // An error should have already been printed. |
| 113 | exit(1); |
| 114 | |
| 115 | if (PrintName) |
Manuel Klimek | bc5f581 | 2016-04-28 06:46:44 +0000 | [diff] [blame] | 116 | errs() << "clang-rename: found name: " << PrevName << "\n"; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 117 | |
| 118 | // Perform the renaming. |
| 119 | rename::RenamingAction RenameAction(NewName, PrevName, USRs, |
| 120 | Tool.getReplacements(), PrintLocations); |
| 121 | auto Factory = tooling::newFrontendActionFactory(&RenameAction); |
| 122 | int res; |
| 123 | |
| 124 | if (Inplace) { |
| 125 | res = Tool.runAndSave(Factory.get()); |
| 126 | } else { |
| 127 | res = Tool.run(Factory.get()); |
| 128 | |
| 129 | // Write every file to stdout. Right now we just barf the files without any |
| 130 | // indication of which files start where, other than that we print the files |
| 131 | // in the same order we see them. |
| 132 | LangOptions DefaultLangOptions; |
| 133 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = |
| 134 | new DiagnosticOptions(); |
| 135 | TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts); |
| 136 | DiagnosticsEngine Diagnostics( |
| 137 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), |
| 138 | &*DiagOpts, &DiagnosticPrinter, false); |
| 139 | auto &FileMgr = Tool.getFiles(); |
| 140 | SourceManager Sources(Diagnostics, FileMgr); |
| 141 | Rewriter Rewrite(Sources, DefaultLangOptions); |
| 142 | |
| 143 | Tool.applyAllReplacements(Rewrite); |
| 144 | for (const auto &File : Files) { |
| 145 | const auto *Entry = FileMgr.getFile(File); |
| 146 | auto ID = Sources.translateFile(Entry); |
| 147 | Rewrite.getEditBuffer(ID).write(outs()); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | exit(res); |
| 152 | } |