clang  6.0.0svn
Refactoring.h
Go to the documentation of this file.
1 //===--- Refactoring.h - Framework for clang refactoring tools --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_CLANG_TOOLING_REFACTORING_H
20 #define LLVM_CLANG_TOOLING_REFACTORING_H
21 
23 #include "clang/Tooling/Tooling.h"
24 #include <map>
25 #include <string>
26 
27 namespace clang {
28 
29 class Rewriter;
30 
31 namespace tooling {
32 
33 /// \brief A tool to run refactorings.
34 ///
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 {
39 public:
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>());
45 
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();
49 
50  /// \brief Call run(), apply all generated replacements, and immediately save
51  /// the results to disk.
52  ///
53  /// \returns 0 upon success. Non-zero upon failure.
54  int runAndSave(FrontendActionFactory *ActionFactory);
55 
56  /// \brief Apply all stored replacements to the given Rewriter.
57  ///
58  /// FileToReplaces will be deduplicated with `groupReplacementsByFile` before
59  /// application.
60  ///
61  /// Replacement applications happen independently of the success of other
62  /// applications.
63  ///
64  /// \returns true if all replacements apply. false otherwise.
65  bool applyAllReplacements(Rewriter &Rewrite);
66 
67 private:
68  /// \brief Write all refactored files to disk.
69  int saveRewrittenFiles(Rewriter &Rewrite);
70 
71 private:
72  std::map<std::string, Replacements> FileToReplaces;
73 };
74 
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.
78 ///
79 /// \pre Replacements must be conflict-free.
80 ///
81 /// FileToReplaces will be deduplicated with `groupReplacementsByFile` before
82 /// application.
83 ///
84 /// Replacement applications happen independently of the success of other
85 /// applications.
86 ///
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.
91 ///
92 /// \returns true if all replacements applied and formatted. false otherwise.
94  const std::map<std::string, Replacements> &FileToReplaces,
95  Rewriter &Rewrite, StringRef Style = "file");
96 
97 } // end namespace tooling
98 } // end namespace clang
99 
100 #endif // LLVM_CLANG_TOOLING_REFACTORING_H
Utility to run a FrontendAction over a set of files.
Definition: Tooling.h:289
Interface to generate clang::FrontendActions.
Definition: Tooling.h:84
A tool to run refactorings.
Definition: Refactoring.h:38
Interface for compilation databases.
int runAndSave(FrontendActionFactory *ActionFactory)
Call run(), apply all generated replacements, and immediately save the results to disk...
Definition: Refactoring.cpp:37
bool formatAndApplyAllReplacements(const std::map< std::string, Replacements > &FileToReplaces, Rewriter &Rewrite, StringRef Style="file")
Groups Replaces by the file path and applies each group of Replacements on the related file in Rewrit...
Definition: Refactoring.cpp:70
Dataflow Directional Tag Classes.
bool applyAllReplacements(Rewriter &Rewrite)
Apply all stored replacements to the given Rewriter.
Definition: Refactoring.cpp:58
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:31
std::map< std::string, Replacements > & getReplacements()
Returns the file path to replacements map to which replacements should be added during the run of the...
Definition: Refactoring.cpp:33
RefactoringTool(const CompilationDatabase &Compilations, ArrayRef< std::string > SourcePaths, std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >())
Definition: Refactoring.cpp:28
const FormatStyle & Style