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