From: Sam McCall Date: Tue, 23 Oct 2018 11:51:53 +0000 (+0000) Subject: Fix range length comparison in DraftStore::UpdateDraft when Unicode characters are... X-Git-Tag: llvmorg-8.0.0-rc1~6048 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=71891129510bffa27c5cd345135483db4522b3a2;p=platform%2Fupstream%2Fllvm.git Fix range length comparison in DraftStore::UpdateDraft when Unicode characters are removed from the document Summary: See http://lists.llvm.org/pipermail/clangd-dev/2018-October/000171.html for context. I kept the error (instead of downgrading to a log message) since the range lengths differing does indicate either a bug in the client or server range calculation or the buffers being out of sync (which both seems serious enough to me to be an error). If any existing clients aside from VSCode break they should only break when accidentally typing a Unicode character which should only be a minor nuisance for a little while until the bug is fixed in the respective client. Patch by Daan De Meyer! Reviewers: sammccall Reviewed By: sammccall Subscribers: ilya-biryukov, ioeric, jkorous, arphaman, kadircet, cfe-commits Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D53527 llvm-svn: 345020 --- diff --git a/clang-tools-extra/clangd/DraftStore.cpp b/clang-tools-extra/clangd/DraftStore.cpp index 825d2b9..b5bcda8 100644 --- a/clang-tools-extra/clangd/DraftStore.cpp +++ b/clang-tools-extra/clangd/DraftStore.cpp @@ -77,8 +77,17 @@ DraftStore::updateDraft(PathRef File, End, Start), llvm::errc::invalid_argument); - if (Change.rangeLength && - (ssize_t)(*EndIndex - *StartIndex) != *Change.rangeLength) + // Since the range length between two LSP positions is dependent on the + // contents of the buffer we compute the range length between the start and + // end position ourselves and compare it to the range length of the LSP + // message to verify the buffers of the client and server are in sync. + + // EndIndex and StartIndex are in bytes, but Change.rangeLength is in UTF-16 + // code units. + ssize_t ComputedRangeLength = + lspLength(Contents.substr(*StartIndex, *EndIndex - *StartIndex)); + + if (Change.rangeLength && ComputedRangeLength != *Change.rangeLength) return make_error( formatv("Change's rangeLength ({0}) doesn't match the " "computed range length ({1}).", diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp index 322b5dc..4312285 100644 --- a/clang-tools-extra/clangd/SourceCode.cpp +++ b/clang-tools-extra/clangd/SourceCode.cpp @@ -67,13 +67,12 @@ static size_t measureUTF16(StringRef U8, int U16Units, bool &Valid) { return std::min(Result, U8.size()); } -// Counts the number of UTF-16 code units needed to represent a string. // Like most strings in clangd, the input is UTF-8 encoded. -static size_t utf16Len(StringRef U8) { +size_t lspLength(StringRef Code) { // A codepoint takes two UTF-16 code unit if it's astral (outside BMP). // Astral codepoints are encoded as 4 bytes in UTF-8, starting with 11110xxx. size_t Count = 0; - iterateCodepoints(U8, [&](int U8Len, int U16Len) { + iterateCodepoints(Code, [&](int U8Len, int U16Len) { Count += U16Len; return false; }); @@ -123,7 +122,7 @@ Position offsetToPosition(StringRef Code, size_t Offset) { size_t StartOfLine = (PrevNL == StringRef::npos) ? 0 : (PrevNL + 1); Position Pos; Pos.line = Lines; - Pos.character = utf16Len(Before.substr(StartOfLine)); + Pos.character = lspLength(Before.substr(StartOfLine)); return Pos; } @@ -139,7 +138,7 @@ Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc) { if (!Invalid) { auto ColumnInBytes = SM.getColumnNumber(FID, Offset) - 1; auto LineSoFar = Code.substr(Offset - ColumnInBytes, ColumnInBytes); - P.character = utf16Len(LineSoFar); + P.character = lspLength(LineSoFar); } return P; } diff --git a/clang-tools-extra/clangd/SourceCode.h b/clang-tools-extra/clangd/SourceCode.h index ed50974..fd5d53b 100644 --- a/clang-tools-extra/clangd/SourceCode.h +++ b/clang-tools-extra/clangd/SourceCode.h @@ -23,6 +23,10 @@ class SourceManager; namespace clangd { +// Counts the number of UTF-16 code units needed to represent a string (LSP +// specifies string lengths in UTF-16 code units). +size_t lspLength(StringRef Code); + /// Turn a [line, column] pair into an offset in Code. /// /// If P.character exceeds the line length, returns the offset at end-of-line. diff --git a/clang-tools-extra/unittests/clangd/SourceCodeTests.cpp b/clang-tools-extra/unittests/clangd/SourceCodeTests.cpp index 9fe923d..3148ccc 100644 --- a/clang-tools-extra/unittests/clangd/SourceCodeTests.cpp +++ b/clang-tools-extra/unittests/clangd/SourceCodeTests.cpp @@ -42,6 +42,16 @@ Range range(const std::pair p1, const std::pair p2) { return range; } +TEST(SourceCodeTests, lspLength) { + EXPECT_EQ(lspLength(""), 0UL); + EXPECT_EQ(lspLength("ascii"), 5UL); + // BMP + EXPECT_EQ(lspLength("↓"), 1UL); + EXPECT_EQ(lspLength("¥"), 1UL); + // astral + EXPECT_EQ(lspLength("😂"), 2UL); +} + TEST(SourceCodeTests, PositionToOffset) { // line out of bounds EXPECT_THAT_EXPECTED(positionToOffset(File, position(-1, 2)), Failed());