[clang] clangd: Show argument names for function pointer struct fields (PR #69011)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 13 09:42:46 PDT 2023
https://github.com/Qwinci created https://github.com/llvm/llvm-project/pull/69011
Show argument names in signature help when calling a function pointer struct field.
>From 1e4b524f4514fcde8d98dbd6448477b82bd3d780 Mon Sep 17 00:00:00 2001
From: Qwinci <32550582+Qwinci at users.noreply.github.com>
Date: Fri, 13 Oct 2023 19:38:19 +0300
Subject: [PATCH] clangd: Show argument names for function pointer struct
fields
---
.../clangd/unittests/CodeCompleteTests.cpp | 17 +++++++++++++++++
clang/lib/Sema/SemaCodeComplete.cpp | 12 +++++++++++-
2 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
index 766998eb4f3c719..bd88a0912b537d1 100644
--- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
@@ -1462,6 +1462,23 @@ TEST(SignatureHelpTest, FunctionPointers) {
typedef void (__stdcall *fn)(int x, int y);
fn foo;
int main() { foo(^); }
+ )cpp",
+ // Field of function pointer type
+ R"cpp(
+ struct S {
+ void (*foo)(int x, int y);
+ };
+ S s;
+ int main() { s.foo(^); }
+ )cpp",
+ // Field of function pointer typedef type
+ R"cpp(
+ typedef void (*fn)(int x, int y);
+ struct S {
+ fn foo;
+ };
+ S s;
+ int main() { s.foo(^); }
)cpp"};
for (auto Test : Tests)
EXPECT_THAT(signatures(Test).signatures,
diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp
index adb82d3f6d176ab..5e3aed53fd8e218 100644
--- a/clang/lib/Sema/SemaCodeComplete.cpp
+++ b/clang/lib/Sema/SemaCodeComplete.cpp
@@ -6133,7 +6133,17 @@ ProduceSignatureHelp(Sema &SemaRef, MutableArrayRef<ResultCandidate> Candidates,
// so that we can recover argument names from it.
static FunctionProtoTypeLoc GetPrototypeLoc(Expr *Fn) {
TypeLoc Target;
- if (const auto *T = Fn->getType().getTypePtr()->getAs<TypedefType>()) {
+
+ if (const auto *ME = dyn_cast<MemberExpr>(Fn)) {
+ const auto *MD = ME->getMemberDecl();
+ if (const auto *FD = dyn_cast<FieldDecl>(MD)) {
+ if (const auto *T = FD->getType().getTypePtr()->getAs<TypedefType>()) {
+ Target = T->getDecl()->getTypeSourceInfo()->getTypeLoc();
+ } else {
+ Target = FD->getTypeSourceInfo()->getTypeLoc();
+ }
+ }
+ } else if (const auto *T = Fn->getType().getTypePtr()->getAs<TypedefType>()) {
Target = T->getDecl()->getTypeSourceInfo()->getTypeLoc();
} else if (const auto *DR = dyn_cast<DeclRefExpr>(Fn)) {
More information about the cfe-commits
mailing list