1 //===--- RenamingAction.h - Clang refactoring library ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Provides an action to rename every symbol at a point.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H
16 #define LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H
18 #include "clang/Tooling/Refactoring.h"
19 #include "clang/Tooling/Refactoring/AtomicChange.h"
20 #include "clang/Tooling/Refactoring/RefactoringActionRules.h"
21 #include "clang/Tooling/Refactoring/RefactoringOptions.h"
22 #include "clang/Tooling/Refactoring/Rename/SymbolOccurrences.h"
23 #include "llvm/Support/Error.h"
27 class CompilerInstance;
31 class RenamingAction {
33 RenamingAction(const std::vector<std::string> &NewNames,
34 const std::vector<std::string> &PrevNames,
35 const std::vector<std::vector<std::string>> &USRList,
36 std::map<std::string, tooling::Replacements> &FileToReplaces,
37 bool PrintLocations = false)
38 : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList),
39 FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
41 std::unique_ptr<ASTConsumer> newASTConsumer();
44 const std::vector<std::string> &NewNames, &PrevNames;
45 const std::vector<std::vector<std::string>> &USRList;
46 std::map<std::string, tooling::Replacements> &FileToReplaces;
50 class RenameOccurrences final : public SourceChangeRefactoringRule {
52 static Expected<RenameOccurrences> initiate(RefactoringRuleContext &Context,
53 SourceRange SelectionRange,
56 static const RefactoringDescriptor &describe();
59 RenameOccurrences(const NamedDecl *ND, std::string NewName)
60 : ND(ND), NewName(std::move(NewName)) {}
62 Expected<AtomicChanges>
63 createSourceReplacements(RefactoringRuleContext &Context) override;
69 class QualifiedRenameRule final : public SourceChangeRefactoringRule {
71 static Expected<QualifiedRenameRule> initiate(RefactoringRuleContext &Context,
72 std::string OldQualifiedName,
73 std::string NewQualifiedName);
75 static const RefactoringDescriptor &describe();
78 QualifiedRenameRule(const NamedDecl *ND,
79 std::string NewQualifiedName)
80 : ND(ND), NewQualifiedName(std::move(NewQualifiedName)) {}
82 Expected<AtomicChanges>
83 createSourceReplacements(RefactoringRuleContext &Context) override;
85 // A NamedDecl which identifies the symbol being renamed.
87 // The new qualified name to change the symbol to.
88 std::string NewQualifiedName;
91 /// Returns source replacements that correspond to the rename of the given
92 /// symbol occurrences.
93 llvm::Expected<std::vector<AtomicChange>>
94 createRenameReplacements(const SymbolOccurrences &Occurrences,
95 const SourceManager &SM, const SymbolName &NewName);
97 /// Rename all symbols identified by the given USRs.
98 class QualifiedRenamingAction {
100 QualifiedRenamingAction(
101 const std::vector<std::string> &NewNames,
102 const std::vector<std::vector<std::string>> &USRList,
103 std::map<std::string, tooling::Replacements> &FileToReplaces)
104 : NewNames(NewNames), USRList(USRList), FileToReplaces(FileToReplaces) {}
106 std::unique_ptr<ASTConsumer> newASTConsumer();
109 /// New symbol names.
110 const std::vector<std::string> &NewNames;
112 /// A list of USRs. Each element represents USRs of a symbol being renamed.
113 const std::vector<std::vector<std::string>> &USRList;
115 /// A file path to replacements map.
116 std::map<std::string, tooling::Replacements> &FileToReplaces;
119 } // end namespace tooling
120 } // end namespace clang
122 #endif // LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H