16 #include "../USRFindingAction.h"
17 #include "../RenamingAction.h"
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"
28 #include "clang/Lex/Lexer.h"
29 #include "clang/Lex/Preprocessor.h"
30 #include "clang/Parse/ParseAST.h"
31 #include "clang/Parse/Parser.h"
32 #include "clang/Rewrite/Core/Rewriter.h"
33 #include "clang/Tooling/CommonOptionsParser.h"
34 #include "clang/Tooling/Refactoring.h"
35 #include "clang/Tooling/ReplacementsYaml.h"
36 #include "clang/Tooling/Tooling.h"
37 #include "llvm/ADT/IntrusiveRefCntPtr.h"
38 #include "llvm/Support/Host.h"
45 static cl::opt<std::string>
48 cl::desc(
"The new name to change the symbol to."),
50 static cl::opt<unsigned>
53 cl::desc(
"Locates the symbol by offset as opposed to <line>:<column>."),
55 static cl::opt<std::string>
58 cl::desc(
"The fully qualified name of the symbol, if -offset is not used."),
63 cl::desc(
"Overwrite edited <file>s."),
68 cl::desc(
"Print the found symbol's name prior to renaming to stderr."),
73 cl::desc(
"Print the locations affected by renaming to stderr."),
75 static cl::opt<std::string>
78 cl::desc(
"YAML file to store suggested fixes in."),
79 cl::value_desc(
"filename"),
82 #define CLANG_RENAME_VERSION "0.0.1"
88 using namespace clang;
90 const char RenameUsage[] =
"A tool to rename symbols in C/C++ code.\n\
91 clang-rename renames every occurrence of a symbol found at <offset> in\n\
92 <source0>. If -i is specified, the edited files are overwritten to disk.\n\
93 Otherwise, the results are written to stdout.\n";
95 int main(
int argc,
const char **argv) {
102 errs() <<
"clang-rename: no new name provided.\n\n";
107 auto Files = OP.getSourcePathList();
108 tooling::RefactoringTool Tool(OP.getCompilations(),
Files);
112 Tool.run(tooling::newFrontendActionFactory(&USRAction).
get());
113 const auto &USRs = USRAction.getUSRs();
114 const auto &
PrevName = USRAction.getUSRSpelling();
122 errs() <<
"clang-rename: found name: " <<
PrevName <<
'\n';
128 auto Factory = tooling::newFrontendActionFactory(&RenameAction);
132 ExitCode = Tool.runAndSave(Factory.get());
134 ExitCode = Tool.run(Factory.get());
138 llvm::raw_fd_ostream OS(
ExportFixes, EC, llvm::sys::fs::F_None);
140 llvm::errs() <<
"Error opening output file: " << EC.message() <<
'\n';
145 tooling::TranslationUnitReplacements TUR;
146 const tooling::Replacements &Replacements = Tool.getReplacements();
147 TUR.Replacements.insert(TUR.Replacements.end(), Replacements.begin(),
150 yaml::Output YAML(OS);
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);
170 Tool.applyAllReplacements(Rewrite);
172 const auto *
Entry = FileMgr.getFile(
File);
173 auto ID = Sources.translateFile(
Entry);
174 Rewrite.getEditBuffer(ID).write(outs());
static cl::opt< bool > PrintName("pn", cl::desc("Print the found symbol's name prior to renaming to stderr."), cl::cat(ClangRenameCategory))
int main(int argc, const char **argv)
static cl::opt< bool > Inplace("i", cl::desc("Overwrite edited <file>s."), cl::cat(ClangRenameCategory))
static cl::opt< bool > PrintLocations("pl", cl::desc("Print the locations affected by renaming to stderr."), cl::cat(ClangRenameCategory))
#define CLANG_RENAME_VERSION
static cl::opt< std::string > ExportFixes("export-fixes", cl::desc("YAML file to store suggested fixes in."), cl::value_desc("filename"), cl::cat(ClangRenameCategory))
static cl::opt< std::string > NewName("new-name", cl::desc("The new name to change the symbol to."), cl::cat(ClangRenameCategory))
const std::string PrevName
static cl::opt< unsigned > SymbolOffset("offset", cl::desc("Locates the symbol by offset as opposed to <line>:<column>."), cl::cat(ClangRenameCategory))
static cl::opt< std::string > OldName("old-name", cl::desc("The fully qualified name of the symbol, if -offset is not used."), cl::cat(ClangRenameCategory))
static void PrintVersion()
IntrusiveRefCntPtr< DiagnosticOptions > DiagOpts
cl::OptionCategory ClangRenameCategory("Clang-rename options")