diff options
author | Edwin Vane <edwin.vane@intel.com> | 2013-08-27 15:44:26 +0000 |
---|---|---|
committer | Edwin Vane <edwin.vane@intel.com> | 2013-08-27 15:44:26 +0000 |
commit | 18e503c9953cf57653d2bac96d9449e679033e66 (patch) | |
tree | 702e90c1751a65a20f0b01afa841b2d8958b1b2f | |
parent | 85ec25d21c69bc90a8d4e8fb2c36b408bfec8e32 (diff) | |
download | bcm5719-llvm-18e503c9953cf57653d2bac96d9449e679033e66.tar.gz bcm5719-llvm-18e503c9953cf57653d2bac96d9449e679033e66.zip |
Adding a vector version of clang::tooling::shiftedCodePosition().
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().
llvm-svn: 189358
-rw-r--r-- | clang/include/clang/Tooling/Refactoring.h | 5 | ||||
-rw-r--r-- | clang/lib/Tooling/Refactoring.cpp | 17 | ||||
-rw-r--r-- | clang/unittests/Tooling/RefactoringTest.cpp | 19 |
3 files changed, 41 insertions, 0 deletions
diff --git a/clang/include/clang/Tooling/Refactoring.h b/clang/include/clang/Tooling/Refactoring.h index aef02087718..4868b5bfc4f 100644 --- a/clang/include/clang/Tooling/Refactoring.h +++ b/clang/include/clang/Tooling/Refactoring.h @@ -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. /// diff --git a/clang/lib/Tooling/Refactoring.cpp b/clang/lib/Tooling/Refactoring.cpp index 7a8203f93d1..b03fb50b383 100644 --- a/clang/lib/Tooling/Refactoring.cpp +++ b/clang/lib/Tooling/Refactoring.cpp @@ -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()) diff --git a/clang/unittests/Tooling/RefactoringTest.cpp b/clang/unittests/Tooling/RefactoringTest.cpp index a88de1c9f45..8c7bfa1c767 100644 --- a/clang/unittests/Tooling/RefactoringTest.cpp +++ b/clang/unittests/Tooling/RefactoringTest.cpp @@ -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\"")); |