• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   /// @{
getFilePath()74   StringRef getFilePath() const { return FilePath; }
getOffset()75   unsigned getOffset() const { return Offset; }
getLength()76   unsigned getLength() const { return Length; }
77   /// @}
78 
79   /// \brief Applies the replacement on the Rewriter.
80   bool apply(Rewriter &Rewrite) const;
81 
82   /// \brief Returns a human readable string representation.
83   std::string toString() const;
84 
85   /// \brief Comparator to be able to use Replacement in std::set for uniquing.
86   class Less {
87   public:
88     bool operator()(const Replacement &R1, const Replacement &R2) const;
89   };
90 
91  private:
92   void setFromSourceLocation(SourceManager &Sources, SourceLocation Start,
93                              unsigned Length, llvm::StringRef ReplacementText);
94   void setFromSourceRange(SourceManager &Sources, const CharSourceRange &Range,
95                           llvm::StringRef ReplacementText);
96 
97   std::string FilePath;
98   unsigned Offset;
99   unsigned Length;
100   std::string ReplacementText;
101 };
102 
103 /// \brief A set of Replacements.
104 /// FIXME: Change to a vector and deduplicate in the RefactoringTool.
105 typedef std::set<Replacement, Replacement::Less> Replacements;
106 
107 /// \brief Apply all replacements on the Rewriter.
108 ///
109 /// If at least one Apply returns false, ApplyAll returns false. Every
110 /// Apply will be executed independently of the result of other
111 /// Apply operations.
112 bool applyAllReplacements(Replacements &Replaces, Rewriter &Rewrite);
113 
114 /// \brief A tool to run refactorings.
115 ///
116 /// This is a refactoring specific version of \see ClangTool.
117 /// All text replacements added to getReplacements() during the run of the
118 /// tool will be applied and saved after all translation units have been
119 /// processed.
120 class RefactoringTool {
121 public:
122   /// \see ClangTool::ClangTool.
123   RefactoringTool(const CompilationDatabase &Compilations,
124                   ArrayRef<std::string> SourcePaths);
125 
126   /// \brief Returns a set of replacements. All replacements added during the
127   /// run of the tool will be applied after all translation units have been
128   /// processed.
129   Replacements &getReplacements();
130 
131   /// \see ClangTool::run.
132   int run(FrontendActionFactory *ActionFactory);
133 
134 private:
135   ClangTool Tool;
136   Replacements Replace;
137 };
138 
139 template <typename Node>
Replacement(SourceManager & Sources,const Node & NodeToReplace,llvm::StringRef ReplacementText)140 Replacement::Replacement(SourceManager &Sources, const Node &NodeToReplace,
141                          llvm::StringRef ReplacementText) {
142   const CharSourceRange Range =
143       CharSourceRange::getTokenRange(NodeToReplace->getSourceRange());
144   setFromSourceRange(Sources, Range, ReplacementText);
145 }
146 
147 } // end namespace tooling
148 } // end namespace clang
149 
150 #endif // end namespace LLVM_CLANG_TOOLING_REFACTORING_H
151 
152