[clang] [clangd] Collect comments from function definitions into the index (PR #67802)

via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 29 06:18:21 PDT 2023


https://github.com/ckandeler created https://github.com/llvm/llvm-project/pull/67802

This is useful with projects that put their (doxygen) comments at the implementation site, rather than the header.

>From 4bb5c6ddd35c7a5a4d7ca2155ddafa51fbfffe0b Mon Sep 17 00:00:00 2001
From: Christian Kandeler <christian.kandeler at qt.io>
Date: Fri, 29 Sep 2023 15:01:58 +0200
Subject: [PATCH] [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.
---
 clang-tools-extra/clangd/index/Symbol.h       |  4 ++-
 .../clangd/index/SymbolCollector.cpp          | 27 ++++++++++++++++---
 clang/lib/AST/ASTContext.cpp                  | 11 +++++++-
 3 files changed, 37 insertions(+), 5 deletions(-)

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) {



More information about the cfe-commits mailing list