-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[clangd] Retrieve documentation for member function instance from index #153337
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
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clangd Author: Nathan Ridge (HighCommander4) ChangesFixes clangd/clangd#2290 Full diff: https://github.com/llvm/llvm-project/pull/153337.diff 2 Files Affected:
diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp
index 9c17b4ca9b706..da51998b9eec0 100644
--- a/clang-tools-extra/clangd/CodeComplete.cpp
+++ b/clang-tools-extra/clangd/CodeComplete.cpp
@@ -1887,7 +1887,19 @@ class CodeCompleteFlow {
for (auto &Cand : C.first) {
if (Cand.SemaResult &&
Cand.SemaResult->Kind == CodeCompletionResult::RK_Declaration) {
- auto ID = clangd::getSymbolID(Cand.SemaResult->getDeclaration());
+ const NamedDecl *DeclToLookup = Cand.SemaResult->getDeclaration();
+ // For instantiations of members of class templates, the
+ // documentation will be stored at the member's original
+ // declaration.
+ // FIXME: We'd like to handle fields too but FieldDecl is missing a
+ // method equivalent to getInstantiatedFromMemberFunction().
+ if (const auto *FD = dyn_cast<FunctionDecl>(DeclToLookup)) {
+ if (const auto *InstantiatedFrom =
+ FD->getInstantiatedFromMemberFunction()) {
+ DeclToLookup = InstantiatedFrom;
+ }
+ }
+ auto ID = clangd::getSymbolID(DeclToLookup);
if (!ID)
continue;
Req.IDs.insert(ID);
diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
index 5a5d815076e2a..57eb9fcdecb21 100644
--- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
@@ -1154,23 +1154,45 @@ TEST(CompletionTest, CommentsOnMembersFromHeader) {
/// This is a member function.
int delta();
};
+
+ template <typename T>
+ struct beta {
+ /// This is a member field inside a template.
+ int omega;
+
+ /// This is a member function inside a template.
+ int epsilon();
+ };
)cpp";
auto File = testPath("foo.cpp");
Annotations Test(R"cpp(
#include "foo.h"
alpha a;
-int x = a.^
+beta<int> b;
+int x = a.$p1^;
+int y = b.$p2^;
)cpp");
runAddDocument(Server, File, Test.code());
auto CompletionList =
- llvm::cantFail(runCodeComplete(Server, File, Test.point(), {}));
+ llvm::cantFail(runCodeComplete(Server, File, Test.point("p1"), {}));
EXPECT_THAT(CompletionList.Completions,
Contains(AllOf(named("gamma"), doc("This is a member field."))));
EXPECT_THAT(
CompletionList.Completions,
Contains(AllOf(named("delta"), doc("This is a member function."))));
+
+ CompletionList =
+ llvm::cantFail(runCodeComplete(Server, File, Test.point("p2"), {}));
+
+ EXPECT_THAT(CompletionList.Completions,
+ Contains(AllOf(named("omega")
+ /* FIXME: Doc retrieval does not work yet*/)));
+ EXPECT_THAT(
+ CompletionList.Completions,
+ Contains(AllOf(named("epsilon"),
+ doc("This is a member function inside a template."))));
}
TEST(CompletionTest, CommentsOnMembersFromHeaderOverloadBundling) {
|
// method equivalent to getInstantiatedFromMemberFunction(). | ||
if (const auto *FD = dyn_cast<FunctionDecl>(DeclToLookup)) { | ||
if (const auto *InstantiatedFrom = | ||
FD->getInstantiatedFromMemberFunction()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I be using FunctionDecl::getTemplateInstantiationPattern
instead?
// documentation will be stored at the member's original | ||
// declaration. | ||
// FIXME: We'd like to handle fields too but FieldDecl is missing a | ||
// method equivalent to getInstantiatedFromMemberFunction(). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found some libSema code that rolls its own equivalent for fields using a DeclContext::lookup()
call; we could consider using that approach here as well.
Fixes clangd/clangd#2290