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"
|
2007-09-16 06:21:22 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &RewriteBuffer::write(raw_ostream &os) const {
|
2010-04-17 02:49:45 +08:00
|
|
|
// FIXME: eliminate the copy by writing out each chunk at a time
|
|
|
|
os << std::string(begin(), end());
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2011-04-08 02:10:12 +08:00
|
|
|
/// \brief Return true if this character is non-new-line whitespace:
|
|
|
|
/// ' ', '\t', '\f', '\v', '\r'.
|
|
|
|
static inline bool isWhitespace(unsigned char c) {
|
|
|
|
switch (c) {
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\f':
|
|
|
|
case '\v':
|
|
|
|
case '\r':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size,
|
|
|
|
bool removeLineIfEmpty) {
|
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");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-13 08:11:23 +08:00
|
|
|
// 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);
|
2011-04-08 02:10:12 +08:00
|
|
|
|
|
|
|
if (removeLineIfEmpty) {
|
|
|
|
// Find the line that the remove occurred and if it is completely empty
|
|
|
|
// remove the line as well.
|
|
|
|
|
|
|
|
iterator curLineStart = begin();
|
|
|
|
unsigned curLineStartOffs = 0;
|
|
|
|
iterator posI = begin();
|
|
|
|
for (unsigned i = 0; i != RealOffset; ++i) {
|
|
|
|
if (*posI == '\n') {
|
|
|
|
curLineStart = posI;
|
|
|
|
++curLineStart;
|
|
|
|
curLineStartOffs = i + 1;
|
|
|
|
}
|
|
|
|
++posI;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned lineSize = 0;
|
|
|
|
posI = curLineStart;
|
|
|
|
while (posI != end() && isWhitespace(*posI)) {
|
|
|
|
++posI;
|
|
|
|
++lineSize;
|
|
|
|
}
|
|
|
|
if (posI != end() && *posI == '\n') {
|
|
|
|
Buffer.erase(curLineStartOffs, lineSize + 1/* + '\n'*/);
|
|
|
|
AddReplaceDelta(curLineStartOffs, -(lineSize + 1/* + '\n'*/));
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 06:21:22 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void RewriteBuffer::InsertText(unsigned OrigOffset, StringRef Str,
|
2008-03-19 05:17:59 +08:00
|
|
|
bool InsertAfter) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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,
|
2011-07-23 18:55:15 +08:00
|
|
|
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.
|
2011-04-08 02:10:12 +08:00
|
|
|
int Rewriter::getRangeSize(const CharSourceRange &Range,
|
2011-04-13 15:15:11 +08:00
|
|
|
RewriteOptions opts) const {
|
2007-10-17 06:36:42 +08:00
|
|
|
if (!isRewritable(Range.getBegin()) ||
|
|
|
|
!isRewritable(Range.getEnd())) return -1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID StartFileID, EndFileID;
|
|
|
|
unsigned StartOff, EndOff;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-17 06:36:42 +08:00
|
|
|
StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
|
|
|
|
EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-17 06:36:42 +08:00
|
|
|
if (StartFileID != EndFileID)
|
|
|
|
return -1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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;
|
2011-04-13 15:15:11 +08:00
|
|
|
EndOff = RB.getMappedOffset(EndOff, opts.IncludeInsertsAtEndOfRange);
|
|
|
|
StartOff = RB.getMappedOffset(StartOff, !opts.IncludeInsertsAtBeginOfRange);
|
2007-10-26 01:17:34 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +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
|
2010-06-19 06:45:06 +08:00
|
|
|
// start of the last token if this is a token range.
|
|
|
|
if (Range.isTokenRange())
|
|
|
|
EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-26 01:18:59 +08:00
|
|
|
return EndOff-StartOff;
|
2007-10-17 06:36:42 +08:00
|
|
|
}
|
|
|
|
|
2011-04-13 15:15:11 +08:00
|
|
|
int Rewriter::getRangeSize(SourceRange Range, RewriteOptions opts) const {
|
|
|
|
return getRangeSize(CharSourceRange::getTokenRange(Range), opts);
|
2010-06-19 06:45:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-08 02:00:35 +08:00
|
|
|
/// getRewrittenText - Return the rewritten form of the text in the specified
|
2008-10-04 07:31:16 +08:00
|
|
|
/// range. If the start or end of the range was unrewritable or if they are
|
2009-09-09 23:08:12 +08:00
|
|
|
/// in different buffers, this returns an empty string.
|
2008-10-04 07:31:16 +08:00
|
|
|
///
|
|
|
|
/// Note that this method is not particularly efficient.
|
|
|
|
///
|
2010-01-08 02:00:35 +08:00
|
|
|
std::string Rewriter::getRewrittenText(SourceRange Range) const {
|
2008-10-04 07:31:16 +08:00
|
|
|
if (!isRewritable(Range.getBegin()) ||
|
|
|
|
!isRewritable(Range.getEnd()))
|
|
|
|
return "";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 07:31:16 +08:00
|
|
|
if (StartFileID != EndFileID)
|
|
|
|
return ""; // Start and end in different buffers.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 07:31:16 +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 =
|
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());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 07:31:16 +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);
|
2008-10-04 07:31:16 +08:00
|
|
|
return std::string(Ptr, Ptr+EndOff-StartOff);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 07:31:16 +08:00
|
|
|
const RewriteBuffer &RB = I->second;
|
|
|
|
EndOff = RB.getMappedOffset(EndOff, true);
|
|
|
|
StartOff = RB.getMappedOffset(StartOff);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 07:31:16 +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);
|
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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 07:31:16 +08:00
|
|
|
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);
|
2009-09-09 23:08:12 +08:00
|
|
|
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()));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MB = SourceMgr->getBufferData(FID);
|
2010-03-16 22:14:31 +08:00
|
|
|
I->second.Initialize(MB.begin(), MB.end());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-12 02:38:32 +08:00
|
|
|
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.
|
2011-07-23 18:55:15 +08:00
|
|
|
bool Rewriter::InsertText(SourceLocation Loc, StringRef Str,
|
2011-06-16 07:02:42 +08:00
|
|
|
bool InsertAfter, bool indentNewLines) {
|
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);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
llvm::SmallString<128> indentedStr;
|
|
|
|
if (indentNewLines && Str.find('\n') != StringRef::npos) {
|
|
|
|
StringRef MB = SourceMgr->getBufferData(FID);
|
|
|
|
|
|
|
|
unsigned lineNo = SourceMgr->getLineNumber(FID, StartOffs) - 1;
|
|
|
|
const SrcMgr::ContentCache *
|
|
|
|
Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache();
|
|
|
|
unsigned lineOffs = Content->SourceLineCache[lineNo];
|
|
|
|
|
|
|
|
// Find the whitespace at the start of the line.
|
|
|
|
StringRef indentSpace;
|
|
|
|
{
|
|
|
|
unsigned i = lineOffs;
|
|
|
|
while (isWhitespace(MB[i]))
|
|
|
|
++i;
|
|
|
|
indentSpace = MB.substr(lineOffs, i-lineOffs);
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<StringRef, 4> lines;
|
2011-06-16 07:02:42 +08:00
|
|
|
Str.split(lines, "\n");
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = lines.size(); i != e; ++i) {
|
|
|
|
indentedStr += lines[i];
|
|
|
|
if (i < e-1) {
|
|
|
|
indentedStr += '\n';
|
|
|
|
indentedStr += indentSpace;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Str = indentedStr.str();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
bool Rewriter::InsertTextAfterToken(SourceLocation Loc, StringRef Str) {
|
2011-04-08 02:10:12 +08:00
|
|
|
if (!isRewritable(Loc)) return true;
|
|
|
|
FileID FID;
|
|
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
|
2011-04-13 15:15:11 +08:00
|
|
|
RewriteOptions rangeOpts;
|
|
|
|
rangeOpts.IncludeInsertsAtBeginOfRange = false;
|
|
|
|
StartOffs += getRangeSize(SourceRange(Loc, Loc), rangeOpts);
|
2011-04-08 02:10:12 +08:00
|
|
|
getEditBuffer(FID).InsertText(StartOffs, Str, /*InsertAfter*/true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-02-01 03:51:04 +08:00
|
|
|
/// RemoveText - Remove the specified text region.
|
2011-04-08 02:10:12 +08:00
|
|
|
bool Rewriter::RemoveText(SourceLocation Start, unsigned Length,
|
2011-04-13 15:15:11 +08:00
|
|
|
RewriteOptions opts) {
|
2008-02-01 03:51:04 +08:00
|
|
|
if (!isRewritable(Start)) return true;
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID FID;
|
|
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Start, FID);
|
2011-04-13 15:15:11 +08:00
|
|
|
getEditBuffer(FID).RemoveText(StartOffs, Length, opts.RemoveLineIfEmpty);
|
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,
|
2011-07-23 18:55:15 +08:00
|
|
|
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);
|
2009-09-09 23:08:12 +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
|
|
|
|
2011-04-08 02:10:12 +08:00
|
|
|
bool Rewriter::ReplaceText(SourceRange range, SourceRange replacementRange) {
|
|
|
|
if (!isRewritable(range.getBegin())) return true;
|
|
|
|
if (!isRewritable(range.getEnd())) return true;
|
|
|
|
if (replacementRange.isInvalid()) return true;
|
|
|
|
SourceLocation start = range.getBegin();
|
|
|
|
unsigned origLength = getRangeSize(range);
|
|
|
|
unsigned newLength = getRangeSize(replacementRange);
|
|
|
|
FileID FID;
|
|
|
|
unsigned newOffs = getLocationOffsetAndFileID(replacementRange.getBegin(),
|
|
|
|
FID);
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MB = SourceMgr->getBufferData(FID);
|
2011-04-08 02:10:12 +08:00
|
|
|
return ReplaceText(start, origLength, MB.substr(newOffs, newLength));
|
|
|
|
}
|
|
|
|
|
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.
|
2010-02-06 00:43:40 +08:00
|
|
|
bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) {
|
2007-10-18 06:35:30 +08:00
|
|
|
// Measaure the old text.
|
2010-02-06 00:43:40 +08:00
|
|
|
int Size = getRangeSize(From->getSourceRange());
|
2007-10-18 06:35:30 +08:00
|
|
|
if (Size == -1)
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-18 06:35:30 +08:00
|
|
|
// 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;
|
|
|
|
}
|
2011-04-12 05:17:02 +08:00
|
|
|
|
|
|
|
std::string Rewriter::ConvertToString(Stmt *From) {
|
|
|
|
std::string SStr;
|
|
|
|
llvm::raw_string_ostream S(SStr);
|
|
|
|
From->printPretty(S, 0, PrintingPolicy(*LangOpts));
|
2011-04-21 00:38:37 +08:00
|
|
|
return S.str();
|
2011-04-12 05:17:02 +08:00
|
|
|
}
|
2011-04-16 09:03:33 +08:00
|
|
|
|
|
|
|
bool Rewriter::IncreaseIndentation(CharSourceRange range,
|
|
|
|
SourceLocation parentIndent) {
|
2011-06-16 07:02:42 +08:00
|
|
|
if (range.isInvalid()) return true;
|
2011-04-16 09:03:33 +08:00
|
|
|
if (!isRewritable(range.getBegin())) return true;
|
|
|
|
if (!isRewritable(range.getEnd())) return true;
|
|
|
|
if (!isRewritable(parentIndent)) return true;
|
|
|
|
|
|
|
|
FileID StartFileID, EndFileID, parentFileID;
|
|
|
|
unsigned StartOff, EndOff, parentOff;
|
|
|
|
|
|
|
|
StartOff = getLocationOffsetAndFileID(range.getBegin(), StartFileID);
|
|
|
|
EndOff = getLocationOffsetAndFileID(range.getEnd(), EndFileID);
|
|
|
|
parentOff = getLocationOffsetAndFileID(parentIndent, parentFileID);
|
|
|
|
|
|
|
|
if (StartFileID != EndFileID || StartFileID != parentFileID)
|
|
|
|
return true;
|
2011-06-16 07:02:42 +08:00
|
|
|
if (StartOff > EndOff)
|
2011-04-16 09:03:33 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
FileID FID = StartFileID;
|
|
|
|
StringRef MB = SourceMgr->getBufferData(FID);
|
|
|
|
|
|
|
|
unsigned parentLineNo = SourceMgr->getLineNumber(FID, parentOff) - 1;
|
|
|
|
unsigned startLineNo = SourceMgr->getLineNumber(FID, StartOff) - 1;
|
|
|
|
unsigned endLineNo = SourceMgr->getLineNumber(FID, EndOff) - 1;
|
|
|
|
|
|
|
|
const SrcMgr::ContentCache *
|
|
|
|
Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache();
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
// Find where the lines start.
|
2011-04-16 09:03:33 +08:00
|
|
|
unsigned parentLineOffs = Content->SourceLineCache[parentLineNo];
|
|
|
|
unsigned startLineOffs = Content->SourceLineCache[startLineNo];
|
|
|
|
|
|
|
|
// Find the whitespace at the start of each line.
|
2011-06-16 07:02:42 +08:00
|
|
|
StringRef parentSpace, startSpace;
|
2011-04-16 09:03:33 +08:00
|
|
|
{
|
|
|
|
unsigned i = parentLineOffs;
|
|
|
|
while (isWhitespace(MB[i]))
|
|
|
|
++i;
|
|
|
|
parentSpace = MB.substr(parentLineOffs, i-parentLineOffs);
|
|
|
|
|
|
|
|
i = startLineOffs;
|
|
|
|
while (isWhitespace(MB[i]))
|
|
|
|
++i;
|
|
|
|
startSpace = MB.substr(startLineOffs, i-startLineOffs);
|
|
|
|
}
|
|
|
|
if (parentSpace.size() >= startSpace.size())
|
|
|
|
return true;
|
|
|
|
if (!startSpace.startswith(parentSpace))
|
|
|
|
return true;
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef indent = startSpace.substr(parentSpace.size());
|
2011-04-16 09:03:33 +08:00
|
|
|
|
|
|
|
// Indent the lines between start/end offsets.
|
|
|
|
RewriteBuffer &RB = getEditBuffer(FID);
|
2011-06-16 07:02:42 +08:00
|
|
|
for (unsigned lineNo = startLineNo; lineNo <= endLineNo; ++lineNo) {
|
|
|
|
unsigned offs = Content->SourceLineCache[lineNo];
|
|
|
|
unsigned i = offs;
|
|
|
|
while (isWhitespace(MB[i]))
|
|
|
|
++i;
|
|
|
|
StringRef origIndent = MB.substr(offs, i-offs);
|
|
|
|
if (origIndent.startswith(startSpace))
|
|
|
|
RB.InsertText(offs, indent, /*InsertAfter=*/false);
|
2011-04-16 09:03:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|