clang  14.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_REFACTOR_REFACTORING_ACTION_RULE_REQUIREMENTS_H
10 #define LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_RULE_REQUIREMENTS_H
11 
12 #include "clang/Basic/LLVM.h"
17 #include "llvm/Support/Error.h"
18 #include <type_traits>
19 
20 namespace clang {
21 namespace 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.
47 public:
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.
61 public:
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
75 public:
77  evaluate(RefactoringRuleContext &Context) const;
78 };
79 
80 /// A base class for any requirement that requires some refactoring options.
82 public:
84 
85  /// Returns the set of refactoring options that are used when evaluating this
86  /// requirement.
88  getRefactoringOptions() const = 0;
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.
95 template <typename OptionType>
97 public:
98  OptionRequirement() : Opt(createRefactoringOption<OptionType>()) {}
99 
101  getRefactoringOptions() const final override {
102  return Opt;
103  }
104 
107  return static_cast<OptionType *>(Opt.get())->getValue();
108  }
109 
110 private:
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_REFACTOR_REFACTORING_ACTION_RULE_REQUIREMENTS_H
clang::tooling::OptionRequirement
A requirement that evaluates to the value of the given OptionType when the OptionType is a required o...
Definition: RefactoringActionRuleRequirements.h:96
ASTSelection.h
RefactoringOption.h
clang::tooling::RefactoringActionRuleRequirement
A refactoring action rule requirement determines when a refactoring action rule can be invoked.
Definition: RefactoringActionRuleRequirements.h:36
llvm::Expected
Definition: LLVM.h:41
getValue
static SVal getValue(SVal val, SValBuilder &svalBuilder)
Definition: ArrayBoundCheckerV2.cpp:277
clang::tooling::OptionRequirement::getRefactoringOptions
ArrayRef< std::shared_ptr< RefactoringOption > > getRefactoringOptions() const final override
Returns the set of refactoring options that are used when evaluating this requirement.
Definition: RefactoringActionRuleRequirements.h:101
clang::tooling::SourceSelectionRequirement
A base class for any requirement that expects some part of the source to be selected in an editor (or...
Definition: RefactoringActionRuleRequirements.h:42
clang::tooling::RefactoringOptionsRequirement
A base class for any requirement that requires some refactoring options.
Definition: RefactoringActionRuleRequirements.h:81
clang::tooling::createRefactoringOption
std::shared_ptr< OptionType > createRefactoringOption()
Constructs a refactoring option of the given type.
Definition: RefactoringOption.h:54
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::OptionRequirement::OptionRequirement
OptionRequirement()
Definition: RefactoringActionRuleRequirements.h:98
clang::tooling::ASTSelectionRequirement
An AST selection requirement is satisfied when any portion of the AST overlaps with the selection ran...
Definition: RefactoringActionRuleRequirements.h:60
RefactoringDiagnostic.h
llvm::ArrayRef
Definition: LLVM.h:34
clang::tooling::SourceRangeSelectionRequirement::evaluate
Expected< SourceRange > evaluate(RefactoringRuleContext &Context) const
Definition: RefactoringActionRuleRequirements.h:48
clang::tooling::SourceRangeSelectionRequirement
A selection requirement that is satisfied when any portion of the source text is selected.
Definition: RefactoringActionRuleRequirements.h:46
LLVM.h
clang::tooling::CodeRangeASTSelectionRequirement::evaluate
Expected< CodeRangeASTSelection > evaluate(RefactoringRuleContext &Context) const
Definition: ASTSelectionRequirements.cpp:31
clang::tooling::OptionRequirement::evaluate
Expected< typename OptionType::ValueType > evaluate(RefactoringRuleContext &) const
Definition: RefactoringActionRuleRequirements.h:106
RefactoringRuleContext.h
clang
Definition: CalledOnceCheck.h:17
clang::tooling::ASTSelectionRequirement::evaluate
Expected< SelectedASTNode > evaluate(RefactoringRuleContext &Context) const
Definition: ASTSelectionRequirements.cpp:16
clang::tooling::RefactoringOptionsRequirement::getRefactoringOptions
virtual ArrayRef< std::shared_ptr< RefactoringOption > > getRefactoringOptions() const =0
Returns the set of refactoring options that are used when evaluating this requirement.
clang::tooling::RefactoringOptionsRequirement::~RefactoringOptionsRequirement
virtual ~RefactoringOptionsRequirement()
Definition: RefactoringActionRuleRequirements.h:83
clang::tooling::CodeRangeASTSelectionRequirement
A selection requirement that is satisfied when the selection range overlaps with a number of neighbou...
Definition: RefactoringActionRuleRequirements.h:74