32 #include "llvm/ADT/STLExtras.h" 33 #include "llvm/Support/Errc.h" 34 #include "llvm/Support/Error.h" 46 findSymbolOccurrences(
const NamedDecl *ND, RefactoringRuleContext &Context) {
47 std::vector<std::string> USRs =
49 std::string PrevName = ND->getNameAsString();
51 Context.getASTContext().getTranslationUnitDecl());
60 "Finds and renames symbols in code with no indexer support",
72 SelectionRange.
getBegin(), diag::err_refactor_selection_no_symbol);
77 const NamedDecl *RenameOccurrences::getRenameDecl()
const {
return ND; }
83 return Occurrences.takeError();
92 std::string OldQualifiedName,
93 std::string NewQualifiedName) {
97 return llvm::make_error<llvm::StringError>(
"Could not find symbol " +
99 llvm::errc::invalid_argument);
105 "local-qualified-rename",
108 R
"(Finds and renames qualified symbols in code within a translation unit. 109 It is used to move/rename a symbol to a new namespace/name: 110 * Supported symbols: classes, class members, functions, enums, and type alias. 111 * Renames all symbol occurrences from the old qualified name to the new 112 qualified name. All symbol references will be correctly qualified; For 113 symbol definitions, only name will be changed. 114 For example, rename "A::Foo" to "B::Bar": 124 New code after rename: 139 assert(!USRs.empty());
148 std::vector<AtomicChange> Changes;
149 for (
const auto &Occurrence : Occurrences) {
152 "Mismatching number of ranges and name pieces");
154 for (
const auto &
Range : llvm::enumerate(Ranges)) {
156 Change.replace(SM, CharSourceRange::getCharRange(
Range.value()),
159 return std::move(
Error);
161 Changes.push_back(std::move(Change));
163 return std::move(Changes);
170 std::map<std::string, tooling::Replacements> *FileToReplaces) {
173 llvm::Error Err = (*FileToReplaces)[Replace.getFilePath()].add(Replace);
175 llvm::errs() <<
"Renaming failed in " << Replace.getFilePath() <<
"! " 185 const std::vector<std::string> &NewNames,
186 const std::vector<std::string> &PrevNames,
187 const std::vector<std::vector<std::string>> &USRList,
188 std::map<std::string, tooling::Replacements> &FileToReplaces,
190 : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList),
191 FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
194 for (
unsigned I = 0; I < NewNames.size(); ++I) {
196 if (PrevNames[I].empty())
199 HandleOneRename(Context, NewNames[I], PrevNames[I], USRList[I]);
204 const std::string &PrevName,
205 const std::vector<std::string> &USRs) {
210 if (PrintLocations) {
211 for (
const auto &Occurrence : Occurrences) {
212 FullSourceLoc FullLoc(Occurrence.getNameRanges()[0].getBegin(),
214 errs() <<
"clang-rename: renamed at: " << SourceMgr.
getFilename(FullLoc)
215 <<
":" << FullLoc.getSpellingLineNumber() <<
":" 216 << FullLoc.getSpellingColumnNumber() <<
"\n";
225 llvm::errs() <<
"Failed to create renaming replacements for '" << PrevName
233 const std::vector<std::string> &NewNames, &PrevNames;
234 const std::vector<std::vector<std::string>> &USRList;
235 std::map<std::string, tooling::Replacements> &FileToReplaces;
246 const std::vector<std::vector<std::string>> &USRList,
247 std::map<std::string, tooling::Replacements> &FileToReplaces)
248 : NewNames(NewNames), USRList(USRList), FileToReplaces(FileToReplaces) {
249 assert(USRList.size() == NewNames.size());
253 for (
unsigned I = 0; I < NewNames.size(); ++I) {
263 const std::vector<std::string> &NewNames;
264 const std::vector<std::vector<std::string>> &USRList;
265 std::map<std::string, tooling::Replacements> &FileToReplaces;
268 std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() {
269 return std::make_unique<RenamingASTConsumer>(NewNames, PrevNames, USRList,
270 FileToReplaces, PrintLocations);
273 std::unique_ptr<ASTConsumer> QualifiedRenamingAction::newASTConsumer() {
274 return std::make_unique<USRSymbolRenamer>(NewNames, USRList, FileToReplaces);
Defines the clang::ASTContext interface.
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs...
Defines the clang::FileManager interface and associated types.
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
llvm::Error createDiagnosticError(SourceLocation Loc, unsigned DiagID)
Creates an llvm::Error value that contains a diagnostic.
Provides an action to find all relevant USRs at a point.
Methods for determining the USR of a symbol at a location in source code.
The refactoring rule context stores all of the inputs that might be needed by a refactoring action ru...
Provides functionality for finding all instances of a USR in a given AST.
Defines the clang::Preprocessor interface.
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
ASTContext & getASTContext() const
Dataflow Directional Tag Classes.
SourceManager & getSourceManager()
TranslationUnitDecl * getTranslationUnitDecl() const
A SourceLocation and its associated SourceManager.
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
A trivial tuple used to represent a source range.
This represents a decl that may have a name.
Provides an action to rename every symbol at a point.
SourceLocation getBegin() const
This class handles loading and caching of source files into memory.