diff options
| author | Daniel Jasper <djasper@google.com> | 2015-11-23 08:33:48 +0000 |
|---|---|---|
| committer | Daniel Jasper <djasper@google.com> | 2015-11-23 08:33:48 +0000 |
| commit | c7652b708852cfb7091791085b6836115ea8c69b (patch) | |
| tree | 54401692374dd1b519aa2df67bf4567790617c8e /lib/Tooling/Core/Replacement.cpp | |
| parent | 55de488d205b13b9aba4757d28474022be14ab25 (diff) | |
| download | clang-c7652b708852cfb7091791085b6836115ea8c69b.tar.gz | |
Fix calculation of shifted cursor/code positions. Specifically support
the case where a specific range is replaced by new text. Previously,
the calculation would shift any position from within a replaced region
to the first character after the region. This is undersirable, e.g. for
clang-format's include sorting.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@253859 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Tooling/Core/Replacement.cpp')
| -rw-r--r-- | lib/Tooling/Core/Replacement.cpp | 40 |
1 files changed, 19 insertions, 21 deletions
diff --git a/lib/Tooling/Core/Replacement.cpp b/lib/Tooling/Core/Replacement.cpp index 301c7b71ca..47bbdeb470 100644 --- a/lib/Tooling/Core/Replacement.cpp +++ b/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, |
