clang 20.0.0git
RefactoringActionRuleRequirements.h
Go to the documentation of this file.
1//===--- RefactoringActionRuleRequirements.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_REFACTORINGACTIONRULEREQUIREMENTS_H
10#define LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGACTIONRULEREQUIREMENTS_H
11
12#include "clang/Basic/LLVM.h"
17#include "llvm/Support/Error.h"
18#include <type_traits>
19
20namespace clang {
21namespace tooling {
22
23/// A refactoring action rule requirement determines when a refactoring action
24/// rule can be invoked. The rule can be invoked only when all of the
25/// requirements are satisfied.
26///
27/// Subclasses must implement the
28/// 'Expected<T> evaluate(RefactoringRuleContext &) const' member function.
29/// \c T is used to determine the return type that is passed to the
30/// refactoring rule's constructor.
31/// For example, the \c SourceRangeSelectionRequirement subclass defines
32/// 'Expected<SourceRange> evaluate(RefactoringRuleContext &Context) const'
33/// function. When this function returns a non-error value, the resulting
34/// source range is passed to the specific refactoring action rule
35/// constructor (provided all other requirements are satisfied).
37 // Expected<T> evaluate(RefactoringRuleContext &Context) const;
38};
39
40/// A base class for any requirement that expects some part of the source to be
41/// selected in an editor (or the refactoring tool with the -selection option).
43
44/// A selection requirement that is satisfied when any portion of the source
45/// text is selected.
47public:
49 if (Context.getSelectionRange().isValid())
50 return Context.getSelectionRange();
51 return Context.createDiagnosticError(diag::err_refactor_no_selection);
52 }
53};
54
55/// An AST selection requirement is satisfied when any portion of the AST
56/// overlaps with the selection range.
57///
58/// The requirement will be evaluated only once during the initiation and
59/// search of matching refactoring action rules.
61public:
63};
64
65/// A selection requirement that is satisfied when the selection range overlaps
66/// with a number of neighbouring statements in the AST. The statemenst must be
67/// contained in declaration like a function. The selection range must be a
68/// non-empty source selection (i.e. cursors won't be accepted).
69///
70/// The requirement will be evaluated only once during the initiation and search
71/// of matching refactoring action rules.
72///
73/// \see CodeRangeASTSelection
75public:
77 evaluate(RefactoringRuleContext &Context) const;
78};
79
80/// A base class for any requirement that requires some refactoring options.
82public:
84
85 /// Returns the set of refactoring options that are used when evaluating this
86 /// requirement.
89};
90
91/// A requirement that evaluates to the value of the given \c OptionType when
92/// the \c OptionType is a required option. When the \c OptionType is an
93/// optional option, the requirement will evaluate to \c None if the option is
94/// not specified or to an appropriate value otherwise.
95template <typename OptionType>
97public:
99
101 getRefactoringOptions() const final {
102 return Opt;
103 }
104
107 return static_cast<OptionType *>(Opt.get())->getValue();
108 }
109
110private:
111 /// The partially-owned option.
112 ///
113 /// The ownership of the option is shared among the different requirements
114 /// because the same option can be used by multiple rules in one refactoring
115 /// action.
116 std::shared_ptr<RefactoringOption> Opt;
117};
118
119} // end namespace tooling
120} // end namespace clang
121
122#endif // LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGACTIONRULEREQUIREMENTS_H
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
An AST selection requirement is satisfied when any portion of the AST overlaps with the selection ran...
Expected< SelectedASTNode > evaluate(RefactoringRuleContext &Context) const
A selection requirement that is satisfied when the selection range overlaps with a number of neighbou...
Expected< CodeRangeASTSelection > evaluate(RefactoringRuleContext &Context) const
A requirement that evaluates to the value of the given OptionType when the OptionType is a required o...
ArrayRef< std::shared_ptr< RefactoringOption > > getRefactoringOptions() const final
Returns the set of refactoring options that are used when evaluating this requirement.
Expected< typename OptionType::ValueType > evaluate(RefactoringRuleContext &) const
A refactoring action rule requirement determines when a refactoring action rule can be invoked.
A base class for any requirement that requires some refactoring options.
virtual ArrayRef< std::shared_ptr< RefactoringOption > > getRefactoringOptions() const =0
Returns the set of refactoring options that are used when evaluating this requirement.
The refactoring rule context stores all of the inputs that might be needed by a refactoring action ru...
A selection requirement that is satisfied when any portion of the source text is selected.
Expected< SourceRange > evaluate(RefactoringRuleContext &Context) const
A base class for any requirement that expects some part of the source to be selected in an editor (or...
std::shared_ptr< OptionType > createRefactoringOption()
Constructs a refactoring option of the given type.
The JSON file list parser is used to communicate input to InstallAPI.