mirror of
https://github.com/Gericom/teak-llvm.git
synced 2025-06-20 12:05:48 -04:00

delta tree. The issue is roughly a conflict in ReplaceText between two kinds of uses. One, it should be possible to replace a replacement: for example, the ObjC rewriter calls ReplaceStmt for an expression, then replaces the resulting expression with another expression. Two, it should be possible to replace text that already has text inserted before it: for example, the HTML rewriter inserts a bunch of tags at the beginning of the line, then tries to escape the first character on the line. This patch distinguishes the two cases by storing the deltas separately; essentially, replacements and insertions no longer interfere with each other. Another possibility would be to add some sort of flag to ReplaceText, but this seems a bit more intuitive and flexible. There are a few downsides to the current solution: one is that there isn't any way to remove/replace an insertion without touching additional surrounding text; if such an operation turns out to be useful, an additional method or flag can be added. Another is that an insertion and replacing a string of length zero are distinct operations; I'm not sure how to resolve this, or whether it will be confusing in practice. This is relatively sensitive code, so please test and tell me if anything breaks. llvm-svn: 72000
229 lines
8.0 KiB
C++
229 lines
8.0 KiB
C++
//===--- Rewriter.cpp - Code rewriting interface --------------------------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file defines the Rewriter class, which is used for code
|
|
// transformations.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Rewrite/Rewriter.h"
|
|
#include "clang/AST/Stmt.h"
|
|
#include "clang/AST/Decl.h"
|
|
#include "clang/Lex/Lexer.h"
|
|
#include "clang/Basic/SourceManager.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
using namespace clang;
|
|
|
|
void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size) {
|
|
// Nothing to remove, exit early.
|
|
if (Size == 0) return;
|
|
|
|
unsigned RealOffset = getMappedOffset(OrigOffset, true);
|
|
assert(RealOffset+Size < Buffer.size() && "Invalid location");
|
|
|
|
// Remove the dead characters.
|
|
Buffer.erase(RealOffset, Size);
|
|
|
|
// Add a delta so that future changes are offset correctly.
|
|
AddReplaceDelta(OrigOffset, -Size);
|
|
}
|
|
|
|
void RewriteBuffer::InsertText(unsigned OrigOffset,
|
|
const char *StrData, unsigned StrLen,
|
|
bool InsertAfter) {
|
|
|
|
// Nothing to insert, exit early.
|
|
if (StrLen == 0) return;
|
|
|
|
unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter);
|
|
Buffer.insert(RealOffset, StrData, StrData+StrLen);
|
|
|
|
// Add a delta so that future changes are offset correctly.
|
|
AddInsertDelta(OrigOffset, StrLen);
|
|
}
|
|
|
|
/// ReplaceText - This method replaces a range of characters in the input
|
|
/// buffer with a new string. This is effectively a combined "remove+insert"
|
|
/// operation.
|
|
void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
|
|
const char *NewStr, unsigned NewLength) {
|
|
unsigned RealOffset = getMappedOffset(OrigOffset, true);
|
|
Buffer.erase(RealOffset, OrigLength);
|
|
Buffer.insert(RealOffset, NewStr, NewStr+NewLength);
|
|
if (OrigLength != NewLength)
|
|
AddReplaceDelta(OrigOffset, NewLength-OrigLength);
|
|
}
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Rewriter class
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// getRangeSize - Return the size in bytes of the specified range if they
|
|
/// are in the same file. If not, this returns -1.
|
|
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 =
|
|
RewriteBuffers.find(StartFileID);
|
|
if (I != RewriteBuffers.end()) {
|
|
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);
|
|
|
|
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.
|
|
///
|
|
/// Note that this method is not particularly efficient.
|
|
///
|
|
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 =
|
|
RewriteBuffers.find(StartFileID);
|
|
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);
|
|
|
|
// Advance the iterators to the right spot, yay for linear time algorithms.
|
|
RewriteBuffer::iterator Start = RB.begin();
|
|
std::advance(Start, StartOff);
|
|
RewriteBuffer::iterator End = Start;
|
|
std::advance(End, EndOff-StartOff);
|
|
|
|
return std::string(Start, End);
|
|
}
|
|
|
|
unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
|
|
FileID &FID) const {
|
|
assert(Loc.isValid() && "Invalid location");
|
|
std::pair<FileID,unsigned> V = SourceMgr->getDecomposedLoc(Loc);
|
|
FID = V.first;
|
|
return V.second;
|
|
}
|
|
|
|
|
|
/// getEditBuffer - Get or create a RewriteBuffer for the specified FileID.
|
|
///
|
|
RewriteBuffer &Rewriter::getEditBuffer(FileID FID) {
|
|
std::map<FileID, RewriteBuffer>::iterator I =
|
|
RewriteBuffers.lower_bound(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;
|
|
}
|
|
|
|
/// InsertText - Insert the specified string at the specified location in the
|
|
/// original buffer.
|
|
bool Rewriter::InsertText(SourceLocation Loc, const char *StrData,
|
|
unsigned StrLen, bool InsertAfter) {
|
|
if (!isRewritable(Loc)) return true;
|
|
FileID FID;
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
|
|
getEditBuffer(FID).InsertText(StartOffs, StrData, StrLen, InsertAfter);
|
|
return false;
|
|
}
|
|
|
|
/// RemoveText - Remove the specified text region.
|
|
bool Rewriter::RemoveText(SourceLocation Start, unsigned Length) {
|
|
if (!isRewritable(Start)) return true;
|
|
FileID FID;
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Start, FID);
|
|
getEditBuffer(FID).RemoveText(StartOffs, Length);
|
|
return false;
|
|
}
|
|
|
|
/// ReplaceText - This method replaces a range of characters in the input
|
|
/// buffer with a new string. This is effectively a combined "remove/insert"
|
|
/// operation.
|
|
bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
|
|
const char *NewStr, unsigned NewLength) {
|
|
if (!isRewritable(Start)) return true;
|
|
FileID StartFileID;
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
|
|
|
|
getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength,
|
|
NewStr, NewLength);
|
|
return false;
|
|
}
|
|
|
|
/// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty
|
|
/// printer to generate the replacement code. This returns true if the input
|
|
/// could not be rewritten, or false if successful.
|
|
bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) {
|
|
// Measaure the old text.
|
|
int Size = getRangeSize(From->getSourceRange());
|
|
if (Size == -1)
|
|
return true;
|
|
|
|
// Get the new text.
|
|
std::string SStr;
|
|
llvm::raw_string_ostream S(SStr);
|
|
To->printPretty(S);
|
|
const std::string &Str = S.str();
|
|
|
|
ReplaceText(From->getLocStart(), Size, &Str[0], Str.size());
|
|
return false;
|
|
}
|
|
|
|
|