1 //===--- RefactoringRuleContext.h - Clang refactoring library -------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_RULE_CONTEXT_H
10 #define LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_RULE_CONTEXT_H
12 #include "clang/Basic/DiagnosticError.h"
13 #include "clang/Basic/SourceManager.h"
14 #include "clang/Tooling/Refactoring/ASTSelection.h"
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.
27 /// The following inputs are stored by the operation:
29 /// - SourceManager: a reference to a valid source manager.
31 /// - SelectionRange: an optional source selection ranges that can be used
32 /// to represent a selection in an editor.
33 class RefactoringRuleContext {
35 RefactoringRuleContext(const SourceManager &SM) : SM(SM) {}
37 const SourceManager &getSources() const { return SM; }
39 /// Returns the current source selection range as set by the
40 /// refactoring engine. Can be invalid.
41 SourceRange getSelectionRange() const { return SelectionRange; }
43 void setSelectionRange(SourceRange R) { SelectionRange = R; }
45 bool hasASTContext() const { return AST; }
47 ASTContext &getASTContext() const {
48 assert(AST && "no AST!");
52 void setASTContext(ASTContext &Context) { AST = &Context; }
54 /// Creates an llvm::Error value that contains a diagnostic.
56 /// The errors should not outlive the context.
57 llvm::Error createDiagnosticError(SourceLocation Loc, unsigned DiagID) {
58 return DiagnosticError::create(Loc, PartialDiagnostic(DiagID, DiagStorage));
61 llvm::Error createDiagnosticError(unsigned DiagID) {
62 return createDiagnosticError(SourceLocation(), DiagID);
65 void setASTSelection(std::unique_ptr<SelectedASTNode> Node) {
66 ASTNodeSelection = std::move(Node);
70 /// The source manager for the translation unit / file on which a refactoring
71 /// action might operate on.
72 const SourceManager &SM;
73 /// An optional source selection range that's commonly used to represent
74 /// a selection in an editor.
75 SourceRange SelectionRange;
76 /// An optional AST for the translation unit on which a refactoring action
78 ASTContext *AST = nullptr;
79 /// The allocator for diagnostics.
80 PartialDiagnostic::StorageAllocator DiagStorage;
82 // FIXME: Remove when memoized.
83 std::unique_ptr<SelectedASTNode> ASTNodeSelection;
86 } // end namespace tooling
87 } // end namespace clang
89 #endif // LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_RULE_CONTEXT_H