|
9 | 9 | #ifndef LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGACTIONRULEREQUIREMENTS_H |
10 | 10 | #define LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGACTIONRULEREQUIREMENTS_H |
11 | 11 |
|
| 12 | +#include "clang/AST/ASTTypeTraits.h" |
| 13 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 14 | +#include "clang/ASTMatchers/ASTMatchers.h" |
| 15 | +#include "clang/ASTMatchers/ASTMatchersMacros.h" |
12 | 16 | #include "clang/Basic/LLVM.h" |
13 | 17 | #include "clang/Basic/SourceLocation.h" |
14 | 18 | #include "clang/Tooling/Refactoring/ASTSelection.h" |
15 | 19 | #include "clang/Tooling/Refactoring/RefactoringDiagnostic.h" |
16 | 20 | #include "clang/Tooling/Refactoring/RefactoringOption.h" |
17 | 21 | #include "clang/Tooling/Refactoring/RefactoringRuleContext.h" |
| 22 | +#include "clang/Tooling/Transformer/RewriteRule.h" |
18 | 23 | #include "llvm/Support/Error.h" |
19 | | -#include <type_traits> |
20 | 24 |
|
21 | 25 | namespace clang { |
22 | 26 | namespace tooling { |
@@ -89,6 +93,90 @@ class SourceLocationRequirement : public RefactoringActionRuleRequirement { |
89 | 93 | } |
90 | 94 | }; |
91 | 95 |
|
| 96 | +AST_POLYMORPHIC_MATCHER_P(hasPointWithin, |
| 97 | + AST_POLYMORPHIC_SUPPORTED_TYPES(Stmt, Decl), |
| 98 | + FullSourceLoc, L) { |
| 99 | + if (!L.hasManager()) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + const SourceRange &SR = Node.getSourceRange(); |
| 103 | + return L.getManager().isPointWithin(L, SR.getBegin(), SR.getEnd()); |
| 104 | +} |
| 105 | + |
| 106 | +/// An AST location match is satisfied when there is match around given |
| 107 | +/// location. In case of several matches inner one is taken. |
| 108 | +/// |
| 109 | +/// The requirement will be evaluated only once during the initiation and |
| 110 | +/// search of matching refactoring action rules. |
| 111 | +template <typename MatcherType> |
| 112 | +class ASTLocMatchRequirement : public SourceLocationRequirement { |
| 113 | +public: |
| 114 | + static_assert( |
| 115 | + std::is_same<ast_matchers::StatementMatcher, MatcherType>::value || |
| 116 | + std::is_same<ast_matchers::DeclarationMatcher, MatcherType>::value, |
| 117 | + "Expected a Statement or Declaration matcher"); |
| 118 | + |
| 119 | + class LocMatchCallback : public ast_matchers::MatchFinder::MatchCallback { |
| 120 | + public: |
| 121 | + void run(const clang::ast_matchers::MatchFinder::MatchResult &R) override { |
| 122 | + Result = std::make_unique<ast_matchers::MatchFinder::MatchResult>(R); |
| 123 | + } |
| 124 | + std::unique_ptr<ast_matchers::MatchFinder::MatchResult> Result; |
| 125 | + }; |
| 126 | + |
| 127 | + Expected<ast_matchers::MatchFinder::MatchResult> |
| 128 | + evaluate(RefactoringRuleContext &Context) const { |
| 129 | + Expected<SourceLocation> Location = |
| 130 | + SourceLocationRequirement::evaluate(Context); |
| 131 | + if (!Location) |
| 132 | + return Location.takeError(); |
| 133 | + MatcherType M = createWrapperMatcher( |
| 134 | + FullSourceLoc(*Location, Context.getASTContext().getSourceManager()), |
| 135 | + Matcher); |
| 136 | + |
| 137 | + ast_matchers::MatchFinder MF; |
| 138 | + LocMatchCallback Callback; |
| 139 | + MF.addMatcher(M, &Callback); |
| 140 | + MF.matchAST(Context.getASTContext()); |
| 141 | + if (!Callback.Result) |
| 142 | + return Context.createDiagnosticError( |
| 143 | + diag::err_refactor_no_location_match); |
| 144 | + return *Callback.Result; |
| 145 | + } |
| 146 | + |
| 147 | + ASTLocMatchRequirement(MatcherType M) : Matcher(M) {} |
| 148 | + |
| 149 | +private: |
| 150 | + ast_matchers::StatementMatcher |
| 151 | + createWrapperMatcher(FullSourceLoc L, |
| 152 | + ast_matchers::StatementMatcher M) const { |
| 153 | + return ast_matchers::stmt(M, hasPointWithin(L)).bind(transformer::RootID); |
| 154 | + } |
| 155 | + |
| 156 | + ast_matchers::DeclarationMatcher |
| 157 | + createWrapperMatcher(FullSourceLoc L, |
| 158 | + ast_matchers::DeclarationMatcher M) const { |
| 159 | + return ast_matchers::decl(M, hasPointWithin(L)).bind(transformer::RootID); |
| 160 | + } |
| 161 | + |
| 162 | + MatcherType Matcher; |
| 163 | +}; |
| 164 | + |
| 165 | +/// Requirement that evaluates to the EditGenerator value given at its creation. |
| 166 | +class EditGeneratorRequirement : public RefactoringActionRuleRequirement { |
| 167 | +public: |
| 168 | + Expected<transformer::EditGenerator> |
| 169 | + evaluate(RefactoringRuleContext &Context) const { |
| 170 | + return EditGenerator; |
| 171 | + } |
| 172 | + |
| 173 | + EditGeneratorRequirement(transformer::EditGenerator EG) |
| 174 | + : EditGenerator(std::move(EG)) {} |
| 175 | + |
| 176 | +private: |
| 177 | + transformer::EditGenerator EditGenerator; |
| 178 | +}; |
| 179 | + |
92 | 180 | /// A base class for any requirement that requires some refactoring options. |
93 | 181 | class RefactoringOptionsRequirement : public RefactoringActionRuleRequirement { |
94 | 182 | public: |
|
0 commit comments