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