clang  13.0.0git
RefactoringRuleContext.h
Go to the documentation of this file.
1 //===--- RefactoringRuleContext.h - Clang refactoring library -------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_RULE_CONTEXT_H
10 #define LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_RULE_CONTEXT_H
11 
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.
34 public:
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 
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  void setASTSelection(std::unique_ptr<SelectedASTNode> Node) {
66  ASTNodeSelection = std::move(Node);
67  }
68 
69 private:
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
77  /// might operate on.
78  ASTContext *AST = nullptr;
79  /// The allocator for diagnostics.
81 
82  // FIXME: Remove when memoized.
83  std::unique_ptr<SelectedASTNode> ASTNodeSelection;
84 };
85 
86 } // end namespace tooling
87 } // end namespace clang
88 
89 #endif // LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_RULE_CONTEXT_H
clang::tooling::RefactoringRuleContext::createDiagnosticError
llvm::Error createDiagnosticError(unsigned DiagID)
Definition: RefactoringRuleContext.h:61
clang::tooling::RefactoringRuleContext::getSelectionRange
SourceRange getSelectionRange() const
Returns the current source selection range as set by the refactoring engine.
Definition: RefactoringRuleContext.h:41
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:212
ASTSelection.h
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:89
SourceManager.h
DiagnosticError.h
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:624
Node
DynTypedNode Node
Definition: ASTMatchFinder.cpp:67
clang::tooling::RefactoringRuleContext::RefactoringRuleContext
RefactoringRuleContext(const SourceManager &SM)
Definition: RefactoringRuleContext.h:35
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:187
clang::tooling::RefactoringRuleContext::hasASTContext
bool hasASTContext() const
Definition: RefactoringRuleContext.h:45
clang::tooling::RefactoringRuleContext
The refactoring rule context stores all of the inputs that might be needed by a refactoring action ru...
Definition: RefactoringRuleContext.h:33
clang::DiagnosticError::create
static llvm::Error create(SourceLocation Loc, PartialDiagnostic Diag)
Creates a new DiagnosticError that contains the given diagnostic at the given location.
Definition: DiagnosticError.h:31
clang::tooling::RefactoringRuleContext::setASTContext
void setASTContext(ASTContext &Context)
Definition: RefactoringRuleContext.h:52
clang::tooling::RefactoringRuleContext::setSelectionRange
void setSelectionRange(SourceRange R)
Definition: RefactoringRuleContext.h:43
clang::PartialDiagnostic
Definition: PartialDiagnostic.h:34
clang
Dataflow Directional Tag Classes.
Definition: CalledOnceCheck.h:17
clang::tooling::RefactoringRuleContext::createDiagnosticError
llvm::Error createDiagnosticError(SourceLocation Loc, unsigned DiagID)
Creates an llvm::Error value that contains a diagnostic.
Definition: RefactoringRuleContext.h:57
clang::tooling::RefactoringRuleContext::getASTContext
ASTContext & getASTContext() const
Definition: RefactoringRuleContext.h:47
clang::tooling::RefactoringRuleContext::getSources
const SourceManager & getSources() const
Definition: RefactoringRuleContext.h:37
SM
#define SM(sm)
Definition: Cuda.cpp:68
clang::StreamingDiagnostic::DiagStorageAllocator
An allocator for DiagnosticStorage objects, which uses a small cache to objects, used to reduce mallo...
Definition: Diagnostic.h:1109
clang::tooling::RefactoringRuleContext::setASTSelection
void setASTSelection(std::unique_ptr< SelectedASTNode > Node)
Definition: RefactoringRuleContext.h:65