Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:11

0001 //===--- RefactoringActionRuleRequirements.h - Clang refactoring library --===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGACTIONRULEREQUIREMENTS_H
0010 #define LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGACTIONRULEREQUIREMENTS_H
0011 
0012 #include "clang/Basic/LLVM.h"
0013 #include "clang/Tooling/Refactoring/ASTSelection.h"
0014 #include "clang/Tooling/Refactoring/RefactoringDiagnostic.h"
0015 #include "clang/Tooling/Refactoring/RefactoringOption.h"
0016 #include "clang/Tooling/Refactoring/RefactoringRuleContext.h"
0017 #include "llvm/Support/Error.h"
0018 #include <type_traits>
0019 
0020 namespace clang {
0021 namespace tooling {
0022 
0023 /// A refactoring action rule requirement determines when a refactoring action
0024 /// rule can be invoked. The rule can be invoked only when all of the
0025 /// requirements are satisfied.
0026 ///
0027 /// Subclasses must implement the
0028 /// 'Expected<T> evaluate(RefactoringRuleContext &) const' member function.
0029 /// \c T is used to determine the return type that is passed to the
0030 /// refactoring rule's constructor.
0031 /// For example, the \c SourceRangeSelectionRequirement subclass defines
0032 /// 'Expected<SourceRange> evaluate(RefactoringRuleContext &Context) const'
0033 /// function. When this function returns a non-error value, the resulting
0034 /// source range is passed to the specific refactoring action rule
0035 /// constructor (provided all other requirements are satisfied).
0036 class RefactoringActionRuleRequirement {
0037   // Expected<T> evaluate(RefactoringRuleContext &Context) const;
0038 };
0039 
0040 /// A base class for any requirement that expects some part of the source to be
0041 /// selected in an editor (or the refactoring tool with the -selection option).
0042 class SourceSelectionRequirement : public RefactoringActionRuleRequirement {};
0043 
0044 /// A selection requirement that is satisfied when any portion of the source
0045 /// text is selected.
0046 class SourceRangeSelectionRequirement : public SourceSelectionRequirement {
0047 public:
0048   Expected<SourceRange> evaluate(RefactoringRuleContext &Context) const {
0049     if (Context.getSelectionRange().isValid())
0050       return Context.getSelectionRange();
0051     return Context.createDiagnosticError(diag::err_refactor_no_selection);
0052   }
0053 };
0054 
0055 /// An AST selection requirement is satisfied when any portion of the AST
0056 /// overlaps with the selection range.
0057 ///
0058 /// The requirement will be evaluated only once during the initiation and
0059 /// search of matching refactoring action rules.
0060 class ASTSelectionRequirement : public SourceRangeSelectionRequirement {
0061 public:
0062   Expected<SelectedASTNode> evaluate(RefactoringRuleContext &Context) const;
0063 };
0064 
0065 /// A selection requirement that is satisfied when the selection range overlaps
0066 /// with a number of neighbouring statements in the AST. The statemenst must be
0067 /// contained in declaration like a function. The selection range must be a
0068 /// non-empty source selection (i.e. cursors won't be accepted).
0069 ///
0070 /// The requirement will be evaluated only once during the initiation and search
0071 /// of matching refactoring action rules.
0072 ///
0073 /// \see CodeRangeASTSelection
0074 class CodeRangeASTSelectionRequirement : public ASTSelectionRequirement {
0075 public:
0076   Expected<CodeRangeASTSelection>
0077   evaluate(RefactoringRuleContext &Context) const;
0078 };
0079 
0080 /// A base class for any requirement that requires some refactoring options.
0081 class RefactoringOptionsRequirement : public RefactoringActionRuleRequirement {
0082 public:
0083   virtual ~RefactoringOptionsRequirement() {}
0084 
0085   /// Returns the set of refactoring options that are used when evaluating this
0086   /// requirement.
0087   virtual ArrayRef<std::shared_ptr<RefactoringOption>>
0088   getRefactoringOptions() const = 0;
0089 };
0090 
0091 /// A requirement that evaluates to the value of the given \c OptionType when
0092 /// the \c OptionType is a required option. When the \c OptionType is an
0093 /// optional option, the requirement will evaluate to \c None if the option is
0094 /// not specified or to an appropriate value otherwise.
0095 template <typename OptionType>
0096 class OptionRequirement : public RefactoringOptionsRequirement {
0097 public:
0098   OptionRequirement() : Opt(createRefactoringOption<OptionType>()) {}
0099 
0100   ArrayRef<std::shared_ptr<RefactoringOption>>
0101   getRefactoringOptions() const final {
0102     return Opt;
0103   }
0104 
0105   Expected<typename OptionType::ValueType>
0106   evaluate(RefactoringRuleContext &) const {
0107     return static_cast<OptionType *>(Opt.get())->getValue();
0108   }
0109 
0110 private:
0111   /// The partially-owned option.
0112   ///
0113   /// The ownership of the option is shared among the different requirements
0114   /// because the same option can be used by multiple rules in one refactoring
0115   /// action.
0116   std::shared_ptr<RefactoringOption> Opt;
0117 };
0118 
0119 } // end namespace tooling
0120 } // end namespace clang
0121 
0122 #endif // LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGACTIONRULEREQUIREMENTS_H