Skip to content

[clang][CodeComplete] skip explicit obj param in SignatureHelp #146649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 63 additions & 7 deletions clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3266,6 +3266,47 @@ TEST(SignatureHelpTest, VariadicType) {
}
}

TEST(SignatureHelpTest, SkipExplicitObjectParameter) {
Annotations Code(R"cpp(
struct A {
void foo(this auto&& self, int arg);
void bar(this A self, int arg);
};
int main() {
A a {};
a.foo($c1^);
(&A::bar)($c2^);
// TODO: (&A::foo)(^c3)
}
)cpp");

auto TU = TestTU::withCode(Code.code());
TU.ExtraArgs = {"-std=c++23"};

MockFS FS;
auto Inputs = TU.inputs(FS);

auto Preamble = TU.preamble();
ASSERT_TRUE(Preamble);

{
const auto Result = signatureHelp(testPath(TU.Filename), Code.point("c1"),
*Preamble, Inputs, MarkupKind::PlainText);

EXPECT_EQ(1, Result.signatures.size());

EXPECT_THAT(Result.signatures[0], AllOf(sig("foo([[int arg]]) -> void")));
}
{
const auto Result = signatureHelp(testPath(TU.Filename), Code.point("c2"),
*Preamble, Inputs, MarkupKind::PlainText);

EXPECT_EQ(1, Result.signatures.size());

EXPECT_THAT(Result.signatures[0], AllOf(sig("([[A]], [[int]]) -> void")));
}
}

TEST(CompletionTest, IncludedCompletionKinds) {
Annotations Test(R"cpp(#include "^)cpp");
auto TU = TestTU::withCode(Test.code());
Expand Down Expand Up @@ -4368,11 +4409,14 @@ TEST(CompletionTest, SkipExplicitObjectParameter) {
Annotations Code(R"cpp(
struct A {
void foo(this auto&& self, int arg);
void bar(this A self, int arg);
};

int main() {
A a {};
a.^
a.$c1^s
(&A::ba$c2^;
// TODO: (&A::fo$c3^
}
)cpp");

Expand All @@ -4386,12 +4430,24 @@ TEST(CompletionTest, SkipExplicitObjectParameter) {

MockFS FS;
auto Inputs = TU.inputs(FS);
auto Result = codeComplete(testPath(TU.Filename), Code.point(),
Preamble.get(), Inputs, Opts);

EXPECT_THAT(Result.Completions,
ElementsAre(AllOf(named("foo"), signature("(int arg)"),
snippetSuffix("(${1:int arg})"))));
{
auto Result = codeComplete(testPath(TU.Filename), Code.point("c1"),
Preamble.get(), Inputs, Opts);

EXPECT_THAT(Result.Completions,
UnorderedElementsAre(AllOf(named("foo"), signature("(int arg)"),
snippetSuffix("(${1:int arg})")),
AllOf(named("bar"), signature("(int arg)"),
snippetSuffix("(${1:int arg})"))));
}
{
auto Result = codeComplete(testPath(TU.Filename), Code.point("c2"),
Preamble.get(), Inputs, Opts);
// TODO: snippet suffix is empty for c2
EXPECT_THAT(Result.Completions,
ElementsAre(AllOf(named("bar"), signature("(int arg)"),
snippetSuffix(""))));
}
}
} // namespace
} // namespace clangd
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Sema/SemaCodeComplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4034,6 +4034,14 @@ static void AddOverloadParameterChunks(
return;
}

// C++23 introduces an explicit object parameter, a.k.a. "deducing this"
// Skip it for autocomplete and treat the next parameter as the first
// parameter
if (Function && FirstParameter &&
Function->getParamDecl(P)->isExplicitObjectParameter()) {
continue;
}

if (FirstParameter)
FirstParameter = false;
else
Expand Down
44 changes: 36 additions & 8 deletions clang/test/CodeCompletion/skip-explicit-object-parameter.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
struct A {
void foo(this A self, int arg);
void foo(this auto&& self, int arg);
void bar(this A self, int arg);
};

int main() {
int func1() {
A a {};
a.
}
// RUN: %clang_cc1 -cc1 -fsyntax-only -code-completion-at=%s:%(line-2):5 -std=c++23 %s | FileCheck %s
// CHECK: COMPLETION: A : A::
// CHECK-NEXT: COMPLETION: foo : [#void#]foo(<#int arg#>)
// CHECK-NEXT: COMPLETION: operator= : [#A &#]operator=(<#const A &#>)
// CHECK-NEXT: COMPLETION: operator= : [#A &#]operator=(<#A &&#>)
// CHECK-NEXT: COMPLETION: ~A : [#void#]~A()
// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:%(line-2):5 -std=c++23 %s | FileCheck -check-prefix=CHECK-CC1 %s
// CHECK-CC1: COMPLETION: A : A::
// CHECK-NEXT-CC1: COMPLETION: bar : [#void#]bar(<#int arg#>)
// CHECK-NEXT-CC1: COMPLETION: foo : [#void#]foo(<#int arg#>)
// CHECK-NEXT-CC1: COMPLETION: operator= : [#A &#]operator=(<#const A &#>)
// CHECK-NEXT-CC1: COMPLETION: operator= : [#A &#]operator=(<#A &&#>)
// CHECK-NEXT-CC1: COMPLETION: ~A : [#void#]~A()

struct B {
template <typename T>
void foo(this T&& self, int arg);
};

int func2() {
B b {};
b.foo();
}
// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:%(line-2):9 -std=c++23 %s | FileCheck -check-prefix=CHECK-CC2 %s
// CHECK-CC2: OVERLOAD: [#void#]foo(int arg)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My current understanding is that this line checks that the given overload exists, but not that other overloads do not. Do I need to ensure that this is the only overload?


int func3() {
// TODO: (&A::foo)
(&A::bar)
}
// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:%(line-2):10 -std=c++23 %s | FileCheck -check-prefix=CHECK-CC3 %s
// CHECK-CC3: COMPLETION: bar : [#void#]bar(<#int arg#>)

int func4() {
// TODO: (&A::foo)(
(&A::bar)(
}
// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:%(line-2):13 -std=c++23 %s | FileCheck -check-prefix=CHECK-CC4 %s
// CHECK-CC4: OVERLOAD: [#void#](<#A#>, int)
Loading