clang 8.0.0

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