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