Coverage Report

Created: 2022-01-25 06:29

/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
86.9k
CXComment clang_Cursor_getParsedComment(CXCursor C) {
29
86.9k
  using namespace clang::cxcursor;
30
31
86.9k
  if (!clang_isDeclaration(C.kind))
32
80.0k
    return createCXComment(nullptr, nullptr);
33
34
6.94k
  const Decl *D = getCursorDecl(C);
35
6.94k
  const ASTContext &Context = getCursorContext(C);
36
6.94k
  const FullComment *FC = Context.getCommentForDecl(D, /*PP=*/nullptr);
37
38
6.94k
  return createCXComment(FC, getCursorTU(C));
39
86.9k
}
40
41
93.3k
enum CXCommentKind clang_Comment_getKind(CXComment CXC) {
42
93.3k
  const Comment *C = getASTNode(CXC);
43
93.3k
  if (!C)
44
85.9k
    return CXComment_Null;
45
46
7.34k
  switch (C->getCommentKind()) {
47
0
  case Comment::NoCommentKind:
48
0
    return CXComment_Null;
49
50
2.31k
  case Comment::TextCommentKind:
51
2.31k
    return CXComment_Text;
52
53
182
  case Comment::InlineCommandCommentKind:
54
182
    return CXComment_InlineCommand;
55
56
47
  case Comment::HTMLStartTagCommentKind:
57
47
    return CXComment_HTMLStartTag;
58
59
31
  case Comment::HTMLEndTagCommentKind:
60
31
    return CXComment_HTMLEndTag;
61
62
1.93k
  case Comment::ParagraphCommentKind:
63
1.93k
    return CXComment_Paragraph;
64
65
509
  case Comment::BlockCommandCommentKind:
66
509
    return CXComment_BlockCommand;
67
68
205
  case Comment::ParamCommandCommentKind:
69
205
    return CXComment_ParamCommand;
70
71
120
  case Comment::TParamCommandCommentKind:
72
120
    return CXComment_TParamCommand;
73
74
9
  case Comment::VerbatimBlockCommentKind:
75
9
    return CXComment_VerbatimBlockCommand;
76
77
17
  case Comment::VerbatimBlockLineCommentKind:
78
17
    return CXComment_VerbatimBlockLine;
79
80
45
  case Comment::VerbatimLineCommentKind:
81
45
    return CXComment_VerbatimLine;
82
83
1.93k
  case Comment::FullCommentKind:
84
1.93k
    return CXComment_FullComment;
85
7.34k
  }
86
0
  llvm_unreachable("unknown CommentKind");
87
0
}
88
89
6.37k
unsigned clang_Comment_getNumChildren(CXComment CXC) {
90
6.37k
  const Comment *C = getASTNode(CXC);
91
6.37k
  if (!C)
92
0
    return 0;
93
94
6.37k
  return C->child_count();
95
6.37k
}
96
97
5.40k
CXComment clang_Comment_getChild(CXComment CXC, unsigned ChildIdx) {
98
5.40k
  const Comment *C = getASTNode(CXC);
99
5.40k
  if (!C || ChildIdx >= C->child_count())
100
0
    return createCXComment(nullptr, nullptr);
101
102
5.40k
  return createCXComment(*(C->child_begin() + ChildIdx), CXC.TranslationUnit);
103
5.40k
}
104
105
4.24k
unsigned clang_Comment_isWhitespace(CXComment CXC) {
106
4.24k
  const Comment *C = getASTNode(CXC);
107
4.24k
  if (!C)
108
0
    return false;
109
110
4.24k
  if (const TextComment *TC = dyn_cast<TextComment>(C))
111
2.31k
    return TC->isWhitespace();
112
113
1.93k
  if (const ParagraphComment *PC = dyn_cast<ParagraphComment>(C))
114
1.93k
    return PC->isWhitespace();
115
116
0
  return false;
117
1.93k
}
118
119
2.57k
unsigned clang_InlineContentComment_hasTrailingNewline(CXComment CXC) {
120
2.57k
  const InlineContentComment *ICC = getASTNodeAs<InlineContentComment>(CXC);
121
2.57k
  if (!ICC)
122
0
    return false;
123
124
2.57k
  return ICC->hasTrailingNewline();
125
2.57k
}
126
127
2.31k
CXString clang_TextComment_getText(CXComment CXC) {
128
2.31k
  const TextComment *TC = getASTNodeAs<TextComment>(CXC);
129
2.31k
  if (!TC)
130
0
    return cxstring::createNull();
131
132
2.31k
  return cxstring::createRef(TC->getText());
133
2.31k
}
134
135
182
CXString clang_InlineCommandComment_getCommandName(CXComment CXC) {
136
182
  const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC);
137
182
  if (!ICC)
138
0
    return cxstring::createNull();
139
140
182
  const CommandTraits &Traits = getCommandTraits(CXC);
141
182
  return cxstring::createRef(ICC->getCommandName(Traits));
142
182
}
143
144
enum CXCommentInlineCommandRenderKind
145
182
clang_InlineCommandComment_getRenderKind(CXComment CXC) {
146
182
  const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC);
147
182
  if (!ICC)
148
0
    return CXCommentInlineCommandRenderKind_Normal;
