clang  3.9.0
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 <string>
25 
26 namespace clang {
27 
28 class Rewriter;
29 
30 namespace tooling {
31 
32 /// \brief A tool to run refactorings.
33 ///
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 {
38 public:
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>());
44 
45  /// \brief Returns the set of replacements to which replacements should
46  /// be added during the run of the tool.
48 
49  /// \brief Call run(), apply all generated replacements, and immediately save
50  /// the results to disk.
51  ///
52  /// \returns 0 upon success. Non-zero upon failure.
53  int runAndSave(FrontendActionFactory *ActionFactory);
54 
55  /// \brief Apply all stored replacements to the given Rewriter.
56  ///
57  /// Replacement applications happen independently of the success of other
58  /// applications.
59  ///
60  /// \returns true if all replacements apply. false otherwise.
61  bool applyAllReplacements(Rewriter &Rewrite);
62 
63 private:
64  /// \brief Write all refactored files to disk.
65  int saveRewrittenFiles(Rewriter &Rewrite);
66 
67 private:
68  Replacements Replace;
69 };
70 
71 /// \brief Groups \p Replaces by the file path and applies each group of
72 /// Replacements on the related file in \p Rewriter. In addition to applying
73 /// given Replacements, this function also formats the changed code.
74 ///
75 /// \pre Replacements must be conflict-free.
76 ///
77 /// Replacement applications happen independently of the success of other
78 /// applications.
79 ///
80 /// \param[in] Replaces Replacements to apply.
81 /// \param[in] Rewrite The `Rewritter` to apply replacements on.
82 /// \param[in] Style The style name used for reformatting. See ```getStyle``` in
83 /// "include/clang/Format/Format.h" for all possible style forms.
84 ///
85 /// \returns true if all replacements applied and formatted. false otherwise.
86 bool formatAndApplyAllReplacements(const Replacements &Replaces,
87  Rewriter &Rewrite, StringRef Style = "file");
88 
89 } // end namespace tooling
90 } // end namespace clang
91 
92 #endif // LLVM_CLANG_TOOLING_REFACTORING_H
Replacements & getReplacements()
Returns the set of replacements to which replacements should be added during the run of the tool...
Definition: Refactoring.cpp:34
std::set< Replacement > Replacements
A set of Replacements.
Definition: Replacement.h:148
Utility to run a FrontendAction over a set of files.
Definition: Tooling.h:284
Interface to generate clang::FrontendActions.
Definition: Tooling.h:83
const FormatStyle & Style
Definition: Format.cpp:1311
A tool to run refactorings.
Definition: Refactoring.h:37
Interface for compilation databases.
int runAndSave(FrontendActionFactory *ActionFactory)
Call run(), apply all generated replacements, and immediately save the results to disk...
Definition: Refactoring.cpp:36
bool applyAllReplacements(Rewriter &Rewrite)
Apply all stored replacements to the given Rewriter.
Definition: Refactoring.cpp:57
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:31
bool formatAndApplyAllReplacements(const Replacements &Replaces, 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:65
RefactoringTool(const CompilationDatabase &Compilations, ArrayRef< std::string > SourcePaths, std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >())
Definition: Refactoring.cpp:29