[PATCH] D24663: When replacements have the same offset, make replacements with smaller length order first in the set.
Eric Liu via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 17 05:29:08 PDT 2016
ioeric updated this revision to Diff 71730.
ioeric added a comment.
- Format code properly.
https://reviews.llvm.org/D24663
Files:
include/clang/Tooling/Core/Replacement.h
lib/Tooling/Core/Replacement.cpp
Index: lib/Tooling/Core/Replacement.cpp
===================================================================
--- lib/Tooling/Core/Replacement.cpp
+++ lib/Tooling/Core/Replacement.cpp
@@ -83,11 +83,8 @@
if (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())
- return LHS.getLength() > RHS.getLength();
+ return LHS.getLength() < RHS.getLength();
if (LHS.getFilePath() != RHS.getFilePath())
return LHS.getFilePath() < RHS.getFilePath();
@@ -407,9 +404,7 @@
bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite) {
bool Result = true;
- for (Replacements::const_iterator I = Replaces.begin(),
- E = Replaces.end();
- I != E; ++I) {
+ for (auto I = Replaces.rbegin(), E = Replaces.rend(); I != E; ++I) {
if (I->isApplicable()) {
Result = I->apply(Rewrite) && Result;
} else {
@@ -436,8 +431,7 @@
"<stdin>", 0, llvm::MemoryBuffer::getMemBuffer(Code, "<stdin>"));
FileID ID = SourceMgr.createFileID(Files.getFile("<stdin>"), SourceLocation(),
clang::SrcMgr::C_User);
- for (Replacements::const_iterator I = Replaces.begin(), E = Replaces.end();
- I != E; ++I) {
+ for (auto I = Replaces.rbegin(), E = Replaces.rend(); I != E; ++I) {
Replacement Replace("<stdin>", I->getOffset(), I->getLength(),
I->getReplacementText());
if (!Replace.apply(Rewrite))
Index: include/clang/Tooling/Core/Replacement.h
===================================================================
--- include/clang/Tooling/Core/Replacement.h
+++ include/clang/Tooling/Core/Replacement.h
@@ -151,6 +151,7 @@
public:
typedef ReplacementsImpl::const_iterator const_iterator;
+ typedef ReplacementsImpl::const_reverse_iterator const_reverse_iterator;
Replacements() = default;
@@ -193,6 +194,10 @@
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 {
return Replaces == RHS.Replaces;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24663.71730.patch
Type: text/x-patch
Size: 2436 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160917/2d3a983a/attachment.bin>
More information about the cfe-commits
mailing list