1 //===--- Rewriter.cpp - Code rewriting interface --------------------------===//
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 #include "clang/Rewrite/Rewriter.h"
16 #include "clang/AST/Stmt.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/Lex/Lexer.h"
19 #include "clang/Basic/SourceManager.h"
20 #include "llvm/ADT/SmallString.h"
21 using namespace clang;
23 raw_ostream &RewriteBuffer::write(raw_ostream &os) const {
24 // FIXME: eliminate the copy by writing out each chunk at a time
25 os << std::string(begin(), end());
29 /// \brief Return true if this character is non-new-line whitespace:
30 /// ' ', '\t', '\f', '\v', '\r'.
31 static inline bool isWhitespace(unsigned char c) {
44 void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size,
45 bool removeLineIfEmpty) {
46 // Nothing to remove, exit early.
47 if (Size == 0) return;
49 unsigned RealOffset = getMappedOffset(OrigOffset, true);
50 assert(RealOffset+Size < Buffer.size() && "Invalid location");
52 // Remove the dead characters.
53 Buffer.erase(RealOffset, Size);
55 // Add a delta so that future changes are offset correctly.
56 AddReplaceDelta(OrigOffset, -Size);
58 if (removeLineIfEmpty) {
59 // Find the line that the remove occurred and if it is completely empty
60 // remove the line as well.
62 iterator curLineStart = begin();
63 unsigned curLineStartOffs = 0;
64 iterator posI = begin();
65 for (unsigned i = 0; i != RealOffset; ++i) {
69 curLineStartOffs = i + 1;
74 unsigned lineSize = 0;
76 while (posI != end() && isWhitespace(*posI)) {
80 if (posI != end() && *posI == '\n') {
81 Buffer.erase(curLineStartOffs, lineSize + 1/* + '\n'*/);
82 AddReplaceDelta(curLineStartOffs, -(lineSize + 1/* + '\n'*/));
87 void RewriteBuffer::InsertText(unsigned OrigOffset, StringRef Str,
90 // Nothing to insert, exit early.
91 if (Str.empty()) return;
93 unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter);
94 Buffer.insert(RealOffset, Str.begin(), Str.end());
96 // Add a delta so that future changes are offset correctly.
97 AddInsertDelta(OrigOffset, Str.size());
100 /// ReplaceText - This method replaces a range of characters in the input
101 /// buffer with a new string. This is effectively a combined "remove+insert"
103 void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
105 unsigned RealOffset = getMappedOffset(OrigOffset, true);
106 Buffer.erase(RealOffset, OrigLength);
107 Buffer.insert(RealOffset, NewStr.begin(), NewStr.end());
108 if (OrigLength != NewStr.size())
109 AddReplaceDelta(OrigOffset, NewStr.size() - OrigLength);
113 //===----------------------------------------------------------------------===//
115 //===----------------------------------------------------------------------===//
117 /// getRangeSize - Return the size in bytes of the specified range if they
118 /// are in the same file. If not, this returns -1.
119 int Rewriter::getRangeSize(const CharSourceRange &Range,
120 RewriteOptions opts) const {
121 if (!isRewritable(Range.getBegin()) ||
122 !isRewritable(Range.getEnd())) return -1;
124 FileID StartFileID, EndFileID;
125 unsigned StartOff, EndOff;
127 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
128 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
130 if (StartFileID != EndFileID)
133 // If edits have been made to this buffer, the delta between the range may
135 std::map<FileID, RewriteBuffer>::const_iterator I =
136 RewriteBuffers.find(StartFileID);
137 if (I != RewriteBuffers.end()) {
138 const RewriteBuffer &RB = I->second;
139 EndOff = RB.getMappedOffset(EndOff, opts.IncludeInsertsAtEndOfRange);
140 StartOff = RB.getMappedOffset(StartOff, !opts.IncludeInsertsAtBeginOfRange);
144 // Adjust the end offset to the end of the last token, instead of being the
145 // start of the last token if this is a token range.
146 if (Range.isTokenRange())
147 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
149 return EndOff-StartOff;
152 int Rewriter::getRangeSize(SourceRange Range, RewriteOptions opts) const {
153 return getRangeSize(CharSourceRange::getTokenRange(Range), opts);
157 /// getRewrittenText - Return the rewritten form of the text in the specified
158 /// range. If the start or end of the range was unrewritable or if they are
159 /// in different buffers, this returns an empty string.
161 /// Note that this method is not particularly efficient.
163 std::string Rewriter::getRewrittenText(SourceRange Range) const {
164 if (!isRewritable(Range.getBegin()) ||
165 !isRewritable(Range.getEnd()))
168 FileID StartFileID, EndFileID;
169 unsigned StartOff, EndOff;
170 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
171 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
173 if (StartFileID != EndFileID)
174 return ""; // Start and end in different buffers.
176 // If edits have been made to this buffer, the delta between the range may
178 std::map<FileID, RewriteBuffer>::const_iterator I =
179 RewriteBuffers.find(StartFileID);
180 if (I == RewriteBuffers.end()) {
181 // If the buffer hasn't been rewritten, just return the text from the input.
182 const char *Ptr = SourceMgr->getCharacterData(Range.getBegin());
184 // Adjust the end offset to the end of the last token, instead of being the
185 // start of the last token.
186 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
187 return std::string(Ptr, Ptr+EndOff-StartOff);
190 const RewriteBuffer &RB = I->second;
191 EndOff = RB.getMappedOffset(EndOff, true);
192 StartOff = RB.getMappedOffset(StartOff);
194 // Adjust the end offset to the end of the last token, instead of being the
195 // start of the last token.
196 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
198 // Advance the iterators to the right spot, yay for linear time algorithms.
199 RewriteBuffer::iterator Start = RB.begin();
200 std::advance(Start, StartOff);
201 RewriteBuffer::iterator End = Start;
202 std::advance(End, EndOff-StartOff);
204 return std::string(Start, End);
207 unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
209 assert(Loc.isValid() && "Invalid location");
210 std::pair<FileID,unsigned> V = SourceMgr->getDecomposedLoc(Loc);
216 /// getEditBuffer - Get or create a RewriteBuffer for the specified FileID.
218 RewriteBuffer &Rewriter::getEditBuffer(FileID FID) {
219 std::map<FileID, RewriteBuffer>::iterator I =
220 RewriteBuffers.lower_bound(FID);
221 if (I != RewriteBuffers.end() && I->first == FID)
223 I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer()));
225 StringRef MB = SourceMgr->getBufferData(FID);
226 I->second.Initialize(MB.begin(), MB.end());
231 /// InsertText - Insert the specified string at the specified location in the
233 bool Rewriter::InsertText(SourceLocation Loc, StringRef Str,
234 bool InsertAfter, bool indentNewLines) {
235 if (!isRewritable(Loc)) return true;
237 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
239 SmallString<128> indentedStr;
240 if (indentNewLines && Str.find('\n') != StringRef::npos) {
241 StringRef MB = SourceMgr->getBufferData(FID);
243 unsigned lineNo = SourceMgr->getLineNumber(FID, StartOffs) - 1;
244 const SrcMgr::ContentCache *
245 Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache();
246 unsigned lineOffs = Content->SourceLineCache[lineNo];
248 // Find the whitespace at the start of the line.
249 StringRef indentSpace;
251 unsigned i = lineOffs;
252 while (isWhitespace(MB[i]))
254 indentSpace = MB.substr(lineOffs, i-lineOffs);
257 SmallVector<StringRef, 4> lines;
258 Str.split(lines, "\n");
260 for (unsigned i = 0, e = lines.size(); i != e; ++i) {
261 indentedStr += lines[i];
264 indentedStr += indentSpace;
267 Str = indentedStr.str();
270 getEditBuffer(FID).InsertText(StartOffs, Str, InsertAfter);
274 bool Rewriter::InsertTextAfterToken(SourceLocation Loc, StringRef Str) {
275 if (!isRewritable(Loc)) return true;
277 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
278 RewriteOptions rangeOpts;
279 rangeOpts.IncludeInsertsAtBeginOfRange = false;
280 StartOffs += getRangeSize(SourceRange(Loc, Loc), rangeOpts);
281 getEditBuffer(FID).InsertText(StartOffs, Str, /*InsertAfter*/true);
285 /// RemoveText - Remove the specified text region.
286 bool Rewriter::RemoveText(SourceLocation Start, unsigned Length,
287 RewriteOptions opts) {
288 if (!isRewritable(Start)) return true;
290 unsigned StartOffs = getLocationOffsetAndFileID(Start, FID);
291 getEditBuffer(FID).RemoveText(StartOffs, Length, opts.RemoveLineIfEmpty);
295 /// ReplaceText - This method replaces a range of characters in the input
296 /// buffer with a new string. This is effectively a combined "remove/insert"
298 bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
300 if (!isRewritable(Start)) return true;
302 unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
304 getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr);
308 bool Rewriter::ReplaceText(SourceRange range, SourceRange replacementRange) {
309 if (!isRewritable(range.getBegin())) return true;
310 if (!isRewritable(range.getEnd())) return true;
311 if (replacementRange.isInvalid()) return true;
312 SourceLocation start = range.getBegin();
313 unsigned origLength = getRangeSize(range);
314 unsigned newLength = getRangeSize(replacementRange);
316 unsigned newOffs = getLocationOffsetAndFileID(replacementRange.getBegin(),
318 StringRef MB = SourceMgr->getBufferData(FID);
319 return ReplaceText(start, origLength, MB.substr(newOffs, newLength));
322 /// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty
323 /// printer to generate the replacement code. This returns true if the input
324 /// could not be rewritten, or false if successful.
325 bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) {
326 // Measaure the old text.
327 int Size = getRangeSize(From->getSourceRange());
333 llvm::raw_string_ostream S(SStr);
334 To->printPretty(S, 0, PrintingPolicy(*LangOpts));
335 const std::string &Str = S.str();
337 ReplaceText(From->getLocStart(), Size, Str);
341 std::string Rewriter::ConvertToString(Stmt *From) {
343 llvm::raw_string_ostream S(SStr);
344 From->printPretty(S, 0, PrintingPolicy(*LangOpts));
348 bool Rewriter::IncreaseIndentation(CharSourceRange range,
349 SourceLocation parentIndent) {
350 if (range.isInvalid()) return true;
351 if (!isRewritable(range.getBegin())) return true;
352 if (!isRewritable(range.getEnd())) return true;
353 if (!isRewritable(parentIndent)) return true;
355 FileID StartFileID, EndFileID, parentFileID;
356 unsigned StartOff, EndOff, parentOff;
358 StartOff = getLocationOffsetAndFileID(range.getBegin(), StartFileID);
359 EndOff = getLocationOffsetAndFileID(range.getEnd(), EndFileID);
360 parentOff = getLocationOffsetAndFileID(parentIndent, parentFileID);
362 if (StartFileID != EndFileID || StartFileID != parentFileID)
364 if (StartOff > EndOff)
367 FileID FID = StartFileID;
368 StringRef MB = SourceMgr->getBufferData(FID);
370 unsigned parentLineNo = SourceMgr->getLineNumber(FID, parentOff) - 1;
371 unsigned startLineNo = SourceMgr->getLineNumber(FID, StartOff) - 1;
372 unsigned endLineNo = SourceMgr->getLineNumber(FID, EndOff) - 1;
374 const SrcMgr::ContentCache *
375 Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache();
377 // Find where the lines start.
378 unsigned parentLineOffs = Content->SourceLineCache[parentLineNo];
379 unsigned startLineOffs = Content->SourceLineCache[startLineNo];
381 // Find the whitespace at the start of each line.
382 StringRef parentSpace, startSpace;
384 unsigned i = parentLineOffs;
385 while (isWhitespace(MB[i]))
387 parentSpace = MB.substr(parentLineOffs, i-parentLineOffs);
390 while (isWhitespace(MB[i]))
392 startSpace = MB.substr(startLineOffs, i-startLineOffs);
394 if (parentSpace.size() >= startSpace.size())
396 if (!startSpace.startswith(parentSpace))
399 StringRef indent = startSpace.substr(parentSpace.size());
401 // Indent the lines between start/end offsets.
402 RewriteBuffer &RB = getEditBuffer(FID);
403 for (unsigned lineNo = startLineNo; lineNo <= endLineNo; ++lineNo) {
404 unsigned offs = Content->SourceLineCache[lineNo];
406 while (isWhitespace(MB[i]))
408 StringRef origIndent = MB.substr(offs, i-offs);
409 if (origIndent.startswith(startSpace))
410 RB.InsertText(offs, indent, /*InsertAfter=*/false);