Chris Lattner | 8bd12b8 | 2007-09-15 22:21:22 +0000 | [diff] [blame] | 1 | //===--- 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 Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 16 | #include "clang/AST/Stmt.h" |
Chris Lattner | 9d62a5b | 2007-10-17 21:23:07 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Lexer.h" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 19 | #include <sstream> |
Chris Lattner | 8bd12b8 | 2007-09-15 22:21:22 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 22 | /// getMappedOffset - Given an offset into the original SourceBuffer that this |
| 23 | /// RewriteBuffer is based on, map it into the offset space of the |
| 24 | /// RewriteBuffer. |
| 25 | unsigned RewriteBuffer::getMappedOffset(unsigned OrigOffset, |
| 26 | bool AfterInserts) const { |
| 27 | unsigned ResultOffset = OrigOffset; |
| 28 | unsigned DeltaIdx = 0; |
| 29 | |
| 30 | // Move past any deltas that are relevant. |
| 31 | // FIXME: binary search. |
| 32 | for (; DeltaIdx != Deltas.size() && |
Chris Lattner | 88d0ed0 | 2007-10-13 00:17:04 +0000 | [diff] [blame] | 33 | Deltas[DeltaIdx].FileLoc < OrigOffset; ++DeltaIdx) |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 34 | ResultOffset += Deltas[DeltaIdx].Delta; |
| 35 | |
| 36 | if (AfterInserts && DeltaIdx != Deltas.size() && |
| 37 | OrigOffset == Deltas[DeltaIdx].FileLoc) |
| 38 | ResultOffset += Deltas[DeltaIdx].Delta; |
| 39 | return ResultOffset; |
| 40 | } |
| 41 | |
| 42 | /// AddDelta - When a change is made that shifts around the text buffer, this |
| 43 | /// method is used to record that info. |
| 44 | void RewriteBuffer::AddDelta(unsigned OrigOffset, int Change) { |
| 45 | assert(Change != 0 && "Not changing anything"); |
| 46 | unsigned DeltaIdx = 0; |
| 47 | |
| 48 | // Skip over any unrelated deltas. |
| 49 | for (; DeltaIdx != Deltas.size() && |
Chris Lattner | 88d0ed0 | 2007-10-13 00:17:04 +0000 | [diff] [blame] | 50 | Deltas[DeltaIdx].FileLoc < OrigOffset; ++DeltaIdx) |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 51 | ; |
| 52 | |
| 53 | // If there is no a delta for this offset, insert a new delta record. |
| 54 | if (DeltaIdx == Deltas.size() || OrigOffset != Deltas[DeltaIdx].FileLoc) { |
| 55 | // If this is a removal, check to see if this can be folded into |
| 56 | // a delta at the end of the deletion. For example, if we have: |
| 57 | // ABCXDEF (X inserted after C) and delete C, we want to end up with no |
| 58 | // delta because X basically replaced C. |
| 59 | if (Change < 0 && DeltaIdx != Deltas.size() && |
| 60 | OrigOffset-Change == Deltas[DeltaIdx].FileLoc) { |
| 61 | // Adjust the start of the delta to be the start of the deleted region. |
| 62 | Deltas[DeltaIdx].FileLoc += Change; |
| 63 | Deltas[DeltaIdx].Delta += Change; |
| 64 | |
| 65 | // If the delta becomes a noop, remove it. |
| 66 | if (Deltas[DeltaIdx].Delta == 0) |
| 67 | Deltas.erase(Deltas.begin()+DeltaIdx); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | // Otherwise, create an entry and return. |
| 72 | Deltas.insert(Deltas.begin()+DeltaIdx, |
| 73 | SourceDelta::get(OrigOffset, Change)); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | // Otherwise, we found a delta record at this offset, adjust it. |
| 78 | Deltas[DeltaIdx].Delta += Change; |
| 79 | |
| 80 | // If it is now dead, remove it. |
Fariborz Jahanian | 24abb10 | 2007-11-06 23:06:16 +0000 | [diff] [blame] | 81 | if (Deltas[DeltaIdx].Delta == 0) |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 82 | Deltas.erase(Deltas.begin()+DeltaIdx); |
| 83 | } |
| 84 | |
Chris Lattner | 8bd12b8 | 2007-09-15 22:21:22 +0000 | [diff] [blame] | 85 | |
| 86 | void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size) { |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 87 | // Nothing to remove, exit early. |
| 88 | if (Size == 0) return; |
| 89 | |
| 90 | unsigned RealOffset = getMappedOffset(OrigOffset, true); |
| 91 | assert(RealOffset+Size < Buffer.size() && "Invalid location"); |
| 92 | |
| 93 | // Remove the dead characters. |
Chris Lattner | fab21b6 | 2007-11-08 20:51:02 +0000 | [diff] [blame^] | 94 | RewriteRope::iterator I = Buffer.getAtOffset(RealOffset); |
| 95 | Buffer.erase(I, I+Size); |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 96 | |
| 97 | // Add a delta so that future changes are offset correctly. |
| 98 | AddDelta(OrigOffset, -Size); |
Chris Lattner | 8bd12b8 | 2007-09-15 22:21:22 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void RewriteBuffer::InsertText(unsigned OrigOffset, |
| 102 | const char *StrData, unsigned StrLen) { |
Chris Lattner | 03b0710 | 2007-10-13 00:21:23 +0000 | [diff] [blame] | 103 | // Nothing to insert, exit early. |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 104 | if (StrLen == 0) return; |
Chris Lattner | 03b0710 | 2007-10-13 00:21:23 +0000 | [diff] [blame] | 105 | |
| 106 | unsigned RealOffset = getMappedOffset(OrigOffset, true); |
| 107 | assert(RealOffset <= Buffer.size() && "Invalid location"); |
| 108 | |
Chris Lattner | d425a27 | 2007-11-08 04:41:04 +0000 | [diff] [blame] | 109 | // Insert the new characters. |
Chris Lattner | fab21b6 | 2007-11-08 20:51:02 +0000 | [diff] [blame^] | 110 | Buffer.insert(Buffer.getAtOffset(RealOffset), StrData, StrData+StrLen); |
Chris Lattner | 03b0710 | 2007-10-13 00:21:23 +0000 | [diff] [blame] | 111 | |
| 112 | // Add a delta so that future changes are offset correctly. |
| 113 | AddDelta(OrigOffset, StrLen); |
Chris Lattner | 8bd12b8 | 2007-09-15 22:21:22 +0000 | [diff] [blame] | 114 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 116 | /// ReplaceText - This method replaces a range of characters in the input |
| 117 | /// buffer with a new string. This is effectively a combined "remove/insert" |
| 118 | /// operation. |
| 119 | void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength, |
| 120 | const char *NewStr, unsigned NewLength) { |
Chris Lattner | 116c089 | 2007-11-08 04:09:59 +0000 | [diff] [blame] | 121 | unsigned RealOffset = getMappedOffset(OrigOffset, true); |
Chris Lattner | 57c337d | 2007-10-13 00:46:29 +0000 | [diff] [blame] | 122 | assert(RealOffset+OrigLength <= Buffer.size() && "Invalid location"); |
| 123 | |
Chris Lattner | fab21b6 | 2007-11-08 20:51:02 +0000 | [diff] [blame^] | 124 | #if 0 |
Chris Lattner | 8b0c2f6 | 2007-11-08 07:35:14 +0000 | [diff] [blame] | 125 | Buffer.erase(Buffer.begin()+RealOffset, Buffer.begin()+RealOffset+OrigLength); |
| 126 | Buffer.insert(Buffer.begin()+RealOffset, NewStr, NewStr+NewLength); |
| 127 | AddDelta(OrigOffset, NewLength-OrigLength); |
| 128 | return; |
Chris Lattner | fab21b6 | 2007-11-08 20:51:02 +0000 | [diff] [blame^] | 129 | #endif |
Chris Lattner | 8b0c2f6 | 2007-11-08 07:35:14 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 57c337d | 2007-10-13 00:46:29 +0000 | [diff] [blame] | 131 | // Overwrite the common piece. |
Chris Lattner | d425a27 | 2007-11-08 04:41:04 +0000 | [diff] [blame] | 132 | unsigned CommonLength = std::min(OrigLength, NewLength); |
Chris Lattner | fab21b6 | 2007-11-08 20:51:02 +0000 | [diff] [blame^] | 133 | std::copy(NewStr, NewStr+CommonLength, Buffer.getAtOffset(RealOffset)); |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 57c337d | 2007-10-13 00:46:29 +0000 | [diff] [blame] | 135 | // If replacing without shifting around, just overwrite the text. |
| 136 | if (OrigLength == NewLength) |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 137 | return; |
Chris Lattner | 57c337d | 2007-10-13 00:46:29 +0000 | [diff] [blame] | 138 | |
| 139 | // If inserting more than existed before, this is like an insertion. |
| 140 | if (NewLength > OrigLength) { |
Chris Lattner | fab21b6 | 2007-11-08 20:51:02 +0000 | [diff] [blame^] | 141 | Buffer.insert(Buffer.getAtOffset(RealOffset+OrigLength), |
Chris Lattner | 57c337d | 2007-10-13 00:46:29 +0000 | [diff] [blame] | 142 | NewStr+OrigLength, NewStr+NewLength); |
| 143 | } else { |
Chris Lattner | fab21b6 | 2007-11-08 20:51:02 +0000 | [diff] [blame^] | 144 | // If inserting less than existed before, this is like a removal. |
| 145 | RewriteRope::iterator I = Buffer.getAtOffset(RealOffset+NewLength); |
| 146 | Buffer.erase(I, I+(OrigLength-NewLength)); |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 147 | } |
Chris Lattner | 57c337d | 2007-10-13 00:46:29 +0000 | [diff] [blame] | 148 | AddDelta(OrigOffset, NewLength-OrigLength); |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 149 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 150 | |
| 151 | |
| 152 | //===----------------------------------------------------------------------===// |
| 153 | // Rewriter class |
| 154 | //===----------------------------------------------------------------------===// |
| 155 | |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 156 | /// getRangeSize - Return the size in bytes of the specified range if they |
| 157 | /// are in the same file. If not, this returns -1. |
| 158 | int Rewriter::getRangeSize(SourceRange Range) const { |
| 159 | if (!isRewritable(Range.getBegin()) || |
| 160 | !isRewritable(Range.getEnd())) return -1; |
| 161 | |
| 162 | unsigned StartOff, StartFileID; |
| 163 | unsigned EndOff , EndFileID; |
| 164 | |
| 165 | StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID); |
| 166 | EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID); |
| 167 | |
| 168 | if (StartFileID != EndFileID) |
| 169 | return -1; |
| 170 | |
Chris Lattner | d6690b2 | 2007-10-25 17:18:59 +0000 | [diff] [blame] | 171 | // If edits have been made to this buffer, the delta between the range may |
| 172 | // have changed. |
Chris Lattner | 075eb6e | 2007-10-25 17:17:34 +0000 | [diff] [blame] | 173 | std::map<unsigned, RewriteBuffer>::const_iterator I = |
| 174 | RewriteBuffers.find(StartFileID); |
Chris Lattner | d6690b2 | 2007-10-25 17:18:59 +0000 | [diff] [blame] | 175 | if (I != RewriteBuffers.end()) { |
Chris Lattner | 075eb6e | 2007-10-25 17:17:34 +0000 | [diff] [blame] | 176 | const RewriteBuffer &RB = I->second; |
Chris Lattner | d6690b2 | 2007-10-25 17:18:59 +0000 | [diff] [blame] | 177 | EndOff = RB.getMappedOffset(EndOff, true); |
| 178 | StartOff = RB.getMappedOffset(StartOff); |
Chris Lattner | 075eb6e | 2007-10-25 17:17:34 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
Chris Lattner | 9d62a5b | 2007-10-17 21:23:07 +0000 | [diff] [blame] | 182 | // Adjust the end offset to the end of the last token, instead of being the |
| 183 | // start of the last token. |
Chris Lattner | d6690b2 | 2007-10-25 17:18:59 +0000 | [diff] [blame] | 184 | EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr); |
Chris Lattner | 9d62a5b | 2007-10-17 21:23:07 +0000 | [diff] [blame] | 185 | |
Chris Lattner | d6690b2 | 2007-10-25 17:18:59 +0000 | [diff] [blame] | 186 | return EndOff-StartOff; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 190 | unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc, |
| 191 | unsigned &FileID) const { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 192 | std::pair<unsigned,unsigned> V = SourceMgr->getDecomposedFileLoc(Loc); |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 193 | FileID = V.first; |
| 194 | return V.second; |
| 195 | } |
| 196 | |
| 197 | |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 198 | /// getEditBuffer - Get or create a RewriteBuffer for the specified FileID. |
| 199 | /// |
| 200 | RewriteBuffer &Rewriter::getEditBuffer(unsigned FileID) { |
| 201 | std::map<unsigned, RewriteBuffer>::iterator I = |
| 202 | RewriteBuffers.lower_bound(FileID); |
| 203 | if (I != RewriteBuffers.end() && I->first == FileID) |
| 204 | return I->second; |
| 205 | I = RewriteBuffers.insert(I, std::make_pair(FileID, RewriteBuffer())); |
| 206 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 207 | std::pair<const char*, const char*> MB = SourceMgr->getBufferData(FileID); |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 208 | I->second.Initialize(MB.first, MB.second); |
| 209 | |
| 210 | return I->second; |
| 211 | } |
| 212 | |
Chris Lattner | 54a2f07 | 2007-11-02 17:26:47 +0000 | [diff] [blame] | 213 | /// InsertText - Insert the specified string at the specified location in the |
| 214 | /// original buffer. This method is only valid on rewritable source |
| 215 | /// locations. |
| 216 | void Rewriter::InsertText(SourceLocation Loc, |
| 217 | const char *StrData, unsigned StrLen) { |
| 218 | unsigned FileID; |
| 219 | unsigned StartOffs = getLocationOffsetAndFileID(Loc, FileID); |
| 220 | getEditBuffer(FileID).InsertText(StartOffs, StrData, StrLen); |
| 221 | } |
| 222 | |
Chris Lattner | 674af95 | 2007-10-16 22:51:17 +0000 | [diff] [blame] | 223 | /// RemoveText - Remove the specified text region. This method is only valid |
| 224 | /// on a rewritable source location. |
| 225 | void Rewriter::RemoveText(SourceLocation Start, unsigned Length) { |
| 226 | unsigned FileID; |
| 227 | unsigned StartOffs = getLocationOffsetAndFileID(Start, FileID); |
| 228 | getEditBuffer(FileID).RemoveText(StartOffs, Length); |
| 229 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 230 | |
Chris Lattner | 674af95 | 2007-10-16 22:51:17 +0000 | [diff] [blame] | 231 | /// ReplaceText - This method replaces a range of characters in the input |
| 232 | /// buffer with a new string. This is effectively a combined "remove/insert" |
| 233 | /// operation. |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 234 | void Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 235 | const char *NewStr, unsigned NewLength) { |
| 236 | assert(isRewritable(Start) && "Not a rewritable location!"); |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 237 | unsigned StartFileID; |
| 238 | unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID); |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 239 | |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 240 | getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, |
| 241 | NewStr, NewLength); |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 242 | } |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 243 | |
| 244 | /// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty |
| 245 | /// printer to generate the replacement code. This returns true if the input |
| 246 | /// could not be rewritten, or false if successful. |
| 247 | bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) { |
| 248 | // Measaure the old text. |
| 249 | int Size = getRangeSize(From->getSourceRange()); |
| 250 | if (Size == -1) |
| 251 | return true; |
| 252 | |
| 253 | // Get the new text. |
| 254 | std::ostringstream S; |
| 255 | To->printPretty(S); |
| 256 | const std::string &Str = S.str(); |
| 257 | |
| 258 | ReplaceText(From->getLocStart(), Size, &Str[0], Str.size()); |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | |