blob: 464b299ccdb00eef7e06d5006f73a3b15dbf3a8b [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"
Chris Lattner8bd12b82007-09-15 22:21:22 +000020using namespace clang;
21
Chris Lattner5f9e2722011-07-23 10:55:15 +000022raw_ostream &RewriteBuffer::write(raw_ostream &os) const {
Nick Lewycky0ade8082010-04-16 18:49:45 +000023 // FIXME: eliminate the copy by writing out each chunk at a time
24 os << std::string(begin(), end());
25 return os;
26}
27
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +000028/// \brief Return true if this character is non-new-line whitespace:
29/// ' ', '\t', '\f', '\v', '\r'.
30static inline bool isWhitespace(unsigned char c) {
31 switch (c) {
32 case ' ':
33 case '\t':
34 case '\f':
35 case '\v':
36 case '\r':
37 return true;
38 default:
39 return false;
40 }
41}
42
43void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size,
44 bool removeLineIfEmpty) {
Chris Lattner7c239602007-10-13 00:11:23 +000045 // Nothing to remove, exit early.
46 if (Size == 0) return;
47
48 unsigned RealOffset = getMappedOffset(OrigOffset, true);
49 assert(RealOffset+Size < Buffer.size() && "Invalid location");
Mike Stump1eb44332009-09-09 15:08:12 +000050
Chris Lattner7c239602007-10-13 00:11:23 +000051 // Remove the dead characters.
Chris Lattnerfebe7192008-04-14 07:17:29 +000052 Buffer.erase(RealOffset, Size);
Chris Lattner7c239602007-10-13 00:11:23 +000053
54 // Add a delta so that future changes are offset correctly.
Eli Friedmana0978c22009-05-18 13:56:52 +000055 AddReplaceDelta(OrigOffset, -Size);
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +000056
57 if (removeLineIfEmpty) {
58 // Find the line that the remove occurred and if it is completely empty
59 // remove the line as well.
60
61 iterator curLineStart = begin();
62 unsigned curLineStartOffs = 0;
63 iterator posI = begin();
64 for (unsigned i = 0; i != RealOffset; ++i) {
65 if (*posI == '\n') {
66 curLineStart = posI;
67 ++curLineStart;
68 curLineStartOffs = i + 1;
69 }
70 ++posI;
71 }
72
73 unsigned lineSize = 0;
74 posI = curLineStart;
75 while (posI != end() && isWhitespace(*posI)) {
76 ++posI;
77 ++lineSize;
78 }
79 if (posI != end() && *posI == '\n') {
80 Buffer.erase(curLineStartOffs, lineSize + 1/* + '\n'*/);
81 AddReplaceDelta(curLineStartOffs, -(lineSize + 1/* + '\n'*/));
82 }
83 }
Chris Lattner8bd12b82007-09-15 22:21:22 +000084}
85
Chris Lattner5f9e2722011-07-23 10:55:15 +000086void RewriteBuffer::InsertText(unsigned OrigOffset, StringRef Str,
Ted Kremenek886c8db2008-03-18 21:17:59 +000087 bool InsertAfter) {
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner03b07102007-10-13 00:21:23 +000089 // Nothing to insert, exit early.
Daniel Dunbard7407dc2009-08-19 19:10:30 +000090 if (Str.empty()) return;
Chris Lattnerfebe7192008-04-14 07:17:29 +000091
Ted Kremenek886c8db2008-03-18 21:17:59 +000092 unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter);
Daniel Dunbard7407dc2009-08-19 19:10:30 +000093 Buffer.insert(RealOffset, Str.begin(), Str.end());
Mike Stump1eb44332009-09-09 15:08:12 +000094
Chris Lattner03b07102007-10-13 00:21:23 +000095 // Add a delta so that future changes are offset correctly.
Daniel Dunbard7407dc2009-08-19 19:10:30 +000096 AddInsertDelta(OrigOffset, Str.size());
Chris Lattner8bd12b82007-09-15 22:21:22 +000097}
Chris Lattner8a12c272007-10-11 18:38:32 +000098
Chris Lattner7c239602007-10-13 00:11:23 +000099/// ReplaceText - This method replaces a range of characters in the input
Chris Lattnerfebe7192008-04-14 07:17:29 +0000100/// buffer with a new string. This is effectively a combined "remove+insert"
Chris Lattner7c239602007-10-13 00:11:23 +0000101/// operation.
102void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000103 StringRef NewStr) {
Eli Friedmana0978c22009-05-18 13:56:52 +0000104 unsigned RealOffset = getMappedOffset(OrigOffset, true);
Chris Lattnerfebe7192008-04-14 07:17:29 +0000105 Buffer.erase(RealOffset, OrigLength);
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000106 Buffer.insert(RealOffset, NewStr.begin(), NewStr.end());
107 if (OrigLength != NewStr.size())
108 AddReplaceDelta(OrigOffset, NewStr.size() - OrigLength);
Chris Lattner7c239602007-10-13 00:11:23 +0000109}
Chris Lattner8a12c272007-10-11 18:38:32 +0000110
111
112//===----------------------------------------------------------------------===//
113// Rewriter class
114//===----------------------------------------------------------------------===//
115
Chris Lattner311ff022007-10-16 22:36:42 +0000116/// getRangeSize - Return the size in bytes of the specified range if they
117/// are in the same file. If not, this returns -1.
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +0000118int Rewriter::getRangeSize(const CharSourceRange &Range,
Argyrios Kyrtzidisfd183ba2011-04-13 07:15:11 +0000119 RewriteOptions opts) const {
Chris Lattner311ff022007-10-16 22:36:42 +0000120 if (!isRewritable(Range.getBegin()) ||
121 !isRewritable(Range.getEnd())) return -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Chris Lattner2b2453a2009-01-17 06:22:33 +0000123 FileID StartFileID, EndFileID;
124 unsigned StartOff, EndOff;
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Chris Lattner311ff022007-10-16 22:36:42 +0000126 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
127 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Chris Lattner311ff022007-10-16 22:36:42 +0000129 if (StartFileID != EndFileID)
130 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Chris Lattnerd6690b22007-10-25 17:18:59 +0000132 // If edits have been made to this buffer, the delta between the range may
133 // have changed.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000134 std::map<FileID, RewriteBuffer>::const_iterator I =
Chris Lattner075eb6e2007-10-25 17:17:34 +0000135 RewriteBuffers.find(StartFileID);
Chris Lattnerd6690b22007-10-25 17:18:59 +0000136 if (I != RewriteBuffers.end()) {
Chris Lattner075eb6e2007-10-25 17:17:34 +0000137 const RewriteBuffer &RB = I->second;
Argyrios Kyrtzidisfd183ba2011-04-13 07:15:11 +0000138 EndOff = RB.getMappedOffset(EndOff, opts.IncludeInsertsAtEndOfRange);
139 StartOff = RB.getMappedOffset(StartOff, !opts.IncludeInsertsAtBeginOfRange);
Chris Lattner075eb6e2007-10-25 17:17:34 +0000140 }
141
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Chris Lattner9d62a5b2007-10-17 21:23:07 +0000143 // Adjust the end offset to the end of the last token, instead of being the
Chris Lattner0a76aae2010-06-18 22:45:06 +0000144 // start of the last token if this is a token range.
145 if (Range.isTokenRange())
146 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Chris Lattnerd6690b22007-10-25 17:18:59 +0000148 return EndOff-StartOff;
Chris Lattner311ff022007-10-16 22:36:42 +0000149}
150
Argyrios Kyrtzidisfd183ba2011-04-13 07:15:11 +0000151int Rewriter::getRangeSize(SourceRange Range, RewriteOptions opts) const {
152 return getRangeSize(CharSourceRange::getTokenRange(Range), opts);
Chris Lattner0a76aae2010-06-18 22:45:06 +0000153}
154
155
Ted Kremenek6a12a142010-01-07 18:00:35 +0000156/// getRewrittenText - Return the rewritten form of the text in the specified
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000157/// range. If the start or end of the range was unrewritable or if they are
Mike Stump1eb44332009-09-09 15:08:12 +0000158/// in different buffers, this returns an empty string.
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000159///
160/// Note that this method is not particularly efficient.
161///
Ted Kremenek6a12a142010-01-07 18:00:35 +0000162std::string Rewriter::getRewrittenText(SourceRange Range) const {
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000163 if (!isRewritable(Range.getBegin()) ||
164 !isRewritable(Range.getEnd()))
165 return "";
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Chris Lattner2b2453a2009-01-17 06:22:33 +0000167 FileID StartFileID, EndFileID;
168 unsigned StartOff, EndOff;
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000169 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
170 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000172 if (StartFileID != EndFileID)
173 return ""; // Start and end in different buffers.
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000175 // If edits have been made to this buffer, the delta between the range may
176 // have changed.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000177 std::map<FileID, RewriteBuffer>::const_iterator I =
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000178 RewriteBuffers.find(StartFileID);
179 if (I == RewriteBuffers.end()) {
180 // If the buffer hasn't been rewritten, just return the text from the input.
181 const char *Ptr = SourceMgr->getCharacterData(Range.getBegin());
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000183 // Adjust the end offset to the end of the last token, instead of being the
184 // start of the last token.
Chris Lattner2c78b872009-04-14 23:22:57 +0000185 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000186 return std::string(Ptr, Ptr+EndOff-StartOff);
187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000189 const RewriteBuffer &RB = I->second;
190 EndOff = RB.getMappedOffset(EndOff, true);
191 StartOff = RB.getMappedOffset(StartOff);
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000193 // Adjust the end offset to the end of the last token, instead of being the
194 // start of the last token.
Chris Lattner2c78b872009-04-14 23:22:57 +0000195 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000196
197 // Advance the iterators to the right spot, yay for linear time algorithms.
198 RewriteBuffer::iterator Start = RB.begin();
199 std::advance(Start, StartOff);
200 RewriteBuffer::iterator End = Start;
201 std::advance(End, EndOff-StartOff);
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Chris Lattnerb5cd09a2008-10-03 23:31:16 +0000203 return std::string(Start, End);
204}
Chris Lattner311ff022007-10-16 22:36:42 +0000205
Chris Lattner7c239602007-10-13 00:11:23 +0000206unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
Chris Lattner2b2453a2009-01-17 06:22:33 +0000207 FileID &FID) const {
Chris Lattner54bd7cb2008-05-28 16:35:02 +0000208 assert(Loc.isValid() && "Invalid location");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000209 std::pair<FileID,unsigned> V = SourceMgr->getDecomposedLoc(Loc);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000210 FID = V.first;
Chris Lattner7c239602007-10-13 00:11:23 +0000211 return V.second;
212}
213
214
Chris Lattner8a12c272007-10-11 18:38:32 +0000215/// getEditBuffer - Get or create a RewriteBuffer for the specified FileID.
216///
Chris Lattner2b2453a2009-01-17 06:22:33 +0000217RewriteBuffer &Rewriter::getEditBuffer(FileID FID) {
218 std::map<FileID, RewriteBuffer>::iterator I =
219 RewriteBuffers.lower_bound(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000220 if (I != RewriteBuffers.end() && I->first == FID)
Chris Lattner8a12c272007-10-11 18:38:32 +0000221 return I->second;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000222 I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer()));
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Chris Lattner5f9e2722011-07-23 10:55:15 +0000224 StringRef MB = SourceMgr->getBufferData(FID);
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000225 I->second.Initialize(MB.begin(), MB.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Chris Lattner8a12c272007-10-11 18:38:32 +0000227 return I->second;
228}
229
Chris Lattner54a2f072007-11-02 17:26:47 +0000230/// InsertText - Insert the specified string at the specified location in the
Chris Lattneraadaf782008-01-31 19:51:04 +0000231/// original buffer.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000232bool Rewriter::InsertText(SourceLocation Loc, StringRef Str,
John McCallf85e1932011-06-15 23:02:42 +0000233 bool InsertAfter, bool indentNewLines) {
Chris Lattnerdcbc5b02008-01-31 19:37:57 +0000234 if (!isRewritable(Loc)) return true;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000235 FileID FID;
236 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
John McCallf85e1932011-06-15 23:02:42 +0000237
238 llvm::SmallString<128> indentedStr;
239 if (indentNewLines && Str.find('\n') != StringRef::npos) {
240 StringRef MB = SourceMgr->getBufferData(FID);
241
242 unsigned lineNo = SourceMgr->getLineNumber(FID, StartOffs) - 1;
243 const SrcMgr::ContentCache *
244 Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache();
245 unsigned lineOffs = Content->SourceLineCache[lineNo];
246
247 // Find the whitespace at the start of the line.
248 StringRef indentSpace;
249 {
250 unsigned i = lineOffs;
251 while (isWhitespace(MB[i]))
252 ++i;
253 indentSpace = MB.substr(lineOffs, i-lineOffs);
254 }
255
Chris Lattner5f9e2722011-07-23 10:55:15 +0000256 SmallVector<StringRef, 4> lines;
John McCallf85e1932011-06-15 23:02:42 +0000257 Str.split(lines, "\n");
258
259 for (unsigned i = 0, e = lines.size(); i != e; ++i) {
260 indentedStr += lines[i];
261 if (i < e-1) {
262 indentedStr += '\n';
263 indentedStr += indentSpace;
264 }
265 }
266 Str = indentedStr.str();
267 }
268
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000269 getEditBuffer(FID).InsertText(StartOffs, Str, InsertAfter);
Chris Lattnerdcbc5b02008-01-31 19:37:57 +0000270 return false;
Chris Lattner54a2f072007-11-02 17:26:47 +0000271}
272
Chris Lattner5f9e2722011-07-23 10:55:15 +0000273bool Rewriter::InsertTextAfterToken(SourceLocation Loc, StringRef Str) {
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +0000274 if (!isRewritable(Loc)) return true;
275 FileID FID;
276 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
Argyrios Kyrtzidisfd183ba2011-04-13 07:15:11 +0000277 RewriteOptions rangeOpts;
278 rangeOpts.IncludeInsertsAtBeginOfRange = false;
279 StartOffs += getRangeSize(SourceRange(Loc, Loc), rangeOpts);
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +0000280 getEditBuffer(FID).InsertText(StartOffs, Str, /*InsertAfter*/true);
281 return false;
282}
283
Chris Lattneraadaf782008-01-31 19:51:04 +0000284/// RemoveText - Remove the specified text region.
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +0000285bool Rewriter::RemoveText(SourceLocation Start, unsigned Length,
Argyrios Kyrtzidisfd183ba2011-04-13 07:15:11 +0000286 RewriteOptions opts) {
Chris Lattneraadaf782008-01-31 19:51:04 +0000287 if (!isRewritable(Start)) return true;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000288 FileID FID;
289 unsigned StartOffs = getLocationOffsetAndFileID(Start, FID);
Argyrios Kyrtzidisfd183ba2011-04-13 07:15:11 +0000290 getEditBuffer(FID).RemoveText(StartOffs, Length, opts.RemoveLineIfEmpty);
Chris Lattneraadaf782008-01-31 19:51:04 +0000291 return false;
Chris Lattner674af952007-10-16 22:51:17 +0000292}
Chris Lattner8a12c272007-10-11 18:38:32 +0000293
Chris Lattner674af952007-10-16 22:51:17 +0000294/// ReplaceText - This method replaces a range of characters in the input
295/// buffer with a new string. This is effectively a combined "remove/insert"
296/// operation.
Chris Lattneraadaf782008-01-31 19:51:04 +0000297bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000298 StringRef NewStr) {
Chris Lattneraadaf782008-01-31 19:51:04 +0000299 if (!isRewritable(Start)) return true;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000300 FileID StartFileID;
Chris Lattner7c239602007-10-13 00:11:23 +0000301 unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000303 getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr);
Chris Lattneraadaf782008-01-31 19:51:04 +0000304 return false;
Chris Lattner8a12c272007-10-11 18:38:32 +0000305}
Chris Lattner01c57482007-10-17 22:35:30 +0000306
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +0000307bool Rewriter::ReplaceText(SourceRange range, SourceRange replacementRange) {
308 if (!isRewritable(range.getBegin())) return true;
309 if (!isRewritable(range.getEnd())) return true;
310 if (replacementRange.isInvalid()) return true;
311 SourceLocation start = range.getBegin();
312 unsigned origLength = getRangeSize(range);
313 unsigned newLength = getRangeSize(replacementRange);
314 FileID FID;
315 unsigned newOffs = getLocationOffsetAndFileID(replacementRange.getBegin(),
316 FID);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000317 StringRef MB = SourceMgr->getBufferData(FID);
Argyrios Kyrtzidisb65ed342011-04-07 18:10:12 +0000318 return ReplaceText(start, origLength, MB.substr(newOffs, newLength));
319}
320
Chris Lattner01c57482007-10-17 22:35:30 +0000321/// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty
322/// printer to generate the replacement code. This returns true if the input
323/// could not be rewritten, or false if successful.
Fariborz Jahanian88906cd2010-02-05 16:43:40 +0000324bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) {
Chris Lattner01c57482007-10-17 22:35:30 +0000325 // Measaure the old text.
Fariborz Jahanian88906cd2010-02-05 16:43:40 +0000326 int Size = getRangeSize(From->getSourceRange());
Chris Lattner01c57482007-10-17 22:35:30 +0000327 if (Size == -1)
328 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Chris Lattner01c57482007-10-17 22:35:30 +0000330 // Get the new text.
Ted Kremeneka95d3752008-09-13 05:16:45 +0000331 std::string SStr;
332 llvm::raw_string_ostream S(SStr);
Chris Lattnere4f21422009-06-30 01:26:17 +0000333 To->printPretty(S, 0, PrintingPolicy(*LangOpts));
Chris Lattner01c57482007-10-17 22:35:30 +0000334 const std::string &Str = S.str();
335
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000336 ReplaceText(From->getLocStart(), Size, Str);
Chris Lattner01c57482007-10-17 22:35:30 +0000337 return false;
338}
Fariborz Jahanian1d015312011-04-11 21:17:02 +0000339
340std::string Rewriter::ConvertToString(Stmt *From) {
341 std::string SStr;
342 llvm::raw_string_ostream S(SStr);
343 From->printPretty(S, 0, PrintingPolicy(*LangOpts));
Fariborz Jahanian85b1c7c2011-04-20 16:38:37 +0000344 return S.str();
Fariborz Jahanian1d015312011-04-11 21:17:02 +0000345}
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000346
347bool Rewriter::IncreaseIndentation(CharSourceRange range,
348 SourceLocation parentIndent) {
John McCallf85e1932011-06-15 23:02:42 +0000349 if (range.isInvalid()) return true;
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000350 if (!isRewritable(range.getBegin())) return true;
351 if (!isRewritable(range.getEnd())) return true;
352 if (!isRewritable(parentIndent)) return true;
353
354 FileID StartFileID, EndFileID, parentFileID;
355 unsigned StartOff, EndOff, parentOff;
356
357 StartOff = getLocationOffsetAndFileID(range.getBegin(), StartFileID);
358 EndOff = getLocationOffsetAndFileID(range.getEnd(), EndFileID);
359 parentOff = getLocationOffsetAndFileID(parentIndent, parentFileID);
360
361 if (StartFileID != EndFileID || StartFileID != parentFileID)
362 return true;
John McCallf85e1932011-06-15 23:02:42 +0000363 if (StartOff > EndOff)
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000364 return true;
365
366 FileID FID = StartFileID;
367 StringRef MB = SourceMgr->getBufferData(FID);
368
369 unsigned parentLineNo = SourceMgr->getLineNumber(FID, parentOff) - 1;
370 unsigned startLineNo = SourceMgr->getLineNumber(FID, StartOff) - 1;
371 unsigned endLineNo = SourceMgr->getLineNumber(FID, EndOff) - 1;
372
373 const SrcMgr::ContentCache *
374 Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache();
375
John McCallf85e1932011-06-15 23:02:42 +0000376 // Find where the lines start.
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000377 unsigned parentLineOffs = Content->SourceLineCache[parentLineNo];
378 unsigned startLineOffs = Content->SourceLineCache[startLineNo];
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000379
380 // Find the whitespace at the start of each line.
John McCallf85e1932011-06-15 23:02:42 +0000381 StringRef parentSpace, startSpace;
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000382 {
383 unsigned i = parentLineOffs;
384 while (isWhitespace(MB[i]))
385 ++i;
386 parentSpace = MB.substr(parentLineOffs, i-parentLineOffs);
387
388 i = startLineOffs;
389 while (isWhitespace(MB[i]))
390 ++i;
391 startSpace = MB.substr(startLineOffs, i-startLineOffs);
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000392 }
393 if (parentSpace.size() >= startSpace.size())
394 return true;
395 if (!startSpace.startswith(parentSpace))
396 return true;
397
Chris Lattner5f9e2722011-07-23 10:55:15 +0000398 StringRef indent = startSpace.substr(parentSpace.size());
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000399
400 // Indent the lines between start/end offsets.
401 RewriteBuffer &RB = getEditBuffer(FID);
John McCallf85e1932011-06-15 23:02:42 +0000402 for (unsigned lineNo = startLineNo; lineNo <= endLineNo; ++lineNo) {
403 unsigned offs = Content->SourceLineCache[lineNo];
404 unsigned i = offs;
405 while (isWhitespace(MB[i]))
406 ++i;
407 StringRef origIndent = MB.substr(offs, i-offs);
408 if (origIndent.startswith(startSpace))
409 RB.InsertText(offs, indent, /*InsertAfter=*/false);
Argyrios Kyrtzidis10c8d9e2011-04-16 01:03:33 +0000410 }
411
412 return false;
413}