]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Rewrite/Rewriter.h
Upgrade our Clang in base to r108428.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Rewrite / Rewriter.h
1 //===--- Rewriter.h - Code rewriting interface ------------------*- 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 //  This file defines the Rewriter class, which is used for code
11 //  transformations.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_REWRITER_H
16 #define LLVM_CLANG_REWRITER_H
17
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Rewrite/DeltaTree.h"
20 #include "clang/Rewrite/RewriteRope.h"
21 #include "llvm/ADT/StringRef.h"
22 #include <cstring>
23 #include <map>
24 #include <string>
25 #include <vector>
26
27 namespace llvm { class raw_ostream; }
28
29 namespace clang {
30   class LangOptions;
31   class Rewriter;
32   class SourceManager;
33   class Stmt;
34
35 /// RewriteBuffer - As code is rewritten, SourceBuffer's from the original
36 /// input with modifications get a new RewriteBuffer associated with them.  The
37 /// RewriteBuffer captures the modified text itself as well as information used
38 /// to map between SourceLocation's in the original input and offsets in the
39 /// RewriteBuffer.  For example, if text is inserted into the buffer, any
40 /// locations after the insertion point have to be mapped.
41 class RewriteBuffer {
42   friend class Rewriter;
43   /// Deltas - Keep track of all the deltas in the source code due to insertions
44   /// and deletions.
45   DeltaTree Deltas;
46
47   /// Buffer - This is the actual buffer itself.  Note that using a vector or
48   /// string is a horribly inefficient way to do this, we should use a rope
49   /// instead.
50   typedef RewriteRope BufferTy;
51   BufferTy Buffer;
52 public:
53   typedef BufferTy::const_iterator iterator;
54   iterator begin() const { return Buffer.begin(); }
55   iterator end() const { return Buffer.end(); }
56   unsigned size() const { return Buffer.size(); }
57
58   llvm::raw_ostream &write(llvm::raw_ostream &) const;
59
60   /// RemoveText - Remove the specified text.
61   void RemoveText(unsigned OrigOffset, unsigned Size);
62
63   /// InsertText - Insert some text at the specified point, where the offset in
64   /// the buffer is specified relative to the original SourceBuffer.  The
65   /// text is inserted after the specified location.
66   ///
67   void InsertText(unsigned OrigOffset, llvm::StringRef Str,
68                   bool InsertAfter = true);
69
70
71   /// InsertTextBefore - Insert some text before the specified point, where the
72   /// offset in the buffer is specified relative to the original
73   /// SourceBuffer. The text is inserted before the specified location.  This is
74   /// method is the same as InsertText with "InsertAfter == false".
75   void InsertTextBefore(unsigned OrigOffset, llvm::StringRef Str) {
76     InsertText(OrigOffset, Str, false);
77   }
78
79   /// InsertTextAfter - Insert some text at the specified point, where the
80   /// offset in the buffer is specified relative to the original SourceBuffer.
81   /// The text is inserted after the specified location.
82   void InsertTextAfter(unsigned OrigOffset, llvm::StringRef Str) {
83     InsertText(OrigOffset, Str);
84   }
85
86   /// ReplaceText - This method replaces a range of characters in the input
87   /// buffer with a new string.  This is effectively a combined "remove/insert"
88   /// operation.
89   void ReplaceText(unsigned OrigOffset, unsigned OrigLength,
90                    llvm::StringRef NewStr);
91
92 private:  // Methods only usable by Rewriter.
93
94   /// Initialize - Start this rewrite buffer out with a copy of the unmodified
95   /// input buffer.
96   void Initialize(const char *BufStart, const char *BufEnd) {
97     Buffer.assign(BufStart, BufEnd);
98   }
99
100   /// getMappedOffset - Given an offset into the original SourceBuffer that this
101   /// RewriteBuffer is based on, map it into the offset space of the
102   /// RewriteBuffer.  If AfterInserts is true and if the OrigOffset indicates a
103   /// position where text is inserted, the location returned will be after any
104   /// inserted text at the position.
105   unsigned getMappedOffset(unsigned OrigOffset,
106                            bool AfterInserts = false) const{
107     return Deltas.getDeltaAt(2*OrigOffset+AfterInserts)+OrigOffset;
108   }
109
110   /// AddInsertDelta - When an insertion is made at a position, this
111   /// method is used to record that information.
112   void AddInsertDelta(unsigned OrigOffset, int Change) {
113     return Deltas.AddDelta(2*OrigOffset, Change);
114   }
115
116   /// AddReplaceDelta - When a replacement/deletion is made at a position, this
117   /// method is used to record that information.
118   void AddReplaceDelta(unsigned OrigOffset, int Change) {
119     return Deltas.AddDelta(2*OrigOffset+1, Change);
120   }
121 };
122
123
124 /// Rewriter - This is the main interface to the rewrite buffers.  Its primary
125 /// job is to dispatch high-level requests to the low-level RewriteBuffers that
126 /// are involved.
127 class Rewriter {
128   SourceManager *SourceMgr;
129   const LangOptions *LangOpts;
130   std::map<FileID, RewriteBuffer> RewriteBuffers;
131 public:
132   typedef std::map<FileID, RewriteBuffer>::iterator buffer_iterator;
133
134   explicit Rewriter(SourceManager &SM, const LangOptions &LO)
135     : SourceMgr(&SM), LangOpts(&LO) {}
136   explicit Rewriter() : SourceMgr(0), LangOpts(0) {}
137
138   void setSourceMgr(SourceManager &SM, const LangOptions &LO) {
139     SourceMgr = &SM;
140     LangOpts = &LO;
141   }
142   SourceManager &getSourceMgr() { return *SourceMgr; }
143   const LangOptions &getLangOpts() { return *LangOpts; }
144
145   /// isRewritable - Return true if this location is a raw file location, which
146   /// is rewritable.  Locations from macros, etc are not rewritable.
147   static bool isRewritable(SourceLocation Loc) {
148     return Loc.isFileID();
149   }
150
151   /// getRangeSize - Return the size in bytes of the specified range if they
152   /// are in the same file.  If not, this returns -1.
153   int getRangeSize(SourceRange Range) const;
154   int getRangeSize(const CharSourceRange &Range) const;
155
156   /// getRewrittenText - Return the rewritten form of the text in the specified
157   /// range.  If the start or end of the range was unrewritable or if they are
158   /// in different buffers, this returns an empty string.
159   ///
160   /// Note that this method is not particularly efficient.
161   ///
162   std::string getRewrittenText(SourceRange Range) const;
163
164   /// InsertText - Insert the specified string at the specified location in the
165   /// original buffer.  This method returns true (and does nothing) if the input
166   /// location was not rewritable, false otherwise.
167   bool InsertText(SourceLocation Loc, llvm::StringRef Str,
168                   bool InsertAfter = true);
169
170   /// InsertTextAfter - Insert the specified string at the specified location in
171   ///  the original buffer.  This method returns true (and does nothing) if
172   ///  the input location was not rewritable, false otherwise.  Text is
173   ///  inserted after any other text that has been previously inserted
174   ///  at the some point (the default behavior for InsertText).
175   bool InsertTextAfter(SourceLocation Loc, llvm::StringRef Str) {
176     return InsertText(Loc, Str);
177   }
178
179   /// InsertText - Insert the specified string at the specified location in the
180   /// original buffer.  This method returns true (and does nothing) if the input
181   /// location was not rewritable, false otherwise.  Text is
182   /// inserted before any other text that has been previously inserted
183   /// at the some point.
184   bool InsertTextBefore(SourceLocation Loc, llvm::StringRef Str) {
185     return InsertText(Loc, Str, false);
186   }
187
188   /// RemoveText - Remove the specified text region.
189   bool RemoveText(SourceLocation Start, unsigned Length);
190
191   /// ReplaceText - This method replaces a range of characters in the input
192   /// buffer with a new string.  This is effectively a combined "remove/insert"
193   /// operation.
194   bool ReplaceText(SourceLocation Start, unsigned OrigLength,
195                    llvm::StringRef NewStr);
196
197   /// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty
198   /// printer to generate the replacement code.  This returns true if the input
199   /// could not be rewritten, or false if successful.
200   bool ReplaceStmt(Stmt *From, Stmt *To);
201
202   /// getEditBuffer - This is like getRewriteBufferFor, but always returns a
203   /// buffer, and allows you to write on it directly.  This is useful if you
204   /// want efficient low-level access to apis for scribbling on one specific
205   /// FileID's buffer.
206   RewriteBuffer &getEditBuffer(FileID FID);
207
208   /// getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
209   /// If no modification has been made to it, return null.
210   const RewriteBuffer *getRewriteBufferFor(FileID FID) const {
211     std::map<FileID, RewriteBuffer>::const_iterator I =
212       RewriteBuffers.find(FID);
213     return I == RewriteBuffers.end() ? 0 : &I->second;
214   }
215
216   // Iterators over rewrite buffers.
217   buffer_iterator buffer_begin() { return RewriteBuffers.begin(); }
218   buffer_iterator buffer_end() { return RewriteBuffers.end(); }
219
220 private:
221   unsigned getLocationOffsetAndFileID(SourceLocation Loc, FileID &FID) const;
222 };
223
224 } // end namespace clang
225
226 #endif