Skip to content

Commit 2c4b876

Browse files
[clangd] introduce doxygen parser (#150790)
Followup work of #140498 to continue the work on clangd/clangd#529 Introduce the use of the Clang doxygen parser to parse the documentation of hovered code. - ASTContext independent doxygen parsing - Parsing doxygen commands to markdown for hover information Note: after this PR I have planned another patch to rearrange the information shown in the hover info. This PR is just for the basic introduction of doxygen parsing for hover information. --------- Co-authored-by: Maksim Ivanov <[email protected]>
1 parent 1b1f352 commit 2c4b876

File tree

16 files changed

+1192
-100
lines changed

16 files changed

+1192
-100
lines changed

clang-tools-extra/clangd/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ add_clang_library(clangDaemon STATIC
108108
SemanticHighlighting.cpp
109109
SemanticSelection.cpp
110110
SourceCode.cpp
111+
SymbolDocumentation.cpp
111112
SystemIncludeExtractor.cpp
112113
TidyProvider.cpp
113114
TUScheduler.cpp

clang-tools-extra/clangd/CodeCompletionStrings.cpp

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "CodeCompletionStrings.h"
10+
#include "Config.h"
11+
#include "SymbolDocumentation.h"
1012
#include "clang-c/Index.h"
1113
#include "clang/AST/ASTContext.h"
14+
#include "clang/AST/Comment.h"
15+
#include "clang/AST/Decl.h"
1216
#include "clang/AST/RawCommentList.h"
1317
#include "clang/Basic/SourceManager.h"
1418
#include "clang/Sema/CodeCompleteConsumer.h"
1519
#include "llvm/Support/Compiler.h"
1620
#include "llvm/Support/JSON.h"
21+
#include "llvm/Support/raw_ostream.h"
1722
#include <limits>
1823
#include <utility>
1924

@@ -100,16 +105,51 @@ std::string getDeclComment(const ASTContext &Ctx, const NamedDecl &Decl) {
100105
// the comments for namespaces.
101106
return "";
102107
}
103-
const RawComment *RC = getCompletionComment(Ctx, &Decl);
104-
if (!RC)
105-
return "";
106-
// Sanity check that the comment does not come from the PCH. We choose to not
107-
// write them into PCH, because they are racy and slow to load.
108-
assert(!Ctx.getSourceManager().isLoadedSourceLocation(RC->getBeginLoc()));
109-
std::string Doc =
110-
RC->getFormattedText(Ctx.getSourceManager(), Ctx.getDiagnostics());
111-
if (!looksLikeDocComment(Doc))
112-
return "";
108+
109+
const RawComment *RC = nullptr;
110+
const Config &Cfg = Config::current();
111+
112+
std::string Doc;
113+
114+
if (Cfg.Documentation.CommentFormat == Config::CommentFormatPolicy::Doxygen &&
115+
isa<ParmVarDecl>(Decl)) {
116+
// Parameters are documented in their declaration context (function or
117+
// template function).
118+
const NamedDecl *ND = dyn_cast<NamedDecl>(Decl.getDeclContext());
119+
if (!ND)
120+
return "";
121+
122+
RC = getCompletionComment(Ctx, ND);
123+
if (!RC)
124+
return "";
125+
126+
// Sanity check that the comment does not come from the PCH. We choose to
127+
// not write them into PCH, because they are racy and slow to load.
128+
assert(!Ctx.getSourceManager().isLoadedSourceLocation(RC->getBeginLoc()));
129+
130+
comments::FullComment *FC = RC->parse(Ctx, /*PP=*/nullptr, ND);
131+
if (!FC)
132+
return "";
133+
134+
SymbolDocCommentVisitor V(FC, Ctx.getLangOpts().CommentOpts);
135+
std::string RawDoc;
136+
llvm::raw_string_ostream OS(RawDoc);
137+
138+
V.parameterDocToString(dyn_cast<ParmVarDecl>(&Decl)->getName(), OS);
139+
140+
Doc = StringRef(RawDoc).trim().str();
141+
} else {
142+
RC = getCompletionComment(Ctx, &Decl);
143+
if (!RC)
144+
return "";
145+
// Sanity check that the comment does not come from the PCH. We choose to
146+
// not write them into PCH, because they are racy and slow to load.
147+
assert(!Ctx.getSourceManager().isLoadedSourceLocation(RC->getBeginLoc()));
148+
Doc = RC->getFormattedText(Ctx.getSourceManager(), Ctx.getDiagnostics());
149+
if (!looksLikeDocComment(Doc))
150+
return "";
151+
}
152+
113153
// Clang requires source to be UTF-8, but doesn't enforce this in comments.
114154
if (!llvm::json::isUTF8(Doc))
115155
Doc = llvm::json::fixUTF8(Doc);

0 commit comments

Comments
 (0)