Refactoring.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- Refactoring.h - Framework for clang refactoring tools --*- 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. // Interfaces supporting refactorings that span multiple translation units.
  15. // While single translation unit refactorings are supported via the Rewriter,
  16. // when refactoring multiple translation units changes must be stored in a
  17. // SourceManager independent form, duplicate changes need to be removed, and
  18. // all changes must be applied at once at the end of the refactoring so that
  19. // the code is always parseable.
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_CLANG_TOOLING_REFACTORING_H
  23. #define LLVM_CLANG_TOOLING_REFACTORING_H
  24. #include "clang/Tooling/Core/Replacement.h"
  25. #include "clang/Tooling/Tooling.h"
  26. #include <map>
  27. #include <string>
  28. namespace clang {
  29. class Rewriter;
  30. namespace tooling {
  31. /// A tool to run refactorings.
  32. ///
  33. /// This is a refactoring specific version of \see ClangTool. FrontendActions
  34. /// passed to run() and runAndSave() should add replacements to
  35. /// getReplacements().
  36. class RefactoringTool : public ClangTool {
  37. public:
  38. /// \see ClangTool::ClangTool.
  39. RefactoringTool(const CompilationDatabase &Compilations,
  40. ArrayRef<std::string> SourcePaths,
  41. std::shared_ptr<PCHContainerOperations> PCHContainerOps =
  42. std::make_shared<PCHContainerOperations>());
  43. /// Returns the file path to replacements map to which replacements
  44. /// should be added during the run of the tool.
  45. std::map<std::string, Replacements> &getReplacements();
  46. /// Call run(), apply all generated replacements, and immediately save
  47. /// the results to disk.
  48. ///
  49. /// \returns 0 upon success. Non-zero upon failure.
  50. int runAndSave(FrontendActionFactory *ActionFactory);
  51. /// Apply all stored replacements to the given Rewriter.
  52. ///
  53. /// FileToReplaces will be deduplicated with `groupReplacementsByFile` before
  54. /// application.
  55. ///
  56. /// Replacement applications happen independently of the success of other
  57. /// applications.
  58. ///
  59. /// \returns true if all replacements apply. false otherwise.
  60. bool applyAllReplacements(Rewriter &Rewrite);
  61. private:
  62. /// Write all refactored files to disk.
  63. int saveRewrittenFiles(Rewriter &Rewrite);
  64. private:
  65. std::map<std::string, Replacements> FileToReplaces;
  66. };
  67. /// Groups \p Replaces by the file path and applies each group of
  68. /// Replacements on the related file in \p Rewriter. In addition to applying
  69. /// given Replacements, this function also formats the changed code.
  70. ///
  71. /// \pre Replacements must be conflict-free.
  72. ///
  73. /// FileToReplaces will be deduplicated with `groupReplacementsByFile` before
  74. /// application.
  75. ///
  76. /// Replacement applications happen independently of the success of other
  77. /// applications.
  78. ///
  79. /// \param[in] FileToReplaces Replacements (grouped by files) to apply.
  80. /// \param[in] Rewrite The `Rewritter` to apply replacements on.
  81. /// \param[in] Style The style name used for reformatting. See ```getStyle``` in
  82. /// "include/clang/Format/Format.h" for all possible style forms.
  83. ///
  84. /// \returns true if all replacements applied and formatted. false otherwise.
  85. bool formatAndApplyAllReplacements(
  86. const std::map<std::string, Replacements> &FileToReplaces,
  87. Rewriter &Rewrite, StringRef Style = "file");
  88. } // end namespace tooling
  89. } // end namespace clang
  90. #endif // LLVM_CLANG_TOOLING_REFACTORING_H
  91. #ifdef __GNUC__
  92. #pragma GCC diagnostic pop
  93. #endif