]> granicus.if.org Git - clang/commitdiff
Adding a vector version of tooling::applyAllReplacements
authorEdwin Vane <edwin.vane@intel.com>
Tue, 13 Aug 2013 17:38:19 +0000 (17:38 +0000)
committerEdwin Vane <edwin.vane@intel.com>
Tue, 13 Aug 2013 17:38:19 +0000 (17:38 +0000)
One day soon, tooling::Replacements will be changed from being implemented as
an std::set to being implemented as an std::vector. Until then, some new code
using vectors of Replacements would enjoy having a version of
applyAllReplacements that takes a vector.

Differential Revision: http://llvm-reviews.chandlerc.com/D1380

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@188295 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Tooling/Refactoring.h
lib/Tooling/Refactoring.cpp
unittests/Tooling/RefactoringTest.cpp

index a976146a778369efaf509fc0ba71d7686c7c2a8a..efac016b199b1ff8621d83a363094c0972b26187 100644 (file)
@@ -147,6 +147,15 @@ typedef std::set<Replacement, Replacement::Less> Replacements;
 /// \returns true if all replacements apply. false otherwise.
 bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite);
 
+/// \brief Apply all replacements in \p Replaces to the Rewriter \p Rewrite.
+///
+/// Replacement applications happen independently of the success of
+/// other applications.
+///
+/// \returns true if all replacements apply. false otherwise.
+bool applyAllReplacements(const std::vector<Replacement> &Replaces,
+                          Rewriter &Rewrite);
+
 /// \brief Applies all replacements in \p Replaces to \p Code.
 ///
 /// This completely ignores the path stored in each replacement. If one or more
index 52daaebfd2191e1b6702746c8cd19738ef3b3cb6..bc94a2b2c1f3a5a74f8cbaaac2fd9d3b9d300ad0 100644 (file)
@@ -143,6 +143,23 @@ bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite) {
   return Result;
 }
 
+// FIXME: Remove this function when Replacements is implemented as std::vector
+// instead of std::set.
+bool applyAllReplacements(const std::vector<Replacement> &Replaces,
+                          Rewriter &Rewrite) {
+  bool Result = true;
+  for (std::vector<Replacement>::const_iterator I = Replaces.begin(),
+                                                E = Replaces.end();
+       I != E; ++I) {
+    if (I->isApplicable()) {
+      Result = I->apply(Rewrite) && Result;
+    } else {
+      Result = false;
+    }
+  }
+  return Result;
+}
+
 std::string applyAllReplacements(StringRef Code, const Replacements &Replaces) {
   FileManager Files((FileSystemOptions()));
   DiagnosticsEngine Diagnostics(
index d9f023d9653e16f2bc713e7b3cb255f3c2298a09..81f9f040aeb7d5aa206355593db132d653354916 100644 (file)
@@ -120,6 +120,21 @@ TEST_F(ReplacementTest, CanApplyReplacements) {
   EXPECT_EQ("line1\nreplaced\nother\nline4", Context.getRewrittenText(ID));
 }
 
+// FIXME: Remove this test case when Replacements is implemented as std::vector
+// instead of std::set. The other ReplacementTest tests will need to be updated
+// at that point as well.
+TEST_F(ReplacementTest, VectorCanApplyReplacements) {
+  FileID ID = Context.createInMemoryFile("input.cpp",
+                                         "line1\nline2\nline3\nline4");
+  std::vector<Replacement> Replaces;
+  Replaces.push_back(Replacement(Context.Sources, Context.getLocation(ID, 2, 1),
+                                 5, "replaced"));
+  Replaces.push_back(
+      Replacement(Context.Sources, Context.getLocation(ID, 3, 1), 5, "other"));
+  EXPECT_TRUE(applyAllReplacements(Replaces, Context.Rewrite));
+  EXPECT_EQ("line1\nreplaced\nother\nline4", Context.getRewrittenText(ID));
+}
+
 TEST_F(ReplacementTest, SkipsDuplicateReplacements) {
   FileID ID = Context.createInMemoryFile("input.cpp",
                                          "line1\nline2\nline3\nline4");