[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
ArcsinX wrote:
> > Hello @playerC Can you please provide an example scenario when this problem
> > occurs? This example can also be used to write a test.
>
> Hello @ArcsinX
>
> When using Emacs 30.2 with Eglot to write C code, clangd frequently becomes
> unresponsive after code completion. After adding logging, I discovered that
> clangd triggers an error and becomes unusable when processing the following
> scenario:
>
> The clangd client sends a `textDocument/didChange` notification where
> `end.character` exceeds the length of the corresponding line in clangd's
> internal document state.
>
> Error log sample:
>
> ```
> V[01:10:18.967] <<<
> {"jsonrpc":"2.0","method":"textDocument/didChange","params":{"contentChanges":[{"range":{"end":{"character":52,"line":40},"start":{"character":31,"line":40}},"rangeLength":21,"text":"C4MK_FS_PATH_MAX_SIZE
> ) {\n
> "}],"textDocument":{"uri":"file:///XX","version":2}}}
>
> I[01:10:18.967] <-- textDocument/didChange
> E[01:10:18.970] Failed to update /: utf-8 offset 52 is
> invalid for line 40
>
> ^~
> V[01:10:18.971] <<<
> {"id":10,"jsonrpc":"2.0","method":"textDocument/hover","params":{"position":{"character":52,"line":40},"textDocument":{"uri":"file:///"}}}
>
> I[01:10:18.971] <-- textDocument/hover(10)
> I[01:10:18.971] --> reply:textDocument/hover(10) 0 ms, error: -32602: trying
> to get AST for non-added document
> V[01:10:18.971] >>> {"error":{"code":-32602,"message":"trying to get AST for
> non-added document"},"id":10,"jsonrpc":"2.0"}
>
> ^~~~
> ```
>
> After add some logger code :
>
> ```
> V[00:45:53.497] <<<
> {"jsonrpc":"2.0","method":"textDocument/didChange","params":{"contentChanges":[{"range":{"end":{"character":52,"line":40},"start":{"character":31,"line":40}},"rangeLength":21,"text":"C4MK_FS_PATH_MAX_SIZE
> ) {\n"}],"textDocument":{"uri":"file:///","version":28}}}
>
> I[00:45:53.497] <-- textDocument/didChange
> I[00:45:53.497] inside clangd text length: [12], outside clangd text length:
> [21], inside clangd text: ["C4MK_FS_ ) {"]
> ```
>
> Inside clangd text length: [12], text: [`"C4MK_FS_ ) {"`] , is OK. outside
> clangd text length: [21], text: [`"C4MK_FS_PATH_MAX_SIZE ) {\n "`], is OK.
>
> But , `end.character` is larger than `start.character` + inside text length.
>
> As you can see, both the text length maintained internally by clangd and the
> text length in the `didChange` notification are correct. However, an error
> occurs when the `didChange` attempts to modify the current line to a length
> greater than its original length.
>
> This change to `didChange` to accept a larger `end.character` , makes text in
> clangd eventually consistent and simplifies the development of clangd
> clients, thereby improving stability.
Thanks for clarification.
But it seems that here we are fixing the symptoms, not the root cause. I mean
that we need to understand why this happens, maybe this is the problem of the
client (if so, clangd is not the right place where we need to fix this)
According to
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_didChange
"Before requesting information from the server (e.g.,
[textDocument/completion](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_completion)
or
[textDocument/signatureHelp](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_signatureHelp)),
the client **must** ensure that the document’s state is synchronized with the
server to guarantee reliable results."
https://github.com/llvm/llvm-project/pull/202267
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
https://github.com/playerC updated
https://github.com/llvm/llvm-project/pull/202267
>From 5ae898162a317c3b30c9cfa7ac580cf94fb16c86 Mon Sep 17 00:00:00 2001
From: player
Date: Mon, 8 Jun 2026 14:27:34 +0800
Subject: [PATCH] [clangd] Fix offset invalid on line.
Fixed the offset invalid on line error,triggered by a
`textDocument/didChange` notification when `range.end.character`
exceeds the corresponding line's length.
---
clang-tools-extra/clangd/SourceCode.cpp | 8 ++-
.../clangd/unittests/SourceCodeTests.cpp | 55 ++-
2 files changed, 57 insertions(+), 6 deletions(-)
diff --git a/clang-tools-extra/clangd/SourceCode.cpp
b/clang-tools-extra/clangd/SourceCode.cpp
index 21c078fd2cdb9..60aba1da64559 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -1132,8 +1132,9 @@ llvm::Error applyChange(std::string &Contents,
if (!StartIndex)
return StartIndex.takeError();
+ // End position may longer than current line .
const Position &End = Change.range->end;
- llvm::Expected EndIndex = positionToOffset(Contents, End, false);
+ llvm::Expected EndIndex = positionToOffset(Contents, End, true);
inferFinalNewline(EndIndex, Contents, End);
if (!EndIndex)
return EndIndex.takeError();
@@ -1153,13 +1154,14 @@ llvm::Error applyChange(std::string &Contents,
ssize_t ComputedRangeLength =
lspLength(Contents.substr(*StartIndex, *EndIndex - *StartIndex));
- if (Change.rangeLength && ComputedRangeLength != *Change.rangeLength)
+ // CoumputedRangeLength may less equal than rangeLength.
+ if (Change.rangeLength && ComputedRangeLength > *Change.rangeLength)
return error(llvm::errc::invalid_argument,
"Change's rangeLength ({0}) doesn't match the "
"computed range length ({1}).",
*Change.rangeLength, ComputedRangeLength);
- Contents.replace(*StartIndex, *EndIndex - *StartIndex, Change.text);
+ Contents.replace(*StartIndex, ComputedRangeLength, Change.text);
return llvm::Error::success();
}
diff --git a/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
index 801d535c1b9d0..c08e24e1e0961 100644
--- a/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
@@ -968,10 +968,57 @@ TEST(ApplyEditsTest, WrongRangeLength) {
Change.range->end.line = 0;
Change.range->end.character = 2;
Change.rangeLength = 10;
+
+ // when rangeLength doesn't match, just use computed range length.
+ EXPECT_THAT_ERROR(applyChange(Code, Change),
+llvm::Succeeded());
+}
+
+TEST(ApplyEditsTest, InvalidOffsetOfLine){
+ std::string Code = "0123456789\n";
+
+ TextDocumentContentChangeEvent Change;
+
+ // NOTE : just use range.start and end to emulate the computed range
+ //length.
+
+ // computedRangeLength > rangeLength , expect error.
+ Change.range.emplace();
+ Change.range->start.line = 0;
+ Change.range->start.character = 0;
+ Change.range->end.line = 0;
+ Change.range->end.character = 5;
+ Change.rangeLength = 2;
EXPECT_THAT_ERROR(applyChange(Code, Change),
-FailedWithMessage("Change's rangeLength (10) doesn't match
"
- "the computed range length (2)."));
+FailedWithMessage("Change's rangeLength (2) doesn't match "
+ "the computed range length (5)."));
+
+ // computedRangeLength < rangeLength ,use computed, expect success.
+ Change.range.emplace();
+ Change.range->start.line = 0;
+ Change.range->start.character = 0;
+ Change.range->end.line = 0;
+ Change.range->end.character = 3;
+ Change.rangeLength = 6;
+ Change.text = "SIX";
+
+ EXPECT_THAT_ERROR(applyChange(Code, Change),
+llvm::Succeeded());
+ EXPECT_EQ(Code, "SIX3456789\n");
+
+ // computedRangeLength == rangeLength , expect success.
+ Change.range.emplace();
+ Change.range->start.line = 0;
+ Change.range->start.character = 0;
+ Change.range->end.line = 0;
+ Change.range->end.character = 3;
+ Change.rangeLength = 3;
+ Change.text = "THREE";
+
+ EXPECT_THAT_ERROR(applyChange(Code, Change),
+llvm::Succeeded());
+ EXPECT_EQ(Code, "THREE3456789\n");
}
// Test that we correct observed buggy edits from Neovim.
@@ -1055,9 +1102,11 @@ TEST(ApplyEditsTest, EndCharOutOfRange) {
Change.range->end.character = 100;
Change.text = "foo";
+ // OK to replace whole line.
EXPECT_THAT_ERROR(
applyChange(Code, Change),
- FailedWithMessage("utf-16 offset 100 is invalid for line 0"));
+ llvm::Succeeded());
+ EXPECT_EQ(Code, "foo\n");
}
TEST(ApplyEditsTest, StartLineOutOfRange) {
___
cfe-commits mailing list
[email protected]
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
playerC wrote: Hello @ArcsinX and @JVApen I have added the unit tests, please check. https://github.com/llvm/llvm-project/pull/202267 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
https://github.com/playerC ready_for_review https://github.com/llvm/llvm-project/pull/202267 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
https://github.com/playerC converted_to_draft https://github.com/llvm/llvm-project/pull/202267 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
playerC wrote: > https://github.com/llvm/llvm-project/blob/32b423ec57d76315cd42b99c0cdd9c63623d8b7b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp > would be a good place to add such a test OK, thanks a lot, i will do this. https://github.com/llvm/llvm-project/pull/202267 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
JVApen wrote: https://github.com/llvm/llvm-project/blob/32b423ec57d76315cd42b99c0cdd9c63623d8b7b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp would be a good place to add such a test https://github.com/llvm/llvm-project/pull/202267 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
https://github.com/playerC commented: OK. https://github.com/llvm/llvm-project/pull/202267 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
https://github.com/playerC updated
https://github.com/llvm/llvm-project/pull/202267
>From 5eab8d2cb821c1a9814ede6f4ebca223dab50794 Mon Sep 17 00:00:00 2001
From: player
Date: Mon, 8 Jun 2026 14:27:34 +0800
Subject: [PATCH] [clangd] Fix offset invalid on line.
Fixed the offset invalid on line error, when the change text longer
than that line.
---
clang-tools-extra/clangd/SourceCode.cpp | 8 +---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clangd/SourceCode.cpp
b/clang-tools-extra/clangd/SourceCode.cpp
index 21c078fd2cdb9..60aba1da64559 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -1132,8 +1132,9 @@ llvm::Error applyChange(std::string &Contents,
if (!StartIndex)
return StartIndex.takeError();
+ // End position may longer than current line .
const Position &End = Change.range->end;
- llvm::Expected EndIndex = positionToOffset(Contents, End, false);
+ llvm::Expected EndIndex = positionToOffset(Contents, End, true);
inferFinalNewline(EndIndex, Contents, End);
if (!EndIndex)
return EndIndex.takeError();
@@ -1153,13 +1154,14 @@ llvm::Error applyChange(std::string &Contents,
ssize_t ComputedRangeLength =
lspLength(Contents.substr(*StartIndex, *EndIndex - *StartIndex));
- if (Change.rangeLength && ComputedRangeLength != *Change.rangeLength)
+ // CoumputedRangeLength may less equal than rangeLength.
+ if (Change.rangeLength && ComputedRangeLength > *Change.rangeLength)
return error(llvm::errc::invalid_argument,
"Change's rangeLength ({0}) doesn't match the "
"computed range length ({1}).",
*Change.rangeLength, ComputedRangeLength);
- Contents.replace(*StartIndex, *EndIndex - *StartIndex, Change.text);
+ Contents.replace(*StartIndex, ComputedRangeLength, Change.text);
return llvm::Error::success();
}
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
https://github.com/playerC updated
https://github.com/llvm/llvm-project/pull/202267
>From d29629dfacb24ca6588e238ab605d0b8e2634cf6 Mon Sep 17 00:00:00 2001
From: player
Date: Mon, 8 Jun 2026 14:27:34 +0800
Subject: [PATCH] [clangd] Fix offset invalid on line.
Fixed the offset invalid on line error, when the change text longer
than that line.
---
clang-tools-extra/clangd/SourceCode.cpp | 8 +---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clangd/SourceCode.cpp
b/clang-tools-extra/clangd/SourceCode.cpp
index 21c078fd2cdb9..60aba1da64559 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -1132,8 +1132,9 @@ llvm::Error applyChange(std::string &Contents,
if (!StartIndex)
return StartIndex.takeError();
+ // End position may longer than current line .
const Position &End = Change.range->end;
- llvm::Expected EndIndex = positionToOffset(Contents, End, false);
+ llvm::Expected EndIndex = positionToOffset(Contents, End, true);
inferFinalNewline(EndIndex, Contents, End);
if (!EndIndex)
return EndIndex.takeError();
@@ -1153,13 +1154,14 @@ llvm::Error applyChange(std::string &Contents,
ssize_t ComputedRangeLength =
lspLength(Contents.substr(*StartIndex, *EndIndex - *StartIndex));
- if (Change.rangeLength && ComputedRangeLength != *Change.rangeLength)
+ // CoumputedRangeLength may less equal than rangeLength.
+ if (Change.rangeLength && ComputedRangeLength > *Change.rangeLength)
return error(llvm::errc::invalid_argument,
"Change's rangeLength ({0}) doesn't match the "
"computed range length ({1}).",
*Change.rangeLength, ComputedRangeLength);
- Contents.replace(*StartIndex, *EndIndex - *StartIndex, Change.text);
+ Contents.replace(*StartIndex, ComputedRangeLength, Change.text);
return llvm::Error::success();
}
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
https://github.com/playerC updated
https://github.com/llvm/llvm-project/pull/202267
>From d29629dfacb24ca6588e238ab605d0b8e2634cf6 Mon Sep 17 00:00:00 2001
From: player
Date: Mon, 8 Jun 2026 14:27:34 +0800
Subject: [PATCH] [clangd] Fix offset invalid on line.
Fixed the offset invalid on line error, when the change text longer
than that line.
---
clang-tools-extra/clangd/SourceCode.cpp | 8 +---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clangd/SourceCode.cpp
b/clang-tools-extra/clangd/SourceCode.cpp
index 21c078fd2cdb9..60aba1da64559 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -1132,8 +1132,9 @@ llvm::Error applyChange(std::string &Contents,
if (!StartIndex)
return StartIndex.takeError();
+ // End position may longer than current line .
const Position &End = Change.range->end;
- llvm::Expected EndIndex = positionToOffset(Contents, End, false);
+ llvm::Expected EndIndex = positionToOffset(Contents, End, true);
inferFinalNewline(EndIndex, Contents, End);
if (!EndIndex)
return EndIndex.takeError();
@@ -1153,13 +1154,14 @@ llvm::Error applyChange(std::string &Contents,
ssize_t ComputedRangeLength =
lspLength(Contents.substr(*StartIndex, *EndIndex - *StartIndex));
- if (Change.rangeLength && ComputedRangeLength != *Change.rangeLength)
+ // CoumputedRangeLength may less equal than rangeLength.
+ if (Change.rangeLength && ComputedRangeLength > *Change.rangeLength)
return error(llvm::errc::invalid_argument,
"Change's rangeLength ({0}) doesn't match the "
"computed range length ({1}).",
*Change.rangeLength, ComputedRangeLength);
- Contents.replace(*StartIndex, *EndIndex - *StartIndex, Change.text);
+ Contents.replace(*StartIndex, ComputedRangeLength, Change.text);
return llvm::Error::success();
}
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
ArcsinX wrote: Hello @playerC Can you please provide an example scenario when this problem occurs? This example can also be used to write a test. https://github.com/llvm/llvm-project/pull/202267 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
https://github.com/playerC updated
https://github.com/llvm/llvm-project/pull/202267
>From d29629dfacb24ca6588e238ab605d0b8e2634cf6 Mon Sep 17 00:00:00 2001
From: player
Date: Mon, 8 Jun 2026 14:27:34 +0800
Subject: [PATCH] [clangd] Fix offset invalid on line.
Fixed the offset invalid on line error, when the change text longer
than that line.
---
clang-tools-extra/clangd/SourceCode.cpp | 8 +---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clangd/SourceCode.cpp
b/clang-tools-extra/clangd/SourceCode.cpp
index 21c078fd2cdb9..60aba1da64559 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -1132,8 +1132,9 @@ llvm::Error applyChange(std::string &Contents,
if (!StartIndex)
return StartIndex.takeError();
+ // End position may longer than current line .
const Position &End = Change.range->end;
- llvm::Expected EndIndex = positionToOffset(Contents, End, false);
+ llvm::Expected EndIndex = positionToOffset(Contents, End, true);
inferFinalNewline(EndIndex, Contents, End);
if (!EndIndex)
return EndIndex.takeError();
@@ -1153,13 +1154,14 @@ llvm::Error applyChange(std::string &Contents,
ssize_t ComputedRangeLength =
lspLength(Contents.substr(*StartIndex, *EndIndex - *StartIndex));
- if (Change.rangeLength && ComputedRangeLength != *Change.rangeLength)
+ // CoumputedRangeLength may less equal than rangeLength.
+ if (Change.rangeLength && ComputedRangeLength > *Change.rangeLength)
return error(llvm::errc::invalid_argument,
"Change's rangeLength ({0}) doesn't match the "
"computed range length ({1}).",
*Change.rangeLength, ComputedRangeLength);
- Contents.replace(*StartIndex, *EndIndex - *StartIndex, Change.text);
+ Contents.replace(*StartIndex, ComputedRangeLength, Change.text);
return llvm::Error::success();
}
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
playerC wrote: > Hello @playerC 👋 > > Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this > is your first PR, here are a few useful links covering our main contribution > policies and review practices. > > * All contributions to LLVM must follow our [LLVM AI Tool Use > Policy](https://llvm.org/docs/AIToolPolicy.html). In particular, if you used > AI while working on this PR, remember to add a note to the PR description. > > * The [LLVM Code-Review Policy and > Practices](https://llvm.org/docs/CodeReview.html) document contains practical > information about the PR process, including how patches are reviewed and > accepted, and who can review a PR. > > * Our [LLVM Developer Policy](https://llvm.org/docs/DeveloperPolicy.html) > describes our expectations for code quality, commit summaries and contains > notes on our CI system. > > > Please reply to this message to confirm that you have read these policies, > especially the LLVM AI Tool Use Policy, and that any AI tool usage has been > noted in the PR description. > ### Frequently asked questions > > **How do I add reviewers?** > > This PR will be automatically labeled, and the relevant teams will be > notified. For some parts of the project, reviewers may also be added > automatically. > > You can also add reviewers manually using the **Reviewers** section on this > page. If you cannot use that section, it is probably because you do not have > write permissions for the repository. In that case, you can request a review > by tagging reviewers in a comment using `@` followed by their GitHub username. > > **What if there are no comments?** > > If you have not received any comments on your PR after a week, you can > request a review by pinging the PR with a comment such as “Ping”. The common > courtesy ping rate is once a week. Please remember that you are asking for > volunteer time from other developers. > > **Are any special GitHub settings required to contribute to LLVM?** > > We only require contributors to have a public email address associated with > their GitHub commits, see this > [section](https://llvm.org/docs/DeveloperPolicy.html#email-addresses) of LLVM > Developer Policy for details. > > If you have questions, feel free to leave a comment on this PR, or ask on > [LLVM Discord](https://discord.com/invite/xS7Z362) or [LLVM > Discourse](https://discourse.llvm.org/). > > Thank you, The LLVM Community Confirm. https://github.com/llvm/llvm-project/pull/202267 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
llvmorg-github-actions[bot] wrote:
@llvm/pr-subscribers-clang-tools-extra
Author: player (playerC)
Changes
Fixed the offset invalid on line error, when the change text longer than that
line.
---
Full diff: https://github.com/llvm/llvm-project/pull/202267.diff
1 Files Affected:
- (modified) clang-tools-extra/clangd/SourceCode.cpp (+5-3)
``diff
diff --git a/clang-tools-extra/clangd/SourceCode.cpp
b/clang-tools-extra/clangd/SourceCode.cpp
index 21c078fd2cdb9..60aba1da64559 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -1132,8 +1132,9 @@ llvm::Error applyChange(std::string &Contents,
if (!StartIndex)
return StartIndex.takeError();
+ // End position may longer than current line .
const Position &End = Change.range->end;
- llvm::Expected EndIndex = positionToOffset(Contents, End, false);
+ llvm::Expected EndIndex = positionToOffset(Contents, End, true);
inferFinalNewline(EndIndex, Contents, End);
if (!EndIndex)
return EndIndex.takeError();
@@ -1153,13 +1154,14 @@ llvm::Error applyChange(std::string &Contents,
ssize_t ComputedRangeLength =
lspLength(Contents.substr(*StartIndex, *EndIndex - *StartIndex));
- if (Change.rangeLength && ComputedRangeLength != *Change.rangeLength)
+ // CoumputedRangeLength may less equal than rangeLength.
+ if (Change.rangeLength && ComputedRangeLength > *Change.rangeLength)
return error(llvm::errc::invalid_argument,
"Change's rangeLength ({0}) doesn't match the "
"computed range length ({1}).",
*Change.rangeLength, ComputedRangeLength);
- Contents.replace(*StartIndex, *EndIndex - *StartIndex, Change.text);
+ Contents.replace(*StartIndex, ComputedRangeLength, Change.text);
return llvm::Error::success();
}
``
https://github.com/llvm/llvm-project/pull/202267
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
llvmorg-github-actions[bot] wrote:
@llvm/pr-subscribers-clangd
Author: player (playerC)
Changes
Fixed the offset invalid on line error, when the change text longer than that
line.
---
Full diff: https://github.com/llvm/llvm-project/pull/202267.diff
1 Files Affected:
- (modified) clang-tools-extra/clangd/SourceCode.cpp (+5-3)
``diff
diff --git a/clang-tools-extra/clangd/SourceCode.cpp
b/clang-tools-extra/clangd/SourceCode.cpp
index 21c078fd2cdb9..60aba1da64559 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -1132,8 +1132,9 @@ llvm::Error applyChange(std::string &Contents,
if (!StartIndex)
return StartIndex.takeError();
+ // End position may longer than current line .
const Position &End = Change.range->end;
- llvm::Expected EndIndex = positionToOffset(Contents, End, false);
+ llvm::Expected EndIndex = positionToOffset(Contents, End, true);
inferFinalNewline(EndIndex, Contents, End);
if (!EndIndex)
return EndIndex.takeError();
@@ -1153,13 +1154,14 @@ llvm::Error applyChange(std::string &Contents,
ssize_t ComputedRangeLength =
lspLength(Contents.substr(*StartIndex, *EndIndex - *StartIndex));
- if (Change.rangeLength && ComputedRangeLength != *Change.rangeLength)
+ // CoumputedRangeLength may less equal than rangeLength.
+ if (Change.rangeLength && ComputedRangeLength > *Change.rangeLength)
return error(llvm::errc::invalid_argument,
"Change's rangeLength ({0}) doesn't match the "
"computed range length ({1}).",
*Change.rangeLength, ComputedRangeLength);
- Contents.replace(*StartIndex, *EndIndex - *StartIndex, Change.text);
+ Contents.replace(*StartIndex, ComputedRangeLength, Change.text);
return llvm::Error::success();
}
``
https://github.com/llvm/llvm-project/pull/202267
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
github-actions[bot] wrote: Hello @playerC :wave: Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices. * All contributions to LLVM must follow our [LLVM AI Tool Use Policy](https://llvm.org/docs/AIToolPolicy.html). In particular, if you used AI while working on this PR, remember to add a note to the PR description. * The [LLVM Code-Review Policy and Practices](https://llvm.org/docs/CodeReview.html) document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR. * Our [LLVM Developer Policy](https://llvm.org/docs/DeveloperPolicy.html) describes our expectations for code quality, commit summaries and contains notes on our CI system. Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description. --- ### Frequently asked questions **How do I add reviewers?** This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically. You can also add reviewers manually using the **Reviewers** section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using `@` followed by their GitHub username. **What if there are no comments?** If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers. **Are any special GitHub settings required to contribute to LLVM?** We only require contributors to have a public email address associated with their GitHub commits, see this [section](https://llvm.org/docs/DeveloperPolicy.html#email-addresses) of LLVM Developer Policy for details. --- If you have questions, feel free to leave a comment on this PR, or ask on [LLVM Discord](https://discord.com/invite/xS7Z362) or [LLVM Discourse](https://discourse.llvm.org/). Thank you, The LLVM Community https://github.com/llvm/llvm-project/pull/202267 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Fix offset invalid on line. (PR #202267)
https://github.com/playerC created
https://github.com/llvm/llvm-project/pull/202267
Fixed the offset invalid on line error, when the change text longer than that
line.
>From 1ea58cde378c378f649cce47175e1c5ba112e4bb Mon Sep 17 00:00:00 2001
From: player
Date: Mon, 8 Jun 2026 14:27:34 +0800
Subject: [PATCH] [clangd] Fix offset invalid on line.
Fixed the offset invalid on line error, when the change text longer
than that line.
---
clang-tools-extra/clangd/SourceCode.cpp | 8 +---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clangd/SourceCode.cpp
b/clang-tools-extra/clangd/SourceCode.cpp
index 21c078fd2cdb9..60aba1da64559 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -1132,8 +1132,9 @@ llvm::Error applyChange(std::string &Contents,
if (!StartIndex)
return StartIndex.takeError();
+ // End position may longer than current line .
const Position &End = Change.range->end;
- llvm::Expected EndIndex = positionToOffset(Contents, End, false);
+ llvm::Expected EndIndex = positionToOffset(Contents, End, true);
inferFinalNewline(EndIndex, Contents, End);
if (!EndIndex)
return EndIndex.takeError();
@@ -1153,13 +1154,14 @@ llvm::Error applyChange(std::string &Contents,
ssize_t ComputedRangeLength =
lspLength(Contents.substr(*StartIndex, *EndIndex - *StartIndex));
- if (Change.rangeLength && ComputedRangeLength != *Change.rangeLength)
+ // CoumputedRangeLength may less equal than rangeLength.
+ if (Change.rangeLength && ComputedRangeLength > *Change.rangeLength)
return error(llvm::errc::invalid_argument,
"Change's rangeLength ({0}) doesn't match the "
"computed range length ({1}).",
*Change.rangeLength, ComputedRangeLength);
- Contents.replace(*StartIndex, *EndIndex - *StartIndex, Change.text);
+ Contents.replace(*StartIndex, ComputedRangeLength, Change.text);
return llvm::Error::success();
}
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
