forked from OSchip/llvm-project
When replacements have the same offset, make replacements with smaller length order first in the set.
Summary: No behavioral change intended. The change makes iterating the replacements set more intuitive in Replacements class implementation. Previously, insertion is ordered before an deletion/replacement with the same offset, which is counter-intuitive for implementation, especially for a followup patch to support adding insertions around replacements. With the current ordering, we only need to make `applyAllReplacements` iterate the replacements set reversely when applying them so that deletion/replacement is still applied before insertion with the same offset. Reviewers: klimek, djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D24663 llvm-svn: 281819
This commit is contained in:
parent
dfbbbc86a1
commit
e990652542
|
@ -151,6 +151,7 @@ class Replacements {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef ReplacementsImpl::const_iterator const_iterator;
|
typedef ReplacementsImpl::const_iterator const_iterator;
|
||||||
|
typedef ReplacementsImpl::const_reverse_iterator const_reverse_iterator;
|
||||||
|
|
||||||
Replacements() = default;
|
Replacements() = default;
|
||||||
|
|
||||||
|
@ -193,6 +194,10 @@ class Replacements {
|
||||||
|
|
||||||
const_iterator end() const { return Replaces.end(); }
|
const_iterator end() const { return Replaces.end(); }
|
||||||
|
|
||||||
|
const_reverse_iterator rbegin() const { return Replaces.rbegin(); }
|
||||||
|
|
||||||
|
const_reverse_iterator rend() const { return Replaces.rend(); }
|
||||||
|
|
||||||
bool operator==(const Replacements &RHS) const {
|
bool operator==(const Replacements &RHS) const {
|
||||||
return Replaces == RHS.Replaces;
|
return Replaces == RHS.Replaces;
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,11 +83,8 @@ bool operator<(const Replacement &LHS, const Replacement &RHS) {
|
||||||
if (LHS.getOffset() != RHS.getOffset())
|
if (LHS.getOffset() != RHS.getOffset())
|
||||||
return LHS.getOffset() < RHS.getOffset();
|
return LHS.getOffset() < RHS.getOffset();
|
||||||
|
|
||||||
// Apply longer replacements first, specifically so that deletions are
|
|
||||||
// executed before insertions. It is (hopefully) never the intention to
|
|
||||||
// delete parts of newly inserted code.
|
|
||||||
if (LHS.getLength() != RHS.getLength())
|
if (LHS.getLength() != RHS.getLength())
|
||||||
return LHS.getLength() > RHS.getLength();
|
return LHS.getLength() < RHS.getLength();
|
||||||
|
|
||||||
if (LHS.getFilePath() != RHS.getFilePath())
|
if (LHS.getFilePath() != RHS.getFilePath())
|
||||||
return LHS.getFilePath() < RHS.getFilePath();
|
return LHS.getFilePath() < RHS.getFilePath();
|
||||||
|
@ -407,9 +404,7 @@ unsigned Replacements::getShiftedCodePosition(unsigned Position) const {
|
||||||
|
|
||||||
bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite) {
|
bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite) {
|
||||||
bool Result = true;
|
bool Result = true;
|
||||||
for (Replacements::const_iterator I = Replaces.begin(),
|
for (auto I = Replaces.rbegin(), E = Replaces.rend(); I != E; ++I) {
|
||||||
E = Replaces.end();
|
|
||||||
I != E; ++I) {
|
|
||||||
if (I->isApplicable()) {
|
if (I->isApplicable()) {
|
||||||
Result = I->apply(Rewrite) && Result;
|
Result = I->apply(Rewrite) && Result;
|
||||||
} else {
|
} else {
|
||||||
|
@ -436,8 +431,7 @@ llvm::Expected<std::string> applyAllReplacements(StringRef Code,
|
||||||
"<stdin>", 0, llvm::MemoryBuffer::getMemBuffer(Code, "<stdin>"));
|
"<stdin>", 0, llvm::MemoryBuffer::getMemBuffer(Code, "<stdin>"));
|
||||||
FileID ID = SourceMgr.createFileID(Files.getFile("<stdin>"), SourceLocation(),
|
FileID ID = SourceMgr.createFileID(Files.getFile("<stdin>"), SourceLocation(),
|
||||||
clang::SrcMgr::C_User);
|
clang::SrcMgr::C_User);
|
||||||
for (Replacements::const_iterator I = Replaces.begin(), E = Replaces.end();
|
for (auto I = Replaces.rbegin(), E = Replaces.rend(); I != E; ++I) {
|
||||||
I != E; ++I) {
|
|
||||||
Replacement Replace("<stdin>", I->getOffset(), I->getLength(),
|
Replacement Replace("<stdin>", I->getOffset(), I->getLength(),
|
||||||
I->getReplacementText());
|
I->getReplacementText());
|
||||||
if (!Replace.apply(Rewrite))
|
if (!Replace.apply(Rewrite))
|
||||||
|
|
Loading…
Reference in New Issue