Skip to content

[clangd] introduce doxygen parser #150790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ add_clang_library(clangDaemon STATIC
SemanticHighlighting.cpp
SemanticSelection.cpp
SourceCode.cpp
SymbolDocumentation.cpp
SystemIncludeExtractor.cpp
TidyProvider.cpp
TUScheduler.cpp
Expand Down
60 changes: 50 additions & 10 deletions clang-tools-extra/clangd/CodeCompletionStrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
//===----------------------------------------------------------------------===//

#include "CodeCompletionStrings.h"
#include "Config.h"
#include "SymbolDocumentation.h"
#include "clang-c/Index.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Comment.h"
#include "clang/AST/Decl.h"
#include "clang/AST/RawCommentList.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Sema/CodeCompleteConsumer.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/raw_ostream.h"
#include <limits>
#include <utility>

Expand Down Expand Up @@ -100,16 +105,51 @@ std::string getDeclComment(const ASTContext &Ctx, const NamedDecl &Decl) {
// the comments for namespaces.
return "";
}
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()));
std::string Doc =
RC->getFormattedText(Ctx.getSourceManager(), Ctx.getDiagnostics());
if (!looksLikeDocComment(Doc))
return "";

const RawComment *RC = nullptr;
const Config &Cfg = Config::current();

std::string Doc;

if (Cfg.Documentation.CommentFormat == Config::CommentFormatPolicy::Doxygen &&
isa<ParmVarDecl>(Decl)) {
// Parameters are documented in their declaration context (function or
// template function).
const NamedDecl *ND = dyn_cast<NamedDecl>(Decl.getDeclContext());
if (!ND)
return "";

RC = getCompletionComment(Ctx, ND);
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()));

comments::FullComment *FC = RC->parse(Ctx, /*PP=*/nullptr, ND);
if (!FC)
return "";

SymbolDocCommentVisitor V(FC, Ctx.getLangOpts().CommentOpts);
std::string RawDoc;
llvm::raw_string_ostream OS(RawDoc);

V.parameterDocToString(dyn_cast<ParmVarDecl>(&Decl)->getName(), OS);

Doc = StringRef(RawDoc).trim().str();
} else {
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()));
Doc = RC->getFormattedText(Ctx.getSourceManager(), Ctx.getDiagnostics());
if (!looksLikeDocComment(Doc))
return "";
}

