r239809 - Tooling: When applying a set of replacements, do deletions before

Daniel Jasper djasper at google.com
Tue Jun 16 03:22:11 PDT 2015


Author: djasper
Date: Tue Jun 16 05:22:10 2015
New Revision: 239809

URL: http://llvm.org/viewvc/llvm-project?rev=239809&view=rev
Log:
Tooling: When applying a set of replacements, do deletions before
insertions. It is unlikely to be the intention to delete parts of newly
inserted code. To do so, changed sorting Replacements at the same offset
to have decreasing length.

Modified:
    cfe/trunk/lib/Tooling/Core/Replacement.cpp
    cfe/trunk/unittests/Tooling/RewriterTest.cpp

Modified: cfe/trunk/lib/Tooling/Core/Replacement.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Core/Replacement.cpp?rev=239809&r1=239808&r2=239809&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/Core/Replacement.cpp (original)
+++ cfe/trunk/lib/Tooling/Core/Replacement.cpp Tue Jun 16 05:22:10 2015
@@ -88,8 +88,13 @@ std::string Replacement::toString() cons
 bool operator<(const Replacement &LHS, const Replacement &RHS) {
   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();
   return LHS.getReplacementText() < RHS.getReplacementText();

Modified: cfe/trunk/unittests/Tooling/RewriterTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RewriterTest.cpp?rev=239809&r1=239808&r2=239809&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/RewriterTest.cpp (original)
+++ cfe/trunk/unittests/Tooling/RewriterTest.cpp Tue Jun 16 05:22:10 2015
@@ -8,9 +8,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "RewriterTestContext.h"
+#include "clang/Tooling/Core/Replacement.h"
 #include "gtest/gtest.h"
 
 namespace clang {
+namespace tooling {
+namespace {
 
 TEST(Rewriter, OverwritesChangedFiles) {
   RewriterTestContext Context;
@@ -34,4 +37,14 @@ TEST(Rewriter, ContinuesOverwritingFiles
             Context.getFileContentFromDisk("working.cpp")); 
 }
 
+TEST(Rewriter, AdjacentInsertAndDelete) {
+  Replacements Replaces;
+  Replaces.emplace("<file>", 6, 6, "");
+  Replaces.emplace("<file>", 6, 0, "replaced\n");
+  EXPECT_EQ("line1\nreplaced\nline3\nline4",
+            applyAllReplacements("line1\nline2\nline3\nline4", Replaces));
+}
+
+} // end namespace
+} // end namespace tooling
 } // end namespace clang





More information about the cfe-commits mailing list