clang  6.0.0svn
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"
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).
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).
44 
45 /// A selection requirement that is satisfied when any portion of the source
46 /// text is selected.
48 public:
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.
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
76 public:
78  evaluate(RefactoringRuleContext &Context) const;
79 };
80 
81 /// A base class for any requirement that requires some refactoring options.
83 public:
85 
86  /// Returns the set of refactoring options that are used when evaluating this
87  /// requirement.
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>
98 public:
99  OptionRequirement() : Opt(createRefactoringOption<OptionType>()) {}
100 
102  getRefactoringOptions() const final override {
103  return Opt;
104  }
105 
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
SourceRange getSelectionRange() const
Returns the current source selection range as set by the refactoring engine.
llvm::Error createDiagnosticError(SourceLocation Loc, unsigned DiagID)
Creates an llvm::Error value that contains a diagnostic.
A refactoring action rule requirement determines when a refactoring action rule can be invoked...
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...
The refactoring rule context stores all of the inputs that might be needed by a refactoring action ru...
Expected< typename OptionType::ValueType > evaluate(RefactoringRuleContext &) const
static SVal getValue(SVal val, SValBuilder &svalBuilder)
A base class for any requirement that requires some refactoring options.
A requirement that evaluates to the value of the given OptionType when the OptionType is a required o...
Dataflow Directional Tag Classes.
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.
bool isValid() const
A selection requirement that is satisfied when the selection range overlaps with a number of neighbou...
A selection requirement that is satisfied when any portion of the source text is selected.
ArrayRef< std::shared_ptr< RefactoringOption > > getRefactoringOptions() const final override
Returns the set of refactoring options that are used when evaluating this requirement.
Expected< SourceRange > evaluate(RefactoringRuleContext &Context) const