Clang Project

clang_source_code/include/clang/Tooling/Refactoring/Rename/RenamingAction.h
1//===--- RenamingAction.h - Clang refactoring library ---------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Provides an action to rename every symbol at a point.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H
15#define LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H
16
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"
23
24namespace clang {
25class ASTConsumer;
26class CompilerInstance;
27
28namespace tooling {
29
30class RenamingAction {
31public:
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::stringtooling::Replacements> &FileToReplaces,
36                 bool PrintLocations = false)
37      : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList),
38        FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
39
40  std::unique_ptr<ASTConsumernewASTConsumer();
41
42private:
43  const std::vector<std::string> &NewNames, &PrevNames;
44  const std::vector<std::vector<std::string>> &USRList;
45  std::map<std::stringtooling::Replacements> &FileToReplaces;
46  bool PrintLocations;
47};
48
49class RenameOccurrences final : public SourceChangeRefactoringRule {
50public:
51  static Expected<RenameOccurrencesinitiate(RefactoringRuleContext &Context,
52                                              SourceRange SelectionRange,
53                                              std::string NewName);
54
55  static const RefactoringDescriptor &describe();
56
57private:
58  RenameOccurrences(const NamedDecl *NDstd::string NewName)
59      : ND(ND), NewName(std::move(NewName)) {}
60
61  Expected<AtomicChanges>
62  createSourceReplacements(RefactoringRuleContext &Context) override;
63
64  const NamedDecl *ND;
65  std::string NewName;
66};
67
68class QualifiedRenameRule final : public SourceChangeRefactoringRule {
69public:
70  static Expected<QualifiedRenameRuleinitiate(RefactoringRuleContext &Context,
71                                                std::string OldQualifiedName,
72                                                std::string NewQualifiedName);
73
74  static const RefactoringDescriptor &describe();
75
76private:
77  QualifiedRenameRule(const NamedDecl *ND,
78                      std::string NewQualifiedName)
79      : ND(ND), NewQualifiedName(std::move(NewQualifiedName)) {}
80
81  Expected<AtomicChanges>
82  createSourceReplacements(RefactoringRuleContext &Context) override;
83
84  // A NamedDecl which identifies the symbol being renamed.
85  const NamedDecl *ND;
86  // The new qualified name to change the symbol to.
87  std::string NewQualifiedName;
88};
89
90/// Returns source replacements that correspond to the rename of the given
91/// symbol occurrences.
92llvm::Expected<std::vector<AtomicChange>>
93createRenameReplacements(const SymbolOccurrences &Occurrences,
94                         const SourceManager &SMconst SymbolName &NewName);
95
96/// Rename all symbols identified by the given USRs.
97class QualifiedRenamingAction {
98public:
99  QualifiedRenamingAction(
100      const std::vector<std::string> &NewNames,
101      const std::vector<std::vector<std::string>> &USRList,
102      std::map<std::stringtooling::Replacements> &FileToReplaces)
103      : NewNames(NewNames), USRList(USRList), FileToReplaces(FileToReplaces) {}
104
105  std::unique_ptr<ASTConsumernewASTConsumer();
106
107private:
108  /// New symbol names.
109  const std::vector<std::string> &NewNames;
110
111  /// A list of USRs. Each element represents USRs of a symbol being renamed.
112  const std::vector<std::vector<std::string>> &USRList;
113
114  /// A file path to replacements map.
115  std::map<std::stringtooling::Replacements> &FileToReplaces;
116};
117
118// end namespace tooling
119// end namespace clang
120
121#endif // LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H
122
clang::tooling::RenamingAction::newASTConsumer
clang::tooling::RenamingAction::NewNames
clang::tooling::RenamingAction::PrevNames
clang::tooling::RenamingAction::USRList
clang::tooling::RenamingAction::FileToReplaces
clang::tooling::RenamingAction::PrintLocations
clang::tooling::RenameOccurrences::initiate
clang::tooling::RenameOccurrences::describe
clang::tooling::RenameOccurrences::createSourceReplacements
clang::tooling::RenameOccurrences::ND
clang::tooling::RenameOccurrences::NewName
clang::tooling::QualifiedRenameRule::initiate
clang::tooling::QualifiedRenameRule::describe
clang::tooling::QualifiedRenameRule::createSourceReplacements
clang::tooling::QualifiedRenameRule::ND
clang::tooling::QualifiedRenameRule::NewQualifiedName
clang::tooling::QualifiedRenamingAction::newASTConsumer
clang::tooling::QualifiedRenamingAction::NewNames
clang::tooling::QualifiedRenamingAction::USRList
clang::tooling::QualifiedRenamingAction::FileToReplaces