clang  14.0.0git
RefactoringActionRules.h
Go to the documentation of this file.
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 
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.
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.
53 public:
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:
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.
75 public:
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:
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
clang::tooling::FindSymbolOccurrencesRefactoringRule
A type of refactoring action rule that finds a set of symbol occurrences that reference a particular ...
Definition: RefactoringActionRules.h:74
RefactoringActionRule.h
clang::tooling::SourceChangeRefactoringRule::invoke
void invoke(RefactoringResultConsumer &Consumer, RefactoringRuleContext &Context) final override
Initiates and performs a specific refactoring action.
Definition: RefactoringActionRules.h:54
clang::tooling::SourceChangeRefactoringRule
A type of refactoring action rule that produces source replacements in the form of atomic changes.
Definition: RefactoringActionRules.h:52
llvm::Expected
Definition: LLVM.h:41
clang::tooling::RefactoringActionRuleBase
A common refactoring action rule interface that defines the 'invoke' function that performs the refac...
Definition: RefactoringActionRule.h:35
clang::tooling::RefactoringActionRules
std::vector< std::unique_ptr< RefactoringActionRule > > RefactoringActionRules
A set of refactoring action rules that should have unique initiation requirements.
Definition: RefactoringActionRules.h:45
clang::tooling::FindSymbolOccurrencesRefactoringRule::invoke
void invoke(RefactoringResultConsumer &Consumer, RefactoringRuleContext &Context) final override
Initiates and performs a specific refactoring action.
Definition: RefactoringActionRules.h:76
clang::tooling::RefactoringRuleContext
The refactoring rule context stores all of the inputs that might be needed by a refactoring action ru...
Definition: RefactoringRuleContext.h:33
clang::tooling::createRefactoringActionRule
std::unique_ptr< RefactoringActionRule > createRefactoringActionRule(const RequirementTypes &... Requirements)
Creates a new refactoring action rule that constructs and invokes the RuleType rule when all of the r...
Definition: RefactoringActionRulesInternal.h:118
clang::tooling::RefactoringResultConsumer
An abstract interface that consumes the various refactoring results that can be produced by refactori...
Definition: RefactoringResultConsumer.h:24
clang
Definition: CalledOnceCheck.h:17
RefactoringActionRulesInternal.h