summaryrefslogtreecommitdiffstats
path: root/clang
diff options
context:
space:
mode:
Diffstat (limited to 'clang')
-rw-r--r--clang/lib/Tooling/Core/Replacement.cpp40
-rw-r--r--clang/unittests/Tooling/RefactoringTest.cpp21
2 files changed, 34 insertions, 27 deletions
diff --git a/clang/lib/Tooling/Core/Replacement.cpp b/clang/lib/Tooling/Core/Replacement.cpp
index 301c7b71ca9..47bbdeb470e 100644
--- a/clang/lib/Tooling/Core/Replacement.cpp
+++ b/clang/lib/Tooling/Core/Replacement.cpp
@@ -143,34 +143,32 @@ void Replacement::setFromSourceRange(const SourceManager &Sources,
ReplacementText);
}
-unsigned shiftedCodePosition(const Replacements &Replaces, unsigned Position) {
- unsigned NewPosition = Position;
- for (Replacements::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();
+template <typename T>
+unsigned shiftedCodePositionInternal(const T &Replaces, unsigned Position) {
+ unsigned Offset = 0;
+ for (const auto& R : Replaces) {
+ if (R.getOffset() + R.getLength() <= Position) {
+ Offset += R.getReplacementText().size() - R.getLength();
+ continue;
+ }
+ if (R.getOffset() < Position &&
+ R.getOffset() + R.getReplacementText().size() <= Position) {
+ Position = R.getOffset() + R.getReplacementText().size() - 1;
+ }
+ break;
}
- return NewPosition;
+ return Position + Offset;
+}
+
+unsigned shiftedCodePosition(const Replacements &Replaces, unsigned Position) {
+ return shiftedCodePositionInternal(Replaces, Position);
}
// 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;
+ return shiftedCodePositionInternal(Replaces, Position);
}
void deduplicate(std::vector<Replacement> &Replaces,
diff --git a/clang/unittests/Tooling/RefactoringTest.cpp b/clang/unittests/Tooling/RefactoringTest.cpp
index d9a87a5690f..ff11aeae117 100644
--- a/clang/unittests/Tooling/RefactoringTest.cpp
+++ b/clang/unittests/Tooling/RefactoringTest.cpp
@@ -176,8 +176,8 @@ TEST(ShiftedCodePositionTest, FindsNewCodePosition) {
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(3u, shiftedCodePosition(Replaces, 5)); // int | i;
+ EXPECT_EQ(3u, shiftedCodePosition(Replaces, 6)); // int |i;
EXPECT_EQ(4u, shiftedCodePosition(Replaces, 7)); // int |;
EXPECT_EQ(5u, shiftedCodePosition(Replaces, 8)); // int i|
}
@@ -195,8 +195,8 @@ TEST(ShiftedCodePositionTest, VectorFindsNewCodePositionWithInserts) {
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(3u, shiftedCodePosition(Replaces, 5)); // int | i;
+ EXPECT_EQ(3u, shiftedCodePosition(Replaces, 6)); // int |i;
EXPECT_EQ(4u, shiftedCodePosition(Replaces, 7)); // int |;
EXPECT_EQ(5u, shiftedCodePosition(Replaces, 8)); // int i|
}
@@ -205,8 +205,17 @@ TEST(ShiftedCodePositionTest, FindsNewCodePositionWithInserts) {
Replacements Replaces;
Replaces.insert(Replacement("", 4, 0, "\"\n\""));
// Assume '"12345678"' is turned into '"1234"\n"5678"'.
- EXPECT_EQ(4u, shiftedCodePosition(Replaces, 4)); // "123|5678"
- EXPECT_EQ(8u, shiftedCodePosition(Replaces, 5)); // "1234|678"
+ EXPECT_EQ(3u, shiftedCodePosition(Replaces, 3)); // "123|5678"
+ EXPECT_EQ(7u, shiftedCodePosition(Replaces, 4)); // "1234|678"
+ EXPECT_EQ(8u, shiftedCodePosition(Replaces, 5)); // "12345|78"
+}
+
+TEST(ShiftedCodePositionTest, FindsNewCodePositionInReplacedText) {
+ Replacements Replaces;
+ // Replace the first four characters with "abcd".
+ Replaces.insert(Replacement("", 0, 4, "abcd"));
+ for (unsigned i = 0; i < 3; ++i)
+ EXPECT_EQ(i, shiftedCodePosition(Replaces, i));
}
class FlushRewrittenFilesTest : public ::testing::Test {
OpenPOWER on IntegriCloud