1 //===--- Rewriter.h - Code rewriting interface ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Rewriter class, which is used for code
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_REWRITER_H
16 #define LLVM_CLANG_REWRITER_H
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Rewrite/RewriteRope.h"
24 #include "clang/Rewrite/DeltaTree.h"
25 #include "llvm/ADT/StringRef.h"
33 /// RewriteBuffer - As code is rewritten, SourceBuffer's from the original
34 /// input with modifications get a new RewriteBuffer associated with them. The
35 /// RewriteBuffer captures the modified text itself as well as information used
36 /// to map between SourceLocation's in the original input and offsets in the
37 /// RewriteBuffer. For example, if text is inserted into the buffer, any
38 /// locations after the insertion point have to be mapped.
40 friend class Rewriter;
41 /// Deltas - Keep track of all the deltas in the source code due to insertions
45 /// Buffer - This is the actual buffer itself. Note that using a vector or
46 /// string is a horribly inefficient way to do this, we should use a rope
48 typedef RewriteRope BufferTy;
51 typedef BufferTy::const_iterator iterator;
52 iterator begin() const { return Buffer.begin(); }
53 iterator end() const { return Buffer.end(); }
54 unsigned size() const { return Buffer.size(); }
56 /// RemoveText - Remove the specified text.
57 void RemoveText(unsigned OrigOffset, unsigned Size);
59 /// InsertText - Insert some text at the specified point, where the offset in
60 /// the buffer is specified relative to the original SourceBuffer. The
61 /// text is inserted after the specified location.
63 void InsertText(unsigned OrigOffset, const llvm::StringRef &Str,
64 bool InsertAfter = true);
67 /// InsertTextBefore - Insert some text before the specified point, where the
68 /// offset in the buffer is specified relative to the original
69 /// SourceBuffer. The text is inserted before the specified location. This is
70 /// method is the same as InsertText with "InsertAfter == false".
71 void InsertTextBefore(unsigned OrigOffset, const llvm::StringRef &Str) {
72 InsertText(OrigOffset, Str, false);
75 /// InsertTextAfter - Insert some text at the specified point, where the
76 /// offset in the buffer is specified relative to the original SourceBuffer.
77 /// The text is inserted after the specified location.
78 void InsertTextAfter(unsigned OrigOffset, const llvm::StringRef &Str) {
79 InsertText(OrigOffset, Str);
82 /// ReplaceText - This method replaces a range of characters in the input
83 /// buffer with a new string. This is effectively a combined "remove/insert"
85 void ReplaceText(unsigned OrigOffset, unsigned OrigLength,
86 const llvm::StringRef &NewStr);
88 private: // Methods only usable by Rewriter.
90 /// Initialize - Start this rewrite buffer out with a copy of the unmodified
92 void Initialize(const char *BufStart, const char *BufEnd) {
93 Buffer.assign(BufStart, BufEnd);
96 /// getMappedOffset - Given an offset into the original SourceBuffer that this
97 /// RewriteBuffer is based on, map it into the offset space of the
98 /// RewriteBuffer. If AfterInserts is true and if the OrigOffset indicates a
99 /// position where text is inserted, the location returned will be after any
100 /// inserted text at the position.
101 unsigned getMappedOffset(unsigned OrigOffset,
102 bool AfterInserts = false) const{
103 return Deltas.getDeltaAt(2*OrigOffset+AfterInserts)+OrigOffset;
106 /// AddInsertDelta - When an insertion is made at a position, this
107 /// method is used to record that information.
108 void AddInsertDelta(unsigned OrigOffset, int Change) {
109 return Deltas.AddDelta(2*OrigOffset, Change);
112 /// AddReplaceDelta - When a replacement/deletion is made at a position, this
113 /// method is used to record that information.
114 void AddReplaceDelta(unsigned OrigOffset, int Change) {
115 return Deltas.AddDelta(2*OrigOffset+1, Change);
120 /// Rewriter - This is the main interface to the rewrite buffers. Its primary
121 /// job is to dispatch high-level requests to the low-level RewriteBuffers that
124 SourceManager *SourceMgr;
125 const LangOptions *LangOpts;
126 std::map<FileID, RewriteBuffer> RewriteBuffers;
128 explicit Rewriter(SourceManager &SM, const LangOptions &LO)
129 : SourceMgr(&SM), LangOpts(&LO) {}
130 explicit Rewriter() : SourceMgr(0), LangOpts(0) {}
132 void setSourceMgr(SourceManager &SM, const LangOptions &LO) {
136 SourceManager &getSourceMgr() { return *SourceMgr; }
137 const LangOptions &getLangOpts() { return *LangOpts; }
139 /// isRewritable - Return true if this location is a raw file location, which
140 /// is rewritable. Locations from macros, etc are not rewritable.
141 static bool isRewritable(SourceLocation Loc) {
142 return Loc.isFileID();
145 /// getRangeSize - Return the size in bytes of the specified range if they
146 /// are in the same file. If not, this returns -1.
147 int getRangeSize(SourceRange Range) const;
149 /// getRewrittenText - Return the rewritten form of the text in the specified
150 /// range. If the start or end of the range was unrewritable or if they are
151 /// in different buffers, this returns an empty string.
153 /// Note that this method is not particularly efficient.
155 std::string getRewrittenText(SourceRange Range) const;
157 /// InsertText - Insert the specified string at the specified location in the
158 /// original buffer. This method returns true (and does nothing) if the input
159 /// location was not rewritable, false otherwise.
160 bool InsertText(SourceLocation Loc, const llvm::StringRef &Str,
161 bool InsertAfter = true);
163 /// InsertTextAfter - Insert the specified string at the specified location in
164 /// the original buffer. This method returns true (and does nothing) if
165 /// the input location was not rewritable, false otherwise. Text is
166 /// inserted after any other text that has been previously inserted
167 /// at the some point (the default behavior for InsertText).
168 bool InsertTextAfter(SourceLocation Loc, const llvm::StringRef &Str) {
169 return InsertText(Loc, Str);
172 /// InsertText - Insert the specified string at the specified location in the
173 /// original buffer. This method returns true (and does nothing) if the input
174 /// location was not rewritable, false otherwise. Text is
175 /// inserted before any other text that has been previously inserted
176 /// at the some point.
177 bool InsertTextBefore(SourceLocation Loc, const llvm::StringRef &Str) {
178 return InsertText(Loc, Str, false);
181 /// RemoveText - Remove the specified text region.
182 bool RemoveText(SourceLocation Start, unsigned Length);
184 /// ReplaceText - This method replaces a range of characters in the input
185 /// buffer with a new string. This is effectively a combined "remove/insert"
187 bool ReplaceText(SourceLocation Start, unsigned OrigLength,
188 const llvm::StringRef &NewStr);
190 /// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty
191 /// printer to generate the replacement code. This returns true if the input
192 /// could not be rewritten, or false if successful.
193 bool ReplaceStmt(Stmt *From, Stmt *To);
195 /// getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
196 /// If no modification has been made to it, return null.
197 const RewriteBuffer *getRewriteBufferFor(FileID FID) const {
198 std::map<FileID, RewriteBuffer>::const_iterator I =
199 RewriteBuffers.find(FID);
200 return I == RewriteBuffers.end() ? 0 : &I->second;
203 /// getEditBuffer - This is like getRewriteBufferFor, but always returns a
204 /// buffer, and allows you to write on it directly. This is useful if you
205 /// want efficient low-level access to apis for scribbling on one specific
207 RewriteBuffer &getEditBuffer(FileID FID);
210 unsigned getLocationOffsetAndFileID(SourceLocation Loc, FileID &FID) const;
213 } // end namespace clang