2018-03-27 08:01:49 +08:00
|
|
|
//===- EditedSource.cpp - Collection of source edits ----------------------===//
|
2012-03-07 04:06:33 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-03-07 04:06:33 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Edit/EditedSource.h"
|
2013-02-09 06:30:41 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2018-03-27 08:01:49 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2012-03-07 04:06:33 +08:00
|
|
|
#include "clang/Edit/Commit.h"
|
|
|
|
#include "clang/Edit/EditsReceiver.h"
|
2018-03-27 08:01:49 +08:00
|
|
|
#include "clang/Edit/FileOffset.h"
|
2012-03-07 04:06:33 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2018-03-27 08:01:49 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-03-07 04:06:33 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2018-03-27 08:01:49 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-03-07 04:06:33 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2018-03-27 08:01:49 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <tuple>
|
|
|
|
#include <utility>
|
2012-03-07 04:06:33 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace edit;
|
|
|
|
|
|
|
|
void EditsReceiver::remove(CharSourceRange range) {
|
|
|
|
replace(range, StringRef());
|
|
|
|
}
|
|
|
|
|
2015-09-12 04:09:11 +08:00
|
|
|
void EditedSource::deconstructMacroArgLoc(SourceLocation Loc,
|
|
|
|
SourceLocation &ExpansionLoc,
|
2017-06-09 05:44:45 +08:00
|
|
|
MacroArgUse &ArgUse) {
|
2015-09-12 04:09:11 +08:00
|
|
|
assert(SourceMgr.isMacroArgExpansion(Loc));
|
2018-04-30 13:25:48 +08:00
|
|
|
SourceLocation DefArgLoc =
|
|
|
|
SourceMgr.getImmediateExpansionRange(Loc).getBegin();
|
2017-06-21 04:46:58 +08:00
|
|
|
SourceLocation ImmediateExpansionLoc =
|
2018-04-30 13:25:48 +08:00
|
|
|
SourceMgr.getImmediateExpansionRange(DefArgLoc).getBegin();
|
2017-06-21 04:46:58 +08:00
|
|
|
ExpansionLoc = ImmediateExpansionLoc;
|
|
|
|
while (SourceMgr.isMacroBodyExpansion(ExpansionLoc))
|
2018-04-30 13:25:48 +08:00
|
|
|
ExpansionLoc =
|
|
|
|
SourceMgr.getImmediateExpansionRange(ExpansionLoc).getBegin();
|
2015-09-12 04:09:11 +08:00
|
|
|
SmallString<20> Buf;
|
|
|
|
StringRef ArgName = Lexer::getSpelling(SourceMgr.getSpellingLoc(DefArgLoc),
|
|
|
|
Buf, SourceMgr, LangOpts);
|
2017-06-21 04:46:58 +08:00
|
|
|
ArgUse = MacroArgUse{nullptr, SourceLocation(), SourceLocation()};
|
2017-06-09 05:44:45 +08:00
|
|
|
if (!ArgName.empty())
|
2017-06-21 04:46:58 +08:00
|
|
|
ArgUse = {&IdentTable.get(ArgName), ImmediateExpansionLoc,
|
|
|
|
SourceMgr.getSpellingLoc(DefArgLoc)};
|
2015-09-12 04:09:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditedSource::startingCommit() {}
|
|
|
|
|
|
|
|
void EditedSource::finishedCommit() {
|
|
|
|
for (auto &ExpArg : CurrCommitMacroArgExps) {
|
|
|
|
SourceLocation ExpLoc;
|
2017-06-09 05:44:45 +08:00
|
|
|
MacroArgUse ArgUse;
|
|
|
|
std::tie(ExpLoc, ArgUse) = ExpArg;
|
|
|
|
auto &ArgUses = ExpansionToArgMap[ExpLoc.getRawEncoding()];
|
2019-03-31 16:48:19 +08:00
|
|
|
if (llvm::find(ArgUses, ArgUse) == ArgUses.end())
|
2017-06-09 05:44:45 +08:00
|
|
|
ArgUses.push_back(ArgUse);
|
2015-09-12 04:09:11 +08:00
|
|
|
}
|
|
|
|
CurrCommitMacroArgExps.clear();
|
|
|
|
}
|
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
StringRef EditedSource::copyString(const Twine &twine) {
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<128> Data;
|
2012-03-07 04:06:33 +08:00
|
|
|
return copyString(twine.toStringRef(Data));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditedSource::canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs) {
|
|
|
|
FileEditsTy::iterator FA = getActionForOffset(Offs);
|
|
|
|
if (FA != FileEdits.end()) {
|
|
|
|
if (FA->first != Offs)
|
|
|
|
return false; // position has been removed.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SourceMgr.isMacroArgExpansion(OrigLoc)) {
|
2015-09-12 04:09:11 +08:00
|
|
|
SourceLocation ExpLoc;
|
2017-06-09 05:44:45 +08:00
|
|
|
MacroArgUse ArgUse;
|
|
|
|
deconstructMacroArgLoc(OrigLoc, ExpLoc, ArgUse);
|
2015-09-12 04:09:11 +08:00
|
|
|
auto I = ExpansionToArgMap.find(ExpLoc.getRawEncoding());
|
|
|
|
if (I != ExpansionToArgMap.end() &&
|
2017-06-21 04:46:58 +08:00
|
|
|
find_if(I->second, [&](const MacroArgUse &U) {
|
|
|
|
return ArgUse.Identifier == U.Identifier &&
|
|
|
|
std::tie(ArgUse.ImmediateExpansionLoc, ArgUse.UseLoc) !=
|
|
|
|
std::tie(U.ImmediateExpansionLoc, U.UseLoc);
|
|
|
|
}) != I->second.end()) {
|
2015-09-12 04:09:11 +08:00
|
|
|
// Trying to write in a macro argument input that has already been
|
|
|
|
// written by a previous commit for another expansion of the same macro
|
|
|
|
// argument name. For example:
|
|
|
|
//
|
|
|
|
// \code
|
|
|
|
// #define MAC(x) ((x)+(x))
|
|
|
|
// MAC(a)
|
|
|
|
// \endcode
|
|
|
|
//
|
|
|
|
// A commit modified the macro argument 'a' due to the first '(x)'
|
|
|
|
// expansion inside the macro definition, and a subsequent commit tried
|
|
|
|
// to modify 'a' again for the second '(x)' expansion. The edits of the
|
|
|
|
// second commit will be rejected.
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-07 04:06:33 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditedSource::commitInsert(SourceLocation OrigLoc,
|
|
|
|
FileOffset Offs, StringRef text,
|
|
|
|
bool beforePreviousInsertions) {
|
|
|
|
if (!canInsertInOffset(OrigLoc, Offs))
|
|
|
|
return false;
|
|
|
|
if (text.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (SourceMgr.isMacroArgExpansion(OrigLoc)) {
|
2017-06-09 05:44:45 +08:00
|
|
|
MacroArgUse ArgUse;
|
2017-06-21 04:46:58 +08:00
|
|
|
SourceLocation ExpLoc;
|
2017-06-09 05:44:45 +08:00
|
|
|
deconstructMacroArgLoc(OrigLoc, ExpLoc, ArgUse);
|
2017-06-21 04:46:58 +08:00
|
|
|
if (ArgUse.Identifier)
|
2017-06-09 05:44:45 +08:00
|
|
|
CurrCommitMacroArgExps.emplace_back(ExpLoc, ArgUse);
|
2012-03-07 04:06:33 +08:00
|
|
|
}
|
2017-06-21 04:46:58 +08:00
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
FileEdit &FA = FileEdits[Offs];
|
|
|
|
if (FA.Text.empty()) {
|
|
|
|
FA.Text = copyString(text);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (beforePreviousInsertions)
|
2014-03-30 00:54:24 +08:00
|
|
|
FA.Text = copyString(Twine(text) + FA.Text);
|
2012-03-07 04:06:33 +08:00
|
|
|
else
|
2014-03-30 00:54:24 +08:00
|
|
|
FA.Text = copyString(Twine(FA.Text) + text);
|
2012-03-07 04:06:33 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditedSource::commitInsertFromRange(SourceLocation OrigLoc,
|
|
|
|
FileOffset Offs,
|
|
|
|
FileOffset InsertFromRangeOffs, unsigned Len,
|
|
|
|
bool beforePreviousInsertions) {
|
|
|
|
if (Len == 0)
|
|
|
|
return true;
|
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<128> StrVec;
|
2012-03-07 04:06:33 +08:00
|
|
|
FileOffset BeginOffs = InsertFromRangeOffs;
|
|
|
|
FileOffset EndOffs = BeginOffs.getWithOffset(Len);
|
|
|
|
FileEditsTy::iterator I = FileEdits.upper_bound(BeginOffs);
|
|
|
|
if (I != FileEdits.begin())
|
|
|
|
--I;
|
|
|
|
|
|
|
|
for (; I != FileEdits.end(); ++I) {
|
|
|
|
FileEdit &FA = I->second;
|
|
|
|
FileOffset B = I->first;
|
|
|
|
FileOffset E = B.getWithOffset(FA.RemoveLen);
|
|
|
|
|
2012-05-15 06:01:53 +08:00
|
|
|
if (BeginOffs == B)
|
|
|
|
break;
|
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
if (BeginOffs < E) {
|
2012-05-15 06:01:53 +08:00
|
|
|
if (BeginOffs > B) {
|
2012-03-07 04:06:33 +08:00
|
|
|
BeginOffs = E;
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; I != FileEdits.end() && EndOffs > I->first; ++I) {
|
|
|
|
FileEdit &FA = I->second;
|
|
|
|
FileOffset B = I->first;
|
|
|
|
FileOffset E = B.getWithOffset(FA.RemoveLen);
|
|
|
|
|
|
|
|
if (BeginOffs < B) {
|
|
|
|
bool Invalid = false;
|
|
|
|
StringRef text = getSourceText(BeginOffs, B, Invalid);
|
|
|
|
if (Invalid)
|
|
|
|
return false;
|
|
|
|
StrVec += text;
|
|
|
|
}
|
|
|
|
StrVec += FA.Text;
|
|
|
|
BeginOffs = E;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BeginOffs < EndOffs) {
|
|
|
|
bool Invalid = false;
|
|
|
|
StringRef text = getSourceText(BeginOffs, EndOffs, Invalid);
|
|
|
|
if (Invalid)
|
|
|
|
return false;
|
|
|
|
StrVec += text;
|
|
|
|
}
|
|
|
|
|
2015-03-18 18:17:07 +08:00
|
|
|
return commitInsert(OrigLoc, Offs, StrVec, beforePreviousInsertions);
|
2012-03-07 04:06:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditedSource::commitRemove(SourceLocation OrigLoc,
|
|
|
|
FileOffset BeginOffs, unsigned Len) {
|
|
|
|
if (Len == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FileOffset EndOffs = BeginOffs.getWithOffset(Len);
|
|
|
|
FileEditsTy::iterator I = FileEdits.upper_bound(BeginOffs);
|
|
|
|
if (I != FileEdits.begin())
|
|
|
|
--I;
|
|
|
|
|
|
|
|
for (; I != FileEdits.end(); ++I) {
|
|
|
|
FileEdit &FA = I->second;
|
|
|
|
FileOffset B = I->first;
|
|
|
|
FileOffset E = B.getWithOffset(FA.RemoveLen);
|
|
|
|
|
|
|
|
if (BeginOffs < E)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileOffset TopBegin, TopEnd;
|
2014-05-09 16:15:10 +08:00
|
|
|
FileEdit *TopFA = nullptr;
|
2012-03-07 04:06:33 +08:00
|
|
|
|
|
|
|
if (I == FileEdits.end()) {
|
|
|
|
FileEditsTy::iterator
|
|
|
|
NewI = FileEdits.insert(I, std::make_pair(BeginOffs, FileEdit()));
|
|
|
|
NewI->second.RemoveLen = Len;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileEdit &FA = I->second;
|
|
|
|
FileOffset B = I->first;
|
|
|
|
FileOffset E = B.getWithOffset(FA.RemoveLen);
|
|
|
|
if (BeginOffs < B) {
|
|
|
|
FileEditsTy::iterator
|
|
|
|
NewI = FileEdits.insert(I, std::make_pair(BeginOffs, FileEdit()));
|
|
|
|
TopBegin = BeginOffs;
|
|
|
|
TopEnd = EndOffs;
|
|
|
|
TopFA = &NewI->second;
|
|
|
|
TopFA->RemoveLen = Len;
|
|
|
|
} else {
|
|
|
|
TopBegin = B;
|
|
|
|
TopEnd = E;
|
|
|
|
TopFA = &I->second;
|
|
|
|
if (TopEnd >= EndOffs)
|
|
|
|
return;
|
|
|
|
unsigned diff = EndOffs.getOffset() - TopEnd.getOffset();
|
|
|
|
TopEnd = EndOffs;
|
|
|
|
TopFA->RemoveLen += diff;
|
2013-04-06 09:13:17 +08:00
|
|
|
if (B == BeginOffs)
|
|
|
|
TopFA->Text = StringRef();
|
2012-03-07 04:06:33 +08:00
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (I != FileEdits.end()) {
|
|
|
|
FileEdit &FA = I->second;
|
|
|
|
FileOffset B = I->first;
|
|
|
|
FileOffset E = B.getWithOffset(FA.RemoveLen);
|
|
|
|
|
|
|
|
if (B >= TopEnd)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (E <= TopEnd) {
|
|
|
|
FileEdits.erase(I++);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (B < TopEnd) {
|
|
|
|
unsigned diff = E.getOffset() - TopEnd.getOffset();
|
|
|
|
TopEnd = E;
|
|
|
|
TopFA->RemoveLen += diff;
|
|
|
|
FileEdits.erase(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditedSource::commit(const Commit &commit) {
|
|
|
|
if (!commit.isCommitable())
|
|
|
|
return false;
|
|
|
|
|
2015-09-12 04:09:11 +08:00
|
|
|
struct CommitRAII {
|
|
|
|
EditedSource &Editor;
|
2018-03-27 08:01:49 +08:00
|
|
|
|
2015-09-12 04:09:11 +08:00
|
|
|
CommitRAII(EditedSource &Editor) : Editor(Editor) {
|
|
|
|
Editor.startingCommit();
|
|
|
|
}
|
2018-03-27 08:01:49 +08:00
|
|
|
|
2015-09-12 04:09:11 +08:00
|
|
|
~CommitRAII() {
|
|
|
|
Editor.finishedCommit();
|
|
|
|
}
|
|
|
|
} CommitRAII(*this);
|
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
for (edit::Commit::edit_iterator
|
|
|
|
I = commit.edit_begin(), E = commit.edit_end(); I != E; ++I) {
|
|
|
|
const edit::Commit::Edit &edit = *I;
|
|
|
|
switch (edit.Kind) {
|
|
|
|
case edit::Commit::Act_Insert:
|
|
|
|
commitInsert(edit.OrigLoc, edit.Offset, edit.Text, edit.BeforePrev);
|
|
|
|
break;
|
|
|
|
case edit::Commit::Act_InsertFromRange:
|
|
|
|
commitInsertFromRange(edit.OrigLoc, edit.Offset,
|
|
|
|
edit.InsertFromRangeOffs, edit.Length,
|
|
|
|
edit.BeforePrev);
|
|
|
|
break;
|
|
|
|
case edit::Commit::Act_Remove:
|
|
|
|
commitRemove(edit.OrigLoc, edit.Offset, edit.Length);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
// Returns true if it is ok to make the two given characters adjacent.
|
2012-12-21 05:05:53 +08:00
|
|
|
static bool canBeJoined(char left, char right, const LangOptions &LangOpts) {
|
2013-02-09 06:30:41 +08:00
|
|
|
// FIXME: Should use TokenConcatenation to make sure we don't allow stuff like
|
2012-12-21 05:05:53 +08:00
|
|
|
// making two '<' adjacent.
|
2013-02-09 06:30:41 +08:00
|
|
|
return !(Lexer::isIdentifierBodyChar(left, LangOpts) &&
|
|
|
|
Lexer::isIdentifierBodyChar(right, LangOpts));
|
2012-12-21 05:05:53 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns true if it is ok to eliminate the trailing whitespace between
|
2012-12-21 05:05:53 +08:00
|
|
|
/// the given characters.
|
|
|
|
static bool canRemoveWhitespace(char left, char beforeWSpace, char right,
|
|
|
|
const LangOptions &LangOpts) {
|
|
|
|
if (!canBeJoined(left, right, LangOpts))
|
|
|
|
return false;
|
2013-02-09 06:30:41 +08:00
|
|
|
if (isWhitespace(left) || isWhitespace(right))
|
2012-12-21 05:05:53 +08:00
|
|
|
return true;
|
|
|
|
if (canBeJoined(beforeWSpace, right, LangOpts))
|
|
|
|
return false; // the whitespace was intentional, keep it.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Check the range that we are going to remove and:
|
2012-12-21 05:05:53 +08:00
|
|
|
/// -Remove any trailing whitespace if possible.
|
|
|
|
/// -Insert a space if removing the range is going to mess up the source tokens.
|
|
|
|
static void adjustRemoval(const SourceManager &SM, const LangOptions &LangOpts,
|
|
|
|
SourceLocation Loc, FileOffset offs,
|
|
|
|
unsigned &len, StringRef &text) {
|
|
|
|
assert(len && text.empty());
|
|
|
|
SourceLocation BeginTokLoc = Lexer::GetBeginningOfToken(Loc, SM, LangOpts);
|
|
|
|
if (BeginTokLoc != Loc)
|
|
|
|
return; // the range is not at the beginning of a token, keep the range.
|
|
|
|
|
|
|
|
bool Invalid = false;
|
|
|
|
StringRef buffer = SM.getBufferData(offs.getFID(), &Invalid);
|
|
|
|
if (Invalid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned begin = offs.getOffset();
|
|
|
|
unsigned end = begin + len;
|
|
|
|
|
2014-09-15 19:47:10 +08:00
|
|
|
// Do not try to extend the removal if we're at the end of the buffer already.
|
|
|
|
if (end == buffer.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(begin < buffer.size() && end < buffer.size() && "Invalid range!");
|
|
|
|
|
2012-12-21 05:05:53 +08:00
|
|
|
// FIXME: Remove newline.
|
|
|
|
|
|
|
|
if (begin == 0) {
|
|
|
|
if (buffer[end] == ' ')
|
|
|
|
++len;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer[end] == ' ') {
|
2015-03-30 02:07:29 +08:00
|
|
|
assert((end + 1 != buffer.size() || buffer.data()[end + 1] == 0) &&
|
|
|
|
"buffer not zero-terminated!");
|
2012-12-21 05:05:53 +08:00
|
|
|
if (canRemoveWhitespace(/*left=*/buffer[begin-1],
|
|
|
|
/*beforeWSpace=*/buffer[end-1],
|
2015-03-30 02:07:29 +08:00
|
|
|
/*right=*/buffer.data()[end + 1], // zero-terminated
|
2012-12-21 05:05:53 +08:00
|
|
|
LangOpts))
|
|
|
|
++len;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!canBeJoined(buffer[begin-1], buffer[end], LangOpts))
|
|
|
|
text = " ";
|
|
|
|
}
|
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
static void applyRewrite(EditsReceiver &receiver,
|
|
|
|
StringRef text, FileOffset offs, unsigned len,
|
2017-04-28 08:25:06 +08:00
|
|
|
const SourceManager &SM, const LangOptions &LangOpts,
|
|
|
|
bool shouldAdjustRemovals) {
|
2015-10-03 18:46:20 +08:00
|
|
|
assert(offs.getFID().isValid());
|
2012-03-07 04:06:33 +08:00
|
|
|
SourceLocation Loc = SM.getLocForStartOfFile(offs.getFID());
|
|
|
|
Loc = Loc.getLocWithOffset(offs.getOffset());
|
|
|
|
assert(Loc.isFileID());
|
2012-12-21 05:05:53 +08:00
|
|
|
|
2017-04-28 08:25:06 +08:00
|
|
|
if (text.empty() && shouldAdjustRemovals)
|
2012-12-21 05:05:53 +08:00
|
|
|
adjustRemoval(SM, LangOpts, Loc, offs, len, text);
|
|
|
|
|
2012-03-07 04:06:33 +08:00
|
|
|
CharSourceRange range = CharSourceRange::getCharRange(Loc,
|
|
|
|
Loc.getLocWithOffset(len));
|
|
|
|
|
|
|
|
if (text.empty()) {
|
|
|
|
assert(len);
|
|
|
|
receiver.remove(range);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len)
|
|
|
|
receiver.replace(range, text);
|
|
|
|
else
|
|
|
|
receiver.insert(Loc, text);
|
|
|
|
}
|
|
|
|
|
2017-04-28 08:25:06 +08:00
|
|
|
void EditedSource::applyRewrites(EditsReceiver &receiver,
|
|
|
|
bool shouldAdjustRemovals) {
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<128> StrVec;
|
2012-03-07 04:06:33 +08:00
|
|
|
FileOffset CurOffs, CurEnd;
|
|
|
|
unsigned CurLen;
|
|
|
|
|
|
|
|
if (FileEdits.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
FileEditsTy::iterator I = FileEdits.begin();
|
|
|
|
CurOffs = I->first;
|
|
|
|
StrVec = I->second.Text;
|
|
|
|
CurLen = I->second.RemoveLen;
|
|
|
|
CurEnd = CurOffs.getWithOffset(CurLen);
|
|
|
|
++I;
|
|
|
|
|
|
|
|
for (FileEditsTy::iterator E = FileEdits.end(); I != E; ++I) {
|
|
|
|
FileOffset offs = I->first;
|
|
|
|
FileEdit act = I->second;
|
|
|
|
assert(offs >= CurEnd);
|
|
|
|
|
|
|
|
if (offs == CurEnd) {
|
|
|
|
StrVec += act.Text;
|
|
|
|
CurLen += act.RemoveLen;
|
|
|
|
CurEnd.getWithOffset(act.RemoveLen);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-04-28 08:25:06 +08:00
|
|
|
applyRewrite(receiver, StrVec, CurOffs, CurLen, SourceMgr, LangOpts,
|
|
|
|
shouldAdjustRemovals);
|
2012-03-07 04:06:33 +08:00
|
|
|
CurOffs = offs;
|
|
|
|
StrVec = act.Text;
|
|
|
|
CurLen = act.RemoveLen;
|
|
|
|
CurEnd = CurOffs.getWithOffset(CurLen);
|
|
|
|
}
|
|
|
|
|
2017-04-28 08:25:06 +08:00
|
|
|
applyRewrite(receiver, StrVec, CurOffs, CurLen, SourceMgr, LangOpts,
|
|
|
|
shouldAdjustRemovals);
|
2012-03-07 04:06:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditedSource::clearRewrites() {
|
|
|
|
FileEdits.clear();
|
|
|
|
StrAlloc.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef EditedSource::getSourceText(FileOffset BeginOffs, FileOffset EndOffs,
|
|
|
|
bool &Invalid) {
|
|
|
|
assert(BeginOffs.getFID() == EndOffs.getFID());
|
|
|
|
assert(BeginOffs <= EndOffs);
|
|
|
|
SourceLocation BLoc = SourceMgr.getLocForStartOfFile(BeginOffs.getFID());
|
|
|
|
BLoc = BLoc.getLocWithOffset(BeginOffs.getOffset());
|
|
|
|
assert(BLoc.isFileID());
|
|
|
|
SourceLocation
|
|
|
|
ELoc = BLoc.getLocWithOffset(EndOffs.getOffset() - BeginOffs.getOffset());
|
|
|
|
return Lexer::getSourceText(CharSourceRange::getCharRange(BLoc, ELoc),
|
|
|
|
SourceMgr, LangOpts, &Invalid);
|
|
|
|
}
|
|
|
|
|
|
|
|
EditedSource::FileEditsTy::iterator
|
|
|
|
EditedSource::getActionForOffset(FileOffset Offs) {
|
|
|
|
FileEditsTy::iterator I = FileEdits.upper_bound(Offs);
|
|
|
|
if (I == FileEdits.begin())
|
|
|
|
return FileEdits.end();
|
|
|
|
--I;
|
|
|
|
FileEdit &FA = I->second;
|
|
|
|
FileOffset B = I->first;
|
|
|
|
FileOffset E = B.getWithOffset(FA.RemoveLen);
|
|
|
|
if (Offs >= B && Offs < E)
|
|
|
|
return I;
|
|
|
|
|
|
|
|
return FileEdits.end();
|
|
|
|
}
|