Skip to content

Commit e0fac46

Browse files
[clangd] Retrieve documentation for member function instance from index
Fixes clangd/clangd#2290
1 parent 95a4c9c commit e0fac46

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,19 @@ class CodeCompleteFlow {
18831883
for (auto &Cand : C.first) {
18841884
if (Cand.SemaResult &&
18851885
Cand.SemaResult->Kind == CodeCompletionResult::RK_Declaration) {
1886-
auto ID = clangd::getSymbolID(Cand.SemaResult->getDeclaration());
1886+
const NamedDecl *DeclToLookup = Cand.SemaResult->getDeclaration();
1887+
// For instantiations of members of class templates, the
1888+
// documentation will be stored at the member's original
1889+
// declaration.
1890+
// FIXME: We'd like to handle fields too but FieldDecl is missing a
1891+
// method equivalent to getInstantiatedFromMemberFunction().
1892+
if (const auto *FD = dyn_cast<FunctionDecl>(DeclToLookup)) {
1893+
if (const auto *InstantiatedFrom =
1894+
FD->getTemplateInstantiationPattern()) {
1895+
DeclToLookup = InstantiatedFrom;
1896+
}
1897+
}
1898+
auto ID = clangd::getSymbolID(DeclToLookup);
18871899
if (!ID)
18881900
continue;
18891901
Req.IDs.insert(ID);

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,23 +1154,45 @@ TEST(CompletionTest, CommentsOnMembersFromHeader) {
11541154
/// This is a member function.
11551155
int delta();
11561156
};
1157+
1158+
template <typename T>
1159+
struct beta {
1160+
/// This is a member field inside a template.
1161+
int omega;
1162+
1163+
/// This is a member function inside a template.
1164+
int epsilon();
1165+
};
11571166
)cpp";
11581167

11591168
auto File = testPath("foo.cpp");
11601169
Annotations Test(R"cpp(
11611170
#include "foo.h"
11621171
alpha a;
1163-
int x = a.^
1172+
beta<int> b;
1173+
int x = a.$p1^;
1174+
int y = b.$p2^;
11641175
)cpp");
11651176
runAddDocument(Server, File, Test.code());
11661177
auto CompletionList =
1167-
llvm::cantFail(runCodeComplete(Server, File, Test.point(), {}));
1178+
llvm::cantFail(runCodeComplete(Server, File, Test.point("p1"), {}));
11681179

11691180
EXPECT_THAT(CompletionList.Completions,
11701181
Contains(AllOf(named("gamma"), doc("This is a member field."))));
11711182
EXPECT_THAT(
11721183
CompletionList.Completions,
11731184
Contains(AllOf(named("delta"), doc("This is a member function."))));
1185+
1186+
CompletionList =
1187+
llvm::cantFail(runCodeComplete(Server, File, Test.point("p2"), {}));
1188+
1189+
EXPECT_THAT(CompletionList.Completions,
1190+
Contains(AllOf(named("omega")
1191+
/* FIXME: Doc retrieval does not work yet*/)));
1192+
EXPECT_THAT(
1193+
CompletionList.Completions,
1194+
Contains(AllOf(named("epsilon"),
1195+
doc("This is a member function inside a template."))));
11741196
}
11751197

11761198
TEST(CompletionTest, CommentsOnMembersFromHeaderOverloadBundling) {

0 commit comments

Comments
 (0)