RefactoringCallbacks.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- RefactoringCallbacks.h - Structural query framework ----*- C++ -*-===//
  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. //
  14. // Provides callbacks to make common kinds of refactorings easy.
  15. //
  16. // The general idea is to construct a matcher expression that describes a
  17. // subtree match on the AST and then replace the corresponding source code
  18. // either by some specific text or some other AST node.
  19. //
  20. // Example:
  21. // int main(int argc, char **argv) {
  22. // ClangTool Tool(argc, argv);
  23. // MatchFinder Finder;
  24. // ReplaceStmtWithText Callback("integer", "42");
  25. // Finder.AddMatcher(id("integer", expression(integerLiteral())), Callback);
  26. // return Tool.run(newFrontendActionFactory(&Finder));
  27. // }
  28. //
  29. // This will replace all integer literals with "42".
  30. //
  31. //===----------------------------------------------------------------------===//
  32. #ifndef LLVM_CLANG_TOOLING_REFACTORINGCALLBACKS_H
  33. #define LLVM_CLANG_TOOLING_REFACTORINGCALLBACKS_H
  34. #include "clang/ASTMatchers/ASTMatchFinder.h"
  35. #include "clang/Tooling/Refactoring.h"
  36. namespace clang {
  37. namespace tooling {
  38. /// Base class for RefactoringCallbacks.
  39. ///
  40. /// Collects \c tooling::Replacements while running.
  41. class RefactoringCallback : public ast_matchers::MatchFinder::MatchCallback {
  42. public:
  43. RefactoringCallback();
  44. Replacements &getReplacements();
  45. protected:
  46. Replacements Replace;
  47. };
  48. /// Adaptor between \c ast_matchers::MatchFinder and \c
  49. /// tooling::RefactoringTool.
  50. ///
  51. /// Runs AST matchers and stores the \c tooling::Replacements in a map.
  52. class ASTMatchRefactorer {
  53. public:
  54. explicit ASTMatchRefactorer(
  55. std::map<std::string, Replacements> &FileToReplaces);
  56. template <typename T>
  57. void addMatcher(const T &Matcher, RefactoringCallback *Callback) {
  58. MatchFinder.addMatcher(Matcher, Callback);
  59. Callbacks.push_back(Callback);
  60. }
  61. void addDynamicMatcher(const ast_matchers::internal::DynTypedMatcher &Matcher,
  62. RefactoringCallback *Callback);
  63. std::unique_ptr<ASTConsumer> newASTConsumer();
  64. private:
  65. friend class RefactoringASTConsumer;
  66. std::vector<RefactoringCallback *> Callbacks;
  67. ast_matchers::MatchFinder MatchFinder;
  68. std::map<std::string, Replacements> &FileToReplaces;
  69. };
  70. /// Replace the text of the statement bound to \c FromId with the text in
  71. /// \c ToText.
  72. class ReplaceStmtWithText : public RefactoringCallback {
  73. public:
  74. ReplaceStmtWithText(StringRef FromId, StringRef ToText);
  75. void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
  76. private:
  77. std::string FromId;
  78. std::string ToText;
  79. };
  80. /// Replace the text of an AST node bound to \c FromId with the result of
  81. /// evaluating the template in \c ToTemplate.
  82. ///
  83. /// Expressions of the form ${NodeName} in \c ToTemplate will be
  84. /// replaced by the text of the node bound to ${NodeName}. The string
  85. /// "$$" will be replaced by "$".
  86. class ReplaceNodeWithTemplate : public RefactoringCallback {
  87. public:
  88. static llvm::Expected<std::unique_ptr<ReplaceNodeWithTemplate>>
  89. create(StringRef FromId, StringRef ToTemplate);
  90. void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
  91. private:
  92. struct TemplateElement {
  93. enum { Literal, Identifier } Type;
  94. std::string Value;
  95. };
  96. ReplaceNodeWithTemplate(llvm::StringRef FromId,
  97. std::vector<TemplateElement> Template);
  98. std::string FromId;
  99. std::vector<TemplateElement> Template;
  100. };
  101. /// Replace the text of the statement bound to \c FromId with the text of
  102. /// the statement bound to \c ToId.
  103. class ReplaceStmtWithStmt : public RefactoringCallback {
  104. public:
  105. ReplaceStmtWithStmt(StringRef FromId, StringRef ToId);
  106. void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
  107. private:
  108. std::string FromId;
  109. std::string ToId;
  110. };
  111. /// Replace an if-statement bound to \c Id with the outdented text of its
  112. /// body, choosing the consequent or the alternative based on whether
  113. /// \c PickTrueBranch is true.
  114. class ReplaceIfStmtWithItsBody : public RefactoringCallback {
  115. public:
  116. ReplaceIfStmtWithItsBody(StringRef Id, bool PickTrueBranch);
  117. void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
  118. private:
  119. std::string Id;
  120. const bool PickTrueBranch;
  121. };
  122. } // end namespace tooling
  123. } // end namespace clang
  124. #endif
  125. #ifdef __GNUC__
  126. #pragma GCC diagnostic pop
  127. #endif