1 //===--- RefactoringActionRules.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 #ifndef LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_RULES_H 10 #define LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_RULES_H 11 12 #include "clang/Tooling/Refactoring/RefactoringActionRule.h" 13 #include "clang/Tooling/Refactoring/RefactoringActionRulesInternal.h" 14 15 namespace clang { 16 namespace tooling { 17 18 /// Creates a new refactoring action rule that constructs and invokes the 19 /// \c RuleType rule when all of the requirements are satisfied. 20 /// 21 /// This function takes in a list of values whose type derives from 22 /// \c RefactoringActionRuleRequirement. These values describe the initiation 23 /// requirements that have to be satisfied by the refactoring engine before 24 /// the provided action rule can be constructed and invoked. The engine 25 /// verifies that the requirements are satisfied by evaluating them (using the 26 /// 'evaluate' member function) and checking that the results don't contain 27 /// any errors. Once all requirements are satisfied, the provided refactoring 28 /// rule is constructed by passing in the values returned by the requirements' 29 /// evaluate functions as arguments to the constructor. The rule is then invoked 30 /// immediately after construction. 31 /// 32 /// The separation of requirements, their evaluation and the invocation of the 33 /// refactoring action rule allows the refactoring clients to: 34 /// - Disable refactoring action rules whose requirements are not supported. 35 /// - Gather the set of options and define a command-line / visual interface 36 /// that allows users to input these options without ever invoking the 37 /// action. 38 template <typename RuleType, typename... RequirementTypes> 39 std::unique_ptr<RefactoringActionRule> 40 createRefactoringActionRule(const RequirementTypes &... Requirements); 41 42 /// A set of refactoring action rules that should have unique initiation 43 /// requirements. 44 using RefactoringActionRules = 45 std::vector<std::unique_ptr<RefactoringActionRule>>; 46 47 /// A type of refactoring action rule that produces source replacements in the 48 /// form of atomic changes. 49 /// 50 /// This action rule is typically used for local refactorings that replace 51 /// source in a single AST unit. 52 class SourceChangeRefactoringRule : public RefactoringActionRuleBase { 53 public: invoke(RefactoringResultConsumer & Consumer,RefactoringRuleContext & Context)54 void invoke(RefactoringResultConsumer &Consumer, 55 RefactoringRuleContext &Context) final override { 56 Expected<AtomicChanges> Changes = createSourceReplacements(Context); 57 if (!Changes) 58 Consumer.handleError(Changes.takeError()); 59 else 60 Consumer.handle(std::move(*Changes)); 61 } 62 63 private: 64 virtual Expected<AtomicChanges> 65 createSourceReplacements(RefactoringRuleContext &Context) = 0; 66 }; 67 68 /// A type of refactoring action rule that finds a set of symbol occurrences 69 /// that reference a particular symbol. 70 /// 71 /// This action rule is typically used for an interactive rename that allows 72 /// users to specify the new name and the set of selected occurrences during 73 /// the refactoring. 74 class FindSymbolOccurrencesRefactoringRule : public RefactoringActionRuleBase { 75 public: invoke(RefactoringResultConsumer & Consumer,RefactoringRuleContext & Context)76 void invoke(RefactoringResultConsumer &Consumer, 77 RefactoringRuleContext &Context) final override { 78 Expected<SymbolOccurrences> Occurrences = findSymbolOccurrences(Context); 79 if (!Occurrences) 80 Consumer.handleError(Occurrences.takeError()); 81 else 82 Consumer.handle(std::move(*Occurrences)); 83 } 84 85 private: 86 virtual Expected<SymbolOccurrences> 87 findSymbolOccurrences(RefactoringRuleContext &Context) = 0; 88 }; 89 90 } // end namespace tooling 91 } // end namespace clang 92 93 #endif // LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_RULES_H 94