1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | #ifndef LLVM_CLANG_TOOLING_REFACTORINGCALLBACKS_H |
29 | #define LLVM_CLANG_TOOLING_REFACTORINGCALLBACKS_H |
30 | |
31 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
32 | #include "clang/Tooling/Refactoring.h" |
33 | |
34 | namespace clang { |
35 | namespace tooling { |
36 | |
37 | |
38 | |
39 | |
40 | class RefactoringCallback : public ast_matchers::MatchFinder::MatchCallback { |
41 | public: |
42 | RefactoringCallback(); |
43 | Replacements &getReplacements(); |
44 | |
45 | protected: |
46 | Replacements Replace; |
47 | }; |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | class ASTMatchRefactorer { |
54 | public: |
55 | explicit ASTMatchRefactorer( |
56 | std::map<std::string, Replacements> &FileToReplaces); |
57 | |
58 | template <typename T> |
59 | void addMatcher(const T &Matcher, RefactoringCallback *Callback) { |
60 | MatchFinder.addMatcher(Matcher, Callback); |
61 | Callbacks.push_back(Callback); |
62 | } |
63 | |
64 | void addDynamicMatcher(const ast_matchers::internal::DynTypedMatcher &Matcher, |
65 | RefactoringCallback *Callback); |
66 | |
67 | std::unique_ptr<ASTConsumer> newASTConsumer(); |
68 | |
69 | private: |
70 | friend class RefactoringASTConsumer; |
71 | std::vector<RefactoringCallback *> Callbacks; |
72 | ast_matchers::MatchFinder MatchFinder; |
73 | std::map<std::string, Replacements> &FileToReplaces; |
74 | }; |
75 | |
76 | |
77 | |
78 | class ReplaceStmtWithText : public RefactoringCallback { |
79 | public: |
80 | ReplaceStmtWithText(StringRef FromId, StringRef ToText); |
81 | void run(const ast_matchers::MatchFinder::MatchResult &Result) override; |
82 | |
83 | private: |
84 | std::string FromId; |
85 | std::string ToText; |
86 | }; |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | |
94 | class ReplaceNodeWithTemplate : public RefactoringCallback { |
95 | public: |
96 | static llvm::Expected<std::unique_ptr<ReplaceNodeWithTemplate>> |
97 | create(StringRef FromId, StringRef ToTemplate); |
98 | void run(const ast_matchers::MatchFinder::MatchResult &Result) override; |
99 | |
100 | private: |
101 | struct TemplateElement { |
102 | enum { Literal, Identifier } Type; |
103 | std::string Value; |
104 | }; |
105 | ReplaceNodeWithTemplate(llvm::StringRef FromId, |
106 | std::vector<TemplateElement> Template); |
107 | std::string FromId; |
108 | std::vector<TemplateElement> Template; |
109 | }; |
110 | |
111 | |
112 | |
113 | class ReplaceStmtWithStmt : public RefactoringCallback { |
114 | public: |
115 | ReplaceStmtWithStmt(StringRef FromId, StringRef ToId); |
116 | void run(const ast_matchers::MatchFinder::MatchResult &Result) override; |
117 | |
118 | private: |
119 | std::string FromId; |
120 | std::string ToId; |
121 | }; |
122 | |
123 | |
124 | |
125 | |
126 | class ReplaceIfStmtWithItsBody : public RefactoringCallback { |
127 | public: |
128 | ReplaceIfStmtWithItsBody(StringRef Id, bool PickTrueBranch); |
129 | void run(const ast_matchers::MatchFinder::MatchResult &Result) override; |
130 | |
131 | private: |
132 | std::string Id; |
133 | const bool PickTrueBranch; |
134 | }; |
135 | |
136 | } |
137 | } |
138 | |
139 | #endif |
140 | |