RefactoringActionRules.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- RefactoringActionRules.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_REFACTORINGACTIONRULES_H
  14. #define LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGACTIONRULES_H
  15. #include "clang/Tooling/Refactoring/RefactoringActionRule.h"
  16. #include "clang/Tooling/Refactoring/RefactoringActionRulesInternal.h"
  17. namespace clang {
  18. namespace tooling {
  19. /// Creates a new refactoring action rule that constructs and invokes the
  20. /// \c RuleType rule when all of the requirements are satisfied.
  21. ///
  22. /// This function takes in a list of values whose type derives from
  23. /// \c RefactoringActionRuleRequirement. These values describe the initiation
  24. /// requirements that have to be satisfied by the refactoring engine before
  25. /// the provided action rule can be constructed and invoked. The engine
  26. /// verifies that the requirements are satisfied by evaluating them (using the
  27. /// 'evaluate' member function) and checking that the results don't contain
  28. /// any errors. Once all requirements are satisfied, the provided refactoring
  29. /// rule is constructed by passing in the values returned by the requirements'
  30. /// evaluate functions as arguments to the constructor. The rule is then invoked
  31. /// immediately after construction.
  32. ///
  33. /// The separation of requirements, their evaluation and the invocation of the
  34. /// refactoring action rule allows the refactoring clients to:
  35. /// - Disable refactoring action rules whose requirements are not supported.
  36. /// - Gather the set of options and define a command-line / visual interface
  37. /// that allows users to input these options without ever invoking the
  38. /// action.
  39. template <typename RuleType, typename... RequirementTypes>
  40. std::unique_ptr<RefactoringActionRule>
  41. createRefactoringActionRule(const RequirementTypes &... Requirements);
  42. /// A set of refactoring action rules that should have unique initiation
  43. /// requirements.
  44. using RefactoringActionRules =
  45. std::vector<std::unique_ptr<RefactoringActionRule>>;
  46. /// A type of refactoring action rule that produces source replacements in the
  47. /// form of atomic changes.
  48. ///
  49. /// This action rule is typically used for local refactorings that replace
  50. /// source in a single AST unit.
  51. class SourceChangeRefactoringRule : public RefactoringActionRuleBase {
  52. public:
  53. void invoke(RefactoringResultConsumer &Consumer,
  54. RefactoringRuleContext &Context) final {
  55. Expected<AtomicChanges> Changes = createSourceReplacements(Context);
  56. if (!Changes)
  57. Consumer.handleError(Changes.takeError());
  58. else
  59. Consumer.handle(std::move(*Changes));
  60. }
  61. private:
  62. virtual Expected<AtomicChanges>
  63. createSourceReplacements(RefactoringRuleContext &Context) = 0;
  64. };
  65. /// A type of refactoring action rule that finds a set of symbol occurrences
  66. /// that reference a particular symbol.
  67. ///
  68. /// This action rule is typically used for an interactive rename that allows
  69. /// users to specify the new name and the set of selected occurrences during
  70. /// the refactoring.
  71. class FindSymbolOccurrencesRefactoringRule : public RefactoringActionRuleBase {
  72. public:
  73. void invoke(RefactoringResultConsumer &Consumer,
  74. RefactoringRuleContext &Context) final {
  75. Expected<SymbolOccurrences> Occurrences = findSymbolOccurrences(Context);
  76. if (!Occurrences)
  77. Consumer.handleError(Occurrences.takeError());
  78. else
  79. Consumer.handle(std::move(*Occurrences));
  80. }
  81. private:
  82. virtual Expected<SymbolOccurrences>
  83. findSymbolOccurrences(RefactoringRuleContext &Context) = 0;
  84. };
  85. } // end namespace tooling
  86. } // end namespace clang
  87. #endif // LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGACTIONRULES_H
  88. #ifdef __GNUC__
  89. #pragma GCC diagnostic pop
  90. #endif