/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/libclang/CXComment.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- CXComment.cpp - libclang APIs for manipulating CXComments ----------===// |
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 | | // This file defines all libclang APIs related to walking comment AST. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "CXComment.h" |
14 | | #include "CXCursor.h" |
15 | | #include "CXString.h" |
16 | | #include "clang-c/Documentation.h" |
17 | | #include "clang-c/Index.h" |
18 | | #include "clang/AST/Decl.h" |
19 | | #include "clang/Index/CommentToXML.h" |
20 | | #include "llvm/ADT/StringExtras.h" |
21 | | #include "llvm/Support/ErrorHandling.h" |
22 | | #include <climits> |
23 | | |
24 | | using namespace clang; |
25 | | using namespace clang::comments; |
26 | | using namespace clang::cxcomment; |
27 | | |
28 | 96.2k | CXComment clang_Cursor_getParsedComment(CXCursor C) { |
29 | 96.2k | using namespace clang::cxcursor; |
30 | | |
31 | 96.2k | if (!clang_isDeclaration(C.kind)) |
32 | 88.8k | return createCXComment(nullptr, nullptr); |
33 | | |
34 | 7.31k | const Decl *D = getCursorDecl(C); |
35 | 7.31k | const ASTContext &Context = getCursorContext(C); |
36 | 7.31k | const FullComment *FC = Context.getCommentForDecl(D, /*PP=*/nullptr); |
37 | | |
38 | 7.31k | return createCXComment(FC, getCursorTU(C)); |
39 | 96.2k | } |
40 | | |
41 | 102k | enum CXCommentKind clang_Comment_getKind(CXComment CXC) { |
42 | 102k | const Comment *C = getASTNode(CXC); |
43 | 102k | if (!C) |
44 | 95.1k | return CXComment_Null; |
45 | | |
46 | 7.53k | switch (C->getCommentKind()) { |
47 | 0 | case CommentKind::None: |
48 | 0 | return CXComment_Null; |
49 | | |
50 | 2.35k | case CommentKind::TextComment: |
51 | 2.35k | return CXComment_Text; |
52 | | |
53 | 185 | case CommentKind::InlineCommandComment: |
54 | 185 | return CXComment_InlineCommand; |
55 | | |
56 | 64 | case CommentKind::HTMLStartTagComment: |
57 | 64 | return CXComment_HTMLStartTag; |
58 | | |
59 | 31 | case CommentKind::HTMLEndTagComment: |
60 | 31 | return CXComment_HTMLEndTag; |
61 | | |
62 | 1.97k | case CommentKind::ParagraphComment: |
63 | 1.97k | return CXComment_Paragraph; |
64 | | |
65 | 512 | case CommentKind::BlockCommandComment: |
66 | 512 | return CXComment_BlockCommand; |
67 | | |
68 | 205 | case CommentKind::ParamCommandComment: |
69 | 205 | return CXComment_ParamCommand; |
70 | | |
71 | 120 | case CommentKind::TParamCommandComment: |
72 | 120 | return CXComment_TParamCommand; |
73 | | |
74 | 9 | case CommentKind::VerbatimBlockComment: |
75 | 9 | return CXComment_VerbatimBlockCommand; |
76 | | |
77 | 17 | case CommentKind::VerbatimBlockLineComment: |
78 | 17 | return CXComment_VerbatimBlockLine; |
79 | | |
80 | 42 | case CommentKind::VerbatimLineComment: |
81 | 42 | return CXComment_VerbatimLine; |
82 | | |
83 | 2.01k | case CommentKind::FullComment: |
84 | 2.01k | return CXComment_FullComment; |
85 | 7.53k | } |
86 | 0 | llvm_unreachable("unknown CommentKind"); |
87 | 0 | } |
88 | | |
89 | 6.52k | unsigned clang_Comment_getNumChildren(CXComment CXC) { |
90 | 6.52k | const Comment *C = getASTNode(CXC); |
91 | 6.52k | if (!C) |
92 | 0 | return 0; |
93 | | |
94 | 6.52k | return C->child_count(); |
95 | 6.52k | } |
96 | | |
97 | 5.51k | CXComment clang_Comment_getChild(CXComment CXC, unsigned ChildIdx) { |
98 | 5.51k | const Comment *C = getASTNode(CXC); |
99 | 5.51k | if (!C || ChildIdx >= C->child_count()) |
100 | 0 | return createCXComment(nullptr, nullptr); |
101 | | |
102 | 5.51k | return createCXComment(*(C->child_begin() + ChildIdx), CXC.TranslationUnit); |
103 | 5.51k | } |
104 | | |
105 | 4.32k | unsigned clang_Comment_isWhitespace(CXComment CXC) { |
106 | 4.32k | const Comment *C = getASTNode(CXC); |
107 | 4.32k | if (!C) |
108 | 0 | return false; |
109 | | |
110 | 4.32k | if (const TextComment *TC = dyn_cast<TextComment>(C)) |
111 | 2.35k | return TC->isWhitespace(); |
112 | | |
113 | 1.97k | if (const ParagraphComment *PC = dyn_cast<ParagraphComment>(C)) |
114 | 1.97k | return PC->isWhitespace(); |
115 | | |
116 | 0 | return false; |
117 | 1.97k | } |
118 | | |
119 | 2.63k | unsigned clang_InlineContentComment_hasTrailingNewline(CXComment CXC) { |
120 | 2.63k | const InlineContentComment *ICC = getASTNodeAs<InlineContentComment>(CXC); |
121 | 2.63k | if (!ICC) |
122 | 0 | return false; |
123 | | |
124 | 2.63k | return ICC->hasTrailingNewline(); |
125 | 2.63k | } |
126 | | |
127 | 2.35k | CXString clang_TextComment_getText(CXComment CXC) { |
128 | 2.35k | const TextComment *TC = getASTNodeAs<TextComment>(CXC); |
129 | 2.35k | if (!TC) |
130 | 0 | return cxstring::createNull(); |
131 | | |
132 | 2.35k | return cxstring::createRef(TC->getText()); |
133 | 2.35k | } |
134 | | |
135 | 185 | CXString clang_InlineCommandComment_getCommandName(CXComment CXC) { |
136 | 185 | const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC); |
137 | 185 | if (!ICC) |
138 | 0 | return cxstring::createNull(); |
139 | | |
140 | 185 | const CommandTraits &Traits = getCommandTraits(CXC); |
141 | 185 | return cxstring::createRef(ICC->getCommandName(Traits)); |
142 | 185 | } |
143 | | |
144 | | enum CXCommentInlineCommandRenderKind |
145 | 185 | clang_InlineCommandComment_getRenderKind(CXComment CXC) { |
146 | 185 | const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC); |
147 | 185 | if (!ICC) |
148 | 0 | return CXCommentInlineCommandRenderKind_Normal; |
149 | | |
150 | 185 | switch (ICC->getRenderKind()) { |
151 | 148 | case InlineCommandRenderKind::Normal: |
152 | 148 | return CXCommentInlineCommandRenderKind_Normal; |
153 | | |
154 | 4 | case InlineCommandRenderKind::Bold: |
155 | 4 | return CXCommentInlineCommandRenderKind_Bold; |
156 | | |
157 | 11 | case InlineCommandRenderKind::Monospaced: |
158 | 11 | return CXCommentInlineCommandRenderKind_Monospaced; |
159 | | |
160 | 18 | case InlineCommandRenderKind::Emphasized: |
161 | 18 | return CXCommentInlineCommandRenderKind_Emphasized; |
162 | | |
163 | 4 | case InlineCommandRenderKind::Anchor: |
164 | 4 | return CXCommentInlineCommandRenderKind_Anchor; |
165 | 185 | } |
166 | 0 | llvm_unreachable("unknown InlineCommandComment::RenderKind"); |
167 | 0 | } |
168 | | |
169 | 185 | unsigned clang_InlineCommandComment_getNumArgs(CXComment CXC) { |
170 | 185 | const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC); |
171 | 185 | if (!ICC) |
172 | 0 | return 0; |
173 | | |
174 | 185 | return ICC->getNumArgs(); |
175 | 185 | } |
176 | | |
177 | | CXString clang_InlineCommandComment_getArgText(CXComment CXC, |
178 | 31 | unsigned ArgIdx) { |
179 | 31 | const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC); |
180 | 31 | if (!ICC || ArgIdx >= ICC->getNumArgs()) |
181 | 0 | return cxstring::createNull(); |
182 | | |
183 | 31 | return cxstring::createRef(ICC->getArgText(ArgIdx)); |
184 | 31 | } |
185 | | |
186 | 95 | CXString clang_HTMLTagComment_getTagName(CXComment CXC) { |
187 | 95 | const HTMLTagComment *HTC = getASTNodeAs<HTMLTagComment>(CXC); |
188 | 95 | if (!HTC) |
189 | 0 | return cxstring::createNull(); |
190 | | |
191 | 95 | return cxstring::createRef(HTC->getTagName()); |
192 | 95 | } |
193 | | |
194 | 64 | unsigned clang_HTMLStartTagComment_isSelfClosing(CXComment CXC) { |
195 | 64 | const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC); |
196 | 64 | if (!HST) |
197 | 0 | return false; |
198 | | |
199 | 64 | return HST->isSelfClosing(); |
200 | 64 | } |
201 | | |
202 | 64 | unsigned clang_HTMLStartTag_getNumAttrs(CXComment CXC) { |
203 | 64 | const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC); |
204 | 64 | if (!HST) |
205 | 0 | return 0; |
206 | | |
207 | 64 | return HST->getNumAttrs(); |
208 | 64 | } |
209 | | |
210 | 32 | CXString clang_HTMLStartTag_getAttrName(CXComment CXC, unsigned AttrIdx) { |
211 | 32 | const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC); |
212 | 32 | if (!HST || AttrIdx >= HST->getNumAttrs()) |
213 | 0 | return cxstring::createNull(); |
214 | | |
215 | 32 | return cxstring::createRef(HST->getAttr(AttrIdx).Name); |
216 | 32 | } |
217 | | |
218 | 32 | CXString clang_HTMLStartTag_getAttrValue(CXComment CXC, unsigned AttrIdx) { |
219 | 32 | const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC); |
220 | 32 | if (!HST || AttrIdx >= HST->getNumAttrs()) |
221 | 0 | return cxstring::createNull(); |
222 | | |
223 | 32 | return cxstring::createRef(HST->getAttr(AttrIdx).Value); |
224 | 32 | } |
225 | | |
226 | 521 | CXString clang_BlockCommandComment_getCommandName(CXComment CXC) { |
227 | 521 | const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC); |
228 | 521 | if (!BCC) |
229 | 0 | return cxstring::createNull(); |
230 | | |
231 | 521 | const CommandTraits &Traits = getCommandTraits(CXC); |
232 | 521 | return cxstring::createRef(BCC->getCommandName(Traits)); |
233 | 521 | } |
234 | | |
235 | 512 | unsigned clang_BlockCommandComment_getNumArgs(CXComment CXC) { |
236 | 512 | const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC); |
237 | 512 | if (!BCC) |
238 | 0 | return 0; |
239 | | |
240 | 512 | return BCC->getNumArgs(); |
241 | 512 | } |
242 | | |
243 | | CXString clang_BlockCommandComment_getArgText(CXComment CXC, |
244 | 4 | unsigned ArgIdx) { |
245 | 4 | const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC); |
246 | 4 | if (!BCC || ArgIdx >= BCC->getNumArgs()) |
247 | 0 | return cxstring::createNull(); |
248 | | |
249 | 4 | return cxstring::createRef(BCC->getArgText(ArgIdx)); |
250 | 4 | } |
251 | | |
252 | 0 | CXComment clang_BlockCommandComment_getParagraph(CXComment CXC) { |
253 | 0 | const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC); |
254 | 0 | if (!BCC) |
255 | 0 | return createCXComment(nullptr, nullptr); |
256 | | |
257 | 0 | return createCXComment(BCC->getParagraph(), CXC.TranslationUnit); |
258 | 0 | } |
259 | | |
260 | 205 | CXString clang_ParamCommandComment_getParamName(CXComment CXC) { |
261 | 205 | const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC); |
262 | 205 | if (!PCC || !PCC->hasParamName()) |
263 | 4 | return cxstring::createNull(); |
264 | | |
265 | 201 | return cxstring::createRef(PCC->getParamNameAsWritten()); |
266 | 205 | } |
267 | | |
268 | 205 | unsigned clang_ParamCommandComment_isParamIndexValid(CXComment CXC) { |
269 | 205 | const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC); |
270 | 205 | if (!PCC) |
271 | 0 | return false; |
272 | | |
273 | 205 | return PCC->isParamIndexValid(); |
274 | 205 | } |
275 | | |
276 | 136 | unsigned clang_ParamCommandComment_getParamIndex(CXComment CXC) { |
277 | 136 | const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC); |
278 | 136 | if (!PCC || !PCC->isParamIndexValid() || PCC->isVarArgParam()) |
279 | 9 | return ParamCommandComment::InvalidParamIndex; |
280 | | |
281 | 127 | return PCC->getParamIndex(); |
282 | 136 | } |
283 | | |
284 | 205 | unsigned clang_ParamCommandComment_isDirectionExplicit(CXComment CXC) { |
285 | 205 | const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC); |
286 | 205 | if (!PCC) |
287 | 0 | return false; |
288 | | |
289 | 205 | return PCC->isDirectionExplicit(); |
290 | 205 | } |
291 | | |
292 | | enum CXCommentParamPassDirection clang_ParamCommandComment_getDirection( |
293 | 205 | CXComment CXC) { |
294 | 205 | const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC); |
295 | 205 | if (!PCC) |
296 | 0 | return CXCommentParamPassDirection_In; |
297 | | |
298 | 205 | switch (PCC->getDirection()) { |
299 | 200 | case ParamCommandPassDirection::In: |
300 | 200 | return CXCommentParamPassDirection_In; |
301 | | |
302 | 3 | case ParamCommandPassDirection::Out: |
303 | 3 | return CXCommentParamPassDirection_Out; |
304 | | |
305 | 2 | case ParamCommandPassDirection::InOut: |
306 | 2 | return CXCommentParamPassDirection_InOut; |
307 | 205 | } |
308 | 0 | llvm_unreachable("unknown ParamCommandComment::PassDirection"); |
309 | 0 | } |
310 | | |
311 | 120 | CXString clang_TParamCommandComment_getParamName(CXComment CXC) { |
312 | 120 | const TParamCommandComment *TPCC = getASTNodeAs<TParamCommandComment>(CXC); |
313 | 120 | if (!TPCC || !TPCC->hasParamName()) |
314 | 6 | return cxstring::createNull(); |
315 | | |
316 | 114 | return cxstring::createRef(TPCC->getParamNameAsWritten()); |
317 | 120 | } |
318 | | |
319 | 120 | unsigned clang_TParamCommandComment_isParamPositionValid(CXComment CXC) { |
320 | 120 | const TParamCommandComment *TPCC = getASTNodeAs<TParamCommandComment>(CXC); |
321 | 120 | if (!TPCC) |
322 | 0 | return false; |
323 | | |
324 | 120 | return TPCC->isPositionValid(); |
325 | 120 | } |
326 | | |
327 | 83 | unsigned clang_TParamCommandComment_getDepth(CXComment CXC) { |
328 | 83 | const TParamCommandComment *TPCC = getASTNodeAs<TParamCommandComment>(CXC); |
329 | 83 | if (!TPCC || !TPCC->isPositionValid()) |
330 | 0 | return 0; |
331 | | |
332 | 83 | return TPCC->getDepth(); |
333 | 83 | } |
334 | | |
335 | 115 | unsigned clang_TParamCommandComment_getIndex(CXComment CXC, unsigned Depth) { |
336 | 115 | const TParamCommandComment *TPCC = getASTNodeAs<TParamCommandComment>(CXC); |
337 | 115 | if (!TPCC || !TPCC->isPositionValid() || Depth >= TPCC->getDepth()) |
338 | 0 | return 0; |
339 | | |
340 | 115 | return TPCC->getIndex(Depth); |
341 | 115 | } |
342 | | |
343 | 17 | CXString clang_VerbatimBlockLineComment_getText(CXComment CXC) { |
344 | 17 | const VerbatimBlockLineComment *VBL = |
345 | 17 | getASTNodeAs<VerbatimBlockLineComment>(CXC); |
346 | 17 | if (!VBL) |
347 | 0 | return cxstring::createNull(); |
348 | | |
349 | 17 | return cxstring::createRef(VBL->getText()); |
350 | 17 | } |
351 | | |
352 | 42 | CXString clang_VerbatimLineComment_getText(CXComment CXC) { |
353 | 42 | const VerbatimLineComment *VLC = getASTNodeAs<VerbatimLineComment>(CXC); |
354 | 42 | if (!VLC) |
355 | 0 | return cxstring::createNull(); |
356 | | |
357 | 42 | return cxstring::createRef(VLC->getText()); |
358 | 42 | } |
359 | | |
360 | | //===----------------------------------------------------------------------===// |
361 | | // Converting comments to XML. |
362 | | //===----------------------------------------------------------------------===// |
363 | | |
364 | 0 | CXString clang_HTMLTagComment_getAsString(CXComment CXC) { |
365 | 0 | const HTMLTagComment *HTC = getASTNodeAs<HTMLTagComment>(CXC); |
366 | 0 | if (!HTC) |
367 | 0 | return cxstring::createNull(); |
368 | | |
369 | 0 | CXTranslationUnit TU = CXC.TranslationUnit; |
370 | 0 | if (!TU->CommentToXML) |
371 | 0 | TU->CommentToXML = new clang::index::CommentToXMLConverter(); |
372 | |
|
373 | 0 | SmallString<128> Text; |
374 | 0 | TU->CommentToXML->convertHTMLTagNodeToText( |
375 | 0 | HTC, Text, cxtu::getASTUnit(TU)->getASTContext()); |
376 | 0 | return cxstring::createDup(Text.str()); |
377 | 0 | } |
378 | | |
379 | 1.00k | CXString clang_FullComment_getAsHTML(CXComment CXC) { |
380 | 1.00k | const FullComment *FC = getASTNodeAs<FullComment>(CXC); |
381 | 1.00k | if (!FC) |
382 | 0 | return cxstring::createNull(); |
383 | | |
384 | 1.00k | CXTranslationUnit TU = CXC.TranslationUnit; |
385 | 1.00k | if (!TU->CommentToXML) |
386 | 42 | TU->CommentToXML = new clang::index::CommentToXMLConverter(); |
387 | | |
388 | 1.00k | SmallString<1024> HTML; |
389 | 1.00k | TU->CommentToXML |
390 | 1.00k | ->convertCommentToHTML(FC, HTML, cxtu::getASTUnit(TU)->getASTContext()); |
391 | 1.00k | return cxstring::createDup(HTML.str()); |
392 | 1.00k | } |
393 | | |
394 | 1.00k | CXString clang_FullComment_getAsXML(CXComment CXC) { |
395 | 1.00k | const FullComment *FC = getASTNodeAs<FullComment>(CXC); |
396 | 1.00k | if (!FC) |
397 | 0 | return cxstring::createNull(); |
398 | | |
399 | 1.00k | CXTranslationUnit TU = CXC.TranslationUnit; |
400 | 1.00k | if (!TU->CommentToXML) |
401 | 0 | TU->CommentToXML = new clang::index::CommentToXMLConverter(); |
402 | | |
403 | 1.00k | SmallString<1024> XML; |
404 | 1.00k | TU->CommentToXML |
405 | 1.00k | ->convertCommentToXML(FC, XML, cxtu::getASTUnit(TU)->getASTContext()); |
406 | 1.00k | return cxstring::createDup(XML.str()); |
407 | 1.00k | } |
408 | | |