Index: clangd/CodeComplete.cpp =================================================================== --- clangd/CodeComplete.cpp +++ clangd/CodeComplete.cpp @@ -729,8 +729,8 @@ // FIXME: for headers, we need to get a comment from the index. ScoredSignatures.push_back(processOverloadCandidate( Candidate, *CCS, - getParameterDocComment(S.getASTContext(), Candidate, CurrentArg, - /*CommentsFromHeaders=*/false))); + getDocComment(S.getASTContext(), Candidate, + /*CommentsFromHeaders=*/false))); } std::sort(ScoredSignatures.begin(), ScoredSignatures.end(), [](const ScoredSignature &L, const ScoredSignature &R) { Index: clangd/CodeCompletionStrings.h =================================================================== --- clangd/CodeCompletionStrings.h +++ clangd/CodeCompletionStrings.h @@ -32,18 +32,10 @@ const CodeCompletionResult &Result, bool CommentsFromHeaders); -/// Gets a minimally formatted documentation for parameter of \p Result, -/// corresponding to argument number \p ArgIndex. -/// This currently looks for comments attached to the parameter itself, and -/// doesn't extract them from function documentation. -/// Returns empty string when no comment is available. -/// If \p CommentsFromHeaders parameter is set, only comments from the main -/// file will be returned. It is used to workaround crashes when parsing -/// comments in the stale headers, coming from completion preamble. std::string -getParameterDocComment(const ASTContext &Ctx, - const CodeCompleteConsumer::OverloadCandidate &Result, - unsigned ArgIndex, bool CommentsFromHeaders); +getDocComment(const ASTContext &Ctx, + const CodeCompleteConsumer::OverloadCandidate &Overload, + bool CommentsFromHeaders); /// Formats the signature for an item, as a display string and snippet. /// e.g. for const_reference std::vector::at(size_type) const, this returns: Index: clangd/CodeCompletionStrings.cpp =================================================================== --- clangd/CodeCompletionStrings.cpp +++ clangd/CodeCompletionStrings.cpp @@ -41,28 +41,17 @@ return CommentText.find_first_not_of("/*-= \t\r\n") != llvm::StringRef::npos; } -} // namespace - -std::string getDocComment(const ASTContext &Ctx, - const CodeCompletionResult &Result, - bool CommentsFromHeaders) { - // FIXME: clang's completion also returns documentation for RK_Pattern if they - // contain a pattern for ObjC properties. Unfortunately, there is no API to - // get this declaration, so we don't show documentation in that case. - if (Result.Kind != CodeCompletionResult::RK_Declaration) - return ""; - auto *Decl = Result.getDeclaration(); - if (!Decl || llvm::isa(Decl)) { +std::string getDeclComment(const ASTContext &Ctx, const NamedDecl &Decl) { + if (llvm::isa(Decl)) { // Namespaces often have too many redecls for any particular redecl comment // to be useful. Moreover, we often confuse file headers or generated // comments with namespace comments. Therefore we choose to just ignore // the comments for namespaces. return ""; } - const RawComment *RC = getCompletionComment(Ctx, Decl); + const RawComment *RC = getCompletionComment(Ctx, &Decl); if (!RC) return ""; - // Sanity check that the comment does not come from the PCH. We choose to not // write them into PCH, because they are racy and slow to load. assert(!Ctx.getSourceManager().isLoadedSourceLocation(RC->getBeginLoc())); @@ -72,23 +61,30 @@ return Doc; } -std::string -getParameterDocComment(const ASTContext &Ctx, - const CodeCompleteConsumer::OverloadCandidate &Result, - unsigned ArgIndex, bool CommentsFromHeaders) { - auto *Func = Result.getFunction(); - if (!Func) +} // namespace + +std::string getDocComment(const ASTContext &Ctx, + const CodeCompletionResult &Result, + bool CommentsFromHeaders) { + // FIXME: clang's completion also returns documentation for RK_Pattern if they + // contain a pattern for ObjC properties. Unfortunately, there is no API to + // get this declaration, so we don't show documentation in that case. + if (Result.Kind != CodeCompletionResult::RK_Declaration) return ""; - const RawComment *RC = getParameterComment(Ctx, Result, ArgIndex); - if (!RC) + auto *Decl = Result.getDeclaration(); + if (!Decl) return ""; - // Sanity check that the comment does not come from the PCH. We choose to not - // write them into PCH, because they are racy and slow to load. - assert(!Ctx.getSourceManager().isLoadedSourceLocation(RC->getBeginLoc())); - std::string Doc = RC->getFormattedText(Ctx.getSourceManager(), Ctx.getDiagnostics()); - if (!looksLikeDocComment(Doc)) + return getDeclComment(Ctx, *Decl); +} + +std::string +getDocComment(const ASTContext &Ctx, + const CodeCompleteConsumer::OverloadCandidate &Overload, + bool CommentsFromHeaders) { + auto *Func = Overload.getFunction(); + if (!Func) return ""; - return Doc; + return getDeclComment(Ctx, *Func); } void getSignature(const CodeCompletionString &CCS, std::string *Signature,