Skip to content

Commit db68723

Browse files
tom-anderstru
authored andcommitted
[clangd] Return earlier when snippet is empty
Fixes github.com/clangd/clangd/issues/1216 If the Snippet string is empty, Snippet.front() would trigger a crash. Move the Snippet->empty() check up a few lines to avoid this. Should not break any existing behavior. Differential Revision: https://reviews.llvm.org/D134137 (cherry picked from commit 60528c6)
1 parent 2d5c43a commit db68723

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,9 @@ struct CodeCompletionBuilder {
486486
// we need to complete 'forward<$1>($0)'.
487487
return "($0)";
488488

489+
if (Snippet->empty())
490+
return "";
491+
489492
bool MayHaveArgList = Completion.Kind == CompletionItemKind::Function ||
490493
Completion.Kind == CompletionItemKind::Method ||
491494
Completion.Kind == CompletionItemKind::Constructor ||
@@ -524,8 +527,6 @@ struct CodeCompletionBuilder {
524527
return *Snippet;
525528

526529
// Replace argument snippets with a simplified pattern.
527-
if (Snippet->empty())
528-
return "";
529530
if (MayHaveArgList) {
530531
// Functions snippets can be of 2 types:
531532
// - containing only function arguments, e.g.

clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,23 @@ TEST(CodeCompleteTest, NoColonColonAtTheEnd) {
10141014
EXPECT_THAT(Results.Completions, Not(Contains(labeled("clang::"))));
10151015
}
10161016

1017+
TEST(CompletionTests, EmptySnippetDoesNotCrash) {
1018+
// See https://github.com/clangd/clangd/issues/1216
1019+
auto Results = completions(R"cpp(
1020+
int main() {
1021+
auto w = [&](auto &&f) { return f(f); };
1022+
auto f = w([&](auto &&f) {
1023+
return [&](auto &&n) {
1024+
if (n == 0) {
1025+
return 1;
1026+
}
1027+
return n * ^(f)(n - 1);
1028+
};
1029+
})(10);
1030+
}
1031+
)cpp");
1032+
}
1033+
10171034
TEST(CompletionTest, BacktrackCrashes) {
10181035
// Sema calls code completion callbacks twice in these cases.
10191036
auto Results = completions(R"cpp(

0 commit comments

Comments
 (0)