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"
|
2008-10-08 07:08:39 +08:00
|
|
|
#include "clang/AST/Decl.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"
|
2008-09-13 13:16:45 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
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.
|
2008-04-14 15:17:29 +08:00
|
|
|
Buffer.erase(RealOffset, Size);
|
2007-10-13 08:11:23 +08:00
|
|
|
|
|
|
|
// Add a delta so that future changes are offset correctly.
|
Fix for PR2386: distinguish between insertion and replacements in the
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
2009-05-18 21:56:52 +08:00
|
|
|
AddReplaceDelta(OrigOffset, -Size);
|
2007-09-16 06:21:22 +08:00
|
|
|
}
|
|
|
|
|
2009-08-20 03:10:30 +08:00
|
|
|
void RewriteBuffer::InsertText(unsigned OrigOffset, const llvm::StringRef &Str,
|
2008-03-19 05:17:59 +08:00
|
|
|
bool InsertAfter) {
|
|
|
|
|
2007-10-13 08:21:23 +08:00
|
|
|
// Nothing to insert, exit early.
|
2009-08-20 03:10:30 +08:00
|
|
|
if (Str.empty()) return;
|
2008-04-14 15:17:29 +08:00
|
|
|
|
2008-03-19 05:17:59 +08:00
|
|
|
unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter);
|
2009-08-20 03:10:30 +08:00
|
|
|
Buffer.insert(RealOffset, Str.begin(), Str.end());
|
2007-10-13 08:21:23 +08:00
|
|
|
|
|
|
|
// Add a delta so that future changes are offset correctly.
|
2009-08-20 03:10:30 +08:00
|
|
|
AddInsertDelta(OrigOffset, Str.size());
|
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
|
2008-04-14 15:17:29 +08:00
|
|
|
/// buffer with a new string. This is effectively a combined "remove+insert"
|
2007-10-13 08:11:23 +08:00
|
|
|
/// operation.
|
|
|
|
void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
|
2009-08-20 03:10:30 +08:00
|
|
|
const llvm::StringRef &NewStr) {
|
Fix for PR2386: distinguish between insertion and replacements in the
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
2009-05-18 21:56:52 +08:00
|
|
|
unsigned RealOffset = getMappedOffset(OrigOffset, true);
|
2008-04-14 15:17:29 +08:00
|
|
|
Buffer.erase(RealOffset, OrigLength);
|
2009-08-20 03:10:30 +08:00
|
|
|
Buffer.insert(RealOffset, NewStr.begin(), NewStr.end());
|
|
|
|
if (OrigLength != NewStr.size())
|
|
|
|
AddReplaceDelta(OrigOffset, NewStr.size() - 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;
|
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID StartFileID, EndFileID;
|
|
|
|
unsigned StartOff, EndOff;
|
2007-10-17 06:36:42 +08:00
|
|
|
|
|
|
|
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.
|
2009-01-17 14:22:33 +08:00
|
|
|
std::map<FileID, RewriteBuffer>::const_iterator I =
|
2007-10-26 01:17:34 +08:00
|
|
|
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.
|
2009-04-15 07:22:57 +08:00
|
|
|
EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
|
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
|
|
|
}
|
|
|
|
|
2008-10-04 07:31:16 +08:00
|
|
|
/// 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 "";
|
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID StartFileID, EndFileID;
|
|
|
|
unsigned StartOff, EndOff;
|
2008-10-04 07:31:16 +08:00
|
|
|
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.
|
2009-01-17 14:22:33 +08:00
|
|
|
std::map<FileID, RewriteBuffer>::const_iterator I =
|
2008-10-04 07:31:16 +08:00
|
|
|
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.
|
2009-04-15 07:22:57 +08:00
|
|
|
EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
|
2008-10-04 07:31:16 +08:00
|
|
|
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.
|
2009-04-15 07:22:57 +08:00
|
|
|
EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
|
2008-10-04 07:31:16 +08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2007-10-17 06:36:42 +08:00
|
|
|
|
2007-10-13 08:11:23 +08:00
|
|
|
unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID &FID) const {
|
2008-05-29 00:35:02 +08:00
|
|
|
assert(Loc.isValid() && "Invalid location");
|
2009-01-26 08:43:02 +08:00
|
|
|
std::pair<FileID,unsigned> V = SourceMgr->getDecomposedLoc(Loc);
|
2009-01-17 14:22:33 +08:00
|
|
|
FID = V.first;
|
2007-10-13 08:11:23 +08:00
|
|
|
return V.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-12 02:38:32 +08:00
|
|
|
/// getEditBuffer - Get or create a RewriteBuffer for the specified FileID.
|
|
|
|
///
|
2009-01-17 14:22:33 +08:00
|
|
|
RewriteBuffer &Rewriter::getEditBuffer(FileID FID) {
|
|
|
|
std::map<FileID, RewriteBuffer>::iterator I =
|
|
|
|
RewriteBuffers.lower_bound(FID);
|
|
|
|
if (I != RewriteBuffers.end() && I->first == FID)
|
2007-10-12 02:38:32 +08:00
|
|
|
return I->second;
|
2009-01-17 14:22:33 +08:00
|
|
|
I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer()));
|
2007-10-12 02:38:32 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
std::pair<const char*, const char*> MB = SourceMgr->getBufferData(FID);
|
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.
|
2009-08-20 03:10:30 +08:00
|
|
|
bool Rewriter::InsertText(SourceLocation Loc, const llvm::StringRef &Str,
|
|
|
|
bool InsertAfter) {
|
2008-02-01 03:37:57 +08:00
|
|
|
if (!isRewritable(Loc)) return true;
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID FID;
|
|
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
|
2009-08-20 03:10:30 +08:00
|
|
|
getEditBuffer(FID).InsertText(StartOffs, Str, 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;
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID FID;
|
|
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Start, FID);
|
|
|
|
getEditBuffer(FID).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,
|
2009-08-20 03:10:30 +08:00
|
|
|
const llvm::StringRef &NewStr) {
|
2008-02-01 03:51:04 +08:00
|
|
|
if (!isRewritable(Start)) return true;
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID StartFileID;
|
2007-10-13 08:11:23 +08:00
|
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
|
2007-10-12 02:38:32 +08:00
|
|
|
|
2009-08-20 03:10:30 +08:00
|
|
|
getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr);
|
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.
|
2008-09-13 13:16:45 +08:00
|
|
|
std::string SStr;
|
|
|
|
llvm::raw_string_ostream S(SStr);
|
2009-06-30 09:26:17 +08:00
|
|
|
To->printPretty(S, 0, PrintingPolicy(*LangOpts));
|
2007-10-18 06:35:30 +08:00
|
|
|
const std::string &Str = S.str();
|
|
|
|
|
2009-08-20 03:10:30 +08:00
|
|
|
ReplaceText(From->getLocStart(), Size, Str);
|
2007-10-18 06:35:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|