blob: 514b82235a47605a64abf1eb23ff16726c4a9246 [file] [log] [blame]
Chris Lattner8bd12b82007-09-15 22:21:22 +00001//===--- Rewriter.cpp - Code rewriting interface --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source 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#include "clang/Rewrite/Rewriter.h"
Chris Lattner9d62a5b2007-10-17 21:23:07 +000016#include "clang/Lex/Lexer.h"
Chris Lattner8a12c272007-10-11 18:38:32 +000017#include "clang/Basic/SourceManager.h"
Chris Lattner8bd12b82007-09-15 22:21:22 +000018using namespace clang;
19
Chris Lattner7c239602007-10-13 00:11:23 +000020/// getMappedOffset - Given an offset into the original SourceBuffer that this
21/// RewriteBuffer is based on, map it into the offset space of the
22/// RewriteBuffer.
23unsigned RewriteBuffer::getMappedOffset(unsigned OrigOffset,
24 bool AfterInserts) const {
25 unsigned ResultOffset = OrigOffset;
26 unsigned DeltaIdx = 0;
27
28 // Move past any deltas that are relevant.
29 // FIXME: binary search.
30 for (; DeltaIdx != Deltas.size() &&
Chris Lattner88d0ed02007-10-13 00:17:04 +000031 Deltas[DeltaIdx].FileLoc < OrigOffset; ++DeltaIdx)
Chris Lattner7c239602007-10-13 00:11:23 +000032 ResultOffset += Deltas[DeltaIdx].Delta;
33
34 if (AfterInserts && DeltaIdx != Deltas.size() &&
35 OrigOffset == Deltas[DeltaIdx].FileLoc)
36 ResultOffset += Deltas[DeltaIdx].Delta;
37 return ResultOffset;
38}
39
40/// AddDelta - When a change is made that shifts around the text buffer, this
41/// method is used to record that info.
42void RewriteBuffer::AddDelta(unsigned OrigOffset, int Change) {
43 assert(Change != 0 && "Not changing anything");
44 unsigned DeltaIdx = 0;
45
46 // Skip over any unrelated deltas.
47 for (; DeltaIdx != Deltas.size() &&
Chris Lattner88d0ed02007-10-13 00:17:04 +000048 Deltas[DeltaIdx].FileLoc < OrigOffset; ++DeltaIdx)
Chris Lattner7c239602007-10-13 00:11:23 +000049 ;
50
51 // If there is no a delta for this offset, insert a new delta record.
52 if (DeltaIdx == Deltas.size() || OrigOffset != Deltas[DeltaIdx].FileLoc) {
53 // If this is a removal, check to see if this can be folded into
54 // a delta at the end of the deletion. For example, if we have:
55 // ABCXDEF (X inserted after C) and delete C, we want to end up with no
56 // delta because X basically replaced C.
57 if (Change < 0 && DeltaIdx != Deltas.size() &&
58 OrigOffset-Change == Deltas[DeltaIdx].FileLoc) {
59 // Adjust the start of the delta to be the start of the deleted region.
60 Deltas[DeltaIdx].FileLoc += Change;
61 Deltas[DeltaIdx].Delta += Change;
62
63 // If the delta becomes a noop, remove it.
64 if (Deltas[DeltaIdx].Delta == 0)
65 Deltas.erase(Deltas.begin()+DeltaIdx);
66 return;
67 }
68
69 // Otherwise, create an entry and return.
70 Deltas.insert(Deltas.begin()+DeltaIdx,
71 SourceDelta::get(OrigOffset, Change));
72 return;
73 }
74
75 // Otherwise, we found a delta record at this offset, adjust it.
76 Deltas[DeltaIdx].Delta += Change;
77
78 // If it is now dead, remove it.
79 if (Deltas[DeltaIdx].Delta)
80 Deltas.erase(Deltas.begin()+DeltaIdx);
81}
82
Chris Lattner8bd12b82007-09-15 22:21:22 +000083
84void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size) {
Chris Lattner7c239602007-10-13 00:11:23 +000085 // Nothing to remove, exit early.
86 if (Size == 0) return;
87
88 unsigned RealOffset = getMappedOffset(OrigOffset, true);
89 assert(RealOffset+Size < Buffer.size() && "Invalid location");
90
91 // Remove the dead characters.
92 Buffer.erase(Buffer.begin()+RealOffset, Buffer.begin()+RealOffset+Size);
93
94 // Add a delta so that future changes are offset correctly.
95 AddDelta(OrigOffset, -Size);
Chris Lattner8bd12b82007-09-15 22:21:22 +000096}
97
98void RewriteBuffer::InsertText(unsigned OrigOffset,
99 const char *StrData, unsigned StrLen) {
Chris Lattner03b07102007-10-13 00:21:23 +0000100 // Nothing to insert, exit early.
Chris Lattner7c239602007-10-13 00:11:23 +0000101 if (StrLen == 0) return;
Chris Lattner03b07102007-10-13 00:21:23 +0000102
103 unsigned RealOffset = getMappedOffset(OrigOffset, true);
104 assert(RealOffset <= Buffer.size() && "Invalid location");
105
106 // Remove the dead characters.
107 Buffer.insert(Buffer.begin()+RealOffset, StrData, StrData+StrLen);
108
109 // Add a delta so that future changes are offset correctly.
110 AddDelta(OrigOffset, StrLen);
Chris Lattner8bd12b82007-09-15 22:21:22 +0000111}
Chris Lattner8a12c272007-10-11 18:38:32 +0000112
Chris Lattner7c239602007-10-13 00:11:23 +0000113/// ReplaceText - This method replaces a range of characters in the input
114/// buffer with a new string. This is effectively a combined "remove/insert"
115/// operation.
116void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
117 const char *NewStr, unsigned NewLength) {
Chris Lattner57c337d2007-10-13 00:46:29 +0000118 unsigned RealOffset = getMappedOffset(OrigOffset);
119 assert(RealOffset+OrigLength <= Buffer.size() && "Invalid location");
120
121 // Overwrite the common piece.
122 memcpy(&Buffer[RealOffset], NewStr, std::min(OrigLength, NewLength));
Chris Lattner7c239602007-10-13 00:11:23 +0000123
Chris Lattner57c337d2007-10-13 00:46:29 +0000124 // If replacing without shifting around, just overwrite the text.
125 if (OrigLength == NewLength)
Chris Lattner7c239602007-10-13 00:11:23 +0000126 return;
Chris Lattner57c337d2007-10-13 00:46:29 +0000127
128 // If inserting more than existed before, this is like an insertion.
129 if (NewLength > OrigLength) {
130 Buffer.insert(Buffer.begin()+RealOffset+OrigLength,
131 NewStr+OrigLength, NewStr+NewLength);
132 } else {
133 // If insertion less than existed before, this is like a removal.
134 Buffer.erase(Buffer.begin()+RealOffset+NewLength,
135 Buffer.begin()+RealOffset+OrigLength);
Chris Lattner7c239602007-10-13 00:11:23 +0000136 }
Chris Lattner57c337d2007-10-13 00:46:29 +0000137 AddDelta(OrigOffset, NewLength-OrigLength);
Chris Lattner7c239602007-10-13 00:11:23 +0000138}
Chris Lattner8a12c272007-10-11 18:38:32 +0000139
140
141//===----------------------------------------------------------------------===//
142// Rewriter class
143//===----------------------------------------------------------------------===//
144
Chris Lattner311ff022007-10-16 22:36:42 +0000145/// getRangeSize - Return the size in bytes of the specified range if they
146/// are in the same file. If not, this returns -1.
147int Rewriter::getRangeSize(SourceRange Range) const {
148 if (!isRewritable(Range.getBegin()) ||
149 !isRewritable(Range.getEnd())) return -1;
150
151 unsigned StartOff, StartFileID;
152 unsigned EndOff , EndFileID;
153
154 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
155 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
156
157 if (StartFileID != EndFileID)
158 return -1;
159
Chris Lattner9d62a5b2007-10-17 21:23:07 +0000160 // Adjust the end offset to the end of the last token, instead of being the
161 // start of the last token.
162 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr);
163
Chris Lattner311ff022007-10-16 22:36:42 +0000164 return EndOff-StartOff;
165}
166
167
Chris Lattner7c239602007-10-13 00:11:23 +0000168unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
169 unsigned &FileID) const {
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000170 std::pair<unsigned,unsigned> V = SourceMgr->getDecomposedFileLoc(Loc);
Chris Lattner7c239602007-10-13 00:11:23 +0000171 FileID = V.first;
172 return V.second;
173}
174
175
Chris Lattner8a12c272007-10-11 18:38:32 +0000176/// getEditBuffer - Get or create a RewriteBuffer for the specified FileID.
177///
178RewriteBuffer &Rewriter::getEditBuffer(unsigned FileID) {
179 std::map<unsigned, RewriteBuffer>::iterator I =
180 RewriteBuffers.lower_bound(FileID);
181 if (I != RewriteBuffers.end() && I->first == FileID)
182 return I->second;
183 I = RewriteBuffers.insert(I, std::make_pair(FileID, RewriteBuffer()));
184
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000185 std::pair<const char*, const char*> MB = SourceMgr->getBufferData(FileID);
Chris Lattner8a12c272007-10-11 18:38:32 +0000186 I->second.Initialize(MB.first, MB.second);
187
188 return I->second;
189}
190
Chris Lattner674af952007-10-16 22:51:17 +0000191/// RemoveText - Remove the specified text region. This method is only valid
192/// on a rewritable source location.
193void Rewriter::RemoveText(SourceLocation Start, unsigned Length) {
194 unsigned FileID;
195 unsigned StartOffs = getLocationOffsetAndFileID(Start, FileID);
196 getEditBuffer(FileID).RemoveText(StartOffs, Length);
197}
Chris Lattner8a12c272007-10-11 18:38:32 +0000198
Chris Lattner674af952007-10-16 22:51:17 +0000199/// ReplaceText - This method replaces a range of characters in the input
200/// buffer with a new string. This is effectively a combined "remove/insert"
201/// operation.
Chris Lattner8a12c272007-10-11 18:38:32 +0000202void Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
203 const char *NewStr, unsigned NewLength) {
204 assert(isRewritable(Start) && "Not a rewritable location!");
Chris Lattner7c239602007-10-13 00:11:23 +0000205 unsigned StartFileID;
206 unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
Chris Lattner8a12c272007-10-11 18:38:32 +0000207
Chris Lattner7c239602007-10-13 00:11:23 +0000208 getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength,
209 NewStr, NewLength);
Chris Lattner8a12c272007-10-11 18:38:32 +0000210}