diff options
Diffstat (limited to 'lib/Rewrite/Rewriter.cpp')
-rw-r--r-- | lib/Rewrite/Rewriter.cpp | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/lib/Rewrite/Rewriter.cpp b/lib/Rewrite/Rewriter.cpp index 6efe31fb89..27a5f8b5ff 100644 --- a/lib/Rewrite/Rewriter.cpp +++ b/lib/Rewrite/Rewriter.cpp @@ -26,7 +26,7 @@ void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size) { unsigned RealOffset = getMappedOffset(OrigOffset, true); assert(RealOffset+Size < Buffer.size() && "Invalid location"); - + // Remove the dead characters. Buffer.erase(RealOffset, Size); @@ -36,13 +36,13 @@ void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size) { void RewriteBuffer::InsertText(unsigned OrigOffset, const llvm::StringRef &Str, bool InsertAfter) { - + // Nothing to insert, exit early. if (Str.empty()) return; unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter); Buffer.insert(RealOffset, Str.begin(), Str.end()); - + // Add a delta so that future changes are offset correctly. AddInsertDelta(OrigOffset, Str.size()); } @@ -69,16 +69,16 @@ void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength, int Rewriter::getRangeSize(SourceRange Range) const { if (!isRewritable(Range.getBegin()) || !isRewritable(Range.getEnd())) return -1; - + FileID StartFileID, EndFileID; unsigned StartOff, EndOff; - + StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID); EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID); - + if (StartFileID != EndFileID) return -1; - + // If edits have been made to this buffer, the delta between the range may // have changed. std::map<FileID, RewriteBuffer>::const_iterator I = @@ -89,17 +89,17 @@ int Rewriter::getRangeSize(SourceRange Range) const { StartOff = RB.getMappedOffset(StartOff); } - + // Adjust the end offset to the end of the last token, instead of being the // start of the last token. EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts); - + return EndOff-StartOff; } /// getRewritenText - Return the rewritten form of the text in the specified /// range. If the start or end of the range was unrewritable or if they are -/// in different buffers, this returns an empty string. +/// in different buffers, this returns an empty string. /// /// Note that this method is not particularly efficient. /// @@ -107,15 +107,15 @@ std::string Rewriter::getRewritenText(SourceRange Range) const { if (!isRewritable(Range.getBegin()) || !isRewritable(Range.getEnd())) return ""; - + FileID StartFileID, EndFileID; unsigned StartOff, EndOff; StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID); EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID); - + if (StartFileID != EndFileID) return ""; // Start and end in different buffers. - + // If edits have been made to this buffer, the delta between the range may // have changed. std::map<FileID, RewriteBuffer>::const_iterator I = @@ -123,17 +123,17 @@ std::string Rewriter::getRewritenText(SourceRange Range) const { if (I == RewriteBuffers.end()) { // If the buffer hasn't been rewritten, just return the text from the input. const char *Ptr = SourceMgr->getCharacterData(Range.getBegin()); - + // Adjust the end offset to the end of the last token, instead of being the // start of the last token. EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts); return std::string(Ptr, Ptr+EndOff-StartOff); } - + const RewriteBuffer &RB = I->second; EndOff = RB.getMappedOffset(EndOff, true); StartOff = RB.getMappedOffset(StartOff); - + // Adjust the end offset to the end of the last token, instead of being the // start of the last token. EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts); @@ -143,7 +143,7 @@ std::string Rewriter::getRewritenText(SourceRange Range) const { std::advance(Start, StartOff); RewriteBuffer::iterator End = Start; std::advance(End, EndOff-StartOff); - + return std::string(Start, End); } @@ -161,13 +161,13 @@ unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc, RewriteBuffer &Rewriter::getEditBuffer(FileID FID) { std::map<FileID, RewriteBuffer>::iterator I = RewriteBuffers.lower_bound(FID); - if (I != RewriteBuffers.end() && I->first == FID) + if (I != RewriteBuffers.end() && I->first == FID) return I->second; I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer())); - + std::pair<const char*, const char*> MB = SourceMgr->getBufferData(FID); I->second.Initialize(MB.first, MB.second); - + return I->second; } @@ -199,7 +199,7 @@ bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength, if (!isRewritable(Start)) return true; FileID StartFileID; unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID); - + getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr); return false; } @@ -212,7 +212,7 @@ bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) { int Size = getRangeSize(From->getSourceRange()); if (Size == -1) return true; - + // Get the new text. std::string SStr; llvm::raw_string_ostream S(SStr); |