23#include "llvm/ADT/STLExtras.h"
24#include "llvm/Support/Errc.h"
25#include "llvm/Support/Error.h"
38 std::vector<std::string> USRs =
40 std::string PrevName = ND->getNameAsString();
51 "Finds and renames symbols in code with no indexer support",
62 return Context.createDiagnosticError(
63 SelectionRange.
getBegin(), diag::err_refactor_selection_no_symbol);
74 return Occurrences.takeError();
78 *Occurrences, Context.getASTContext().getSourceManager(), Name);
83 std::string OldQualifiedName,
84 std::string NewQualifiedName) {
88 return llvm::make_error<llvm::StringError>(
"Could not find symbol " +
90 llvm::errc::invalid_argument);
91 return QualifiedRenameRule(ND, std::move(NewQualifiedName));
96 "local-qualified-rename",
99 R
"(Finds and renames qualified symbols in code within a translation unit.
100It is used to move/rename a symbol to a new namespace/name:
101 * Supported symbols: classes, class members, functions, enums, and type alias.
102 * Renames all symbol occurrences from the old qualified name to the new
103 qualified name. All symbol references will be correctly qualified; For
104 symbol definitions, only name will be changed.
105For example, rename "A::Foo" to "B::Bar":
115 New code after rename:
128QualifiedRenameRule::createSourceReplacements(RefactoringRuleContext &Context) {
130 assert(!USRs.empty());
132 USRs, NewQualifiedName, Context.getASTContext().getTranslationUnitDecl());
139 std::vector<AtomicChange> Changes;
140 for (
const auto &Occurrence : Occurrences) {
143 "Mismatching number of ranges and name pieces");
145 for (
const auto &
Range : llvm::enumerate(Ranges)) {
150 return std::move(
Error);
152 Changes.push_back(std::move(Change));
154 return std::move(Changes);
161 std::map<std::string, tooling::Replacements> *FileToReplaces) {
165 (*FileToReplaces)[std::string(Replace.getFilePath())].add(Replace);
167 llvm::errs() <<
"Renaming failed in " << Replace.getFilePath() <<
"! "
168 << llvm::toString(std::move(Err)) <<
"\n";
177 const std::vector<std::string> &NewNames,
178 const std::vector<std::string> &PrevNames,
179 const std::vector<std::vector<std::string>> &USRList,
180 std::map<std::string, tooling::Replacements> &FileToReplaces,
182 : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList),
183 FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
186 for (
unsigned I = 0; I < NewNames.size(); ++I) {
188 if (PrevNames[I].empty())
196 const std::string &PrevName,
197 const std::vector<std::string> &USRs) {
202 if (PrintLocations) {
203 for (
const auto &Occurrence : Occurrences) {
204 FullSourceLoc FullLoc(Occurrence.getNameRanges()[0].getBegin(),
206 errs() <<
"clang-rename: renamed at: " << SourceMgr.getFilename(FullLoc)
207 <<
":" << FullLoc.getSpellingLineNumber() <<
":"
208 << FullLoc.getSpellingColumnNumber() <<
"\n";
217 llvm::errs() <<
"Failed to create renaming replacements for '" << PrevName
218 <<
"'! " << llvm::toString(Change.takeError()) <<
"\n";
225 const std::vector<std::string> &NewNames, &PrevNames;
226 const std::vector<std::vector<std::string>> &USRList;
227 std::map<std::string, tooling::Replacements> &FileToReplaces;
238 const std::vector<std::vector<std::string>> &USRList,
239 std::map<std::string, tooling::Replacements> &FileToReplaces)
240 : NewNames(NewNames), USRList(USRList), FileToReplaces(FileToReplaces) {
241 assert(USRList.size() == NewNames.size());
245 for (
unsigned I = 0; I < NewNames.size(); ++I) {
249 USRList[I], NewNames[I], Context.getTranslationUnitDecl());
255 const std::vector<std::string> &NewNames;
256 const std::vector<std::vector<std::string>> &USRList;
257 std::map<std::string, tooling::Replacements> &FileToReplaces;
261 return std::make_unique<RenamingASTConsumer>(NewNames, PrevNames, USRList,
262 FileToReplaces, PrintLocations);
266 return std::make_unique<USRSymbolRenamer>(NewNames, USRList, FileToReplaces);
Defines the clang::ASTContext interface.
Provides an action to rename every symbol at a point.
Methods for determining the USR of a symbol at a location in source code.
Provides an action to find all relevant USRs at a point.
Provides functionality for finding all instances of a USR in a given AST.
SourceManager & getSourceManager()
TranslationUnitDecl * getTranslationUnitDecl() const
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
static CharSourceRange getCharRange(SourceRange R)
ASTContext & getASTContext() const LLVM_READONLY
This represents a decl that may have a name.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
SourceLocation getBegin() const
The refactoring rule context stores all of the inputs that might be needed by a refactoring action ru...
The JSON file list parser is used to communicate input to InstallAPI.
Diagnostic wrappers for TextAPI types for error reporting.