]> granicus.if.org Git - clang/commitdiff
Adding a vector version of clang::tooling::shiftedCodePosition().
authorEdwin Vane <edwin.vane@intel.com>
Tue, 27 Aug 2013 15:44:26 +0000 (15:44 +0000)
committerEdwin Vane <edwin.vane@intel.com>
Tue, 27 Aug 2013 15:44:26 +0000 (15:44 +0000)
During the transition of clang::tooling::Replacements from std::set to
std::vector, functions such as clang::tooling::applyAllReplacements() have been
duplicated to take a std::vector<Replacement>. Applying this same temporary
duplication to clang::tooling::shiftedCodePosition().

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

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

index aef0208771847d82534a2ffee27cdf236b0c16a9..4868b5bfc4f592426b9b4e8f78b58597fbc9ec29 100644 (file)
@@ -162,6 +162,11 @@ std::string applyAllReplacements(StringRef Code, const Replacements &Replaces);
 /// applied.
 unsigned shiftedCodePosition(const Replacements& Replaces, unsigned Position);
 
+/// \brief Calculates how a code \p Position is shifted when \p Replaces are
+/// applied.
+unsigned shiftedCodePosition(const std::vector<Replacement> &Replaces,
+                             unsigned Position);
+
 /// \brief Removes duplicate Replacements and reports if Replacements conflict
 /// with one another.
 ///
index 7a8203f93d11b6c8339a3e9a1de787aeaccc5c94..b03fb50b383ea940cc34dbb59c83bcabb43bd980 100644 (file)
@@ -203,6 +203,23 @@ unsigned shiftedCodePosition(const Replacements &Replaces, unsigned Position) {
   return NewPosition;
 }
 
+// FIXME: Remove this function when Replacements is implemented as std::vector
+// instead of std::set.
+unsigned shiftedCodePosition(const std::vector<Replacement> &Replaces,
+                             unsigned Position) {
+  unsigned NewPosition = Position;
+  for (std::vector<Replacement>::const_iterator I = Replaces.begin(),
+                                                E = Replaces.end();
+       I != E; ++I) {
+    if (I->getOffset() >= Position)
+      break;
+    if (I->getOffset() + I->getLength() > Position)
+      NewPosition += I->getOffset() + I->getLength() - Position;
+    NewPosition += I->getReplacementText().size() - I->getLength();
+  }
+  return NewPosition;
+}
+
 void deduplicate(std::vector<Replacement> &Replaces,
                  std::vector<Range> &Conflicts) {
   if (Replaces.empty())
index a88de1c9f4564720109cb664185da3adbc5df5e6..8c7bfa1c767a344924b1a3d6f60dc3975bf824aa 100644 (file)
@@ -182,6 +182,25 @@ TEST(ShiftedCodePositionTest, FindsNewCodePosition) {
   EXPECT_EQ(5u, shiftedCodePosition(Replaces, 8)); //  int   i|
 }
 
+// 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(ShiftedCodePositionTest, VectorFindsNewCodePositionWithInserts) {
+  std::vector<Replacement> Replaces;
+  Replaces.push_back(Replacement("", 0, 1, ""));
+  Replaces.push_back(Replacement("", 4, 3, " "));
+  // Assume ' int   i;' is turned into 'int i;' and cursor is located at '|'.
+  EXPECT_EQ(0u, shiftedCodePosition(Replaces, 0)); // |int   i;
+  EXPECT_EQ(0u, shiftedCodePosition(Replaces, 1)); //  |nt   i;
+  EXPECT_EQ(1u, shiftedCodePosition(Replaces, 2)); //  i|t   i;
+  EXPECT_EQ(2u, shiftedCodePosition(Replaces, 3)); //  in|   i;
+  EXPECT_EQ(3u, shiftedCodePosition(Replaces, 4)); //  int|  i;
+  EXPECT_EQ(4u, shiftedCodePosition(Replaces, 5)); //  int | i;
+  EXPECT_EQ(4u, shiftedCodePosition(Replaces, 6)); //  int  |i;
+  EXPECT_EQ(4u, shiftedCodePosition(Replaces, 7)); //  int   |;
+  EXPECT_EQ(5u, shiftedCodePosition(Replaces, 8)); //  int   i|
+}
+
 TEST(ShiftedCodePositionTest, FindsNewCodePositionWithInserts) {
   Replacements Replaces;
   Replaces.insert(Replacement("", 4, 0, "\"\n\""));