Skip to content

Commit 4bb5c6d

Browse files
committed
[clangd] Collect comments from function definitions into the index
This is useful with projects that put their (doxygen) comments at the implementation site, rather than the header.
1 parent 97187e1 commit 4bb5c6d

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

clang-tools-extra/clangd/index/Symbol.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ struct Symbol {
7575
/// (When snippets are disabled, the symbol name alone is used).
7676
/// Only set when the symbol is indexed for completion.
7777
llvm::StringRef CompletionSnippetSuffix;
78-
/// Documentation including comment for the symbol declaration.
78+
/// Comment for the symbol declaration.
79+
llvm::StringRef DocComment;
80+
/// Documentation including DocComment.
7981
llvm::StringRef Documentation;
8082
/// Type when this symbol is used in an expression. (Short display form).
8183
/// e.g. return type of a function, or type of a variable.

clang-tools-extra/clangd/index/SymbolCollector.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,15 +1016,16 @@ const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
10161016
*ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
10171017
*CompletionTUInfo,
10181018
/*IncludeBriefComments*/ false);
1019-
std::string Documentation =
1020-
formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion,
1021-
/*CommentsFromHeaders=*/true));
1019+
std::string DocComment = getDocComment(Ctx, SymbolCompletion,
1020+
/*CommentsFromHeaders=*/true);
1021+
std::string Documentation = formatDocumentation(*CCS, DocComment);
10221022
if (!(S.Flags & Symbol::IndexedForCodeCompletion)) {
10231023
if (Opts.StoreAllDocumentation)
10241024
S.Documentation = Documentation;
10251025
Symbols.insert(S);
10261026
return Symbols.find(S.ID);
10271027
}
1028+
S.DocComment = DocComment;
10281029
S.Documentation = Documentation;
10291030
std::string Signature;
10301031
std::string SnippetSuffix;
@@ -1065,6 +1066,26 @@ void SymbolCollector::addDefinition(const NamedDecl &ND,
10651066
Symbol S = DeclSym;
10661067
// FIXME: use the result to filter out symbols.
10671068
S.Definition = *DefLoc;
1069+
1070+
std::string DocComment;
1071+
std::string Documentation;
1072+
if (S.DocComment.empty() &&
1073+
(llvm::isa<FunctionDecl>(ND) || llvm::isa<CXXMethodDecl>(ND))) {
1074+
CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
1075+
const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
1076+
*ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
1077+
*CompletionTUInfo,
1078+
/*IncludeBriefComments*/ false);
1079+
DocComment = getDocComment(ND.getASTContext(), SymbolCompletion,
1080+
/*CommentsFromHeaders=*/true);
1081+
if (!S.Documentation.empty())
1082+
Documentation = S.Documentation.str() + '\n' + DocComment;
1083+
else
1084+
Documentation = formatDocumentation(*CCS, DocComment);
1085+
S.DocComment = DocComment;
1086+
S.Documentation = Documentation;
1087+
}
1088+
10681089
Symbols.insert(S);
10691090
}
10701091

clang/lib/AST/ASTContext.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,17 @@ const RawComment *ASTContext::getRawCommentForAnyRedecl(
509509
if (LastCheckedRedecl) {
510510
if (LastCheckedRedecl == Redecl) {
511511
LastCheckedRedecl = nullptr;
512+
continue;
513+
}
514+
if (auto F = llvm::dyn_cast<FunctionDecl>(Redecl)) {
515+
if (!F->isThisDeclarationADefinition())
516+
continue;
517+
} else if (auto M = llvm::dyn_cast<CXXMethodDecl>(Redecl)) {
518+
if (!M->isThisDeclarationADefinition())
519+
continue;
520+
} else {
521+
continue;
512522
}
513-
continue;
514523
}
515524
const RawComment *RedeclComment = getRawCommentForDeclNoCache(Redecl);
516525
if (RedeclComment) {

0 commit comments

Comments
 (0)