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/Tooling.h"
36 #include "llvm/ADT/IntrusiveRefCntPtr.h"
37 #include "llvm/Support/Host.h"
49 static cl::opt<std::string>
52 cl::desc(
"The new name to change the symbol to."),
54 static cl::opt<unsigned>
57 cl::desc(
"Locates the symbol by offset as opposed to <line>:<column>."),
62 cl::desc(
"Overwrite edited <file>s."),
67 cl::desc(
"Print the found symbol's name prior to renaming to stderr."),
72 cl::desc(
"Print the locations affected by renaming to stderr."),
75 #define CLANG_RENAME_VERSION "0.0.1"
81 using namespace clang;
83 const char RenameUsage[] =
"A tool to rename symbols in C/C++ code.\n\
84 clang-rename renames every occurrence of a symbol found at <offset> in\n\
85 <source0>. If -i is specified, the edited files are overwritten to disk.\n\
86 Otherwise, the results are written to stdout.\n";
88 int main(
int argc,
const char **argv) {
95 errs() <<
"clang-rename: no new name provided.\n\n";
96 cl::PrintHelpMessage();
101 auto Files = OP.getSourcePathList();
102 tooling::RefactoringTool Tool(OP.getCompilations(),
Files);
106 Tool.run(tooling::newFrontendActionFactory(&USRAction).
get());
107 const auto &USRs = USRAction.getUSRs();
108 const auto &PrevName = USRAction.getUSRSpelling();
110 if (PrevName.empty())
115 errs() <<
"clang-rename: found name: " << PrevName;
120 auto Factory = tooling::newFrontendActionFactory(&RenameAction);
124 res = Tool.runAndSave(Factory.get());
126 res = Tool.run(Factory.get());
131 LangOptions DefaultLangOptions;
132 IntrusiveRefCntPtr<DiagnosticOptions>
DiagOpts =
133 new DiagnosticOptions();
134 TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts);
135 DiagnosticsEngine Diagnostics(
136 IntrusiveRefCntPtr<DiagnosticIDs>(
new DiagnosticIDs()),
137 &*DiagOpts, &DiagnosticPrinter,
false);
138 auto &FileMgr = Tool.getFiles();
139 SourceManager Sources(Diagnostics, FileMgr);
140 Rewriter
Rewrite(Sources, DefaultLangOptions);
142 Tool.applyAllReplacements(Rewrite);
144 const auto *
Entry = FileMgr.getFile(
File);
145 auto ID = Sources.translateFile(
Entry);
146 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 > NewName("new-name", cl::desc("The new name to change the symbol to."), cl::cat(ClangRenameCategory))
static cl::opt< unsigned > SymbolOffset("offset", cl::desc("Locates the symbol by offset as opposed to <line>:<column>."), cl::cat(ClangRenameCategory))
static void PrintVersion()
IntrusiveRefCntPtr< DiagnosticOptions > DiagOpts
cl::OptionCategory ClangRenameCategory("Clang-rename options")