From: Edwin Vane Date: Tue, 27 Aug 2013 15:44:26 +0000 (+0000) Subject: Adding a vector version of clang::tooling::shiftedCodePosition(). X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a778cde0ddf4c47a63786fb1067f25c1fbfcf19d;p=clang 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. 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 --- diff --git a/include/clang/Tooling/Refactoring.h b/include/clang/Tooling/Refactoring.h index aef0208771..4868b5bfc4 100644 --- a/include/clang/Tooling/Refactoring.h +++ b/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 &Replaces, + unsigned Position); + /// \brief Removes duplicate Replacements and reports if Replacements conflict /// with one another. /// diff --git a/lib/Tooling/Refactoring.cpp b/lib/Tooling/Refactoring.cpp index 7a8203f93d..b03fb50b38 100644 --- a/lib/Tooling/Refactoring.cpp +++ b/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 &Replaces, + unsigned Position) { + unsigned NewPosition = Position; + for (std::vector::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 &Replaces, std::vector &Conflicts) { if (Replaces.empty()) diff --git a/unittests/Tooling/RefactoringTest.cpp b/unittests/Tooling/RefactoringTest.cpp index a88de1c9f4..8c7bfa1c76 100644 --- a/unittests/Tooling/RefactoringTest.cpp +++ b/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 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\""));