2007-09-16 06:21:22 +08:00
|
|
|
//===--- Rewriter.cpp - Code rewriting interface --------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-09-16 06:21:22 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the Rewriter class, which is used for code
|
|
|
|
// transformations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Rewrite/Rewriter.h"
|
2007-10-18 06:35:30 +08:00
|
|
|
#include "clang/AST/Stmt.h"
|
2007-10-18 05:23:07 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2007-10-12 02:38:32 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2007-10-18 06:35:30 +08:00
|
|
|
#include <sstream>
|
2007-09-16 06:21:22 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size) {
|
2007-10-13 08:11:23 +08:00
|
|
|
// 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.
|
2007-11-09 04:51:02 +08:00
|
|
|
RewriteRope::iterator I = Buffer.getAtOffset(RealOffset);
|
|
|
|
Buffer.erase(I, I+Size);
|
2007-10-13 08:11:23 +08:00
|
|
|
|
|
|
|
// Add a delta so that future changes are offset correctly.
|
|
|
|
AddDelta(OrigOffset, -Size);
|
2007-09-16 06:21:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBuffer::InsertText(unsigned OrigOffset,
|
2008-03-19 05:17:59 +08:00
|
|
|
const char *StrData, unsigned StrLen,
|
|
|
|
bool InsertAfter) {
|
|
|
|
|
2007-10-13 08:21:23 +08:00
|
|
|
// Nothing to insert, exit early.
|
2007-10-13 08:11:23 +08:00
|
|
|
if (StrLen == 0) return;
|
2007-10-13 08:21:23 +08:00
|
|
|
|
2008-03-19 05:17:59 +08:00
|
|
|
unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter);
|
2007-10-13 08:21:23 +08:00
|
|
|
assert(RealOffset <= Buffer.size() && "Invalid location");
|
|
|
|
|
2007-11-08 12:41:04 +08:00
|
|
|
// Insert the new characters.
|
2007-11-09 04:51:02 +08:00
|
|
|
Buffer.insert(Buffer.getAtOffset(RealOffset), StrData, StrData+StrLen);
|
2007-10-13 08:21:23 +08:00
|
|
|
|
|
|
|
// Add a delta so that future changes are offset correctly.
|
|
|
|
AddDelta(OrigOffset, StrLen);
|
2007-09-16 06:21:22 +08:00
|
|
|
}
|
2007-10-12 02:38:32 +08:00
|
|
|
|
2007-10-13 08:11:23 +08:00
|
|
|
/// 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) {
|
2007-11-08 12:09:59 +08:00
|
|
|
unsigned RealOffset = getMappedOffset(OrigOffset, true);
|
2007-10-13 08:46:29 +08:00
|
|
|
assert(RealOffset+OrigLength <= Buffer.size() && "Invalid location");
|
|
|
|
|
|
|
|
// Overwrite the common piece.
|
2007-11-08 12:41:04 +08:00
|
|
|
unsigned CommonLength = std::min(OrigLength, NewLength);
|
2007-11-09 04:51:02 +08:00
|
|
|
std::copy(NewStr, NewStr+CommonLength, Buffer.getAtOffset(RealOffset));
|
2007-10-13 08:11:23 +08:00
|
|
|
|
2007-10-13 08:46:29 +08:00
|
|
|
// If replacing without shifting around, just overwrite the text.
|
|
|
|
if (OrigLength == NewLength)
|
2007-10-13 08:11:23 +08:00
|
|
|
return;
|
2007-10-13 08:46:29 +08:00
|
|
|
|
|
|
|
// If inserting more than existed before, this is like an insertion.
|
|
|
|
if (NewLength > OrigLength) {
|
2007-11-09 04:51:02 +08:00
|
|
|
Buffer.insert(Buffer.getAtOffset(RealOffset+OrigLength),
|
2007-10-13 08:46:29 +08:00
|
|
|
NewStr+OrigLength, NewStr+NewLength);
|
|
|
|
} else {
|
2007-11-09 04:51:02 +08:00
|
|
|
// If inserting less than existed before, this is like a removal.
|
|
|
|
RewriteRope::iterator I = Buffer.getAtOffset(RealOffset+NewLength);
|
|
|
|
Buffer.erase(I, I+(OrigLength-NewLength));
|
2007-10-13 08:11:23 +08:00
|
|
|
}
|
2007-10-13 08:46:29 +08:00
|
|
|
AddDelta(OrigOffset, NewLength-OrigLength);
|
2007-10-13 08:11:23 +08:00
|
|
|
}
|
2007-10-12 02:38:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Rewriter class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-10-17 06:36:42 +08:00
|
|
|
/// 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;
|
|
|
|
|
|
|
|
unsigned StartOff, StartFileID;
|
|
|
|
unsigned EndOff , EndFileID;
|
|
|
|
|
|
|
|
StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
|
|
|
|
EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
|
|
|
|
|
|
|
|
if (StartFileID != EndFileID)
|
|
|
|
return -1;
|
|
|
|
|
2007-10-26 01:18:59 +08:00
|
|
|
// If edits have been made to this buffer, the delta between the range may
|
|
|
|
// have changed.
|
2007-10-26 01:17:34 +08:00
|
|
|
std::map<unsigned, RewriteBuffer>::const_iterator I =
|
|
|
|
RewriteBuffers.find(StartFileID);
|
2007-10-26 01:18:59 +08:00
|
|
|
if (I != RewriteBuffers.end()) {
|
2007-10-26 01:17:34 +08:00
|
|
|
const RewriteBuffer &RB = I->second;
|
2007-10-26 01:18:59 +08:00
|
|
|
EndOff = RB.getMappedOffset(EndOff, true);
|
|
|
|
StartOff = RB.getMappedOffset(StartOff);
|
2007-10-26 01:17:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-18 05:23:07 +08:00
|
|
|
// Adjust the end offset to the end of the last token, instead of being the
|
|
|
|
// start of the last token.
|
2007-10-26 01:18:59 +08:00
|
|
|
EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr);
|
2007-10-18 05:23:07 +08:00
|
|
|
|
2007-10-26 01:18:59 +08:00
|
|
|
return EndOff-StartOff;
|
2007-10-17 06:36:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-13 08:11:23 +08:00
|
|
|
unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
|
|
|
|
unsigned &FileID) const {
|
2007-10-17 05:07:07 +08:00
|
|
|
std::pair<unsigned,unsigned> V = SourceMgr->getDecomposedFileLoc(Loc);
|
2007-10-13 08:11:23 +08:00
|
|
|
FileID = V.first;
|
|
|
|
return V.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-12 02:38:32 +08:00
|
|
|
/// getEditBuffer - Get or create a RewriteBuffer for the specified FileID.
|
|
|
|
///
|
|
|
|
RewriteBuffer &Rewriter::getEditBuffer(unsigned FileID) {
|
|
|
|
std::map<unsigned, RewriteBuffer>::iterator I =
|
|
|
|
RewriteBuffers.lower_bound(FileID);
|
|
|
|
if (I != RewriteBuffers.end() && I->first == FileID)
|
|
|
|
return I->second;
|
|
|
|
I = RewriteBuffers.insert(I, std::make_pair(FileID, RewriteBuffer()));
|
|
|
|
|
2007-10-17 05:07:07 +08:00
|
|
|
std::pair<const char*, const char*> MB = SourceMgr->getBufferData(FileID);
|
2007-10-12 02:38:32 +08:00
|
|
|
I->second.Initialize(MB.first, MB.second);
|
|
|
|
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2007-11-03 01:26:47 +08:00
|
|
|
/// InsertText - Insert the specified string at the specified location in the
|
2008-02-01 03:51:04 +08:00
|
|
|
/// original buffer.
|
2008-03-19 05:17:59 +08:00
|
|
|
bool Rewriter::InsertText(SourceLocation Loc, const char *StrData,
|
|
|
|
unsigned StrLen, bool InsertAfter) {
|
2008-02-01 03:37:57 +08:00
|
|
|
if (!isRewritable(Loc)) return true;
|
2007-11-03 01:26:47 +08:00
|
|
|
unsigned FileID;
|
|
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Loc, FileID);
|
2008-03-19 05:17:59 +08:00
|
|
|
getEditBuffer(FileID).InsertText(StartOffs, StrData, StrLen, InsertAfter);
|
2008-02-01 03:37:57 +08:00
|
|
|
return false;
|
2007-11-03 01:26:47 +08:00
|
|
|
}
|
|
|
|
|
2008-02-01 03:51:04 +08:00
|
|
|
/// RemoveText - Remove the specified text region.
|
|
|
|
bool Rewriter::RemoveText(SourceLocation Start, unsigned Length) {
|
|
|
|
if (!isRewritable(Start)) return true;
|
2007-10-17 06:51:17 +08:00
|
|
|
unsigned FileID;
|
|
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Start, FileID);
|
|
|
|
getEditBuffer(FileID).RemoveText(StartOffs, Length);
|
2008-02-01 03:51:04 +08:00
|
|
|
return false;
|
2007-10-17 06:51:17 +08:00
|
|
|
}
|
2007-10-12 02:38:32 +08:00
|
|
|
|
2007-10-17 06:51:17 +08:00
|
|
|
/// ReplaceText - This method replaces a range of characters in the input
|
|
|
|
/// buffer with a new string. This is effectively a combined "remove/insert"
|
|
|
|
/// operation.
|
2008-02-01 03:51:04 +08:00
|
|
|
bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
|
2007-10-12 02:38:32 +08:00
|
|
|
const char *NewStr, unsigned NewLength) {
|
2008-02-01 03:51:04 +08:00
|
|
|
if (!isRewritable(Start)) return true;
|
2007-10-13 08:11:23 +08:00
|
|
|
unsigned StartFileID;
|
|
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
|
2007-10-12 02:38:32 +08:00
|
|
|
|
2007-10-13 08:11:23 +08:00
|
|
|
getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength,
|
|
|
|
NewStr, NewLength);
|
2008-02-01 03:51:04 +08:00
|
|
|
return false;
|
2007-10-12 02:38:32 +08:00
|
|
|
}
|
2007-10-18 06:35:30 +08:00
|
|
|
|
|
|
|
/// 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::ostringstream S;
|
|
|
|
To->printPretty(S);
|
|
|
|
const std::string &Str = S.str();
|
|
|
|
|
|
|
|
ReplaceText(From->getLocStart(), Size, &Str[0], Str.size());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|