149
150
182
  switch (ICC->getRenderKind()) {
151
145
  case InlineCommandComment::RenderNormal:
152
145
    return CXCommentInlineCommandRenderKind_Normal;
153
154
4
  case InlineCommandComment::RenderBold:
155
4
    return CXCommentInlineCommandRenderKind_Bold;
156
157
11
  case InlineCommandComment::RenderMonospaced:
158
11
    return CXCommentInlineCommandRenderKind_Monospaced;
159
160
18
  case InlineCommandComment::RenderEmphasized:
161
18
    return CXCommentInlineCommandRenderKind_Emphasized;
162
163
4
  case InlineCommandComment::RenderAnchor:
164
4
    return CXCommentInlineCommandRenderKind_Anchor;
165
182
  }
166
0
  llvm_unreachable("unknown InlineCommandComment::RenderKind");
167
0
}
168
169
182
unsigned clang_InlineCommandComment_getNumArgs(CXComment CXC) {
170
182
  const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC);
171
182
  if (!ICC)
172
0
    return 0;
173
174
182
  return ICC->getNumArgs();
175
182
}
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
78
CXString clang_HTMLTagComment_getTagName(CXComment CXC) {
187
78
  const HTMLTagComment *HTC = getASTNodeAs<HTMLTagComment>(CXC);
188
78
  if (!HTC)
189
0
    return cxstring::createNull();
190
191
78
  return cxstring::createRef(HTC->getTagName());
192
78
}
193
194
47
unsigned clang_HTMLStartTagComment_isSelfClosing(CXComment CXC) {
195
47
  const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC);
196
47
  if (!HST)
197
0
    return false;
198
199
47
  return HST->isSelfClosing();
200
47
}
201
202
47
unsigned clang_HTMLStartTag_getNumAttrs(CXComment CXC) {
203
47
  const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC);
204
47
  if (!HST)
205
0
    return 0;
206
207
47
  return HST->getNumAttrs();
208
47
}
209
210
21
CXString clang_HTMLStartTag_getAttrName(CXComment CXC, unsigned AttrIdx) {
211
21
  const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC);
212
21
  if (!HST || AttrIdx >= HST->getNumAttrs())
213
0
    return cxstring::createNull();
214
215
21
  return cxstring::createRef(HST->getAttr(AttrIdx).Name);
216
21
}
217
218
21
CXString clang_HTMLStartTag_getAttrValue(CXComment CXC, unsigned AttrIdx) {
219
21
  const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC);
220
21
  if (!HST || AttrIdx >= HST->getNumAttrs())
221
0
    return cxstring::createNull();
222
223
21
  return cxstring::createRef(HST->getAttr(AttrIdx).Value);
224
21
}
225
226
518
CXString clang_BlockCommandComment_getCommandName(CXComment CXC) {
227
518
  const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC);
228
518
  if (!BCC)
229
0
    return cxstring::createNull();
230
231
518
  const CommandTraits &Traits = getCommandTraits(CXC);
232
518
  return cxstring::createRef(BCC->getCommandName(Traits));
233
518
}
234
235
509
unsigned clang_BlockCommandComment_getNumArgs(CXComment CXC) {
236
509
  const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC);
237
509
  if (!BCC)
238
0
    return 0;
239
240
509
  return BCC->getNumArgs();
241
509
}
242
243
CXString clang_BlockCommandComment_getArgText(CXComment CXC,
244
0
                                              unsigned ArgIdx) {
245
0
  const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC);
246
0
  if (!BCC || ArgIdx >= BCC->getNumArgs())
247
0
    return cxstring::createNull();
248
249
0
  return cxstring::createRef(BCC->getArgText(ArgIdx));
250
0
}
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 ParamCommandComment::In:
300
200
    return CXCommentParamPassDirection_In;
301
302
3
  case ParamCommandComment::Out:
303
3
    return CXCommentParamPassDirection_Out;
304
305
2
  case ParamCommandComment::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
45
CXString clang_VerbatimLineComment_getText(CXComment CXC) {
353
45
  const VerbatimLineComment *VLC = getASTNodeAs<VerbatimLineComment>(CXC);
354
45
  if (!VLC)
355
0
    return cxstring::createNull();
356
357
45
  return cxstring::createRef(VLC->getText());
358
45
}
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
969
CXString clang_FullComment_getAsHTML(CXComment CXC) {
380
969
  const FullComment *FC = getASTNodeAs<FullComment>(CXC);
381
969
  if (!FC)
382
0
    return cxstring::createNull();
383
384
969
  CXTranslationUnit TU = CXC.TranslationUnit;
385
969
  if (!TU->CommentToXML)
386
42
    TU->CommentToXML = new clang::index::CommentToXMLConverter();
387
388
969
  SmallString<1024> HTML;
389
969
  TU->CommentToXML
390
969
      ->convertCommentToHTML(FC, HTML, cxtu::getASTUnit(TU)->getASTContext());
391
969
  return cxstring::createDup(HTML.str());
392
969
}
393
394
969
CXString clang_FullComment_getAsXML(CXComment CXC) {
395
969
  const FullComment *FC = getASTNodeAs<FullComment>(CXC);
396
969
  if (!FC)
397
0
    return cxstring::createNull();
398
399
969
  CXTranslationUnit TU = CXC.TranslationUnit;
400
969
  if (!TU->CommentToXML)
401
0
    TU->CommentToXML = new clang::index::CommentToXMLConverter();
402
403
969
  SmallString<1024> XML;
404
969
  TU->CommentToXML
405
969
      ->convertCommentToXML(FC, XML, cxtu::getASTUnit(TU)->getASTContext());
406
969
  return cxstring::createDup(XML.str());
407
969
}
408