[PATCH] D30735: Add missing implementation for AtomicChange::replace(...)

Eric Liu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 8 06:52:35 PST 2017


ioeric created this revision.

Just realized the implementation is missing...


https://reviews.llvm.org/D30735

Files:
  lib/Tooling/Refactoring/AtomicChange.cpp
  unittests/Tooling/RefactoringTest.cpp


Index: unittests/Tooling/RefactoringTest.cpp
===================================================================
--- unittests/Tooling/RefactoringTest.cpp
+++ unittests/Tooling/RefactoringTest.cpp
@@ -1173,8 +1173,10 @@
   EXPECT_EQ(ExpectedChange.getKey(), ActualChange.getKey());
   EXPECT_EQ(ExpectedChange.getFilePath(), ActualChange.getFilePath());
   EXPECT_EQ(ExpectedChange.getError(), ActualChange.getError());
-  EXPECT_EQ(ExpectedChange.getInsertedHeaders(), ActualChange.getInsertedHeaders());
-  EXPECT_EQ(ExpectedChange.getRemovedHeaders(), ActualChange.getRemovedHeaders());
+  EXPECT_EQ(ExpectedChange.getInsertedHeaders(),
+            ActualChange.getInsertedHeaders());
+  EXPECT_EQ(ExpectedChange.getRemovedHeaders(),
+            ActualChange.getRemovedHeaders());
   EXPECT_EQ(ExpectedChange.getReplacements().size(),
             ActualChange.getReplacements().size());
   EXPECT_EQ(2u, ActualChange.getReplacements().size());
@@ -1191,6 +1193,22 @@
   EXPECT_EQ("input.cpp", Change.getFilePath());
 }
 
+TEST_F(AtomicChangeTest, Replace) {
+  setUp();
+  AtomicChange Change(Context.Sources, DefaultLoc);
+  llvm::Error Err = Change.replace(Context.Sources, DefaultLoc, 2, "aa");
+  ASSERT_TRUE(!Err);
+  EXPECT_EQ(Change.getReplacements().size(), 1u);
+  EXPECT_EQ(*Change.getReplacements().begin(),
+            Replacement(Context.Sources, DefaultLoc, 2, "aa"));
+
+  // Add a new replacement that conflicts with the existing one.
+  Err = Change.replace(Context.Sources, DefaultLoc, 3, "ab");
+  EXPECT_TRUE((bool)Err);
+  llvm::consumeError(std::move(Err));
+  EXPECT_EQ(Change.getReplacements().size(), 1u);
+}
+
 TEST_F(AtomicChangeTest, InsertBefore) {
   setUp();
   AtomicChange Change(Context.Sources, DefaultLoc);
Index: lib/Tooling/Refactoring/AtomicChange.cpp
===================================================================
--- lib/Tooling/Refactoring/AtomicChange.cpp
+++ lib/Tooling/Refactoring/AtomicChange.cpp
@@ -132,6 +132,12 @@
   return E;
 }
 
+llvm::Error AtomicChange::replace(const SourceManager &SM, SourceLocation Loc,
+                                  unsigned Length, llvm::StringRef Text) {
+  Replacement R(SM, Loc, Length, Text);
+  return Replaces.add(R);
+}
+
 llvm::Error AtomicChange::insert(const SourceManager &SM, SourceLocation Loc,
                                  llvm::StringRef Text, bool InsertAfter) {
   if (Text.empty())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30735.91008.patch
Type: text/x-patch
Size: 2400 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170308/a983d8c8/attachment.bin>


More information about the cfe-commits mailing list