Skip to content

Commit d9d9ab8

Browse files
authored
[clang][CodeComplete] skip explicit obj param in code completion string (#146258)
Fixes clangd/clangd#2339
1 parent b8b7494 commit d9d9ab8

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4363,6 +4363,36 @@ TEST(CompletionTest, PreambleFromDifferentTarget) {
43634363
EXPECT_THAT(Result.Completions, Not(testing::IsEmpty()));
43644364
EXPECT_THAT(Signatures.signatures, Not(testing::IsEmpty()));
43654365
}
4366+
4367+
TEST(CompletionTest, SkipExplicitObjectParameter) {
4368+
Annotations Code(R"cpp(
4369+
struct A {
4370+
void foo(this auto&& self, int arg);
4371+
};
4372+
4373+
int main() {
4374+
A a {};
4375+
a.^
4376+
}
4377+
)cpp");
4378+
4379+
auto TU = TestTU::withCode(Code.code());
4380+
TU.ExtraArgs = {"-std=c++23"};
4381+
4382+
auto Preamble = TU.preamble();
4383+
ASSERT_TRUE(Preamble);
4384+
4385+
CodeCompleteOptions Opts{};
4386+
4387+
MockFS FS;
4388+
auto Inputs = TU.inputs(FS);
4389+
auto Result = codeComplete(testPath(TU.Filename), Code.point(),
4390+
Preamble.get(), Inputs, Opts);
4391+
4392+
EXPECT_THAT(Result.Completions,
4393+
ElementsAre(AllOf(named("foo"), signature("(int arg)"),
4394+
snippetSuffix("(${1:int arg})"))));
4395+
}
43664396
} // namespace
43674397
} // namespace clangd
43684398
} // namespace clang

clang/lib/Sema/SemaCodeComplete.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3260,6 +3260,13 @@ static void AddFunctionParameterChunks(Preprocessor &PP,
32603260
break;
32613261
}
32623262

3263+
// C++23 introduces an explicit object parameter, a.k.a. "deducing this"
3264+
// Skip it for autocomplete and treat the next parameter as the first
3265+
// parameter
3266+
if (FirstParameter && Param->isExplicitObjectParameter()) {
3267+
continue;
3268+
}
3269+
32633270
if (FirstParameter)
32643271
FirstParameter = false;
32653272
else
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
struct A {
2+
void foo(this A self, int arg);
3+
};
4+
5+
int main() {
6+
A a {};
7+
a.
8+
}
9+
// RUN: %clang_cc1 -cc1 -fsyntax-only -code-completion-at=%s:%(line-2):5 -std=c++23 %s | FileCheck %s
10+
// CHECK: COMPLETION: A : A::
11+
// CHECK-NEXT: COMPLETION: foo : [#void#]foo(<#int arg#>)
12+
// CHECK-NEXT: COMPLETION: operator= : [#A &#]operator=(<#const A &#>)
13+
// CHECK-NEXT: COMPLETION: operator= : [#A &#]operator=(<#A &&#>)
14+
// CHECK-NEXT: COMPLETION: ~A : [#void#]~A()

0 commit comments

Comments
 (0)