blob: 43fb01bb1c345b229172fc76610de7c570dcf408 [file] [log] [blame]
Chris Lattner8bd12b82007-09-15 22:21:22 +00001//===--- Rewriter.cpp - Code rewriting interface --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8bd12b82007-09-15 22:21:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the Rewriter class, which is used for code
11// transformations.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Rewrite/Rewriter.h"
Chris Lattner01c57482007-10-17 22:35:30 +000016#include "clang/AST/Stmt.h"
Ted Kremenek2c3352b2008-10-07 23:08:39 +000017#include "clang/AST/Decl.h"
Chris Lattner9d62a5b2007-10-17 21:23:07 +000018#include "clang/Lex/Lexer.h"
Chris Lattner8a12c272007-10-11 18:38:32 +000019#include "clang/Basic/SourceManager.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000020#include "llvm/ADT/SmallString.h"
Chris Lattner8bd12b82007-09-15 22:21:22 +000021using namespace clang;
22
Chris Lattner5f9e2722011-07-23 10:55:15 +000023raw_ostream &RewriteBuffer::write(raw_ostream &os) const {
Nick Lewycky0ade8082010-04-16 18:49:45 +000024 // FIXME: eliminate the copy by writing out each chunk at a time
25 os << std::string(begin(), end());
26 return os;
27}
28
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +000029/// \brief Return true if this character is non-new-line whitespace:
30/// ' ', '\t', '\f', '\v', '\r'.
31static inline bool isWhitespace(unsigned char c) {
32 switch (c) {
33 case ' ':
34 case '\t':
35 case '\f':
36 case '\v':
37 case '\r':
38 return true;
39 default:
40 return false;
41 }
42}
43
44void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size,
45 bool removeLineIfEmpty) {
Chris Lattner7c239602007-10-13 00:11:23 +000046 // Nothing to remove, exit early.
47 if (Size == 0) return;
48
49 unsigned RealOffset = getMappedOffset(OrigOffset, true);
50 assert(RealOffset+Size < Buffer.size() && "Invalid location");
Mike Stump1eb44332009-09-09 15:08:12 +000051
Chris Lattner7c239602007-10-13 00:11:23 +000052 // Remove the dead characters.
Chris Lattnerfebe7192008-04-14 07:17:29 +000053 Buffer.erase(RealOffset, Size);
Chris Lattner7c239602007-10-13 00:11:23 +000054
55 // Add a delta so that future changes are offset correctly.
Eli Friedmana0978c22009-05-18 13:56:52 +000056 AddReplaceDelta(OrigOffset, -Size);
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +000057
58 if (removeLineIfEmpty) {
59 // Find the line that the remove occurred and if it is completely empty
60 // remove the line as well.
61
62 iterator curLineStart = begin();
63 unsigned curLineStartOffs = 0;
64 iterator posI = begin();
65 for (unsigned i = 0; i != RealOffset; ++i) {
66 if (*posI == '\n') {
67 curLineStart = posI;
68 ++curLineStart;
69 curLineStartOffs = i + 1;
70 }
71 ++posI;
72 }
73
74 unsigned lineSize = 0;
75 posI = curLineStart;
76 while (posI != end() && isWhitespace(*posI)) {
77 ++posI;
78 ++lineSize;
79 }
80 if (posI != end() && *posI == '\n') {
81 Buffer.erase(curLineStartOffs, lineSize + 1/* + '\n'*/);
82 AddReplaceDelta(curLineStartOffs, -(lineSize + 1/* + '\n'*/));
83 }
84 }
Chris Lattner8bd12b82007-09-15 22:21:22 +000085}
86
Chris Lattner5f9e2722011-07-23 10:55:15 +000087void RewriteBuffer::InsertText(unsigned OrigOffset, StringRef Str,
Ted Kremenek886c8db2008-03-18 21:17:59 +000088 bool InsertAfter) {
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner03b07102007-10-13 00:21:23 +000090 // Nothing to insert, exit early.
Daniel Dunbard7407dc2009-08-19 19:10:30 +000091 if (Str.empty()) return;
Chris Lattnerfebe7192008-04-14 07:17:29 +000092
Ted Kremenek886c8db2008-03-18 21:17:59 +000093 unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter);
Daniel Dunbard7407dc2009-08-19 19:10:30 +000094 Buffer.insert(RealOffset, Str.begin(), Str.end());
Mike Stump1eb44332009-09-09 15:08:12 +000095
Chris Lattner03b07102007-10-13 00:21:23 +000096 // Add a delta so that future changes are offset correctly.
Daniel Dunbard7407dc2009-08-19 19:10:30 +000097 AddInsertDelta(OrigOffset, Str.size());
Chris Lattner8bd12b82007-09-15 22:21:22 +000098}
Chris Lattner8a12c272007-10-11 18:38:32 +000099
Chris Lattner7c239602007-10-13 00:11:23 +0000100/// ReplaceText - This method replaces a range of characters in the input
Chris Lattnerfebe7192008-04-14 07:17:29 +0000101/// buffer with a new string. This is effectively a combined "remove+insert"
Chris Lattner7c239602007-10-13 00:11:23 +0000102/// operation.
103void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000104 StringRef NewStr) {
Eli Friedmana0978c22009-05-18 13:56:52 +0000105 unsigned RealOffset = getMappedOffset(OrigOffset, true);
Chris Lattnerfebe7192008-04-14 07:17:29 +0000106 Buffer.erase(RealOffset, OrigLength);
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000107 Buffer.insert(RealOffset, NewStr.begin(), NewStr.end());
108 if (OrigLength != NewStr.size())
109 AddReplaceDelta(OrigOffset, NewStr.size() - OrigLength);
Chris Lattner7c239602007-10-13 00:11:23 +0000110}
Chris Lattner8a12c272007-10-11 18:38:32 +0000111
112
113//===----------------------------------------------------------------------===//
114// Rewriter class
115//===----------------------------------------------------------------------===//
116
Chris Lattner311ff022007-10-16 22:36:42 +0000117/// getRangeSize - Return the size in bytes of the specified range if they
118/// are in the same file. If not, this returns -1.
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +0000119int Rewriter::getRangeSize(const CharSourceRange &Range,
Argyrios Kyrtzidisfd183ba2011-04-13 07:15:11 +0000120 RewriteOptions opts) const {
Chris Lattner311ff022007-10-16 22:36:42 +0000121 if (!isRewritable(Range.getBegin()) ||
122 !isRewritable(Range.getEnd())) return -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Chris Lattner2b2453a2009-01-17 06:22:33 +0000124 FileID StartFileID, EndFileID;
125 unsigned StartOff, EndOff;
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Chris Lattner311ff022007-10-16 22:36:42 +0000127 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
128 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Chris Lattner311ff022007-10-16 22:36:42 +0000130 if (StartFileID != EndFileID)
131 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Chris Lattnerd6690b22007-10-25 17:18:59 +0000133 // If edits have been made to this buffer, the delta between the range may
134 // have changed.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000135 std::map<FileID, RewriteBuffer>::const_iterator I =
Chris Lattner075eb6e2007-10-25 17:17:34 +0000136 RewriteBuffers.find(StartFileID);
Chris Lattnerd6690b22007-10-25 17:18:59 +0000137 if (I != RewriteBuffers.end()) {
Chris Lattner075eb6e2007-10-25 17:17:34 +0000138 const RewriteBuffer &RB = I->second;
Argyrios Kyrtzidisfd183ba2011-04-13 07:15:11 +0000139 EndOff = RB.getMappedOffset(EndOff, opts.IncludeInsertsAtEndOfRange);
140 StartOff = RB.getMappedOffset(StartOff, !opts.IncludeInsertsAtBeginOfRange);
Chris Lattner075eb6e2007-10-25 17:17:34 +0000141 }
142
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Chris Lattner9d62a5b2007-10-17 21:23:07 +0000144 // Adjust the end offset to the end of the last token, instead of being the
Chris Lattner0a76aae2010-06-18 22:45:06 +0000145 // start of the last token if this is a token range.
146 if (Range.isTokenRange())
147 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Chris Lattnerd6690b22007-10-25 17:18:59 +0000149 return EndOff-StartOff;
Chris Lattner311ff022007-10-16 22:36:42 +0000150}
151
Argyrios Kyrtzidisfd183ba2011-04-13 07:15:11 +0000152int Rewriter::getRangeSize(SourceRange Range, RewriteOptions opts) const {
153 return getRangeSize(CharSourceRange::getTokenRange(Range), opts);
Chris Lattner0a76aae2010-06-18 22:45:06 +0000154}
155
156
Ted Kremenek6a12a142010-01-07 18:00:35 +0000157/// getRewrittenText - Return the rewritten form of the text in the specified
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000158/// range. If the start or end of the range was unrewritable or if they are
Mike Stump1eb44332009-09-09 15:08:12 +0000159/// in different buffers, this returns an empty string.
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000160///
161/// Note that this method is not particularly efficient.
162///
Ted Kremenek6a12a142010-01-07 18:00:35 +0000163std::string Rewriter::getRewrittenText(SourceRange Range) const {
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000164 if (!isRewritable(Range.getBegin()) ||
165 !isRewritable(Range.getEnd()))
166 return "";
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Chris Lattner2b2453a2009-01-17 06:22:33 +0000168 FileID StartFileID, EndFileID;
169 unsigned StartOff, EndOff;
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000170 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
171 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000173 if (StartFileID != EndFileID)
174 return ""; // Start and end in different buffers.
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000176 // If edits have been made to this buffer, the delta between the range may
177 // have changed.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000178 std::map<FileID, RewriteBuffer>::const_iterator I =
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000179 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());
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000184 // Adjust the end offset to the end of the last token, instead of being the
185 // start of the last token.
Chris Lattner2c78b872009-04-14 23:22:57 +0000186 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000187 return std::string(Ptr, Ptr+EndOff-StartOff);
188 }
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000190 const RewriteBuffer &RB = I->second;
191 EndOff = RB.getMappedOffset(EndOff, true);
192 StartOff = RB.getMappedOffset(StartOff);
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000194 // Adjust the end offset to the end of the last token, instead of being the
195 // start of the last token.
Chris Lattner2c78b872009-04-14 23:22:57 +0000196 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000197
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);
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000204 return std::string(Start, End);
205}
Chris Lattner311ff022007-10-16 22:36:42 +0000206
Chris Lattner7c239602007-10-13 00:11:23 +0000207unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
Chris Lattner2b2453a2009-01-17 06:22:33 +0000208 FileID &FID) const {
Chris Lattner54bd7cb2008-05-28 16:35:02 +0000209 assert(Loc.isValid() && "Invalid location");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000210 std::pair<FileID,unsigned> V = SourceMgr->getDecomposedLoc(Loc);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000211 FID = V.first;
Chris Lattner7c239602007-10-13 00:11:23 +0000212 return V.second;
213}
214
215
Chris Lattner8a12c272007-10-11 18:38:32 +0000216/// getEditBuffer - Get or create a RewriteBuffer for the specified FileID.
217///
Chris Lattner2b2453a2009-01-17 06:22:33 +0000218RewriteBuffer &Rewriter::getEditBuffer(FileID FID) {
219 std::map<FileID, RewriteBuffer>::iterator I =
220 RewriteBuffers.lower_bound(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000221 if (I != RewriteBuffers.end() && I->first == FID)
Chris Lattner8a12c272007-10-11 18:38:32 +0000222 return I->second;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000223 I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer()));
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Chris Lattner5f9e2722011-07-23 10:55:15 +0000225 StringRef MB = SourceMgr->getBufferData(FID);
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000226 I->second.Initialize(MB.begin(), MB.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Chris Lattner8a12c272007-10-11 18:38:32 +0000228 return I->second;
229}
230
Chris Lattner54a2f072007-11-02 17:26:47 +0000231/// InsertText - Insert the specified string at the specified location in the
Chris Lattneraadaf782008-01-31 19:51:04 +0000232/// original buffer.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000233bool Rewriter::InsertText(SourceLocation Loc, StringRef Str,
John McCallf85e1932011-06-15 23:02:42 +0000234 bool InsertAfter, bool indentNewLines) {
Chris Lattnerdcbc5b02008-01-31 19:37:57 +0000235 if (!isRewritable(Loc)) return true;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000236 FileID FID;
237 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
John McCallf85e1932011-06-15 23:02:42 +0000238
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000239 SmallString<128> indentedStr;
John McCallf85e1932011-06-15 23:02:42 +0000240 if (indentNewLines && Str.find('\n') != StringRef::npos) {
241 StringRef MB = SourceMgr->getBufferData(FID);
242
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];
247
248 // Find the whitespace at the start of the line.
249 StringRef indentSpace;
250 {
251 unsigned i = lineOffs;
252 while (isWhitespace(MB[i]))
253 ++i;
254 indentSpace = MB.substr(lineOffs, i-lineOffs);
255 }
256
Chris Lattner5f9e2722011-07-23 10:55:15 +0000257 SmallVector<StringRef, 4> lines;
John McCallf85e1932011-06-15 23:02:42 +0000258 Str.split(lines, "\n");
259
260 for (unsigned i = 0, e = lines.size(); i != e; ++i) {
261 indentedStr += lines[i];
262 if (i < e-1) {
263 indentedStr += '\n';
264 indentedStr += indentSpace;
265 }
266 }
267 Str = indentedStr.str();
268 }
269
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000270 getEditBuffer(FID).InsertText(StartOffs, Str, InsertAfter);
Chris Lattnerdcbc5b02008-01-31 19:37:57 +0000271 return false;
Chris Lattner54a2f072007-11-02 17:26:47 +0000272}
273
Chris Lattner5f9e2722011-07-23 10:55:15 +0000274bool Rewriter::InsertTextAfterToken(SourceLocation Loc, StringRef Str) {
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +0000275 if (!isRewritable(Loc)) return true;
276 FileID FID;
277 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
Argyrios Kyrtzidisfd183ba2011-04-13 07:15:11 +0000278 RewriteOptions rangeOpts;
279 rangeOpts.IncludeInsertsAtBeginOfRange = false;
280 StartOffs += getRangeSize(SourceRange(Loc, Loc), rangeOpts);
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +0000281 getEditBuffer(FID).InsertText(StartOffs, Str, /*InsertAfter*/true);
282 return false;
283}
284
Chris Lattneraadaf782008-01-31 19:51:04 +0000285/// RemoveText - Remove the specified text region.
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +0000286bool Rewriter::RemoveText(SourceLocation Start, unsigned Length,
Argyrios Kyrtzidisfd183ba2011-04-13 07:15:11 +0000287 RewriteOptions opts) {
Chris Lattneraadaf782008-01-31 19:51:04 +0000288 if (!isRewritable(Start)) return true;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000289 FileID FID;
290 unsigned StartOffs = getLocationOffsetAndFileID(Start, FID);
Argyrios Kyrtzidisfd183ba2011-04-13 07:15:11 +0000291 getEditBuffer(FID).RemoveText(StartOffs, Length, opts.RemoveLineIfEmpty);
Chris Lattneraadaf782008-01-31 19:51:04 +0000292 return false;
Chris Lattner674af952007-10-16 22:51:17 +0000293}
Chris Lattner8a12c272007-10-11 18:38:32 +0000294
Chris Lattner674af952007-10-16 22:51:17 +0000295/// ReplaceText - This method replaces a range of characters in the input
296/// buffer with a new string. This is effectively a combined "remove/insert"
297/// operation.
Chris Lattneraadaf782008-01-31 19:51:04 +0000298bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000299 StringRef NewStr) {
Chris Lattneraadaf782008-01-31 19:51:04 +0000300 if (!isRewritable(Start)) return true;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000301 FileID StartFileID;
Chris Lattner7c239602007-10-13 00:11:23 +0000302 unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000304 getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr);
Chris Lattneraadaf782008-01-31 19:51:04 +0000305 return false;
Chris Lattner8a12c272007-10-11 18:38:32 +0000306}
Chris Lattner01c57482007-10-17 22:35:30 +0000307
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +0000308bool 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);
315 FileID FID;
316 unsigned newOffs = getLocationOffsetAndFileID(replacementRange.getBegin(),
317 FID);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000318 StringRef MB = SourceMgr->getBufferData(FID);
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +0000319 return ReplaceText(start, origLength, MB.substr(newOffs, newLength));
320}
321
Chris Lattner01c57482007-10-17 22:35:30 +0000322/// 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.
Fariborz Jahanian88906cd2010-02-05 16:43:40 +0000325bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) {
Chris Lattner01c57482007-10-17 22:35:30 +0000326 // Measaure the old text.
Fariborz Jahanian88906cd2010-02-05 16:43:40 +0000327 int Size = getRangeSize(From->getSourceRange());
Chris Lattner01c57482007-10-17 22:35:30 +0000328 if (Size == -1)
329 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Chris Lattner01c57482007-10-17 22:35:30 +0000331 // Get the new text.
Ted Kremeneka95d3752008-09-13 05:16:45 +0000332 std::string SStr;
333 llvm::raw_string_ostream S(SStr);
Chris Lattnere4f21422009-06-30 01:26:17 +0000334 To->printPretty(S, 0, PrintingPolicy(*LangOpts));
Chris Lattner01c57482007-10-17 22:35:30 +0000335 const std::string &Str = S.str();
336
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000337 ReplaceText(From->getLocStart(), Size, Str);
Chris Lattner01c57482007-10-17 22:35:30 +0000338 return false;
339}
Fariborz Jahanian1d015312011-04-11 21:17:02 +0000340
341std::string Rewriter::ConvertToString(Stmt *From) {
342 std::string SStr;
343 llvm::raw_string_ostream S(SStr);
344 From->printPretty(S, 0, PrintingPolicy(*LangOpts));
Fariborz Jahanian85b1c7c2011-04-20 16:38:37 +0000345 return S.str();
Fariborz Jahanian1d015312011-04-11 21:17:02 +0000346}
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000347
348bool Rewriter::IncreaseIndentation(CharSourceRange range,
349 SourceLocation parentIndent) {
John McCallf85e1932011-06-15 23:02:42 +0000350 if (range.isInvalid()) return true;
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000351 if (!isRewritable(range.getBegin())) return true;
352 if (!isRewritable(range.getEnd())) return true;
353 if (!isRewritable(parentIndent)) return true;
354
355 FileID StartFileID, EndFileID, parentFileID;
356 unsigned StartOff, EndOff, parentOff;
357
358 StartOff = getLocationOffsetAndFileID(range.getBegin(), StartFileID);
359 EndOff = getLocationOffsetAndFileID(range.getEnd(), EndFileID);
360 parentOff = getLocationOffsetAndFileID(parentIndent, parentFileID);
361
362 if (StartFileID != EndFileID || StartFileID != parentFileID)
363 return true;
John McCallf85e1932011-06-15 23:02:42 +0000364 if (StartOff > EndOff)
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000365 return true;
366
367 FileID FID = StartFileID;
368 StringRef MB = SourceMgr->getBufferData(FID);
369
370 unsigned parentLineNo = SourceMgr->getLineNumber(FID, parentOff) - 1;
371 unsigned startLineNo = SourceMgr->getLineNumber(FID, StartOff) - 1;
372 unsigned endLineNo = SourceMgr->getLineNumber(FID, EndOff) - 1;
373
374 const SrcMgr::ContentCache *
375 Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache();
376
John McCallf85e1932011-06-15 23:02:42 +0000377 // Find where the lines start.
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000378 unsigned parentLineOffs = Content->SourceLineCache[parentLineNo];
379 unsigned startLineOffs = Content->SourceLineCache[startLineNo];
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000380
381 // Find the whitespace at the start of each line.
John McCallf85e1932011-06-15 23:02:42 +0000382 StringRef parentSpace, startSpace;
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000383 {
384 unsigned i = parentLineOffs;
385 while (isWhitespace(MB[i]))
386 ++i;
387 parentSpace = MB.substr(parentLineOffs, i-parentLineOffs);
388
389 i = startLineOffs;
390 while (isWhitespace(MB[i]))
391 ++i;
392 startSpace = MB.substr(startLineOffs, i-startLineOffs);
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000393 }
394 if (parentSpace.size() >= startSpace.size())
395 return true;
396 if (!startSpace.startswith(parentSpace))
397 return true;
398
Chris Lattner5f9e2722011-07-23 10:55:15 +0000399 StringRef indent = startSpace.substr(parentSpace.size());
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000400
401 // Indent the lines between start/end offsets.
402 RewriteBuffer &RB = getEditBuffer(FID);
John McCallf85e1932011-06-15 23:02:42 +0000403 for (unsigned lineNo = startLineNo; lineNo <= endLineNo; ++lineNo) {
404 unsigned offs = Content->SourceLineCache[lineNo];
405 unsigned i = offs;
406 while (isWhitespace(MB[i]))
407 ++i;
408 StringRef origIndent = MB.substr(offs, i-offs);
409 if (origIndent.startswith(startSpace))
410 RB.InsertText(offs, indent, /*InsertAfter=*/false);
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000411 }
412
413 return false;
414}