RefactoringRuleContext.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- RefactoringRuleContext.h - Clang refactoring library -------------===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGRULECONTEXT_H
  14. #define LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGRULECONTEXT_H
  15. #include "clang/Basic/DiagnosticError.h"
  16. #include "clang/Basic/SourceManager.h"
  17. #include "clang/Tooling/Refactoring/ASTSelection.h"
  18. namespace clang {
  19. class ASTContext;
  20. namespace tooling {
  21. /// The refactoring rule context stores all of the inputs that might be needed
  22. /// by a refactoring action rule. It can create the specialized
  23. /// \c ASTRefactoringOperation or \c PreprocessorRefactoringOperation values
  24. /// that can be used by the refactoring action rules.
  25. ///
  26. /// The following inputs are stored by the operation:
  27. ///
  28. /// - SourceManager: a reference to a valid source manager.
  29. ///
  30. /// - SelectionRange: an optional source selection ranges that can be used
  31. /// to represent a selection in an editor.
  32. class RefactoringRuleContext {
  33. public:
  34. RefactoringRuleContext(const SourceManager &SM) : SM(SM) {}
  35. const SourceManager &getSources() const { return SM; }
  36. /// Returns the current source selection range as set by the
  37. /// refactoring engine. Can be invalid.
  38. SourceRange getSelectionRange() const { return SelectionRange; }
  39. void setSelectionRange(SourceRange R) { SelectionRange = R; }
  40. bool hasASTContext() const { return AST; }
  41. ASTContext &getASTContext() const {
  42. assert(AST && "no AST!");
  43. return *AST;
  44. }
  45. void setASTContext(ASTContext &Context) { AST = &Context; }
  46. /// Creates an llvm::Error value that contains a diagnostic.
  47. ///
  48. /// The errors should not outlive the context.
  49. llvm::Error createDiagnosticError(SourceLocation Loc, unsigned DiagID) {
  50. return DiagnosticError::create(Loc, PartialDiagnostic(DiagID, DiagStorage));
  51. }
  52. llvm::Error createDiagnosticError(unsigned DiagID) {
  53. return createDiagnosticError(SourceLocation(), DiagID);
  54. }
  55. void setASTSelection(std::unique_ptr<SelectedASTNode> Node) {
  56. ASTNodeSelection = std::move(Node);
  57. }
  58. private:
  59. /// The source manager for the translation unit / file on which a refactoring
  60. /// action might operate on.
  61. const SourceManager &SM;
  62. /// An optional source selection range that's commonly used to represent
  63. /// a selection in an editor.
  64. SourceRange SelectionRange;
  65. /// An optional AST for the translation unit on which a refactoring action
  66. /// might operate on.
  67. ASTContext *AST = nullptr;
  68. /// The allocator for diagnostics.
  69. PartialDiagnostic::DiagStorageAllocator DiagStorage;
  70. // FIXME: Remove when memoized.
  71. std::unique_ptr<SelectedASTNode> ASTNodeSelection;
  72. };
  73. } // end namespace tooling
  74. } // end namespace clang
  75. #endif // LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGRULECONTEXT_H
  76. #ifdef __GNUC__
  77. #pragma GCC diagnostic pop
  78. #endif