17 #include "clang/AST/ASTConsumer.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/Basic/FileManager.h"
20 #include "clang/Frontend/CompilerInstance.h"
21 #include "clang/Frontend/FrontendAction.h"
22 #include "clang/Lex/Lexer.h"
23 #include "clang/Lex/Preprocessor.h"
24 #include "clang/Tooling/CommonOptionsParser.h"
25 #include "clang/Tooling/Refactoring.h"
26 #include "clang/Tooling/Tooling.h"
41 const std::string &PrevName,
42 const std::vector<std::string> &USRs,
43 tooling::Replacements &Replaces,
45 : NewName(NewName), PrevName(PrevName), USRs(USRs), Replaces(Replaces),
46 PrintLocations(PrintLocations) {
50 const auto &
SourceMgr = Context.getSourceManager();
51 std::vector<SourceLocation> RenamingCandidates;
52 std::vector<SourceLocation> NewCandidates;
54 for (
const auto &
USR : USRs) {
56 RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(),
58 NewCandidates.clear();
61 auto PrevNameLen = PrevName.length();
63 for (
const auto &
Loc : RenamingCandidates) {
65 errs() <<
"clang-rename: renamed at: " <<
SourceMgr.getFilename(
Loc)
66 <<
":" << FullLoc.getSpellingLineNumber() <<
":"
67 << FullLoc.getSpellingColumnNumber() <<
"\n";
68 Replaces.insert(tooling::Replacement(
SourceMgr,
Loc, PrevNameLen,
72 for (
const auto &
Loc : RenamingCandidates)
73 Replaces.insert(tooling::Replacement(
SourceMgr,
Loc, PrevNameLen,
78 const std::string &
NewName, &PrevName;
79 const std::vector<std::string> &USRs;
80 tooling::Replacements &Replaces;
84 std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() {
85 return llvm::make_unique<RenamingASTConsumer>(
NewName, PrevName, USRs,
SourceLocation Loc
'#' location in the include directive
RenamingASTConsumer(const std::string &NewName, const std::string &PrevName, const std::vector< std::string > &USRs, tooling::Replacements &Replaces, bool PrintLocations)
static cl::opt< bool > PrintLocations("pl", cl::desc("Print the locations affected by renaming to stderr."), cl::cat(ClangRenameCategory))
void HandleTranslationUnit(ASTContext &Context) override
static cl::opt< std::string > NewName("new-name", cl::desc("The new name to change the symbol to."), cl::cat(ClangRenameCategory))
Provides functionality for finding all instances of a USR in a given AST.
std::vector< SourceLocation > getLocationsOfUSR(const std::string USR, Decl *Decl)
ClangTidyContext & Context
Provides an action to rename every symbol at a point.