[clang-tools-extra] [clangd] Collect comments from function definitions into the index (PR #67802)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 29 06:19:24 PDT 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangd
<details>
<summary>Changes</summary>
This is useful with projects that put their (doxygen) comments at the implementation site, rather than the header.
---
Full diff: https://github.com/llvm/llvm-project/pull/67802.diff
3 Files Affected:
- (modified) clang-tools-extra/clangd/index/Symbol.h (+3-1)
- (modified) clang-tools-extra/clangd/index/SymbolCollector.cpp (+24-3)
- (modified) clang/lib/AST/ASTContext.cpp (+10-1)
``````````diff
diff --git a/clang-tools-extra/clangd/index/Symbol.h b/clang-tools-extra/clangd/index/Symbol.h
index 1aa5265299231b7..0917abd3c7edad9 100644
--- a/clang-tools-extra/clangd/index/Symbol.h
+++ b/clang-tools-extra/clangd/index/Symbol.h
@@ -75,7 +75,9 @@ struct Symbol {
/// (When snippets are disabled, the symbol name alone is used).
/// Only set when the symbol is indexed for completion.
llvm::StringRef CompletionSnippetSuffix;
- /// Documentation including comment for the symbol declaration.
+ /// Comment for the symbol declaration.
+ llvm::StringRef DocComment;
+ /// Documentation including DocComment.
llvm::StringRef Documentation;
/// Type when this symbol is used in an expression. (Short display form).
/// e.g. return type of a function, or type of a variable.
diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp
index 74aca9b99c8a584..71833b56d8a02e2 100644
--- a/clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -1016,15 +1016,16 @@ const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
*ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
*CompletionTUInfo,
/*IncludeBriefComments*/ false);
- std::string Documentation =
- formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion,
- /*CommentsFromHeaders=*/true));
+ std::string DocComment = getDocComment(Ctx, SymbolCompletion,
+ /*CommentsFromHeaders=*/true);
+ std::string Documentation = formatDocumentation(*CCS, DocComment);
if (!(S.Flags & Symbol::IndexedForCodeCompletion)) {
if (Opts.StoreAllDocumentation)
S.Documentation = Documentation;
Symbols.insert(S);
return Symbols.find(S.ID);
}
+ S.DocComment = DocComment;
S.Documentation = Documentation;
std::string Signature;
std::string SnippetSuffix;
@@ -1065,6 +1066,26 @@ void SymbolCollector::addDefinition(const NamedDecl &ND,
Symbol S = DeclSym;
// FIXME: use the result to filter out symbols.
S.Definition = *DefLoc;
+
+ std::string DocComment;
+ std::string Documentation;
+ if (S.DocComment.empty() &&
+ (llvm::isa<FunctionDecl>(ND) || llvm::isa<CXXMethodDecl>(ND))) {
+ CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
+ const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
+ *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
+ *CompletionTUInfo,
+ /*IncludeBriefComments*/ false);
+ DocComment = getDocComment(ND.getASTContext(), SymbolCompletion,
+ /*CommentsFromHeaders=*/true);
+ if (!S.Documentation.empty())
+ Documentation = S.Documentation.str() + '\n' + DocComment;
+ else
+ Documentation = formatDocumentation(*CCS, DocComment);
+ S.DocComment = DocComment;
+ S.Documentation = Documentation;
+ }
+
Symbols.insert(S);
}
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 57aaa05b1d81ddb..28cdc70ed444440 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -509,8 +509,17 @@ const RawComment *ASTContext::getRawCommentForAnyRedecl(
if (LastCheckedRedecl) {
if (LastCheckedRedecl == Redecl) {
LastCheckedRedecl = nullptr;
+ continue;
+ }
+ if (auto F = llvm::dyn_cast<FunctionDecl>(Redecl)) {
+ if (!F->isThisDeclarationADefinition())
+ continue;
+ } else if (auto M = llvm::dyn_cast<CXXMethodDecl>(Redecl)) {
+ if (!M->isThisDeclarationADefinition())
+ continue;
+ } else {
+ continue;
}
- continue;
}
const RawComment *RedeclComment = getRawCommentForDeclNoCache(Redecl);
if (RedeclComment) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/67802
More information about the cfe-commits
mailing list