]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Tooling/Refactoring.h
Update to bmake-20130330
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Tooling / Refactoring.h
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
22 #include "llvm/ADT/StringRef.h"
23 #include "clang/Basic/SourceLocation.h"
24 #include "clang/Tooling/Tooling.h"
25 #include <set>
26 #include <string>
27
28 namespace clang {
29
30 class Rewriter;
31 class SourceLocation;
32
33 namespace tooling {
34
35 /// \brief A text replacement.
36 ///
37 /// Represents a SourceManager independent replacement of a range of text in a
38 /// specific file.
39 class Replacement {
40 public:
41   /// \brief Creates an invalid (not applicable) replacement.
42   Replacement();
43
44   /// \brief Creates a replacement of the range [Offset, Offset+Length) in
45   /// FilePath with ReplacementText.
46   ///
47   /// \param FilePath A source file accessible via a SourceManager.
48   /// \param Offset The byte offset of the start of the range in the file.
49   /// \param Length The length of the range in bytes.
50   Replacement(llvm::StringRef FilePath, unsigned Offset,
51               unsigned Length, llvm::StringRef ReplacementText);
52
53   /// \brief Creates a Replacement of the range [Start, Start+Length) with
54   /// ReplacementText.
55   Replacement(SourceManager &Sources, SourceLocation Start, unsigned Length,
56               llvm::StringRef ReplacementText);
57
58   /// \brief Creates a Replacement of the given range with ReplacementText.
59   Replacement(SourceManager &Sources, const CharSourceRange &Range,
60               llvm::StringRef ReplacementText);
61
62   /// \brief Creates a Replacement of the node with ReplacementText.
63   template <typename Node>
64   Replacement(SourceManager &Sources, const Node &NodeToReplace,
65               llvm::StringRef ReplacementText);
66
67   /// \brief Returns whether this replacement can be applied to a file.
68   ///
69   /// Only replacements that are in a valid file can be applied.
70   bool isApplicable() const;
71
72   /// \brief Accessors.
73   /// @{
74   StringRef getFilePath() const { return FilePath; }
75   unsigned getOffset() const { return Offset; }
76   unsigned getLength() const { return Length; }
77   StringRef getReplacementText() const { return ReplacementText; }
78   /// @}
79
80   /// \brief Applies the replacement on the Rewriter.
81   bool apply(Rewriter &Rewrite) const;
82
83   /// \brief Returns a human readable string representation.
84   std::string toString() const;
85
86   /// \brief Comparator to be able to use Replacement in std::set for uniquing.
87   class Less {
88   public:
89     bool operator()(const Replacement &R1, const Replacement &R2) const;
90   };
91
92  private:
93   void setFromSourceLocation(SourceManager &Sources, SourceLocation Start,
94                              unsigned Length, llvm::StringRef ReplacementText);
95   void setFromSourceRange(SourceManager &Sources, const CharSourceRange &Range,
96                           llvm::StringRef ReplacementText);
97
98   std::string FilePath;
99   unsigned Offset;
100   unsigned Length;
101   std::string ReplacementText;
102 };
103
104 /// \brief A set of Replacements.
105 /// FIXME: Change to a vector and deduplicate in the RefactoringTool.
106 typedef std::set<Replacement, Replacement::Less> Replacements;
107
108 /// \brief Apply all replacements on the Rewriter.
109 ///
110 /// If at least one Apply returns false, ApplyAll returns false. Every
111 /// Apply will be executed independently of the result of other
112 /// Apply operations.
113 bool applyAllReplacements(Replacements &Replaces, Rewriter &Rewrite);
114
115 /// \brief A tool to run refactorings.
116 ///
117 /// This is a refactoring specific version of \see ClangTool.
118 /// All text replacements added to getReplacements() during the run of the
119 /// tool will be applied and saved after all translation units have been
120 /// processed.
121 class RefactoringTool {
122 public:
123   /// \see ClangTool::ClangTool.
124   RefactoringTool(const CompilationDatabase &Compilations,
125                   ArrayRef<std::string> SourcePaths);
126
127   /// \brief Returns a set of replacements. All replacements added during the
128   /// run of the tool will be applied after all translation units have been
129   /// processed.
130   Replacements &getReplacements();
131
132   /// \see ClangTool::run.
133   int run(FrontendActionFactory *ActionFactory);
134
135 private:
136   ClangTool Tool;
137   Replacements Replace;
138 };
139
140 template <typename Node>
141 Replacement::Replacement(SourceManager &Sources, const Node &NodeToReplace,
142                          llvm::StringRef ReplacementText) {
143   const CharSourceRange Range =
144       CharSourceRange::getTokenRange(NodeToReplace->getSourceRange());
145   setFromSourceRange(Sources, Range, ReplacementText);
146 }
147
148 } // end namespace tooling
149 } // end namespace clang
150
151 #endif // end namespace LLVM_CLANG_TOOLING_REFACTORING_H
152