[clang][CodeComplete] skip explicit obj param in code completion string (#146258)

Fixes clangd/clangd#2339
This commit is contained in:
Mythreya
2025-07-01 05:33:56 -07:00
committed by GitHub
parent b8b7494551
commit d9d9ab8698
3 changed files with 51 additions and 0 deletions

View File

@@ -4363,6 +4363,36 @@ TEST(CompletionTest, PreambleFromDifferentTarget) {
EXPECT_THAT(Result.Completions, Not(testing::IsEmpty()));
EXPECT_THAT(Signatures.signatures, Not(testing::IsEmpty()));
}
TEST(CompletionTest, SkipExplicitObjectParameter) {
Annotations Code(R"cpp(
struct A {
void foo(this auto&& self, int arg);
};
int main() {
A a {};
a.^
}
)cpp");
auto TU = TestTU::withCode(Code.code());
TU.ExtraArgs = {"-std=c++23"};
auto Preamble = TU.preamble();
ASSERT_TRUE(Preamble);
CodeCompleteOptions Opts{};
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})"))));
}
} // namespace
} // namespace clangd
} // namespace clang

View File

@@ -3260,6 +3260,13 @@ static void AddFunctionParameterChunks(Preprocessor &PP,
break;
}
// 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 (FirstParameter && Param->isExplicitObjectParameter()) {
continue;
}
if (FirstParameter)
FirstParameter = false;
else

View File

@@ -0,0 +1,14 @@
struct A {
void foo(this A self, int arg);
};
int main() {
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()