Rewriter.cpp revision 10c8d9e63bcc96d55f788e7c08b72ce626c8aeec
1//===--- Rewriter.cpp - Code rewriting interface --------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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" 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/Support/raw_ostream.h" 21using namespace clang; 22 23llvm::raw_ostream &RewriteBuffer::write(llvm::raw_ostream &os) const { 24 // FIXME: eliminate the copy by writing out each chunk at a time 25 os << std::string(begin(), end()); 26 return os; 27} 28 29/// \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) { 46 // 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"); 51 52 // Remove the dead characters. 53 Buffer.erase(RealOffset, Size); 54 55 // Add a delta so that future changes are offset correctly. 56 AddReplaceDelta(OrigOffset, -Size); 57 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 } 85} 86 87void RewriteBuffer::InsertText(unsigned OrigOffset, llvm::StringRef Str, 88 bool InsertAfter) { 89 90 // Nothing to insert, exit early. 91 if (Str.empty()) return; 92 93 unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter); 94 Buffer.insert(RealOffset, Str.begin(), Str.end()); 95 96 // Add a delta so that future changes are offset correctly. 97 AddInsertDelta(OrigOffset, Str.size()); 98} 99 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" 102/// operation. 103void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength, 104 llvm::StringRef NewStr) { 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); 110} 111 112 113//===----------------------------------------------------------------------===// 114// Rewriter class 115//===----------------------------------------------------------------------===// 116 117/// getRangeSize - Return the size in bytes of the specified range if they 118/// are in the same file. If not, this returns -1. 119int Rewriter::getRangeSize(const CharSourceRange &Range, 120 RewriteOptions opts) const { 121 if (!isRewritable(Range.getBegin()) || 122 !isRewritable(Range.getEnd())) return -1; 123 124 FileID StartFileID, EndFileID; 125 unsigned StartOff, EndOff; 126 127 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID); 128 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID); 129 130 if (StartFileID != EndFileID) 131 return -1; 132 133 // If edits have been made to this buffer, the delta between the range may 134 // have changed. 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); 141 } 142 143 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); 148 149 return EndOff-StartOff; 150} 151 152int Rewriter::getRangeSize(SourceRange Range, RewriteOptions opts) const { 153 return getRangeSize(CharSourceRange::getTokenRange(Range), opts); 154} 155 156 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. 160/// 161/// Note that this method is not particularly efficient. 162/// 163std::string Rewriter::getRewrittenText(SourceRange Range) const { 164 if (!isRewritable(Range.getBegin()) || 165 !isRewritable(Range.getEnd())) 166 return ""; 167 168 FileID StartFileID, EndFileID; 169 unsigned StartOff, EndOff; 170 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID); 171 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID); 172 173 if (StartFileID != EndFileID) 174 return ""; // Start and end in different buffers. 175 176 // If edits have been made to this buffer, the delta between the range may 177 // have changed. 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()); 183 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); 188 } 189 190 const RewriteBuffer &RB = I->second; 191 EndOff = RB.getMappedOffset(EndOff, true); 192 StartOff = RB.getMappedOffset(StartOff); 193 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); 197 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); 203 204 return std::string(Start, End); 205} 206 207unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc, 208 FileID &FID) const { 209 assert(Loc.isValid() && "Invalid location"); 210 std::pair<FileID,unsigned> V = SourceMgr->getDecomposedLoc(Loc); 211 FID = V.first; 212 return V.second; 213} 214 215 216/// getEditBuffer - Get or create a RewriteBuffer for the specified FileID. 217/// 218RewriteBuffer &Rewriter::getEditBuffer(FileID FID) { 219 std::map<FileID, RewriteBuffer>::iterator I = 220 RewriteBuffers.lower_bound(FID); 221 if (I != RewriteBuffers.end() && I->first == FID) 222 return I->second; 223 I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer())); 224 225 llvm::StringRef MB = SourceMgr->getBufferData(FID); 226 I->second.Initialize(MB.begin(), MB.end()); 227 228 return I->second; 229} 230 231/// InsertText - Insert the specified string at the specified location in the 232/// original buffer. 233bool Rewriter::InsertText(SourceLocation Loc, llvm::StringRef Str, 234 bool InsertAfter) { 235 if (!isRewritable(Loc)) return true; 236 FileID FID; 237 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID); 238 getEditBuffer(FID).InsertText(StartOffs, Str, InsertAfter); 239 return false; 240} 241 242bool Rewriter::InsertTextAfterToken(SourceLocation Loc, llvm::StringRef Str) { 243 if (!isRewritable(Loc)) return true; 244 FileID FID; 245 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID); 246 RewriteOptions rangeOpts; 247 rangeOpts.IncludeInsertsAtBeginOfRange = false; 248 StartOffs += getRangeSize(SourceRange(Loc, Loc), rangeOpts); 249 getEditBuffer(FID).InsertText(StartOffs, Str, /*InsertAfter*/true); 250 return false; 251} 252 253/// RemoveText - Remove the specified text region. 254bool Rewriter::RemoveText(SourceLocation Start, unsigned Length, 255 RewriteOptions opts) { 256 if (!isRewritable(Start)) return true; 257 FileID FID; 258 unsigned StartOffs = getLocationOffsetAndFileID(Start, FID); 259 getEditBuffer(FID).RemoveText(StartOffs, Length, opts.RemoveLineIfEmpty); 260 return false; 261} 262 263/// ReplaceText - This method replaces a range of characters in the input 264/// buffer with a new string. This is effectively a combined "remove/insert" 265/// operation. 266bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength, 267 llvm::StringRef NewStr) { 268 if (!isRewritable(Start)) return true; 269 FileID StartFileID; 270 unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID); 271 272 getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr); 273 return false; 274} 275 276bool Rewriter::ReplaceText(SourceRange range, SourceRange replacementRange) { 277 if (!isRewritable(range.getBegin())) return true; 278 if (!isRewritable(range.getEnd())) return true; 279 if (replacementRange.isInvalid()) return true; 280 SourceLocation start = range.getBegin(); 281 unsigned origLength = getRangeSize(range); 282 unsigned newLength = getRangeSize(replacementRange); 283 FileID FID; 284 unsigned newOffs = getLocationOffsetAndFileID(replacementRange.getBegin(), 285 FID); 286 llvm::StringRef MB = SourceMgr->getBufferData(FID); 287 return ReplaceText(start, origLength, MB.substr(newOffs, newLength)); 288} 289 290/// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty 291/// printer to generate the replacement code. This returns true if the input 292/// could not be rewritten, or false if successful. 293bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) { 294 // Measaure the old text. 295 int Size = getRangeSize(From->getSourceRange()); 296 if (Size == -1) 297 return true; 298 299 // Get the new text. 300 std::string SStr; 301 llvm::raw_string_ostream S(SStr); 302 To->printPretty(S, 0, PrintingPolicy(*LangOpts)); 303 const std::string &Str = S.str(); 304 305 ReplaceText(From->getLocStart(), Size, Str); 306 return false; 307} 308 309std::string Rewriter::ConvertToString(Stmt *From) { 310 std::string SStr; 311 llvm::raw_string_ostream S(SStr); 312 From->printPretty(S, 0, PrintingPolicy(*LangOpts)); 313 return SStr; 314} 315 316bool Rewriter::IncreaseIndentation(CharSourceRange range, 317 SourceLocation parentIndent) { 318 using llvm::StringRef; 319 320 if (!isRewritable(range.getBegin())) return true; 321 if (!isRewritable(range.getEnd())) return true; 322 if (!isRewritable(parentIndent)) return true; 323 324 FileID StartFileID, EndFileID, parentFileID; 325 unsigned StartOff, EndOff, parentOff; 326 327 StartOff = getLocationOffsetAndFileID(range.getBegin(), StartFileID); 328 EndOff = getLocationOffsetAndFileID(range.getEnd(), EndFileID); 329 parentOff = getLocationOffsetAndFileID(parentIndent, parentFileID); 330 331 if (StartFileID != EndFileID || StartFileID != parentFileID) 332 return true; 333 if (StartOff >= EndOff || parentOff >= StartOff) 334 return true; 335 336 FileID FID = StartFileID; 337 StringRef MB = SourceMgr->getBufferData(FID); 338 339 unsigned parentLineNo = SourceMgr->getLineNumber(FID, parentOff) - 1; 340 unsigned startLineNo = SourceMgr->getLineNumber(FID, StartOff) - 1; 341 unsigned endLineNo = SourceMgr->getLineNumber(FID, EndOff) - 1; 342 343 const SrcMgr::ContentCache * 344 Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache(); 345 346 // Find where the line starts for the three offsets. 347 unsigned parentLineOffs = Content->SourceLineCache[parentLineNo]; 348 unsigned startLineOffs = Content->SourceLineCache[startLineNo]; 349 unsigned endLineOffs = Content->SourceLineCache[endLineNo]; 350 351 if (startLineOffs == endLineOffs || startLineOffs == parentLineOffs) 352 return true; 353 354 // Find the whitespace at the start of each line. 355 StringRef parentSpace, startSpace, endSpace; 356 { 357 unsigned i = parentLineOffs; 358 while (isWhitespace(MB[i])) 359 ++i; 360 parentSpace = MB.substr(parentLineOffs, i-parentLineOffs); 361 362 i = startLineOffs; 363 while (isWhitespace(MB[i])) 364 ++i; 365 startSpace = MB.substr(startLineOffs, i-startLineOffs); 366 367 i = endLineOffs; 368 while (isWhitespace(MB[i])) 369 ++i; 370 endSpace = MB.substr(endLineOffs, i-endLineOffs); 371 } 372 if (parentSpace.size() >= startSpace.size()) 373 return true; 374 if (!startSpace.startswith(parentSpace)) 375 return true; 376 377 llvm::StringRef indent = startSpace.substr(parentSpace.size()); 378 379 // Indent the lines between start/end offsets. 380 RewriteBuffer &RB = getEditBuffer(FID); 381 for (unsigned i = startLineOffs; i != endLineOffs; ++i) { 382 if (MB[i] == '\n') { 383 unsigned startOfLine = i+1; 384 if (startOfLine == endLineOffs) 385 break; 386 StringRef origIndent; 387 unsigned ws = startOfLine; 388 while (isWhitespace(MB[ws])) 389 ++ws; 390 origIndent = MB.substr(startOfLine, ws-startOfLine); 391 if (origIndent.startswith(startSpace)) 392 RB.InsertText(startOfLine, indent, /*InsertAfter=*/false); 393 } 394 } 395 396 return false; 397} 398