1 //===--- RenamingAction.h - Clang refactoring library ---------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// Provides an action to rename every symbol at a point.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H
15 #define LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H
17 #include "clang/Tooling/Refactoring.h"
18 #include "clang/Tooling/Refactoring/AtomicChange.h"
19 #include "clang/Tooling/Refactoring/RefactoringActionRules.h"
20 #include "clang/Tooling/Refactoring/RefactoringOptions.h"
21 #include "clang/Tooling/Refactoring/Rename/SymbolOccurrences.h"
22 #include "llvm/Support/Error.h"
26 class CompilerInstance;
30 class RenamingAction {
32 RenamingAction(const std::vector<std::string> &NewNames,
33 const std::vector<std::string> &PrevNames,
34 const std::vector<std::vector<std::string>> &USRList,
35 std::map<std::string, tooling::Replacements> &FileToReplaces,
36 bool PrintLocations = false)
37 : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList),
38 FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
40 std::unique_ptr<ASTConsumer> newASTConsumer();
43 const std::vector<std::string> &NewNames, &PrevNames;
44 const std::vector<std::vector<std::string>> &USRList;
45 std::map<std::string, tooling::Replacements> &FileToReplaces;
49 class RenameOccurrences final : public SourceChangeRefactoringRule {
51 static Expected<RenameOccurrences> initiate(RefactoringRuleContext &Context,
52 SourceRange SelectionRange,
55 static const RefactoringDescriptor &describe();
57 const NamedDecl *getRenameDecl() const;
60 RenameOccurrences(const NamedDecl *ND, std::string NewName)
61 : ND(ND), NewName(std::move(NewName)) {}
63 Expected<AtomicChanges>
64 createSourceReplacements(RefactoringRuleContext &Context) override;
70 class QualifiedRenameRule final : public SourceChangeRefactoringRule {
72 static Expected<QualifiedRenameRule> initiate(RefactoringRuleContext &Context,
73 std::string OldQualifiedName,
74 std::string NewQualifiedName);
76 static const RefactoringDescriptor &describe();
79 QualifiedRenameRule(const NamedDecl *ND,
80 std::string NewQualifiedName)
81 : ND(ND), NewQualifiedName(std::move(NewQualifiedName)) {}
83 Expected<AtomicChanges>
84 createSourceReplacements(RefactoringRuleContext &Context) override;
86 // A NamedDecl which identifies the symbol being renamed.
88 // The new qualified name to change the symbol to.
89 std::string NewQualifiedName;
92 /// Returns source replacements that correspond to the rename of the given
93 /// symbol occurrences.
94 llvm::Expected<std::vector<AtomicChange>>
95 createRenameReplacements(const SymbolOccurrences &Occurrences,
96 const SourceManager &SM, const SymbolName &NewName);
98 /// Rename all symbols identified by the given USRs.
99 class QualifiedRenamingAction {
101 QualifiedRenamingAction(
102 const std::vector<std::string> &NewNames,
103 const std::vector<std::vector<std::string>> &USRList,
104 std::map<std::string, tooling::Replacements> &FileToReplaces)
105 : NewNames(NewNames), USRList(USRList), FileToReplaces(FileToReplaces) {}
107 std::unique_ptr<ASTConsumer> newASTConsumer();
110 /// New symbol names.
111 const std::vector<std::string> &NewNames;
113 /// A list of USRs. Each element represents USRs of a symbol being renamed.
114 const std::vector<std::vector<std::string>> &USRList;
116 /// A file path to replacements map.
117 std::map<std::string, tooling::Replacements> &FileToReplaces;
120 } // end namespace tooling
121 } // end namespace clang
123 #endif // LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H