clang 8.0.0

RenamingAction.h
Go to the documentation of this file.
1 //===--- RenamingAction.h - Clang refactoring library ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// Provides an action to rename every symbol at a point.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H
16 #define LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H
17
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"
24
25 namespace clang {
26 class ASTConsumer;
27 class CompilerInstance;
28
29 namespace tooling {
30
31 class RenamingAction {
32 public:
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) {}
40
41 std::unique_ptr<ASTConsumer> newASTConsumer();
42
43 private:
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;
47 bool PrintLocations;
48 };
49
50 class RenameOccurrences final : public SourceChangeRefactoringRule {
51 public:
52 static Expected<RenameOccurrences> initiate(RefactoringRuleContext &Context,
53 SourceRange SelectionRange,
54 std::string NewName);
55
56 static const RefactoringDescriptor &describe();
57
58 private:
59 RenameOccurrences(const NamedDecl *ND, std::string NewName)
60 : ND(ND), NewName(std::move(NewName)) {}
61
62 Expected<AtomicChanges>
63 createSourceReplacements(RefactoringRuleContext &Context) override;
64
65 const NamedDecl *ND;
66 std::string NewName;
67 };
68
69 class QualifiedRenameRule final : public SourceChangeRefactoringRule {
70 public:
71 static Expected<QualifiedRenameRule> initiate(RefactoringRuleContext &Context,
72 std::string OldQualifiedName,
73 std::string NewQualifiedName);
74
75 static const RefactoringDescriptor &describe();
76
77 private:
78 QualifiedRenameRule(const NamedDecl *ND,
79 std::string NewQualifiedName)
80 : ND(ND), NewQualifiedName(std::move(NewQualifiedName)) {}
81
82 Expected<AtomicChanges>
83 createSourceReplacements(RefactoringRuleContext &Context) override;
84
85 // A NamedDecl which identifies the symbol being renamed.
86 const NamedDecl *ND;
87 // The new qualified name to change the symbol to.
88 std::string NewQualifiedName;
89 };
90
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);
96
97 /// Rename all symbols identified by the given USRs.
98 class QualifiedRenamingAction {
99 public:
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) {}
105
106 std::unique_ptr<ASTConsumer> newASTConsumer();
107
108 private:
109 /// New symbol names.
110 const std::vector<std::string> &NewNames;
111
112 /// A list of USRs. Each element represents USRs of a symbol being renamed.
113 const std::vector<std::vector<std::string>> &USRList;
114
115 /// A file path to replacements map.
116 std::map<std::string, tooling::Replacements> &FileToReplaces;
117 };
118
119 } // end namespace tooling
120 } // end namespace clang
121
122 #endif // LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H
clang::tooling::RenameOccurrences
Definition: RenamingAction.h:50
AtomicChange.h
clang::tooling::SymbolName
A name of a symbol.
Definition: SymbolName.h:30
clang::tooling::RenamingAction::RenamingAction
RenamingAction(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=false)
Definition: RenamingAction.h:33
clang::tooling::RenamingAction::newASTConsumer
std::unique_ptr< ASTConsumer > newASTConsumer()
Definition: RenamingAction.cpp:267
clang::tooling::QualifiedRenameRule
Definition: RenamingAction.h:69
clang::tooling::QualifiedRenamingAction::QualifiedRenamingAction
QualifiedRenamingAction(const std::vector< std::string > &NewNames, const std::vector< std::vector< std::string >> &USRList, std::map< std::string, tooling::Replacements > &FileToReplaces)
Definition: RenamingAction.h:100
clang::tooling::createRenameReplacements
llvm::Expected< std::vector< AtomicChange > > createRenameReplacements(const SymbolOccurrences &Occurrences, const SourceManager &SM, const SymbolName &NewName)
Returns source replacements that correspond to the rename of the given symbol occurrences.
Definition: RenamingAction.cpp:144
RefactoringOptions.h
RefactoringActionRules.h
llvm::Expected
Definition: LLVM.h:39
Refactoring.h
clang::tooling::RefactoringRuleContext
The refactoring rule context stores all of the inputs that might be needed by a refactoring action ru...
Definition: RefactoringRuleContext.h:34
SymbolOccurrences.h
clang::tooling::QualifiedRenamingAction
Rename all symbols identified by the given USRs.
Definition: RenamingAction.h:98
SM
const SourceManager & SM
Definition: Format.cpp:1490
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::tooling::RenamingAction
Definition: RenamingAction.h:31
clang::tooling::SymbolOccurrences
std::vector< SymbolOccurrence > SymbolOccurrences
Definition: SymbolOccurrences.h:86
clang::tooling::SourceChangeRefactoringRule
A type of refactoring action rule that produces source replacements in the form of atomic changes...
Definition: RefactoringActionRules.h:53
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:198
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:249
clang::tooling::RefactoringDescriptor
Definition: RefactoringActionRule.h:25
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:582