git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@49587 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2008-04-12 20:34:05 +00:00
Родитель 5c9dc5ac75
Коммит 77257889f5
2 изменённых файлов: 8 добавлений и 87 удалений

Просмотреть файл

@ -23,8 +23,6 @@
#include <string>
#include "clang/Rewrite/DeltaTree.h"
//#define USE_VECTOR 1
namespace clang {
class SourceManager;
class Rewriter;
@ -38,13 +36,9 @@ namespace clang {
/// locations after the insertion point have to be mapped.
class RewriteBuffer {
friend class Rewriter;
#ifdef USE_VECTOR
/// Deltas - Keep track of all the deltas in the source code due to insertions
/// and deletions. These are kept in sorted order based on the FileLoc.
std::vector<SourceDelta> Deltas;
#else
/// and deletions.
DeltaTree Deltas;
#endif
/// Buffer - This is the actual buffer itself. Note that using a vector or
/// string is a horribly inefficient way to do this, we should use a rope
@ -69,12 +63,16 @@ private: // Methods only usable by Rewriter.
/// RewriteBuffer. If AfterInserts is true and if the OrigOffset indicates a
/// position where text is inserted, the location returned will be after any
/// inserted text at the position.
unsigned getMappedOffset(unsigned OrigOffset, bool AfterInserts = false)const;
unsigned getMappedOffset(unsigned OrigOffset,
bool AfterInserts = false) const{
return Deltas.getDeltaAt(OrigOffset+AfterInserts)+OrigOffset;
}
/// AddDelta - When a change is made that shifts around the text buffer, this
/// method is used to record that info.
void AddDelta(unsigned OrigOffset, int Change);
void AddDelta(unsigned OrigOffset, int Change) {
return Deltas.AddDelta(OrigOffset, Change);
}
/// RemoveText - Remove the specified text.
void RemoveText(unsigned OrigOffset, unsigned Size);

Просмотреть файл

@ -19,83 +19,6 @@
#include <sstream>
using namespace clang;
/// getMappedOffset - Given an offset into the original SourceBuffer that this
/// RewriteBuffer is based on, map it into the offset space of the
/// RewriteBuffer.
unsigned RewriteBuffer::getMappedOffset(unsigned OrigOffset,
bool AfterInserts) const {
unsigned ResultOffset = 0;
#if !defined(USE_VECTOR)
ResultOffset += Deltas.getDeltaAt(OrigOffset+AfterInserts);
#else
unsigned DeltaIdx = 0;
// Move past any deltas that are relevant.
// FIXME: binary search.
for (; DeltaIdx != Deltas.size() &&
Deltas[DeltaIdx].FileLoc < OrigOffset; ++DeltaIdx)
ResultOffset += Deltas[DeltaIdx].Delta;
if (AfterInserts)
for (; DeltaIdx != Deltas.size() &&
OrigOffset == Deltas[DeltaIdx].FileLoc; ++DeltaIdx)
ResultOffset += Deltas[DeltaIdx].Delta;
#endif
// printf("Map: %d/%d -> %d\n", OrigOffset, AfterInserts, ResultOffset);
return ResultOffset+OrigOffset;
}
/// AddDelta - When a change is made that shifts around the text buffer, this
/// method is used to record that info.
void RewriteBuffer::AddDelta(unsigned OrigOffset, int Change) {
// printf("AddDelta: %d/%d\n", OrigOffset, Change);
#if !defined(USE_VECTOR)
return Deltas.AddDelta(OrigOffset, Change);
#else
assert(Change != 0 && "Not changing anything");
unsigned DeltaIdx = 0;
// Skip over any unrelated deltas.
for (; DeltaIdx != Deltas.size() &&
Deltas[DeltaIdx].FileLoc < OrigOffset; ++DeltaIdx)
;
// If there is no a delta for this offset, insert a new delta record.
if (DeltaIdx == Deltas.size() || OrigOffset != Deltas[DeltaIdx].FileLoc) {
// If this is a removal, check to see if this can be folded into
// a delta at the end of the deletion. For example, if we have:
// ABCXDEF (X inserted after C) and delete C, we want to end up with no
// delta because X basically replaced C.
if (Change < 0 && DeltaIdx != Deltas.size() &&
OrigOffset-Change == Deltas[DeltaIdx].FileLoc) {
// Adjust the start of the delta to be the start of the deleted region.
Deltas[DeltaIdx].FileLoc += Change;
Deltas[DeltaIdx].Delta += Change;
// If the delta becomes a noop, remove it.
if (Deltas[DeltaIdx].Delta == 0)
Deltas.erase(Deltas.begin()+DeltaIdx);
return;
}
// Otherwise, create an entry and return.
Deltas.insert(Deltas.begin()+DeltaIdx,
SourceDelta::get(OrigOffset, Change));
return;
}
// Otherwise, we found a delta record at this offset, adjust it.
Deltas[DeltaIdx].Delta += Change;
// If it is now dead, remove it.
if (Deltas[DeltaIdx].Delta == 0)
Deltas.erase(Deltas.begin()+DeltaIdx);
#endif
}
void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size) {
// Nothing to remove, exit early.
if (Size == 0) return;