xref: /openbsd-src/gnu/llvm/clang/lib/Tooling/Refactoring/Rename/RenamingAction.cpp (revision ec727ea710c91afd8ce4f788c5aaa8482b7b69b2)
1e5dd7070Spatrick //===--- RenamingAction.cpp - Clang refactoring library -------------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick ///
9e5dd7070Spatrick /// \file
10e5dd7070Spatrick /// Provides an action to rename every symbol at a point.
11e5dd7070Spatrick ///
12e5dd7070Spatrick //===----------------------------------------------------------------------===//
13e5dd7070Spatrick 
14e5dd7070Spatrick #include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
15e5dd7070Spatrick #include "clang/AST/ASTConsumer.h"
16e5dd7070Spatrick #include "clang/AST/ASTContext.h"
17e5dd7070Spatrick #include "clang/Basic/FileManager.h"
18e5dd7070Spatrick #include "clang/Frontend/CompilerInstance.h"
19e5dd7070Spatrick #include "clang/Frontend/FrontendAction.h"
20e5dd7070Spatrick #include "clang/Lex/Lexer.h"
21e5dd7070Spatrick #include "clang/Lex/Preprocessor.h"
22e5dd7070Spatrick #include "clang/Tooling/CommonOptionsParser.h"
23e5dd7070Spatrick #include "clang/Tooling/Refactoring.h"
24e5dd7070Spatrick #include "clang/Tooling/Refactoring/RefactoringAction.h"
25e5dd7070Spatrick #include "clang/Tooling/Refactoring/RefactoringDiagnostic.h"
26e5dd7070Spatrick #include "clang/Tooling/Refactoring/RefactoringOptions.h"
27e5dd7070Spatrick #include "clang/Tooling/Refactoring/Rename/SymbolName.h"
28e5dd7070Spatrick #include "clang/Tooling/Refactoring/Rename/USRFinder.h"
29e5dd7070Spatrick #include "clang/Tooling/Refactoring/Rename/USRFindingAction.h"
30e5dd7070Spatrick #include "clang/Tooling/Refactoring/Rename/USRLocFinder.h"
31e5dd7070Spatrick #include "clang/Tooling/Tooling.h"
32e5dd7070Spatrick #include "llvm/ADT/STLExtras.h"
33e5dd7070Spatrick #include "llvm/Support/Errc.h"
34e5dd7070Spatrick #include "llvm/Support/Error.h"
35e5dd7070Spatrick #include <string>
36e5dd7070Spatrick #include <vector>
37e5dd7070Spatrick 
38e5dd7070Spatrick using namespace llvm;
39e5dd7070Spatrick 
40e5dd7070Spatrick namespace clang {
41e5dd7070Spatrick namespace tooling {
42e5dd7070Spatrick 
43e5dd7070Spatrick namespace {
44e5dd7070Spatrick 
45e5dd7070Spatrick Expected<SymbolOccurrences>
findSymbolOccurrences(const NamedDecl * ND,RefactoringRuleContext & Context)46e5dd7070Spatrick findSymbolOccurrences(const NamedDecl *ND, RefactoringRuleContext &Context) {
47e5dd7070Spatrick   std::vector<std::string> USRs =
48e5dd7070Spatrick       getUSRsForDeclaration(ND, Context.getASTContext());
49e5dd7070Spatrick   std::string PrevName = ND->getNameAsString();
50e5dd7070Spatrick   return getOccurrencesOfUSRs(USRs, PrevName,
51e5dd7070Spatrick                               Context.getASTContext().getTranslationUnitDecl());
52e5dd7070Spatrick }
53e5dd7070Spatrick 
54e5dd7070Spatrick } // end anonymous namespace
55e5dd7070Spatrick 
describe()56e5dd7070Spatrick const RefactoringDescriptor &RenameOccurrences::describe() {
57e5dd7070Spatrick   static const RefactoringDescriptor Descriptor = {
58e5dd7070Spatrick       "local-rename",
59e5dd7070Spatrick       "Rename",
60e5dd7070Spatrick       "Finds and renames symbols in code with no indexer support",
61e5dd7070Spatrick   };
62e5dd7070Spatrick   return Descriptor;
63e5dd7070Spatrick }
64e5dd7070Spatrick 
65e5dd7070Spatrick Expected<RenameOccurrences>
initiate(RefactoringRuleContext & Context,SourceRange SelectionRange,std::string NewName)66e5dd7070Spatrick RenameOccurrences::initiate(RefactoringRuleContext &Context,
67e5dd7070Spatrick                             SourceRange SelectionRange, std::string NewName) {
68e5dd7070Spatrick   const NamedDecl *ND =
69e5dd7070Spatrick       getNamedDeclAt(Context.getASTContext(), SelectionRange.getBegin());
70e5dd7070Spatrick   if (!ND)
71e5dd7070Spatrick     return Context.createDiagnosticError(
72e5dd7070Spatrick         SelectionRange.getBegin(), diag::err_refactor_selection_no_symbol);
73e5dd7070Spatrick   return RenameOccurrences(getCanonicalSymbolDeclaration(ND),
74e5dd7070Spatrick                            std::move(NewName));
75e5dd7070Spatrick }
76e5dd7070Spatrick 
getRenameDecl() const77e5dd7070Spatrick const NamedDecl *RenameOccurrences::getRenameDecl() const { return ND; }
78e5dd7070Spatrick 
79e5dd7070Spatrick Expected<AtomicChanges>
createSourceReplacements(RefactoringRuleContext & Context)80e5dd7070Spatrick RenameOccurrences::createSourceReplacements(RefactoringRuleContext &Context) {
81e5dd7070Spatrick   Expected<SymbolOccurrences> Occurrences = findSymbolOccurrences(ND, Context);
82e5dd7070Spatrick   if (!Occurrences)
83e5dd7070Spatrick     return Occurrences.takeError();
84e5dd7070Spatrick   // FIXME: Verify that the new name is valid.
85e5dd7070Spatrick   SymbolName Name(NewName);
86e5dd7070Spatrick   return createRenameReplacements(
87e5dd7070Spatrick       *Occurrences, Context.getASTContext().getSourceManager(), Name);
88e5dd7070Spatrick }
89e5dd7070Spatrick 
90e5dd7070Spatrick Expected<QualifiedRenameRule>
initiate(RefactoringRuleContext & Context,std::string OldQualifiedName,std::string NewQualifiedName)91e5dd7070Spatrick QualifiedRenameRule::initiate(RefactoringRuleContext &Context,
92e5dd7070Spatrick                               std::string OldQualifiedName,
93e5dd7070Spatrick                               std::string NewQualifiedName) {
94e5dd7070Spatrick   const NamedDecl *ND =
95e5dd7070Spatrick       getNamedDeclFor(Context.getASTContext(), OldQualifiedName);
96e5dd7070Spatrick   if (!ND)
97e5dd7070Spatrick     return llvm::make_error<llvm::StringError>("Could not find symbol " +
98e5dd7070Spatrick                                                    OldQualifiedName,
99e5dd7070Spatrick                                                llvm::errc::invalid_argument);
100e5dd7070Spatrick   return QualifiedRenameRule(ND, std::move(NewQualifiedName));
101e5dd7070Spatrick }
102e5dd7070Spatrick 
describe()103e5dd7070Spatrick const RefactoringDescriptor &QualifiedRenameRule::describe() {
104e5dd7070Spatrick   static const RefactoringDescriptor Descriptor = {
105e5dd7070Spatrick       /*Name=*/"local-qualified-rename",
106e5dd7070Spatrick       /*Title=*/"Qualified Rename",
107e5dd7070Spatrick       /*Description=*/
108e5dd7070Spatrick       R"(Finds and renames qualified symbols in code within a translation unit.
109e5dd7070Spatrick It is used to move/rename a symbol to a new namespace/name:
110e5dd7070Spatrick   * Supported symbols: classes, class members, functions, enums, and type alias.
111e5dd7070Spatrick   * Renames all symbol occurrences from the old qualified name to the new
112e5dd7070Spatrick     qualified name. All symbol references will be correctly qualified; For
113e5dd7070Spatrick     symbol definitions, only name will be changed.
114e5dd7070Spatrick For example, rename "A::Foo" to "B::Bar":
115e5dd7070Spatrick   Old code:
116e5dd7070Spatrick     namespace foo {
117e5dd7070Spatrick     class A {};
118e5dd7070Spatrick     }
119e5dd7070Spatrick 
120e5dd7070Spatrick     namespace bar {
121e5dd7070Spatrick     void f(foo::A a) {}
122e5dd7070Spatrick     }
123e5dd7070Spatrick 
124e5dd7070Spatrick   New code after rename:
125e5dd7070Spatrick     namespace foo {
126e5dd7070Spatrick     class B {};
127e5dd7070Spatrick     }
128e5dd7070Spatrick 
129e5dd7070Spatrick     namespace bar {
130e5dd7070Spatrick     void f(B b) {}
131e5dd7070Spatrick     })"
132e5dd7070Spatrick   };
133e5dd7070Spatrick   return Descriptor;
134e5dd7070Spatrick }
135e5dd7070Spatrick 
136e5dd7070Spatrick Expected<AtomicChanges>
createSourceReplacements(RefactoringRuleContext & Context)137e5dd7070Spatrick QualifiedRenameRule::createSourceReplacements(RefactoringRuleContext &Context) {
138e5dd7070Spatrick   auto USRs = getUSRsForDeclaration(ND, Context.getASTContext());
139e5dd7070Spatrick   assert(!USRs.empty());
140e5dd7070Spatrick   return tooling::createRenameAtomicChanges(
141e5dd7070Spatrick       USRs, NewQualifiedName, Context.getASTContext().getTranslationUnitDecl());
142e5dd7070Spatrick }
143e5dd7070Spatrick 
144e5dd7070Spatrick Expected<std::vector<AtomicChange>>
createRenameReplacements(const SymbolOccurrences & Occurrences,const SourceManager & SM,const SymbolName & NewName)145e5dd7070Spatrick createRenameReplacements(const SymbolOccurrences &Occurrences,
146e5dd7070Spatrick                          const SourceManager &SM, const SymbolName &NewName) {
147e5dd7070Spatrick   // FIXME: A true local rename can use just one AtomicChange.
148e5dd7070Spatrick   std::vector<AtomicChange> Changes;
149e5dd7070Spatrick   for (const auto &Occurrence : Occurrences) {
150e5dd7070Spatrick     ArrayRef<SourceRange> Ranges = Occurrence.getNameRanges();
151e5dd7070Spatrick     assert(NewName.getNamePieces().size() == Ranges.size() &&
152e5dd7070Spatrick            "Mismatching number of ranges and name pieces");
153e5dd7070Spatrick     AtomicChange Change(SM, Ranges[0].getBegin());
154e5dd7070Spatrick     for (const auto &Range : llvm::enumerate(Ranges)) {
155e5dd7070Spatrick       auto Error =
156e5dd7070Spatrick           Change.replace(SM, CharSourceRange::getCharRange(Range.value()),
157e5dd7070Spatrick                          NewName.getNamePieces()[Range.index()]);
158e5dd7070Spatrick       if (Error)
159e5dd7070Spatrick         return std::move(Error);
160e5dd7070Spatrick     }
161e5dd7070Spatrick     Changes.push_back(std::move(Change));
162e5dd7070Spatrick   }
163e5dd7070Spatrick   return std::move(Changes);
164e5dd7070Spatrick }
165e5dd7070Spatrick 
166e5dd7070Spatrick /// Takes each atomic change and inserts its replacements into the set of
167e5dd7070Spatrick /// replacements that belong to the appropriate file.
convertChangesToFileReplacements(ArrayRef<AtomicChange> AtomicChanges,std::map<std::string,tooling::Replacements> * FileToReplaces)168e5dd7070Spatrick static void convertChangesToFileReplacements(
169e5dd7070Spatrick     ArrayRef<AtomicChange> AtomicChanges,
170e5dd7070Spatrick     std::map<std::string, tooling::Replacements> *FileToReplaces) {
171e5dd7070Spatrick   for (const auto &AtomicChange : AtomicChanges) {
172e5dd7070Spatrick     for (const auto &Replace : AtomicChange.getReplacements()) {
173*ec727ea7Spatrick       llvm::Error Err =
174*ec727ea7Spatrick           (*FileToReplaces)[std::string(Replace.getFilePath())].add(Replace);
175e5dd7070Spatrick       if (Err) {
176e5dd7070Spatrick         llvm::errs() << "Renaming failed in " << Replace.getFilePath() << "! "
177e5dd7070Spatrick                      << llvm::toString(std::move(Err)) << "\n";
178e5dd7070Spatrick       }
179e5dd7070Spatrick     }
180e5dd7070Spatrick   }
181e5dd7070Spatrick }
182e5dd7070Spatrick 
183e5dd7070Spatrick class RenamingASTConsumer : public ASTConsumer {
184e5dd7070Spatrick public:
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)185e5dd7070Spatrick   RenamingASTConsumer(
186e5dd7070Spatrick       const std::vector<std::string> &NewNames,
187e5dd7070Spatrick       const std::vector<std::string> &PrevNames,
188e5dd7070Spatrick       const std::vector<std::vector<std::string>> &USRList,
189e5dd7070Spatrick       std::map<std::string, tooling::Replacements> &FileToReplaces,
190e5dd7070Spatrick       bool PrintLocations)
191e5dd7070Spatrick       : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList),
192e5dd7070Spatrick         FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
193e5dd7070Spatrick 
HandleTranslationUnit(ASTContext & Context)194e5dd7070Spatrick   void HandleTranslationUnit(ASTContext &Context) override {
195e5dd7070Spatrick     for (unsigned I = 0; I < NewNames.size(); ++I) {
196e5dd7070Spatrick       // If the previous name was not found, ignore this rename request.
197e5dd7070Spatrick       if (PrevNames[I].empty())
198e5dd7070Spatrick         continue;
199e5dd7070Spatrick 
200e5dd7070Spatrick       HandleOneRename(Context, NewNames[I], PrevNames[I], USRList[I]);
201e5dd7070Spatrick     }
202e5dd7070Spatrick   }
203e5dd7070Spatrick 
HandleOneRename(ASTContext & Context,const std::string & NewName,const std::string & PrevName,const std::vector<std::string> & USRs)204e5dd7070Spatrick   void HandleOneRename(ASTContext &Context, const std::string &NewName,
205e5dd7070Spatrick                        const std::string &PrevName,
206e5dd7070Spatrick                        const std::vector<std::string> &USRs) {
207e5dd7070Spatrick     const SourceManager &SourceMgr = Context.getSourceManager();
208e5dd7070Spatrick 
209e5dd7070Spatrick     SymbolOccurrences Occurrences = tooling::getOccurrencesOfUSRs(
210e5dd7070Spatrick         USRs, PrevName, Context.getTranslationUnitDecl());
211e5dd7070Spatrick     if (PrintLocations) {
212e5dd7070Spatrick       for (const auto &Occurrence : Occurrences) {
213e5dd7070Spatrick         FullSourceLoc FullLoc(Occurrence.getNameRanges()[0].getBegin(),
214e5dd7070Spatrick                               SourceMgr);
215e5dd7070Spatrick         errs() << "clang-rename: renamed at: " << SourceMgr.getFilename(FullLoc)
216e5dd7070Spatrick                << ":" << FullLoc.getSpellingLineNumber() << ":"
217e5dd7070Spatrick                << FullLoc.getSpellingColumnNumber() << "\n";
218e5dd7070Spatrick       }
219e5dd7070Spatrick     }
220e5dd7070Spatrick     // FIXME: Support multi-piece names.
221e5dd7070Spatrick     // FIXME: better error handling (propagate error out).
222e5dd7070Spatrick     SymbolName NewNameRef(NewName);
223e5dd7070Spatrick     Expected<std::vector<AtomicChange>> Change =
224e5dd7070Spatrick         createRenameReplacements(Occurrences, SourceMgr, NewNameRef);
225e5dd7070Spatrick     if (!Change) {
226e5dd7070Spatrick       llvm::errs() << "Failed to create renaming replacements for '" << PrevName
227e5dd7070Spatrick                    << "'! " << llvm::toString(Change.takeError()) << "\n";
228e5dd7070Spatrick       return;
229e5dd7070Spatrick     }
230e5dd7070Spatrick     convertChangesToFileReplacements(*Change, &FileToReplaces);
231e5dd7070Spatrick   }
232e5dd7070Spatrick 
233e5dd7070Spatrick private:
234e5dd7070Spatrick   const std::vector<std::string> &NewNames, &PrevNames;
235e5dd7070Spatrick   const std::vector<std::vector<std::string>> &USRList;
236e5dd7070Spatrick   std::map<std::string, tooling::Replacements> &FileToReplaces;
237e5dd7070Spatrick   bool PrintLocations;
238e5dd7070Spatrick };
239e5dd7070Spatrick 
240e5dd7070Spatrick // A renamer to rename symbols which are identified by a give USRList to
241e5dd7070Spatrick // new name.
242e5dd7070Spatrick //
243e5dd7070Spatrick // FIXME: Merge with the above RenamingASTConsumer.
244e5dd7070Spatrick class USRSymbolRenamer : public ASTConsumer {
245e5dd7070Spatrick public:
USRSymbolRenamer(const std::vector<std::string> & NewNames,const std::vector<std::vector<std::string>> & USRList,std::map<std::string,tooling::Replacements> & FileToReplaces)246e5dd7070Spatrick   USRSymbolRenamer(const std::vector<std::string> &NewNames,
247e5dd7070Spatrick                    const std::vector<std::vector<std::string>> &USRList,
248e5dd7070Spatrick                    std::map<std::string, tooling::Replacements> &FileToReplaces)
249e5dd7070Spatrick       : NewNames(NewNames), USRList(USRList), FileToReplaces(FileToReplaces) {
250e5dd7070Spatrick     assert(USRList.size() == NewNames.size());
251e5dd7070Spatrick   }
252e5dd7070Spatrick 
HandleTranslationUnit(ASTContext & Context)253e5dd7070Spatrick   void HandleTranslationUnit(ASTContext &Context) override {
254e5dd7070Spatrick     for (unsigned I = 0; I < NewNames.size(); ++I) {
255e5dd7070Spatrick       // FIXME: Apply AtomicChanges directly once the refactoring APIs are
256e5dd7070Spatrick       // ready.
257e5dd7070Spatrick       auto AtomicChanges = tooling::createRenameAtomicChanges(
258e5dd7070Spatrick           USRList[I], NewNames[I], Context.getTranslationUnitDecl());
259e5dd7070Spatrick       convertChangesToFileReplacements(AtomicChanges, &FileToReplaces);
260e5dd7070Spatrick     }
261e5dd7070Spatrick   }
262e5dd7070Spatrick 
263e5dd7070Spatrick private:
264e5dd7070Spatrick   const std::vector<std::string> &NewNames;
265e5dd7070Spatrick   const std::vector<std::vector<std::string>> &USRList;
266e5dd7070Spatrick   std::map<std::string, tooling::Replacements> &FileToReplaces;
267e5dd7070Spatrick };
268e5dd7070Spatrick 
newASTConsumer()269e5dd7070Spatrick std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() {
270e5dd7070Spatrick   return std::make_unique<RenamingASTConsumer>(NewNames, PrevNames, USRList,
271e5dd7070Spatrick                                                 FileToReplaces, PrintLocations);
272e5dd7070Spatrick }
273e5dd7070Spatrick 
newASTConsumer()274e5dd7070Spatrick std::unique_ptr<ASTConsumer> QualifiedRenamingAction::newASTConsumer() {
275e5dd7070Spatrick   return std::make_unique<USRSymbolRenamer>(NewNames, USRList, FileToReplaces);
276e5dd7070Spatrick }
277e5dd7070Spatrick 
278e5dd7070Spatrick } // end namespace tooling
279e5dd7070Spatrick } // end namespace clang
280