clang-tools 23.0.0git
SymbolDocumentation.h
Go to the documentation of this file.
1//===--- SymbolDocumentation.h ==---------------------------------*- C++-*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Class to parse doxygen comments into a flat structure for consumption
10// in e.g. Hover and Code Completion
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SYMBOLDOCUMENTATION_H
15#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SYMBOLDOCUMENTATION_H
16
17#include "support/Markup.h"
18#include "clang/AST/Comment.h"
19#include "clang/AST/CommentLexer.h"
20#include "clang/AST/CommentParser.h"
21#include "clang/AST/CommentSema.h"
22#include "clang/AST/CommentVisitor.h"
23#include "clang/Basic/SourceManager.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/Support/raw_ostream.h"
26#include <string>
27
28namespace clang {
29namespace clangd {
30
32 : public comments::ConstCommentVisitor<SymbolDocCommentVisitor> {
33public:
34 SymbolDocCommentVisitor(llvm::StringRef Documentation,
35 const CommentOptions &CommentOpts)
36 : Traits(Allocator, CommentOpts), Allocator() {
37
38 if (Documentation.empty())
39 return;
40
41 CommentWithMarkers.reserve(Documentation.size() +
42 Documentation.count('\n') * 3);
43
44 preprocessDocumentation(Documentation);
45
46 SourceManagerForFile SourceMgrForFile("mock_file.cpp", CommentWithMarkers);
47
48 SourceManager &SourceMgr = SourceMgrForFile.get();
49 // The doxygen Sema requires a Diagostics consumer, since it reports
50 // warnings e.g. when parameters are not documented correctly. These
51 // warnings are not relevant for us, so we can ignore them.
52 SourceMgr.getDiagnostics().setClient(new IgnoringDiagConsumer);
53
54 comments::Sema S(Allocator, SourceMgr, SourceMgr.getDiagnostics(), Traits,
55 /*PP=*/nullptr);
56 comments::Lexer L(Allocator, SourceMgr.getDiagnostics(), Traits,
57 SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()),
58 CommentWithMarkers.data(),
59 CommentWithMarkers.data() + CommentWithMarkers.size());
60 comments::Parser P(L, S, Allocator, SourceMgr, SourceMgr.getDiagnostics(),
61 Traits);
62 comments::FullComment *FC = P.parseFullComment();
63
64 if (!FC)
65 return;
66
67 for (auto *Block : FC->getBlocks()) {
68 visit(Block);
69 }
70
71 // If we have not seen a brief command, use the very first free paragraph as
72 // the brief.
73 if (!BriefParagraph && !FreeParagraphs.empty() &&
74 FreeParagraphs.contains(0)) {
75 BriefParagraph = FreeParagraphs.lookup(0);
76 FreeParagraphs.erase(0);
77 }
78 }
79
80 bool isParameterDocumented(StringRef ParamName) const {
81 return Parameters.contains(ParamName);
82 }
83
84 bool isTemplateTypeParmDocumented(StringRef ParamName) const {
85 return TemplateParameters.contains(ParamName);
86 }
87
88 bool hasBriefCommand() const { return BriefParagraph; }
89
90 bool hasReturnCommand() const { return ReturnParagraph; }
91
92 bool hasDetailedDoc() const {
93 return !FreeParagraphs.empty() || !BlockCommands.empty();
94 }
95
96 /// Converts all unhandled comment commands to a markup document.
97 void detailedDocToMarkup(markup::Document &Out) const;
98 /// Converts the "brief" command(s) to a markup document.
99 void briefToMarkup(markup::Paragraph &Out) const;
100 /// Converts the "return" command(s) to a markup document.
101 void returnToMarkup(markup::Paragraph &Out) const;
102 /// Converts the "retval" command(s) to a markup document.
103 void retvalsToMarkup(markup::Document &Out) const;
104
105 void visitBlockCommandComment(const comments::BlockCommandComment *B);
106
107 void templateTypeParmDocToMarkup(StringRef TemplateParamName,
108 markup::Paragraph &Out) const;
109
110 void templateTypeParmDocToString(StringRef TemplateParamName,
111 llvm::raw_string_ostream &Out) const;
112
113 void parameterDocToMarkup(StringRef ParamName, markup::Paragraph &Out) const;
114
115 void parameterDocToString(StringRef ParamName,
116 llvm::raw_string_ostream &Out) const;
117
118 void visitParagraphComment(const comments::ParagraphComment *P) {
119 if (!P->isWhitespace()) {
120 FreeParagraphs[CommentPartIndex] = P;
121 CommentPartIndex++;
122 }
123 }
124
125 void visitParamCommandComment(const comments::ParamCommandComment *P) {
126 Parameters[P->getParamNameAsWritten()] = P;
127 }
128
129 void visitTParamCommandComment(const comments::TParamCommandComment *TP) {
130 TemplateParameters[TP->getParamNameAsWritten()] = std::move(TP);
131 }
132
133 /// \brief Preprocesses the raw documentation string to prepare it for doxygen
134 /// parsing.
135 ///
136 /// This is a workaround to provide better support for markdown in
137 /// doxygen. Clang's doxygen parser e.g. does not handle markdown code blocks.
138 ///
139 /// The documentation string is preprocessed to replace some markdown
140 /// constructs with parsable doxygen commands. E.g. markdown code blocks are
141 /// replaced with doxygen \\code{.lang} ...
142 /// \\endcode blocks.
143 ///
144 /// Additionally, potential doxygen commands inside markdown
145 /// inline code spans are escaped to avoid that doxygen tries to interpret
146 /// them as commands.
147 ///
148 /// \note Although this is a workaround, it is very similar to what
149 /// doxygen itself does for markdown. In doxygen, the first parsing step is
150 /// also a markdown preprocessing step.
151 /// See https://www.doxygen.nl/manual/markdown.html
152 void preprocessDocumentation(StringRef Doc);
153
154private:
155 comments::CommandTraits Traits;
156 llvm::BumpPtrAllocator Allocator;
157 std::string CommentWithMarkers;
158
159 /// Index to keep track of the order of the comments.
160 /// We want to rearange some commands like \\param.
161 /// This index allows us to keep the order of the other comment parts.
162 unsigned CommentPartIndex = 0;
163
164 /// Paragraph of the "brief" command.
165 const comments::ParagraphComment *BriefParagraph = nullptr;
166
167 /// Paragraph of the "return" command.
168 const comments::ParagraphComment *ReturnParagraph = nullptr;
169
170 /// All the "retval" command(s)
171 llvm::SmallVector<const comments::BlockCommandComment *> RetvalCommands;
172
173 /// All the parsed doxygen block commands.
174 /// They might have special handling internally like \\note or \\warning
175 llvm::SmallDenseMap<unsigned, const comments::BlockCommandComment *>
176 BlockCommands;
177
178 /// Parsed paragaph(s) of the "param" comamnd(s)
179 llvm::SmallDenseMap<StringRef, const comments::ParamCommandComment *>
180 Parameters;
181
182 /// Parsed paragaph(s) of the "tparam" comamnd(s)
183 llvm::SmallDenseMap<StringRef, const comments::TParamCommandComment *>
184 TemplateParameters;
185
186 /// All "free" text paragraphs.
187 llvm::SmallDenseMap<unsigned, const comments::ParagraphComment *>
188 FreeParagraphs;
189};
190
191} // namespace clangd
192} // namespace clang
193
194#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_SYMBOLDOCUMENTATION_H
void visitParagraphComment(const comments::ParagraphComment *P)
void visitParamCommandComment(const comments::ParamCommandComment *P)
void visitTParamCommandComment(const comments::TParamCommandComment *TP)
void detailedDocToMarkup(markup::Document &Out) const
Converts all unhandled comment commands to a markup document.
bool isTemplateTypeParmDocumented(StringRef ParamName) const
SymbolDocCommentVisitor(llvm::StringRef Documentation, const CommentOptions &CommentOpts)
void templateTypeParmDocToMarkup(StringRef TemplateParamName, markup::Paragraph &Out) const
void returnToMarkup(markup::Paragraph &Out) const
Converts the "return" command(s) to a markup document.
void parameterDocToMarkup(StringRef ParamName, markup::Paragraph &Out) const
void templateTypeParmDocToString(StringRef TemplateParamName, llvm::raw_string_ostream &Out) const
void preprocessDocumentation(StringRef Doc)
Preprocesses the raw documentation string to prepare it for doxygen parsing.
void parameterDocToString(StringRef ParamName, llvm::raw_string_ostream &Out) const
void visitBlockCommandComment(const comments::BlockCommandComment *B)
void briefToMarkup(markup::Paragraph &Out) const
Converts the "brief" command(s) to a markup document.
bool isParameterDocumented(StringRef ParamName) const
void retvalsToMarkup(markup::Document &Out) const
Converts the "retval" command(s) to a markup document.
Represents parts of the markup that can contain strings, like inline code, code block or plain text.
Definition Markup.h:45
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:44
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//