// Clang requires source to be UTF-8, but doesn't enforce this in comments.
if (!llvm::json::isUTF8(Doc))
Doc = llvm::json::fixUTF8(Doc);
Expand Down
180 changes: 170 additions & 10 deletions clang-tools-extra/clangd/Hover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Protocol.h"
#include "Selection.h"
#include "SourceCode.h"
#include "SymbolDocumentation.h"
#include "clang-include-cleaner/Analysis.h"
#include "clang-include-cleaner/IncludeSpeller.h"
#include "clang-include-cleaner/Types.h"
Expand All @@ -41,6 +42,7 @@
#include "clang/AST/Type.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/Specifiers.h"
Expand Down Expand Up @@ -627,6 +629,9 @@ HoverInfo getHoverContents(const NamedDecl *D, const PrintingPolicy &PP,
HI.Name = printName(Ctx, *D);
const auto *CommentD = getDeclForComment(D);
HI.Documentation = getDeclComment(Ctx, *CommentD);
// save the language options to be able to create the comment::CommandTraits
// to parse the documentation
HI.CommentOpts = D->getASTContext().getLangOpts().CommentOpts;
enhanceFromIndex(HI, *CommentD, Index);
if (HI.Documentation.empty())
HI.Documentation = synthesizeDocumentation(D);
Expand Down Expand Up @@ -1388,9 +1393,166 @@ static std::string formatOffset(uint64_t OffsetInBits) {
return Offset;
}

markup::Document HoverInfo::present() const {
markup::Document HoverInfo::presentDoxygen() const {
// NOTE: this function is currently almost identical to presentDefault().
// This is to have a minimal change when introducing the doxygen parser.
// This function will be changed when rearranging the output for doxygen
// parsed documentation.

markup::Document Output;
// Header contains a text of the form:
// variable `var`
//
// class `X`
//
// function `foo`
//
// expression
//
// Note that we are making use of a level-3 heading because VSCode renders
// level 1 and 2 headers in a huge font, see
// https://github.com/microsoft/vscode/issues/88417 for details.
markup::Paragraph &Header = Output.addHeading(3);
if (Kind != index::SymbolKind::Unknown)
Header.appendText(index::getSymbolKindString(Kind)).appendSpace();
assert(!Name.empty() && "hover triggered on a nameless symbol");

Header.appendCode(Name);

if (!Provider.empty()) {
markup::Paragraph &DI = Output.addParagraph();
DI.appendText("provided by");
DI.appendSpace();
DI.appendCode(Provider);
Output.addRuler();
}

// Put a linebreak after header to increase readability.
Output.addRuler();
// Print Types on their own lines to reduce chances of getting line-wrapped by
// editor, as they might be long.
if (ReturnType) {
// For functions we display signature in a list form, e.g.:
// → `x`
// Parameters:
// - `bool param1`
// - `int param2 = 5`
Output.addParagraph().appendText("→ ").appendCode(
llvm::to_string(*ReturnType));
}

SymbolDocCommentVisitor SymbolDoc(Documentation, CommentOpts);

if (Parameters && !Parameters->empty()) {
Output.addParagraph().appendText("Parameters:");
markup::BulletList &L = Output.addBulletList();
for (const auto &Param : *Parameters) {
markup::Paragraph &P = L.addItem().addParagraph();
P.appendCode(llvm::to_string(Param));

if (SymbolDoc.isParameterDocumented(llvm::to_string(Param.Name))) {
P.appendText(" -");
SymbolDoc.parameterDocToMarkup(llvm::to_string(Param.Name), P);
}
}
}
// Don't print Type after Parameters or ReturnType as this will just duplicate
// the information
if (Type && !ReturnType && !Parameters)
Output.addParagraph().appendText("Type: ").appendCode(
llvm::to_string(*Type));

if (Value) {
markup::Paragraph &P = Output.addParagraph();
P.appendText("Value = ");
P.appendCode(*Value);
}

if (Offset)
Output.addParagraph().appendText("Offset: " + formatOffset(*Offset));
if (Size) {
auto &P = Output.addParagraph().appendText("Size: " + formatSize(*Size));
if (Padding && *Padding != 0) {
P.appendText(
llvm::formatv(" (+{0} padding)", formatSize(*Padding)).str());
}
if (Align)
P.appendText(", alignment " + formatSize(*Align));
}

if (CalleeArgInfo) {
assert(CallPassType);
std::string Buffer;
llvm::raw_string_ostream OS(Buffer);
OS << "Passed ";
if (CallPassType->PassBy != HoverInfo::PassType::Value) {
OS << "by ";
if (CallPassType->PassBy == HoverInfo::PassType::ConstRef)
OS << "const ";
OS << "reference ";
}
if (CalleeArgInfo->Name)
OS << "as " << CalleeArgInfo->Name;
else if (CallPassType->PassBy == HoverInfo::PassType::Value)
OS << "by value";
if (CallPassType->Converted && CalleeArgInfo->Type)
OS << " (converted to " << CalleeArgInfo->Type->Type << ")";
Output.addParagraph().appendText(OS.str());
}

SymbolDoc.docToMarkup(Output);

if (!Definition.empty()) {
Output.addRuler();
std::string Buffer;

if (!Definition.empty()) {
// Append scope comment, dropping trailing "::".
// Note that we don't print anything for global namespace, to not annoy
// non-c++ projects or projects that are not making use of namespaces.
if (!LocalScope.empty()) {
// Container name, e.g. class, method, function.
// We might want to propagate some info about container type to print
// function foo, class X, method X::bar, etc.
Buffer +=
"// In " + llvm::StringRef(LocalScope).rtrim(':').str() + '\n';
} else if (NamespaceScope && !NamespaceScope->empty()) {
Buffer += "// In namespace " +
llvm::StringRef(*NamespaceScope).rtrim(':').str() + '\n';
}

if (!AccessSpecifier.empty()) {
Buffer += AccessSpecifier + ": ";
}

Buffer += Definition;
}

Output.addCodeBlock(Buffer, DefinitionLanguage);
}

if (!UsedSymbolNames.empty()) {
Output.addRuler();
markup::Paragraph &P = Output.addParagraph();
P.appendText("provides ");

const std::vector<std::string>::size_type SymbolNamesLimit = 5;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least this constant deserves to be shared between two branches?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the common parts into own functions now

auto Front = llvm::ArrayRef(UsedSymbolNames).take_front(SymbolNamesLimit);

llvm::interleave(
Front, [&](llvm::StringRef Sym) { P.appendCode(Sym); },
[&] { P.appendText(", "); });
if (UsedSymbolNames.size() > Front.size()) {
P.appendText(" and ");
P.appendText(std::to_string(UsedSymbolNames.size() - Front.size()));
P.appendText(" more");
}
}
return Output;
}

markup::Document HoverInfo::presentDefault() const {
markup::Document Output;
// Header contains a text of the form:
// variable `var`
//
Expand Down Expand Up @@ -1538,21 +1700,19 @@ markup::Document HoverInfo::present() const {
std::string HoverInfo::present(MarkupKind Kind) const {
if (Kind == MarkupKind::Markdown) {
const Config &Cfg = Config::current();
if ((Cfg.Documentation.CommentFormat ==
Config::CommentFormatPolicy::Markdown) ||
(Cfg.Documentation.CommentFormat ==
Config::CommentFormatPolicy::Doxygen))
// If the user prefers Markdown, we use the present() method to generate
// the Markdown output.
return present().asMarkdown();
if (Cfg.Documentation.CommentFormat ==
Config::CommentFormatPolicy::Markdown)
return presentDefault().asMarkdown();
if (Cfg.Documentation.CommentFormat == Config::CommentFormatPolicy::Doxygen)
return presentDoxygen().asMarkdown();
if (Cfg.Documentation.CommentFormat ==
Config::CommentFormatPolicy::PlainText)
// If the user prefers plain text, we use the present() method to generate
// the plain text output.
return present().asEscapedMarkdown();
return presentDefault().asEscapedMarkdown();
}

return present().asPlainText();
return presentDefault().asPlainText();
}

// If the backtick at `Offset` starts a probable quoted range, return the range
Expand Down
13 changes: 10 additions & 3 deletions clang-tools-extra/clangd/Hover.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ struct HoverInfo {
std::optional<Range> SymRange;
index::SymbolKind Kind = index::SymbolKind::Unknown;
std::string Documentation;
// required to create a comments::CommandTraits object without the ASTContext
CommentOptions CommentOpts;
/// Source code containing the definition of the symbol.
std::string Definition;
const char *DefinitionLanguage = "cpp";
Expand Down Expand Up @@ -118,10 +120,15 @@ struct HoverInfo {
// alphabetical order.
std::vector<std::string> UsedSymbolNames;

/// Produce a user-readable information.
markup::Document present() const;

/// Produce a user-readable information based on the specified markup kind.
std::string present(MarkupKind Kind) const;

private:
/// Parse and render the hover information as Doxygen documentation.
markup::Document presentDoxygen() const;

/// Render the hover information as a default documentation.
markup::Document presentDefault() const;
};

inline bool operator==(const HoverInfo::PrintedType &LHS,
Expand Down
Loading