blob: 06f719abfb94f145c9be3ea5cea5aa30fab58a22 [file] [log] [blame]
Manuel Klimekde237262014-08-20 01:39:05 +00001//===--- 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 Klimek3f840a92014-10-13 11:30:27 +000016#include "../USRFindingAction.h"
17#include "../RenamingAction.h"
Manuel Klimekde237262014-08-20 01:39:05 +000018#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 Klimekde237262014-08-20 01:39:05 +000028#include "clang/Lex/Lexer.h"
Chandler Carruth3cbd71c2015-01-14 11:24:38 +000029#include "clang/Lex/Preprocessor.h"
Manuel Klimekde237262014-08-20 01:39:05 +000030#include "clang/Parse/ParseAST.h"
Chandler Carruth3cbd71c2015-01-14 11:24:38 +000031#include "clang/Parse/Parser.h"
Manuel Klimekde237262014-08-20 01:39:05 +000032#include "clang/Rewrite/Core/Rewriter.h"
33#include "clang/Tooling/CommonOptionsParser.h"
34#include "clang/Tooling/Refactoring.h"
Miklos Vajnaa2ca3ed2016-06-27 19:34:47 +000035#include "clang/Tooling/ReplacementsYaml.h"
Manuel Klimekde237262014-08-20 01:39:05 +000036#include "clang/Tooling/Tooling.h"
37#include "llvm/ADT/IntrusiveRefCntPtr.h"
38#include "llvm/Support/Host.h"
Eugene Zelenko05f7e6a2016-03-17 17:02:25 +000039#include <cstdlib>
Manuel Klimekde237262014-08-20 01:39:05 +000040#include <string>
Manuel Klimekde237262014-08-20 01:39:05 +000041
42using namespace llvm;
43
44cl::OptionCategory ClangRenameCategory("Clang-rename options");
45
46static cl::opt<std::string>
47NewName(
48 "new-name",
49 cl::desc("The new name to change the symbol to."),
50 cl::cat(ClangRenameCategory));
51static cl::opt<unsigned>
52SymbolOffset(
53 "offset",
54 cl::desc("Locates the symbol by offset as opposed to <line>:<column>."),
55 cl::cat(ClangRenameCategory));
Miklos Vajna47bd4632016-06-21 19:48:57 +000056static cl::opt<std::string>
57OldName(
58 "old-name",
59 cl::desc("The fully qualified name of the symbol, if -offset is not used."),
60 cl::cat(ClangRenameCategory));
Manuel Klimekde237262014-08-20 01:39:05 +000061static cl::opt<bool>
62Inplace(
63 "i",
64 cl::desc("Overwrite edited <file>s."),
65 cl::cat(ClangRenameCategory));
66static cl::opt<bool>
67PrintName(
68 "pn",
69 cl::desc("Print the found symbol's name prior to renaming to stderr."),
70 cl::cat(ClangRenameCategory));
71static cl::opt<bool>
72PrintLocations(
73 "pl",
74 cl::desc("Print the locations affected by renaming to stderr."),
75 cl::cat(ClangRenameCategory));
Miklos Vajnaa2ca3ed2016-06-27 19:34:47 +000076static cl::opt<std::string>
77ExportFixes(
78 "export-fixes",
79 cl::desc("YAML file to store suggested fixes in."),
80 cl::value_desc("filename"),
81 cl::cat(ClangRenameCategory));
Manuel Klimekde237262014-08-20 01:39:05 +000082
83#define CLANG_RENAME_VERSION "0.0.1"
84
85static void PrintVersion() {
86 outs() << "clang-rename version " << CLANG_RENAME_VERSION << "\n";
87}
88
89using namespace clang;
90
91const char RenameUsage[] = "A tool to rename symbols in C/C++ code.\n\
92clang-rename renames every occurrence of a symbol found at <offset> in\n\
93<source0>. If -i is specified, the edited files are overwritten to disk.\n\
94Otherwise, the results are written to stdout.\n";
95
96int main(int argc, const char **argv) {
97 cl::SetVersionPrinter(PrintVersion);
98 tooling::CommonOptionsParser OP(argc, argv, ClangRenameCategory, RenameUsage);
99
100 // Check the arguments for correctness.
101
102 if (NewName.empty()) {
103 errs() << "clang-rename: no new name provided.\n\n";
104 cl::PrintHelpMessage();
105 exit(1);
106 }
107
108 // Get the USRs.
109 auto Files = OP.getSourcePathList();
110 tooling::RefactoringTool Tool(OP.getCompilations(), Files);
Miklos Vajna47bd4632016-06-21 19:48:57 +0000111 rename::USRFindingAction USRAction(SymbolOffset, OldName);
Manuel Klimekde237262014-08-20 01:39:05 +0000112
113 // Find the USRs.
114 Tool.run(tooling::newFrontendActionFactory(&USRAction).get());
115 const auto &USRs = USRAction.getUSRs();
116 const auto &PrevName = USRAction.getUSRSpelling();
117
118 if (PrevName.empty())
119 // An error should have already been printed.
120 exit(1);
121
122 if (PrintName)
Manuel Klimekbc5f5812016-04-28 06:46:44 +0000123 errs() << "clang-rename: found name: " << PrevName << "\n";
Manuel Klimekde237262014-08-20 01:39:05 +0000124
125 // Perform the renaming.
126 rename::RenamingAction RenameAction(NewName, PrevName, USRs,
127 Tool.getReplacements(), PrintLocations);
128 auto Factory = tooling::newFrontendActionFactory(&RenameAction);
129 int res;
130
131 if (Inplace) {
132 res = Tool.runAndSave(Factory.get());
133 } else {
134 res = Tool.run(Factory.get());
135
Miklos Vajnaa2ca3ed2016-06-27 19:34:47 +0000136 if (!ExportFixes.empty()) {
137 std::error_code EC;
138 llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None);
139 if (EC) {
140 llvm::errs() << "Error opening output file: " << EC.message() << '\n';
141 exit(1);
142 }
143
144 // Export replacements.
145 tooling::TranslationUnitReplacements TUR;
146 const tooling::Replacements &Replacements = Tool.getReplacements();
147 TUR.Replacements.insert(TUR.Replacements.end(), Replacements.begin(),
148 Replacements.end());
149
150 yaml::Output YAML(OS);
151 YAML << TUR;
152 OS.close();
153 exit(0);
154 }
155
Manuel Klimekde237262014-08-20 01:39:05 +0000156 // Write every file to stdout. Right now we just barf the files without any
157 // indication of which files start where, other than that we print the files
158 // in the same order we see them.
159 LangOptions DefaultLangOptions;
160 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
161 new DiagnosticOptions();
162 TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts);
163 DiagnosticsEngine Diagnostics(
164 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
165 &*DiagOpts, &DiagnosticPrinter, false);
166 auto &FileMgr = Tool.getFiles();
167 SourceManager Sources(Diagnostics, FileMgr);
168 Rewriter Rewrite(Sources, DefaultLangOptions);
169
170 Tool.applyAllReplacements(Rewrite);
171 for (const auto &File : Files) {
172 const auto *Entry = FileMgr.getFile(File);
173 auto ID = Sources.translateFile(Entry);
174 Rewrite.getEditBuffer(ID).write(outs());
175 }
176 }
177
178 exit(res);
179}