Home | History | Annotate | Download | only in Refactoring
      1 //===--- RefactoringRuleContext.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_RULE_CONTEXT_H
     11 #define LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_RULE_CONTEXT_H
     12 
     13 #include "clang/Basic/DiagnosticError.h"
     14 #include "clang/Basic/SourceManager.h"
     15 
     16 namespace clang {
     17 
     18 class ASTContext;
     19 
     20 namespace tooling {
     21 
     22 /// The refactoring rule context stores all of the inputs that might be needed
     23 /// by a refactoring action rule. It can create the specialized
     24 /// \c ASTRefactoringOperation or \c PreprocessorRefactoringOperation values
     25 /// that can be used by the refactoring action rules.
     26 ///
     27 /// The following inputs are stored by the operation:
     28 ///
     29 ///   - SourceManager: a reference to a valid source manager.
     30 ///
     31 ///   - SelectionRange: an optional source selection ranges that can be used
     32 ///     to represent a selection in an editor.
     33 class RefactoringRuleContext {
     34 public:
     35   RefactoringRuleContext(const SourceManager &SM) : SM(SM) {}
     36 
     37   const SourceManager &getSources() const { return SM; }
     38 
     39   /// Returns the current source selection range as set by the
     40   /// refactoring engine. Can be invalid.
     41   SourceRange getSelectionRange() const { return SelectionRange; }
     42 
     43   void setSelectionRange(SourceRange R) { SelectionRange = R; }
     44 
     45   bool hasASTContext() const { return AST; }
     46 
     47   ASTContext &getASTContext() const {
     48     assert(AST && "no AST!");
     49     return *AST;
     50   }
     51 
     52   void setASTContext(ASTContext &Context) { AST = &Context; }
     53 
     54   /// Creates an llvm::Error value that contains a diagnostic.
     55   ///
     56   /// The errors should not outlive the context.
     57   llvm::Error createDiagnosticError(SourceLocation Loc, unsigned DiagID) {
     58     return DiagnosticError::create(Loc, PartialDiagnostic(DiagID, DiagStorage));
     59   }
     60 
     61   llvm::Error createDiagnosticError(unsigned DiagID) {
     62     return createDiagnosticError(SourceLocation(), DiagID);
     63   }
     64 
     65 private:
     66   /// The source manager for the translation unit / file on which a refactoring
     67   /// action might operate on.
     68   const SourceManager &SM;
     69   /// An optional source selection range that's commonly used to represent
     70   /// a selection in an editor.
     71   SourceRange SelectionRange;
     72   /// An optional AST for the translation unit on which a refactoring action
     73   /// might operate on.
     74   ASTContext *AST = nullptr;
     75   /// The allocator for diagnostics.
     76   PartialDiagnostic::StorageAllocator DiagStorage;
     77 };
     78 
     79 } // end namespace tooling
     80 } // end namespace clang
     81 
     82 #endif // LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_RULE_CONTEXT_H
     83