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; } 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,7 +431,8 @@ "", 0, llvm::MemoryBuffer::getMemBuffer(Code, "")); FileID ID = SourceMgr.createFileID(Files.getFile(""), SourceLocation(), 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) { Replacement Replace("", I->getOffset(), I->getLength(), I->getReplacementText());