[PATCH] D24383: Add addOrMerge interface to tooling::Replacements.

Eric Liu via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 9 03:34:33 PDT 2016


ioeric created this revision.
ioeric added reviewers: klimek, djasper.
ioeric added subscribers: alexshap, hokein, omtcyfz, cfe-commits.
Herald added a subscriber: klimek.

https://reviews.llvm.org/D24383

Files:
  include/clang/Tooling/Core/Replacement.h
  lib/Tooling/Core/Replacement.cpp
  unittests/Tooling/RefactoringTest.cpp

Index: unittests/Tooling/RefactoringTest.cpp
===================================================================
--- unittests/Tooling/RefactoringTest.cpp
+++ unittests/Tooling/RefactoringTest.cpp
@@ -177,6 +177,23 @@
   llvm::consumeError(std::move(Err));
 }
 
+TEST_F(ReplacementTest, AddOrMerge) {
+  Replacements Replaces;
+  auto Err = Replaces.add(Replacement("x.cc", 10, 0, "a"));
+  EXPECT_TRUE(!Err);
+  llvm::consumeError(std::move(Err));
+
+  Replacement R("x.cc", 10, 0, "b");
+  Err = Replaces.add(R);
+  EXPECT_TRUE((bool)Err);
+  llvm::consumeError(std::move(Err));
+
+  Replaces.addOrMerge(R);
+
+  Replacements Expected(Replacement("x.cc", 10, 0, "ab"));
+  EXPECT_EQ(Expected, Replaces);
+}
+
 TEST_F(ReplacementTest, CanApplyReplacements) {
   FileID ID = Context.createInMemoryFile("input.cpp",
                                          "line1\nline2\nline3\nline4");
Index: lib/Tooling/Core/Replacement.cpp
===================================================================
--- lib/Tooling/Core/Replacement.cpp
+++ lib/Tooling/Core/Replacement.cpp
@@ -190,6 +190,27 @@
       llvm::inconvertibleErrorCode());
 }
 
+// Returns `R` with new range that refers to code after `Replaces` being
+// applied.
+Replacement
+Replacements::getReplacementInChangedCode(const Replacement &R) {
+  unsigned NewStart = getShiftedCodePosition(R.getOffset());
+  unsigned NewEnd = getShiftedCodePosition(R.getOffset() + R.getLength());
+  return Replacement(R.getFilePath(), NewStart, NewEnd - NewStart,
+                     R.getReplacementText());
+}
+
+void Replacements::addOrMerge(const Replacement &R) {
+  auto Err = add(R);
+  if (!Err)
+    return;
+  llvm::consumeError(std::move(Err));
+  // Failed to add R. Use `merge`.
+  Replacements NewReplaces =
+      merge(Replacements(getReplacementInChangedCode(R)));
+  Replaces = NewReplaces.Replaces;
+}
+
 namespace {
 
 // Represents a merged replacement, i.e. a replacement consisting of multiple
Index: include/clang/Tooling/Core/Replacement.h
===================================================================
--- include/clang/Tooling/Core/Replacement.h
+++ include/clang/Tooling/Core/Replacement.h
@@ -175,6 +175,12 @@
   /// refers to code after applying the current replacements.
   Replacements merge(const Replacements &Replaces) const;
 
+  /// \brief Adds `R` to the current set of replacements. If this fails,
+  /// calculates the range of R in the code after applying all existing
+  /// replacements and sets the the range of `R` to be the new range. `R` with
+  /// new range is then merged into the current replacements.
+  void addOrMerge(const Replacement &R);
+
   // Returns the affected ranges in the changed code.
   std::vector<Range> getAffectedRanges() const;
 
@@ -204,6 +210,8 @@
 
   Replacements mergeReplacements(const ReplacementsImpl &Second) const;
 
+  Replacement getReplacementInChangedCode(const Replacement &R);
+
   ReplacementsImpl Replaces;
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24383.70810.patch
Type: text/x-patch
Size: 2956 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160909/12f30646/attachment.bin>


More information about the cfe-commits mailing list