1 //===--- Refactoring.h - Framework for clang refactoring tools --*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Interfaces supporting refactorings that span multiple translation units.
10 // While single translation unit refactorings are supported via the Rewriter,
11 // when refactoring multiple translation units changes must be stored in a
12 // SourceManager independent form, duplicate changes need to be removed, and
13 // all changes must be applied at once at the end of the refactoring so that
14 // the code is always parseable.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_CLANG_TOOLING_REFACTORING_H
19 #define LLVM_CLANG_TOOLING_REFACTORING_H
21 #include "clang/Tooling/Core/Replacement.h"
22 #include "clang/Tooling/Tooling.h"
32 /// A tool to run refactorings.
34 /// This is a refactoring specific version of \see ClangTool. FrontendActions
35 /// passed to run() and runAndSave() should add replacements to
36 /// getReplacements().
37 class RefactoringTool : public ClangTool {
39 /// \see ClangTool::ClangTool.
40 RefactoringTool(const CompilationDatabase &Compilations,
41 ArrayRef<std::string> SourcePaths,
42 std::shared_ptr<PCHContainerOperations> PCHContainerOps =
43 std::make_shared<PCHContainerOperations>());
45 /// Returns the file path to replacements map to which replacements
46 /// should be added during the run of the tool.
47 std::map<std::string, Replacements> &getReplacements();
49 /// Call run(), apply all generated replacements, and immediately save
50 /// the results to disk.
52 /// \returns 0 upon success. Non-zero upon failure.
53 int runAndSave(FrontendActionFactory *ActionFactory);
55 /// Apply all stored replacements to the given Rewriter.
57 /// FileToReplaces will be deduplicated with `groupReplacementsByFile` before
60 /// Replacement applications happen independently of the success of other
63 /// \returns true if all replacements apply. false otherwise.
64 bool applyAllReplacements(Rewriter &Rewrite);
67 /// Write all refactored files to disk.
68 int saveRewrittenFiles(Rewriter &Rewrite);
71 std::map<std::string, Replacements> FileToReplaces;
74 /// Groups \p Replaces by the file path and applies each group of
75 /// Replacements on the related file in \p Rewriter. In addition to applying
76 /// given Replacements, this function also formats the changed code.
78 /// \pre Replacements must be conflict-free.
80 /// FileToReplaces will be deduplicated with `groupReplacementsByFile` before
83 /// Replacement applications happen independently of the success of other
86 /// \param[in] FileToReplaces Replacements (grouped by files) to apply.
87 /// \param[in] Rewrite The `Rewritter` to apply replacements on.
88 /// \param[in] Style The style name used for reformatting. See ```getStyle``` in
89 /// "include/clang/Format/Format.h" for all possible style forms.
91 /// \returns true if all replacements applied and formatted. false otherwise.
92 bool formatAndApplyAllReplacements(
93 const std::map<std::string, Replacements> &FileToReplaces,
94 Rewriter &Rewrite, StringRef Style = "file");
96 } // end namespace tooling
97 } // end namespace clang
99 #endif // LLVM_CLANG_TOOLING_REFACTORING_H