@@ -635,17 +635,21 @@ bool SymbolCollector::handleDeclOccurrence(
635635 return true ;
636636
637637 const Symbol *BasicSymbol = Symbols.find (ID);
638- if (isPreferredDeclaration (*OriginalDecl, Roles))
638+ bool SkipDocCheckInDef = false ;
639+ if (isPreferredDeclaration (*OriginalDecl, Roles)) {
639640 // If OriginalDecl is preferred, replace/create the existing canonical
640641 // declaration (e.g. a class forward declaration). There should be at most
641642 // one duplicate as we expect to see only one preferred declaration per
642643 // TU, because in practice they are definitions.
643644 BasicSymbol = addDeclaration (*OriginalDecl, std::move (ID), IsMainFileOnly);
644- else if (!BasicSymbol || DeclIsCanonical)
645+ SkipDocCheckInDef = true ;
646+ } else if (!BasicSymbol || DeclIsCanonical) {
645647 BasicSymbol = addDeclaration (*ND, std::move (ID), IsMainFileOnly);
648+ SkipDocCheckInDef = true ;
649+ }
646650
647651 if (Roles & static_cast <unsigned >(index::SymbolRole::Definition))
648- addDefinition (*OriginalDecl, *BasicSymbol);
652+ addDefinition (*OriginalDecl, *BasicSymbol, SkipDocCheckInDef );
649653
650654 return true ;
651655}
@@ -1025,16 +1029,28 @@ const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
10251029 *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
10261030 *CompletionTUInfo,
10271031 /* IncludeBriefComments*/ false );
1028- std::string Documentation =
1029- formatDocumentation (*CCS, getDocComment (Ctx, SymbolCompletion,
1030- /* CommentsFromHeaders=*/ true ));
1032+ std::string DocComment;
1033+ std::string Documentation;
1034+ bool AlreadyHasDoc = S.Flags & Symbol::HasDocComment;
1035+ if (!AlreadyHasDoc) {
1036+ DocComment = getDocComment (Ctx, SymbolCompletion,
1037+ /* CommentsFromHeaders=*/ true );
1038+ Documentation = formatDocumentation (*CCS, DocComment);
1039+ }
1040+ const auto UpdateDoc = [&] {
1041+ if (!AlreadyHasDoc) {
1042+ if (!DocComment.empty ())
1043+ S.Flags |= Symbol::HasDocComment;
1044+ S.Documentation = Documentation;
1045+ }
1046+ };
10311047 if (!(S.Flags & Symbol::IndexedForCodeCompletion)) {
10321048 if (Opts.StoreAllDocumentation )
1033- S. Documentation = Documentation ;
1049+ UpdateDoc () ;
10341050 Symbols.insert (S);
10351051 return Symbols.find (S.ID );
10361052 }
1037- S. Documentation = Documentation ;
1053+ UpdateDoc () ;
10381054 std::string Signature;
10391055 std::string SnippetSuffix;
10401056 getSignature (*CCS, &Signature, &SnippetSuffix, SymbolCompletion.Kind ,
@@ -1058,8 +1074,8 @@ const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
10581074 return Symbols.find (S.ID );
10591075}
10601076
1061- void SymbolCollector::addDefinition (const NamedDecl &ND,
1062- const Symbol &DeclSym ) {
1077+ void SymbolCollector::addDefinition (const NamedDecl &ND, const Symbol &DeclSym,
1078+ bool SkipDocCheck ) {
10631079 if (DeclSym.Definition )
10641080 return ;
10651081 const auto &SM = ND.getASTContext ().getSourceManager ();
@@ -1074,6 +1090,27 @@ void SymbolCollector::addDefinition(const NamedDecl &ND,
10741090 Symbol S = DeclSym;
10751091 // FIXME: use the result to filter out symbols.
10761092 S.Definition = *DefLoc;
1093+
1094+ std::string DocComment;
1095+ std::string Documentation;
1096+ if (!SkipDocCheck && !(S.Flags & Symbol::HasDocComment) &&
1097+ (llvm::isa<FunctionDecl>(ND) || llvm::isa<CXXMethodDecl>(ND))) {
1098+ CodeCompletionResult SymbolCompletion (&getTemplateOrThis (ND), 0 );
1099+ const auto *CCS = SymbolCompletion.CreateCodeCompletionString (
1100+ *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
1101+ *CompletionTUInfo,
1102+ /* IncludeBriefComments*/ false );
1103+ DocComment = getDocComment (ND.getASTContext (), SymbolCompletion,
1104+ /* CommentsFromHeaders=*/ true );
1105+ if (!S.Documentation .empty ())
1106+ Documentation = S.Documentation .str () + ' \n ' + DocComment;
1107+ else
1108+ Documentation = formatDocumentation (*CCS, DocComment);
1109+ if (!DocComment.empty ())
1110+ S.Flags |= Symbol::HasDocComment;
1111+ S.Documentation = Documentation;
1112+ }
1113+
10771114 Symbols.insert (S);
10781115}
10791116
0 commit comments