diff options
author | Daniel Jasper <djasper@google.com> | 2015-06-16 10:22:10 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2015-06-16 10:22:10 +0000 |
commit | 0f291276361f863ed2898e1eecbbf3da94e2dfe8 (patch) | |
tree | 681ac9ffc9a71f516e55a848d1dece8807fd71f6 /clang | |
parent | c535d93b47c79add08a84917e3dcad1daf0323dd (diff) | |
download | bcm5719-llvm-0f291276361f863ed2898e1eecbbf3da94e2dfe8.tar.gz bcm5719-llvm-0f291276361f863ed2898e1eecbbf3da94e2dfe8.zip |
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.
llvm-svn: 239809
Diffstat (limited to 'clang')
-rw-r--r-- | clang/lib/Tooling/Core/Replacement.cpp | 7 | ||||
-rw-r--r-- | clang/unittests/Tooling/RewriterTest.cpp | 13 |
2 files changed, 19 insertions, 1 deletions
diff --git a/clang/lib/Tooling/Core/Replacement.cpp b/clang/lib/Tooling/Core/Replacement.cpp index 32e8e5bd6b9..6d37a49db38 100644 --- a/clang/lib/Tooling/Core/Replacement.cpp +++ b/clang/lib/Tooling/Core/Replacement.cpp @@ -88,8 +88,13 @@ std::string Replacement::toString() const { 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(); diff --git a/clang/unittests/Tooling/RewriterTest.cpp b/clang/unittests/Tooling/RewriterTest.cpp index c53e50a87d7..4928b17cfa2 100644 --- a/clang/unittests/Tooling/RewriterTest.cpp +++ b/clang/unittests/Tooling/RewriterTest.cpp @@ -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, ContinuesOverwritingFilesOnError) { 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 |