clang 19.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_REFACTORING_REFACTORINGACTIONRULES_H
10#define LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGACTIONRULES_H
11
14
15namespace clang {
16namespace 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.
38template <typename RuleType, typename... RequirementTypes>
39std::unique_ptr<RefactoringActionRule>
40createRefactoringActionRule(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.
53public:
55 RefactoringRuleContext &Context) final {
56 Expected<AtomicChanges> Changes = createSourceReplacements(Context);
57 if (!Changes)
58 Consumer.handleError(Changes.takeError());
59 else
60 Consumer.handle(std::move(*Changes));
61 }
62
63private:
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.
75public:
77 RefactoringRuleContext &Context) final {
78 Expected<SymbolOccurrences> Occurrences = findSymbolOccurrences(Context);
79 if (!Occurrences)
80 Consumer.handleError(Occurrences.takeError());
81 else
82 Consumer.handle(std::move(*Occurrences));
83 }
84
85private:
87 findSymbolOccurrences(RefactoringRuleContext &Context) = 0;
88};
89
90} // end namespace tooling
91} // end namespace clang
92
93#endif // LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGACTIONRULES_H
A type of refactoring action rule that finds a set of symbol occurrences that reference a particular ...
void invoke(RefactoringResultConsumer &Consumer, RefactoringRuleContext &Context) final
Initiates and performs a specific refactoring action.
A common refactoring action rule interface that defines the 'invoke' function that performs the refac...
An abstract interface that consumes the various refactoring results that can be produced by refactori...
The refactoring rule context stores all of the inputs that might be needed by a refactoring action ru...
A type of refactoring action rule that produces source replacements in the form of atomic changes.
void invoke(RefactoringResultConsumer &Consumer, RefactoringRuleContext &Context) final
Initiates and performs a specific refactoring action.
std::vector< std::unique_ptr< RefactoringActionRule > > RefactoringActionRules
A set of refactoring action rules that should have unique initiation requirements.
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...
The JSON file list parser is used to communicate input to InstallAPI.