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"
38 const std::vector<std::string> &
NewNames,
39 const std::vector<std::string> &PrevNames,
40 const std::vector<std::vector<std::string>> &USRList,
41 std::map<std::string, tooling::Replacements> &FileToReplaces,
43 : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList),
44 FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
47 for (
unsigned I = 0; I <
NewNames.size(); ++I)
48 HandleOneRename(Context,
NewNames[I], PrevNames[I], USRList[I]);
53 const std::vector<std::string> &USRs) {
54 const SourceManager &
SourceMgr = Context.getSourceManager();
55 std::vector<SourceLocation> RenamingCandidates;
56 std::vector<SourceLocation> NewCandidates;
60 RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(),
63 unsigned PrevNameLen = PrevName.length();
64 for (
const auto &
Loc : RenamingCandidates) {
66 FullSourceLoc FullLoc(
Loc, SourceMgr);
67 errs() <<
"clang-rename: renamed at: " << SourceMgr.getFilename(
Loc)
68 <<
":" << FullLoc.getSpellingLineNumber() <<
":"
69 << FullLoc.getSpellingColumnNumber() <<
"\n";
72 tooling::Replacement Replace(SourceMgr,
Loc, PrevNameLen, NewName);
73 llvm::Error Err = FileToReplaces[Replace.getFilePath()].add(Replace);
75 llvm::errs() <<
"Renaming failed in " << Replace.getFilePath() <<
"! "
81 const std::vector<std::string> &
NewNames, &PrevNames;
82 const std::vector<std::vector<std::string>> &USRList;
83 std::map<std::string, tooling::Replacements> &FileToReplaces;
87 std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() {
88 return llvm::make_unique<RenamingASTConsumer>(
NewNames, PrevNames, USRList,
SourceLocation Loc
'#' location in the include directive
static cl::opt< bool > PrintLocations("pl", cl::desc("Print the locations affected by renaming to stderr."), cl::cat(ClangRenameOptions))
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
static cl::list< std::string > NewNames("new-name", cl::desc("The new name to change the symbol to."), cl::ZeroOrMore, cl::cat(ClangRenameOptions))
void HandleTranslationUnit(ASTContext &Context) override
const std::string PrevName
Provides functionality for finding all instances of a USR in a given AST.
void HandleOneRename(ASTContext &Context, const std::string &NewName, const std::string &PrevName, const std::vector< std::string > &USRs)
ClangTidyContext & Context
std::vector< SourceLocation > getLocationsOfUSRs(const std::vector< std::string > &USRs, StringRef PrevName, Decl *Decl)
RenamingASTConsumer(const std::vector< std::string > &NewNames, const std::vector< std::string > &PrevNames, const std::vector< std::vector< std::string >> &USRList, std::map< std::string, tooling::Replacements > &FileToReplaces, bool PrintLocations)
Provides an action to rename every symbol at a point.