33 #include "llvm/ADT/STLExtras.h" 34 #include "llvm/Support/Errc.h" 35 #include "llvm/Support/Error.h" 47 findSymbolOccurrences(
const NamedDecl *ND, RefactoringRuleContext &Context) {
48 std::vector<std::string> USRs =
50 std::string PrevName = ND->getNameAsString();
52 Context.getASTContext().getTranslationUnitDecl());
61 "Finds and renames symbols in code with no indexer support",
73 SelectionRange.
getBegin(), diag::err_refactor_selection_no_symbol);
82 return Occurrences.takeError();
91 std::string OldQualifiedName,
92 std::string NewQualifiedName) {
96 return llvm::make_error<llvm::StringError>(
"Could not find symbol " +
98 llvm::errc::invalid_argument);
104 "local-qualified-rename",
107 R
"(Finds and renames qualified symbols in code within a translation unit. 108 It is used to move/rename a symbol to a new namespace/name: 109 * Supported symbols: classes, class members, functions, enums, and type alias. 110 * Renames all symbol occurrences from the old qualified name to the new 111 qualified name. All symbol references will be correctly qualified; For 112 symbol definitions, only name will be changed. 113 For example, rename "A::Foo" to "B::Bar": 123 New code after rename: 138 assert(!USRs.empty());
147 std::vector<AtomicChange> Changes;
148 for (
const auto &Occurrence : Occurrences) {
151 "Mismatching number of ranges and name pieces");
153 for (
const auto &
Range : llvm::enumerate(Ranges)) {
155 Change.replace(SM, CharSourceRange::getCharRange(
Range.value()),
158 return std::move(Error);
160 Changes.push_back(std::move(Change));
162 return std::move(Changes);
169 std::map<std::string, tooling::Replacements> *FileToReplaces) {
172 llvm::Error Err = (*FileToReplaces)[Replace.getFilePath()].add(Replace);
174 llvm::errs() <<
"Renaming failed in " << Replace.getFilePath() <<
"! " 184 const std::vector<std::string> &NewNames,
185 const std::vector<std::string> &PrevNames,
186 const std::vector<std::vector<std::string>> &USRList,
187 std::map<std::string, tooling::Replacements> &FileToReplaces,
189 : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList),
190 FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
193 for (
unsigned I = 0; I < NewNames.size(); ++I) {
195 if (PrevNames[I].empty())
198 HandleOneRename(Context, NewNames[I], PrevNames[I], USRList[I]);
203 const std::string &PrevName,
204 const std::vector<std::string> &USRs) {
209 if (PrintLocations) {
210 for (
const auto &Occurrence : Occurrences) {
211 FullSourceLoc FullLoc(Occurrence.getNameRanges()[0].getBegin(),
213 errs() <<
"clang-rename: renamed at: " << SourceMgr.
getFilename(FullLoc)
214 <<
":" << FullLoc.getSpellingLineNumber() <<
":" 215 << FullLoc.getSpellingColumnNumber() <<
"\n";
224 llvm::errs() <<
"Failed to create renaming replacements for '" << PrevName
232 const std::vector<std::string> &NewNames, &PrevNames;
233 const std::vector<std::vector<std::string>> &USRList;
234 std::map<std::string, tooling::Replacements> &FileToReplaces;
245 const std::vector<std::vector<std::string>> &USRList,
246 std::map<std::string, tooling::Replacements> &FileToReplaces)
247 : NewNames(NewNames), USRList(USRList), FileToReplaces(FileToReplaces) {
248 assert(USRList.size() == NewNames.size());
252 for (
unsigned I = 0; I < NewNames.size(); ++I) {
262 const std::vector<std::string> &NewNames;
263 const std::vector<std::vector<std::string>> &USRList;
264 std::map<std::string, tooling::Replacements> &FileToReplaces;
267 std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() {
268 return llvm::make_unique<RenamingASTConsumer>(NewNames, PrevNames, USRList,
269 FileToReplaces, PrintLocations);
272 std::unique_ptr<ASTConsumer> QualifiedRenamingAction::newASTConsumer() {
273 return llvm::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.
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
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.