Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===--- RefactoringRuleContext.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_REFACTORINGRULECONTEXT_H
0010 #define LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGRULECONTEXT_H
0011 
0012 #include "clang/Basic/DiagnosticError.h"
0013 #include "clang/Basic/SourceManager.h"
0014 #include "clang/Tooling/Refactoring/ASTSelection.h"
0015 
0016 namespace clang {
0017 
0018 class ASTContext;
0019 
0020 namespace tooling {
0021 
0022 /// The refactoring rule context stores all of the inputs that might be needed
0023 /// by a refactoring action rule. It can create the specialized
0024 /// \c ASTRefactoringOperation or \c PreprocessorRefactoringOperation values
0025 /// that can be used by the refactoring action rules.
0026 ///
0027 /// The following inputs are stored by the operation:
0028 ///
0029 ///   - SourceManager: a reference to a valid source manager.
0030 ///
0031 ///   - SelectionRange: an optional source selection ranges that can be used
0032 ///     to represent a selection in an editor.
0033 class RefactoringRuleContext {
0034 public:
0035   RefactoringRuleContext(const SourceManager &SM) : SM(SM) {}
0036 
0037   const SourceManager &getSources() const { return SM; }
0038 
0039   /// Returns the current source selection range as set by the
0040   /// refactoring engine. Can be invalid.
0041   SourceRange getSelectionRange() const { return SelectionRange; }
0042 
0043   void setSelectionRange(SourceRange R) { SelectionRange = R; }
0044 
0045   bool hasASTContext() const { return AST; }
0046 
0047   ASTContext &getASTContext() const {
0048     assert(AST && "no AST!");
0049     return *AST;
0050   }
0051 
0052   void setASTContext(ASTContext &Context) { AST = &Context; }
0053 
0054   /// Creates an llvm::Error value that contains a diagnostic.
0055   ///
0056   /// The errors should not outlive the context.
0057   llvm::Error createDiagnosticError(SourceLocation Loc, unsigned DiagID) {
0058     return DiagnosticError::create(Loc, PartialDiagnostic(DiagID, DiagStorage));
0059   }
0060 
0061   llvm::Error createDiagnosticError(unsigned DiagID) {
0062     return createDiagnosticError(SourceLocation(), DiagID);
0063   }
0064 
0065   void setASTSelection(std::unique_ptr<SelectedASTNode> Node) {
0066     ASTNodeSelection = std::move(Node);
0067   }
0068 
0069 private:
0070   /// The source manager for the translation unit / file on which a refactoring
0071   /// action might operate on.
0072   const SourceManager &SM;
0073   /// An optional source selection range that's commonly used to represent
0074   /// a selection in an editor.
0075   SourceRange SelectionRange;
0076   /// An optional AST for the translation unit on which a refactoring action
0077   /// might operate on.
0078   ASTContext *AST = nullptr;
0079   /// The allocator for diagnostics.
0080   PartialDiagnostic::DiagStorageAllocator DiagStorage;
0081 
0082   // FIXME: Remove when memoized.
0083   std::unique_ptr<SelectedASTNode> ASTNodeSelection;
0084 };
0085 
0086 } // end namespace tooling
0087 } // end namespace clang
0088 
0089 #endif // LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGRULECONTEXT_H