blob: 1c127e1589de02381a18d46b5318d0691c3a6d0d [file] [log] [blame]
Dmitri Gribenkoae99b752012-07-20 21:34:34 +00001//===- CXComment.cpp - libclang APIs for manipulating CXComments ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines all libclang APIs related to walking comment AST.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang-c/Index.h"
Dmitri Gribenkoae99b752012-07-20 21:34:34 +000015#include "CXComment.h"
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +000016#include "CXCursor.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000017#include "CXString.h"
Fariborz Jahanian88b95212012-12-18 23:02:59 +000018#include "SimpleFormatContext.h"
Dmitri Gribenkod1db1252012-08-09 17:33:20 +000019#include "clang/AST/CommentCommandTraits.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000020#include "clang/AST/CommentVisitor.h"
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +000021#include "clang/AST/Decl.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000022#include "clang/AST/PrettyPrinter.h"
Chandler Carruthb99083e2013-01-02 10:28:36 +000023#include "clang/Format/Format.h"
24#include "clang/Lex/Lexer.h"
Fariborz Jahanian88b95212012-12-18 23:02:59 +000025#include "llvm/ADT/StringExtras.h"
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +000026#include "llvm/ADT/StringSwitch.h"
Dmitri Gribenkoae99b752012-07-20 21:34:34 +000027#include "llvm/Support/ErrorHandling.h"
Dmitri Gribenko3e63d332012-07-21 01:47:43 +000028#include "llvm/Support/raw_ostream.h"
Dmitri Gribenko221a6d72012-07-30 17:49:32 +000029#include <climits>
30
Dmitri Gribenkoae99b752012-07-20 21:34:34 +000031using namespace clang;
Dmitri Gribenkoae99b752012-07-20 21:34:34 +000032using namespace clang::comments;
33using namespace clang::cxcomment;
34
35extern "C" {
36
37enum CXCommentKind clang_Comment_getKind(CXComment CXC) {
38 const Comment *C = getASTNode(CXC);
39 if (!C)
40 return CXComment_Null;
41
42 switch (C->getCommentKind()) {
43 case Comment::NoCommentKind:
44 return CXComment_Null;
45
46 case Comment::TextCommentKind:
47 return CXComment_Text;
48
49 case Comment::InlineCommandCommentKind:
50 return CXComment_InlineCommand;
51
52 case Comment::HTMLStartTagCommentKind:
53 return CXComment_HTMLStartTag;
54
55 case Comment::HTMLEndTagCommentKind:
56 return CXComment_HTMLEndTag;
57
58 case Comment::ParagraphCommentKind:
59 return CXComment_Paragraph;
60
61 case Comment::BlockCommandCommentKind:
62 return CXComment_BlockCommand;
63
64 case Comment::ParamCommandCommentKind:
65 return CXComment_ParamCommand;
66
Dmitri Gribenko96b09862012-07-31 22:37:06 +000067 case Comment::TParamCommandCommentKind:
68 return CXComment_TParamCommand;
69
Dmitri Gribenkoae99b752012-07-20 21:34:34 +000070 case Comment::VerbatimBlockCommentKind:
71 return CXComment_VerbatimBlockCommand;
72
73 case Comment::VerbatimBlockLineCommentKind:
74 return CXComment_VerbatimBlockLine;
75
76 case Comment::VerbatimLineCommentKind:
77 return CXComment_VerbatimLine;
78
79 case Comment::FullCommentKind:
80 return CXComment_FullComment;
81 }
82 llvm_unreachable("unknown CommentKind");
83}
84
85unsigned clang_Comment_getNumChildren(CXComment CXC) {
86 const Comment *C = getASTNode(CXC);
87 if (!C)
88 return 0;
89
90 return C->child_count();
91}
92
93CXComment clang_Comment_getChild(CXComment CXC, unsigned ChildIdx) {
94 const Comment *C = getASTNode(CXC);
95 if (!C || ChildIdx >= C->child_count())
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000096 return createCXComment(NULL, NULL);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +000097
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000098 return createCXComment(*(C->child_begin() + ChildIdx), CXC.TranslationUnit);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +000099}
100
101unsigned clang_Comment_isWhitespace(CXComment CXC) {
102 const Comment *C = getASTNode(CXC);
103 if (!C)
104 return false;
105
106 if (const TextComment *TC = dyn_cast<TextComment>(C))
107 return TC->isWhitespace();
108
109 if (const ParagraphComment *PC = dyn_cast<ParagraphComment>(C))
110 return PC->isWhitespace();
111
112 return false;
113}
114
115unsigned clang_InlineContentComment_hasTrailingNewline(CXComment CXC) {
116 const InlineContentComment *ICC = getASTNodeAs<InlineContentComment>(CXC);
117 if (!ICC)
118 return false;
119
120 return ICC->hasTrailingNewline();
121}
122
123CXString clang_TextComment_getText(CXComment CXC) {
124 const TextComment *TC = getASTNodeAs<TextComment>(CXC);
125 if (!TC)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000126 return cxstring::createNull();
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000127
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000128 return cxstring::createRef(TC->getText());
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000129}
130
131CXString clang_InlineCommandComment_getCommandName(CXComment CXC) {
132 const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC);
133 if (!ICC)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000134 return cxstring::createNull();
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000135
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000136 const CommandTraits &Traits = getCommandTraits(CXC);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000137 return cxstring::createRef(ICC->getCommandName(Traits));
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000138}
139
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000140enum CXCommentInlineCommandRenderKind
141clang_InlineCommandComment_getRenderKind(CXComment CXC) {
142 const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC);
143 if (!ICC)
144 return CXCommentInlineCommandRenderKind_Normal;
145
146 switch (ICC->getRenderKind()) {
147 case InlineCommandComment::RenderNormal:
148 return CXCommentInlineCommandRenderKind_Normal;
149
150 case InlineCommandComment::RenderBold:
151 return CXCommentInlineCommandRenderKind_Bold;
152
153 case InlineCommandComment::RenderMonospaced:
154 return CXCommentInlineCommandRenderKind_Monospaced;
155
156 case InlineCommandComment::RenderEmphasized:
157 return CXCommentInlineCommandRenderKind_Emphasized;
158 }
159 llvm_unreachable("unknown InlineCommandComment::RenderKind");
160}
161
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000162unsigned clang_InlineCommandComment_getNumArgs(CXComment CXC) {
163 const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC);
164 if (!ICC)
165 return 0;
166
167 return ICC->getNumArgs();
168}
169
170CXString clang_InlineCommandComment_getArgText(CXComment CXC,
171 unsigned ArgIdx) {
172 const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC);
173 if (!ICC || ArgIdx >= ICC->getNumArgs())
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000174 return cxstring::createNull();
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000175
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000176 return cxstring::createRef(ICC->getArgText(ArgIdx));
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000177}
178
179CXString clang_HTMLTagComment_getTagName(CXComment CXC) {
180 const HTMLTagComment *HTC = getASTNodeAs<HTMLTagComment>(CXC);
181 if (!HTC)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000182 return cxstring::createNull();
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000183
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000184 return cxstring::createRef(HTC->getTagName());
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000185}
186
187unsigned clang_HTMLStartTagComment_isSelfClosing(CXComment CXC) {
188 const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC);
189 if (!HST)
190 return false;
191
192 return HST->isSelfClosing();
193}
194
195unsigned clang_HTMLStartTag_getNumAttrs(CXComment CXC) {
196 const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC);
197 if (!HST)
198 return 0;
199
200 return HST->getNumAttrs();
201}
202
203CXString clang_HTMLStartTag_getAttrName(CXComment CXC, unsigned AttrIdx) {
204 const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC);
205 if (!HST || AttrIdx >= HST->getNumAttrs())
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000206 return cxstring::createNull();
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000207
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000208 return cxstring::createRef(HST->getAttr(AttrIdx).Name);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000209}
210
211CXString clang_HTMLStartTag_getAttrValue(CXComment CXC, unsigned AttrIdx) {
212 const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC);
213 if (!HST || AttrIdx >= HST->getNumAttrs())
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000214 return cxstring::createNull();
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000215
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000216 return cxstring::createRef(HST->getAttr(AttrIdx).Value);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000217}
218
219CXString clang_BlockCommandComment_getCommandName(CXComment CXC) {
220 const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC);
221 if (!BCC)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000222 return cxstring::createNull();
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000223
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000224 const CommandTraits &Traits = getCommandTraits(CXC);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000225 return cxstring::createRef(BCC->getCommandName(Traits));
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000226}
227
228unsigned clang_BlockCommandComment_getNumArgs(CXComment CXC) {
229 const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC);
230 if (!BCC)
231 return 0;
232
233 return BCC->getNumArgs();
234}
235
236CXString clang_BlockCommandComment_getArgText(CXComment CXC,
237 unsigned ArgIdx) {
238 const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC);
239 if (!BCC || ArgIdx >= BCC->getNumArgs())
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000240 return cxstring::createNull();
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000241
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000242 return cxstring::createRef(BCC->getArgText(ArgIdx));
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000243}
244
245CXComment clang_BlockCommandComment_getParagraph(CXComment CXC) {
246 const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC);
247 if (!BCC)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000248 return createCXComment(NULL, NULL);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000249
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000250 return createCXComment(BCC->getParagraph(), CXC.TranslationUnit);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000251}
252
253CXString clang_ParamCommandComment_getParamName(CXComment CXC) {
254 const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC);
Dmitri Gribenko1f8c5292012-07-23 19:41:49 +0000255 if (!PCC || !PCC->hasParamName())
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000256 return cxstring::createNull();
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000257
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000258 return cxstring::createRef(PCC->getParamNameAsWritten());
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000259}
260
261unsigned clang_ParamCommandComment_isParamIndexValid(CXComment CXC) {
262 const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC);
263 if (!PCC)
264 return false;
265
266 return PCC->isParamIndexValid();
267}
268
269unsigned clang_ParamCommandComment_getParamIndex(CXComment CXC) {
270 const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC);
Dmitri Gribenkob7403162012-07-30 17:38:19 +0000271 if (!PCC || !PCC->isParamIndexValid())
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000272 return ParamCommandComment::InvalidParamIndex;
273
274 return PCC->getParamIndex();
275}
276
277unsigned clang_ParamCommandComment_isDirectionExplicit(CXComment CXC) {
278 const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC);
279 if (!PCC)
280 return false;
281
282 return PCC->isDirectionExplicit();
283}
284
285enum CXCommentParamPassDirection clang_ParamCommandComment_getDirection(
286 CXComment CXC) {
287 const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC);
288 if (!PCC)
289 return CXCommentParamPassDirection_In;
290
291 switch (PCC->getDirection()) {
292 case ParamCommandComment::In:
293 return CXCommentParamPassDirection_In;
294
295 case ParamCommandComment::Out:
296 return CXCommentParamPassDirection_Out;
297
298 case ParamCommandComment::InOut:
299 return CXCommentParamPassDirection_InOut;
300 }
301 llvm_unreachable("unknown ParamCommandComment::PassDirection");
302}
303
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000304CXString clang_TParamCommandComment_getParamName(CXComment CXC) {
305 const TParamCommandComment *TPCC = getASTNodeAs<TParamCommandComment>(CXC);
306 if (!TPCC || !TPCC->hasParamName())
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000307 return cxstring::createNull();
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000308
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000309 return cxstring::createRef(TPCC->getParamNameAsWritten());
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000310}
311
312unsigned clang_TParamCommandComment_isParamPositionValid(CXComment CXC) {
313 const TParamCommandComment *TPCC = getASTNodeAs<TParamCommandComment>(CXC);
314 if (!TPCC)
315 return false;
316
317 return TPCC->isPositionValid();
318}
319
320unsigned clang_TParamCommandComment_getDepth(CXComment CXC) {
321 const TParamCommandComment *TPCC = getASTNodeAs<TParamCommandComment>(CXC);
322 if (!TPCC || !TPCC->isPositionValid())
323 return 0;
324
325 return TPCC->getDepth();
326}
327
328unsigned clang_TParamCommandComment_getIndex(CXComment CXC, unsigned Depth) {
329 const TParamCommandComment *TPCC = getASTNodeAs<TParamCommandComment>(CXC);
330 if (!TPCC || !TPCC->isPositionValid() || Depth >= TPCC->getDepth())
331 return 0;
332
333 return TPCC->getIndex(Depth);
334}
335
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000336CXString clang_VerbatimBlockLineComment_getText(CXComment CXC) {
337 const VerbatimBlockLineComment *VBL =
338 getASTNodeAs<VerbatimBlockLineComment>(CXC);
339 if (!VBL)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000340 return cxstring::createNull();
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000341
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000342 return cxstring::createRef(VBL->getText());
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000343}
344
345CXString clang_VerbatimLineComment_getText(CXComment CXC) {
346 const VerbatimLineComment *VLC = getASTNodeAs<VerbatimLineComment>(CXC);
347 if (!VLC)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000348 return cxstring::createNull();
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000349
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000350 return cxstring::createRef(VLC->getText());
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000351}
352
353} // end extern "C"
354
355//===----------------------------------------------------------------------===//
356// Helpers for converting comment AST to HTML.
357//===----------------------------------------------------------------------===//
358
359namespace {
360
Dmitri Gribenkoe5db09c2012-07-30 19:47:34 +0000361/// This comparison will sort parameters with valid index by index and
362/// invalid (unresolved) parameters last.
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000363class ParamCommandCommentCompareIndex {
364public:
365 bool operator()(const ParamCommandComment *LHS,
366 const ParamCommandComment *RHS) const {
Dmitri Gribenkob7403162012-07-30 17:38:19 +0000367 unsigned LHSIndex = UINT_MAX;
368 unsigned RHSIndex = UINT_MAX;
369 if (LHS->isParamIndexValid())
370 LHSIndex = LHS->getParamIndex();
371 if (RHS->isParamIndexValid())
372 RHSIndex = RHS->getParamIndex();
373
374 return LHSIndex < RHSIndex;
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000375 }
376};
377
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000378/// This comparison will sort template parameters in the following order:
379/// \li real template parameters (depth = 1) in index order;
380/// \li all other names (depth > 1);
381/// \li unresolved names.
382class TParamCommandCommentComparePosition {
383public:
384 bool operator()(const TParamCommandComment *LHS,
385 const TParamCommandComment *RHS) const {
386 // Sort unresolved names last.
387 if (!LHS->isPositionValid())
388 return false;
389 if (!RHS->isPositionValid())
390 return true;
391
392 if (LHS->getDepth() > 1)
393 return false;
394 if (RHS->getDepth() > 1)
395 return true;
396
397 // Sort template parameters in index order.
398 if (LHS->getDepth() == 1 && RHS->getDepth() == 1)
399 return LHS->getIndex(0) < RHS->getIndex(0);
400
401 // Leave all other names in source order.
402 return true;
403 }
404};
405
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000406/// Separate parts of a FullComment.
407struct FullCommentParts {
408 /// Take a full comment apart and initialize members accordingly.
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000409 FullCommentParts(const FullComment *C,
410 const CommandTraits &Traits);
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000411
412 const BlockContentComment *Brief;
Fariborz Jahanianf843a582013-01-31 23:12:39 +0000413 const BlockContentComment *Headerfile;
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000414 const ParagraphComment *FirstParagraph;
415 const BlockCommandComment *Returns;
416 SmallVector<const ParamCommandComment *, 8> Params;
417 SmallVector<const TParamCommandComment *, 4> TParams;
418 SmallVector<const BlockContentComment *, 8> MiscBlocks;
419};
420
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000421FullCommentParts::FullCommentParts(const FullComment *C,
422 const CommandTraits &Traits) :
Fariborz Jahanianf843a582013-01-31 23:12:39 +0000423 Brief(NULL), Headerfile(NULL), FirstParagraph(NULL), Returns(NULL) {
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000424 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
425 I != E; ++I) {
426 const Comment *Child = *I;
427 if (!Child)
428 continue;
429 switch (Child->getCommentKind()) {
430 case Comment::NoCommentKind:
431 continue;
432
433 case Comment::ParagraphCommentKind: {
434 const ParagraphComment *PC = cast<ParagraphComment>(Child);
435 if (PC->isWhitespace())
436 break;
437 if (!FirstParagraph)
438 FirstParagraph = PC;
439
440 MiscBlocks.push_back(PC);
441 break;
442 }
443
444 case Comment::BlockCommandCommentKind: {
445 const BlockCommandComment *BCC = cast<BlockCommandComment>(Child);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000446 const CommandInfo *Info = Traits.getCommandInfo(BCC->getCommandID());
447 if (!Brief && Info->IsBriefCommand) {
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000448 Brief = BCC;
449 break;
450 }
Fariborz Jahanianf843a582013-01-31 23:12:39 +0000451 if (!Headerfile && Info->IsHeaderfileCommand) {
452 Headerfile = BCC;
453 break;
454 }
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000455 if (!Returns && Info->IsReturnsCommand) {
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000456 Returns = BCC;
457 break;
458 }
459 MiscBlocks.push_back(BCC);
460 break;
461 }
462
463 case Comment::ParamCommandCommentKind: {
464 const ParamCommandComment *PCC = cast<ParamCommandComment>(Child);
465 if (!PCC->hasParamName())
466 break;
467
468 if (!PCC->isDirectionExplicit() && !PCC->hasNonWhitespaceParagraph())
469 break;
470
471 Params.push_back(PCC);
472 break;
473 }
474
475 case Comment::TParamCommandCommentKind: {
476 const TParamCommandComment *TPCC = cast<TParamCommandComment>(Child);
477 if (!TPCC->hasParamName())
478 break;
479
480 if (!TPCC->hasNonWhitespaceParagraph())
481 break;
482
483 TParams.push_back(TPCC);
484 break;
485 }
486
487 case Comment::VerbatimBlockCommentKind:
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000488 MiscBlocks.push_back(cast<BlockCommandComment>(Child));
489 break;
490
Dmitri Gribenko62290ae2012-08-09 18:20:29 +0000491 case Comment::VerbatimLineCommentKind: {
492 const VerbatimLineComment *VLC = cast<VerbatimLineComment>(Child);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000493 const CommandInfo *Info = Traits.getCommandInfo(VLC->getCommandID());
494 if (!Info->IsDeclarationCommand)
Dmitri Gribenko62290ae2012-08-09 18:20:29 +0000495 MiscBlocks.push_back(VLC);
496 break;
497 }
498
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000499 case Comment::TextCommentKind:
500 case Comment::InlineCommandCommentKind:
501 case Comment::HTMLStartTagCommentKind:
502 case Comment::HTMLEndTagCommentKind:
503 case Comment::VerbatimBlockLineCommentKind:
504 case Comment::FullCommentKind:
505 llvm_unreachable("AST node of this kind can't be a child of "
506 "a FullComment");
507 }
508 }
509
510 // Sort params in order they are declared in the function prototype.
511 // Unresolved parameters are put at the end of the list in the same order
512 // they were seen in the comment.
513 std::stable_sort(Params.begin(), Params.end(),
514 ParamCommandCommentCompareIndex());
515
516 std::stable_sort(TParams.begin(), TParams.end(),
517 TParamCommandCommentComparePosition());
518}
519
520void PrintHTMLStartTagComment(const HTMLStartTagComment *C,
521 llvm::raw_svector_ostream &Result) {
522 Result << "<" << C->getTagName();
523
524 if (C->getNumAttrs() != 0) {
525 for (unsigned i = 0, e = C->getNumAttrs(); i != e; i++) {
526 Result << " ";
527 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
528 Result << Attr.Name;
529 if (!Attr.Value.empty())
530 Result << "=\"" << Attr.Value << "\"";
531 }
532 }
533
534 if (!C->isSelfClosing())
535 Result << ">";
536 else
537 Result << "/>";
538}
539
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000540class CommentASTToHTMLConverter :
541 public ConstCommentVisitor<CommentASTToHTMLConverter> {
542public:
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000543 /// \param Str accumulator for HTML.
Dmitri Gribenko8cfabf22012-10-19 16:51:38 +0000544 CommentASTToHTMLConverter(const FullComment *FC,
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000545 SmallVectorImpl<char> &Str,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000546 const CommandTraits &Traits) :
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000547 FC(FC), Result(Str), Traits(Traits)
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000548 { }
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000549
550 // Inline content.
551 void visitTextComment(const TextComment *C);
552 void visitInlineCommandComment(const InlineCommandComment *C);
553 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
554 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
555
556 // Block content.
557 void visitParagraphComment(const ParagraphComment *C);
558 void visitBlockCommandComment(const BlockCommandComment *C);
559 void visitParamCommandComment(const ParamCommandComment *C);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000560 void visitTParamCommandComment(const TParamCommandComment *C);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000561 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
562 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
563 void visitVerbatimLineComment(const VerbatimLineComment *C);
564
565 void visitFullComment(const FullComment *C);
566
567 // Helpers.
568
569 /// Convert a paragraph that is not a block by itself (an argument to some
570 /// command).
571 void visitNonStandaloneParagraphComment(const ParagraphComment *C);
572
573 void appendToResultWithHTMLEscaping(StringRef S);
574
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000575private:
Dmitri Gribenko8cfabf22012-10-19 16:51:38 +0000576 const FullComment *FC;
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000577 /// Output stream for HTML.
578 llvm::raw_svector_ostream Result;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000579
580 const CommandTraits &Traits;
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000581};
582} // end unnamed namespace
583
584void CommentASTToHTMLConverter::visitTextComment(const TextComment *C) {
585 appendToResultWithHTMLEscaping(C->getText());
586}
587
588void CommentASTToHTMLConverter::visitInlineCommandComment(
589 const InlineCommandComment *C) {
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000590 // Nothing to render if no arguments supplied.
591 if (C->getNumArgs() == 0)
592 return;
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000593
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000594 // Nothing to render if argument is empty.
595 StringRef Arg0 = C->getArgText(0);
596 if (Arg0.empty())
597 return;
598
599 switch (C->getRenderKind()) {
600 case InlineCommandComment::RenderNormal:
Dmitri Gribenko59500fe2012-08-01 00:21:12 +0000601 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i) {
602 appendToResultWithHTMLEscaping(C->getArgText(i));
603 Result << " ";
604 }
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000605 return;
606
607 case InlineCommandComment::RenderBold:
608 assert(C->getNumArgs() == 1);
Dmitri Gribenko59500fe2012-08-01 00:21:12 +0000609 Result << "<b>";
610 appendToResultWithHTMLEscaping(Arg0);
611 Result << "</b>";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000612 return;
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000613 case InlineCommandComment::RenderMonospaced:
614 assert(C->getNumArgs() == 1);
Dmitri Gribenko59500fe2012-08-01 00:21:12 +0000615 Result << "<tt>";
616 appendToResultWithHTMLEscaping(Arg0);
617 Result<< "</tt>";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000618 return;
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000619 case InlineCommandComment::RenderEmphasized:
620 assert(C->getNumArgs() == 1);
Dmitri Gribenko59500fe2012-08-01 00:21:12 +0000621 Result << "<em>";
622 appendToResultWithHTMLEscaping(Arg0);
623 Result << "</em>";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000624 return;
625 }
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000626}
627
628void CommentASTToHTMLConverter::visitHTMLStartTagComment(
629 const HTMLStartTagComment *C) {
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000630 PrintHTMLStartTagComment(C, Result);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000631}
632
633void CommentASTToHTMLConverter::visitHTMLEndTagComment(
634 const HTMLEndTagComment *C) {
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000635 Result << "</" << C->getTagName() << ">";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000636}
637
638void CommentASTToHTMLConverter::visitParagraphComment(
639 const ParagraphComment *C) {
640 if (C->isWhitespace())
641 return;
642
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000643 Result << "<p>";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000644 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
645 I != E; ++I) {
646 visit(*I);
647 }
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000648 Result << "</p>";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000649}
650
651void CommentASTToHTMLConverter::visitBlockCommandComment(
652 const BlockCommandComment *C) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000653 const CommandInfo *Info = Traits.getCommandInfo(C->getCommandID());
654 if (Info->IsBriefCommand) {
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000655 Result << "<p class=\"para-brief\">";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000656 visitNonStandaloneParagraphComment(C->getParagraph());
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000657 Result << "</p>";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000658 return;
659 }
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000660 if (Info->IsReturnsCommand) {
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000661 Result << "<p class=\"para-returns\">"
662 "<span class=\"word-returns\">Returns</span> ";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000663 visitNonStandaloneParagraphComment(C->getParagraph());
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000664 Result << "</p>";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000665 return;
666 }
667 // We don't know anything about this command. Just render the paragraph.
668 visit(C->getParagraph());
669}
670
671void CommentASTToHTMLConverter::visitParamCommandComment(
672 const ParamCommandComment *C) {
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000673 if (C->isParamIndexValid()) {
674 Result << "<dt class=\"param-name-index-"
675 << C->getParamIndex()
676 << "\">";
Fariborz Jahanian262e60c2012-10-18 21:42:42 +0000677 appendToResultWithHTMLEscaping(C->getParamName(FC));
678 } else {
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000679 Result << "<dt class=\"param-name-index-invalid\">";
Fariborz Jahanian262e60c2012-10-18 21:42:42 +0000680 appendToResultWithHTMLEscaping(C->getParamNameAsWritten());
681 }
Dmitri Gribenko59500fe2012-08-01 00:21:12 +0000682 Result << "</dt>";
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000683
684 if (C->isParamIndexValid()) {
685 Result << "<dd class=\"param-descr-index-"
686 << C->getParamIndex()
687 << "\">";
688 } else
689 Result << "<dd class=\"param-descr-index-invalid\">";
690
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000691 visitNonStandaloneParagraphComment(C->getParagraph());
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000692 Result << "</dd>";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000693}
694
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000695void CommentASTToHTMLConverter::visitTParamCommandComment(
696 const TParamCommandComment *C) {
697 if (C->isPositionValid()) {
698 if (C->getDepth() == 1)
Dmitri Gribenko6a425522012-08-01 23:47:30 +0000699 Result << "<dt class=\"tparam-name-index-"
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000700 << C->getIndex(0)
701 << "\">";
702 else
Dmitri Gribenko6a425522012-08-01 23:47:30 +0000703 Result << "<dt class=\"tparam-name-index-other\">";
Fariborz Jahanian262e60c2012-10-18 21:42:42 +0000704 appendToResultWithHTMLEscaping(C->getParamName(FC));
705 } else {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000706 Result << "<dt class=\"tparam-name-index-invalid\">";
Fariborz Jahanian262e60c2012-10-18 21:42:42 +0000707 appendToResultWithHTMLEscaping(C->getParamNameAsWritten());
708 }
709
Dmitri Gribenko59500fe2012-08-01 00:21:12 +0000710 Result << "</dt>";
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000711
712 if (C->isPositionValid()) {
713 if (C->getDepth() == 1)
714 Result << "<dd class=\"tparam-descr-index-"
715 << C->getIndex(0)
716 << "\">";
717 else
718 Result << "<dd class=\"tparam-descr-index-other\">";
719 } else
720 Result << "<dd class=\"tparam-descr-index-invalid\">";
721
722 visitNonStandaloneParagraphComment(C->getParagraph());
723 Result << "</dd>";
724}
725
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000726void CommentASTToHTMLConverter::visitVerbatimBlockComment(
727 const VerbatimBlockComment *C) {
728 unsigned NumLines = C->getNumLines();
729 if (NumLines == 0)
730 return;
731
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000732 Result << "<pre>";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000733 for (unsigned i = 0; i != NumLines; ++i) {
734 appendToResultWithHTMLEscaping(C->getText(i));
735 if (i + 1 != NumLines)
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000736 Result << '\n';
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000737 }
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000738 Result << "</pre>";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000739}
740
741void CommentASTToHTMLConverter::visitVerbatimBlockLineComment(
742 const VerbatimBlockLineComment *C) {
743 llvm_unreachable("should not see this AST node");
744}
745
746void CommentASTToHTMLConverter::visitVerbatimLineComment(
747 const VerbatimLineComment *C) {
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000748 Result << "<pre>";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000749 appendToResultWithHTMLEscaping(C->getText());
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000750 Result << "</pre>";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000751}
752
753void CommentASTToHTMLConverter::visitFullComment(const FullComment *C) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000754 FullCommentParts Parts(C, Traits);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000755
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000756 bool FirstParagraphIsBrief = false;
Fariborz Jahanianf843a582013-01-31 23:12:39 +0000757 if (Parts.Headerfile)
758 visit(Parts.Headerfile);
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000759 if (Parts.Brief)
760 visit(Parts.Brief);
761 else if (Parts.FirstParagraph) {
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000762 Result << "<p class=\"para-brief\">";
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000763 visitNonStandaloneParagraphComment(Parts.FirstParagraph);
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000764 Result << "</p>";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000765 FirstParagraphIsBrief = true;
766 }
767
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000768 for (unsigned i = 0, e = Parts.MiscBlocks.size(); i != e; ++i) {
769 const Comment *C = Parts.MiscBlocks[i];
770 if (FirstParagraphIsBrief && C == Parts.FirstParagraph)
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000771 continue;
772 visit(C);
773 }
774
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000775 if (Parts.TParams.size() != 0) {
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000776 Result << "<dl>";
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000777 for (unsigned i = 0, e = Parts.TParams.size(); i != e; ++i)
778 visit(Parts.TParams[i]);
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000779 Result << "</dl>";
780 }
781
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000782 if (Parts.Params.size() != 0) {
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000783 Result << "<dl>";
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000784 for (unsigned i = 0, e = Parts.Params.size(); i != e; ++i)
785 visit(Parts.Params[i]);
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000786 Result << "</dl>";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000787 }
788
Dmitri Gribenko2ff84b52012-08-01 22:48:16 +0000789 if (Parts.Returns)
790 visit(Parts.Returns);
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000791
792 Result.flush();
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000793}
794
795void CommentASTToHTMLConverter::visitNonStandaloneParagraphComment(
796 const ParagraphComment *C) {
797 if (!C)
798 return;
799
800 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
801 I != E; ++I) {
802 visit(*I);
803 }
804}
805
806void CommentASTToHTMLConverter::appendToResultWithHTMLEscaping(StringRef S) {
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000807 for (StringRef::iterator I = S.begin(), E = S.end(); I != E; ++I) {
808 const char C = *I;
809 switch (C) {
810 case '&':
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000811 Result << "&amp;";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000812 break;
813 case '<':
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000814 Result << "&lt;";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000815 break;
816 case '>':
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000817 Result << "&gt;";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000818 break;
819 case '"':
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000820 Result << "&quot;";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000821 break;
822 case '\'':
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000823 Result << "&#39;";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000824 break;
825 case '/':
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000826 Result << "&#47;";
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000827 break;
828 default:
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000829 Result << C;
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000830 break;
831 }
832 }
833}
834
835extern "C" {
836
837CXString clang_HTMLTagComment_getAsString(CXComment CXC) {
838 const HTMLTagComment *HTC = getASTNodeAs<HTMLTagComment>(CXC);
839 if (!HTC)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000840 return cxstring::createNull();
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000841
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000842 SmallString<128> HTML;
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000843 CommentASTToHTMLConverter Converter(0, HTML, getCommandTraits(CXC));
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000844 Converter.visit(HTC);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000845 return cxstring::createDup(HTML.str());
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000846}
847
848CXString clang_FullComment_getAsHTML(CXComment CXC) {
849 const FullComment *FC = getASTNodeAs<FullComment>(CXC);
850 if (!FC)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000851 return cxstring::createNull();
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000852
Dmitri Gribenko3e63d332012-07-21 01:47:43 +0000853 SmallString<1024> HTML;
Dmitri Gribenko8cfabf22012-10-19 16:51:38 +0000854 CommentASTToHTMLConverter Converter(FC, HTML, getCommandTraits(CXC));
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000855 Converter.visit(FC);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000856 return cxstring::createDup(HTML.str());
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000857}
858
859} // end extern "C"
860
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000861namespace {
862class CommentASTToXMLConverter :
863 public ConstCommentVisitor<CommentASTToXMLConverter> {
864public:
865 /// \param Str accumulator for XML.
Dmitri Gribenko8cfabf22012-10-19 16:51:38 +0000866 CommentASTToXMLConverter(const FullComment *FC,
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000867 SmallVectorImpl<char> &Str,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000868 const CommandTraits &Traits,
Fariborz Jahanian88b95212012-12-18 23:02:59 +0000869 const SourceManager &SM,
Fariborz Jahanian7c106832012-12-19 00:01:48 +0000870 SimpleFormatContext &SFC,
Fariborz Jahanian88b95212012-12-18 23:02:59 +0000871 unsigned FUID) :
872 FC(FC), Result(Str), Traits(Traits), SM(SM),
Fariborz Jahanian7c106832012-12-19 00:01:48 +0000873 FormatRewriterContext(SFC),
Fariborz Jahanian88b95212012-12-18 23:02:59 +0000874 FormatInMemoryUniqueId(FUID) { }
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000875
876 // Inline content.
877 void visitTextComment(const TextComment *C);
878 void visitInlineCommandComment(const InlineCommandComment *C);
879 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
880 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
881
882 // Block content.
883 void visitParagraphComment(const ParagraphComment *C);
Dmitri Gribenkoaf01bed2013-02-01 20:23:57 +0000884
885 void appendParagraphCommentWithKind(const ParagraphComment *C,
886 StringRef Kind);
887
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000888 void visitBlockCommandComment(const BlockCommandComment *C);
889 void visitParamCommandComment(const ParamCommandComment *C);
890 void visitTParamCommandComment(const TParamCommandComment *C);
891 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
892 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
893 void visitVerbatimLineComment(const VerbatimLineComment *C);
894
895 void visitFullComment(const FullComment *C);
896
897 // Helpers.
898 void appendToResultWithXMLEscaping(StringRef S);
Dmitri Gribenkoaf01bed2013-02-01 20:23:57 +0000899
Fariborz Jahanian7c106832012-12-19 00:01:48 +0000900 void formatTextOfDeclaration(const DeclInfo *DI,
901 SmallString<128> &Declaration);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000902
903private:
Dmitri Gribenko8cfabf22012-10-19 16:51:38 +0000904 const FullComment *FC;
905
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000906 /// Output stream for XML.
907 llvm::raw_svector_ostream Result;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000908
909 const CommandTraits &Traits;
910 const SourceManager &SM;
Fariborz Jahanian88b95212012-12-18 23:02:59 +0000911 SimpleFormatContext &FormatRewriterContext;
912 unsigned FormatInMemoryUniqueId;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000913};
Dmitri Gribenko7c984992012-10-25 18:28:26 +0000914
915void getSourceTextOfDeclaration(const DeclInfo *ThisDecl,
916 SmallVectorImpl<char> &Str) {
917 ASTContext &Context = ThisDecl->CurrentDecl->getASTContext();
918 const LangOptions &LangOpts = Context.getLangOpts();
919 llvm::raw_svector_ostream OS(Str);
920 PrintingPolicy PPolicy(LangOpts);
Fariborz Jahanian40902d82012-12-19 23:36:00 +0000921 PPolicy.PolishForDeclaration = true;
Dmitri Gribenko7c984992012-10-25 18:28:26 +0000922 PPolicy.TerseOutput = true;
923 ThisDecl->CurrentDecl->print(OS, PPolicy,
Dmitri Gribenko27c2cb22013-01-07 18:45:48 +0000924 /*Indentation*/0, /*PrintInstantiation*/false);
Dmitri Gribenko7c984992012-10-25 18:28:26 +0000925}
Fariborz Jahanian88b95212012-12-18 23:02:59 +0000926
Fariborz Jahanian7c106832012-12-19 00:01:48 +0000927void CommentASTToXMLConverter::formatTextOfDeclaration(
928 const DeclInfo *DI,
929 SmallString<128> &Declaration) {
Fariborz Jahanian7c106832012-12-19 00:01:48 +0000930 // FIXME. formatting API expects null terminated input string.
931 // There might be more efficient way of doing this.
932 std::string StringDecl = Declaration.str();
Fariborz Jahanian88b95212012-12-18 23:02:59 +0000933
Fariborz Jahanian7c106832012-12-19 00:01:48 +0000934 // Formatter specific code.
935 // Form a unique in memory buffer name.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000936 SmallString<128> filename;
Fariborz Jahanian7c106832012-12-19 00:01:48 +0000937 filename += "xmldecl";
938 filename += llvm::utostr(FormatInMemoryUniqueId);
939 filename += ".xd";
940 FileID ID = FormatRewriterContext.createInMemoryFile(filename, StringDecl);
941 SourceLocation Start =
942 FormatRewriterContext.Sources.getLocForStartOfFile(ID).getLocWithOffset(0);
943 unsigned Length = Declaration.size();
Fariborz Jahanian88b95212012-12-18 23:02:59 +0000944
Fariborz Jahanian7c106832012-12-19 00:01:48 +0000945 std::vector<CharSourceRange>
946 Ranges(1, CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));
947 ASTContext &Context = DI->CurrentDecl->getASTContext();
948 const LangOptions &LangOpts = Context.getLangOpts();
949 Lexer Lex(ID, FormatRewriterContext.Sources.getBuffer(ID),
950 FormatRewriterContext.Sources, LangOpts);
951 tooling::Replacements Replace =
952 reformat(format::getLLVMStyle(), Lex, FormatRewriterContext.Sources, Ranges);
953 applyAllReplacements(Replace, FormatRewriterContext.Rewrite);
954 Declaration = FormatRewriterContext.getRewrittenText(ID);
Fariborz Jahanian88b95212012-12-18 23:02:59 +0000955}
Dmitri Gribenko7c984992012-10-25 18:28:26 +0000956
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000957} // end unnamed namespace
958
959void CommentASTToXMLConverter::visitTextComment(const TextComment *C) {
960 appendToResultWithXMLEscaping(C->getText());
961}
962
963void CommentASTToXMLConverter::visitInlineCommandComment(const InlineCommandComment *C) {
964 // Nothing to render if no arguments supplied.
965 if (C->getNumArgs() == 0)
966 return;
967
968 // Nothing to render if argument is empty.
969 StringRef Arg0 = C->getArgText(0);
970 if (Arg0.empty())
971 return;
972
973 switch (C->getRenderKind()) {
974 case InlineCommandComment::RenderNormal:
975 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i) {
976 appendToResultWithXMLEscaping(C->getArgText(i));
977 Result << " ";
978 }
979 return;
980 case InlineCommandComment::RenderBold:
981 assert(C->getNumArgs() == 1);
982 Result << "<bold>";
983 appendToResultWithXMLEscaping(Arg0);
984 Result << "</bold>";
985 return;
986 case InlineCommandComment::RenderMonospaced:
987 assert(C->getNumArgs() == 1);
988 Result << "<monospaced>";
989 appendToResultWithXMLEscaping(Arg0);
990 Result << "</monospaced>";
991 return;
992 case InlineCommandComment::RenderEmphasized:
993 assert(C->getNumArgs() == 1);
994 Result << "<emphasized>";
995 appendToResultWithXMLEscaping(Arg0);
996 Result << "</emphasized>";
997 return;
998 }
999}
1000
1001void CommentASTToXMLConverter::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
1002 Result << "<rawHTML><![CDATA[";
1003 PrintHTMLStartTagComment(C, Result);
1004 Result << "]]></rawHTML>";
1005}
1006
1007void CommentASTToXMLConverter::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
1008 Result << "<rawHTML>&lt;/" << C->getTagName() << "&gt;</rawHTML>";
1009}
1010
1011void CommentASTToXMLConverter::visitParagraphComment(const ParagraphComment *C) {
Dmitri Gribenkoaf01bed2013-02-01 20:23:57 +00001012 appendParagraphCommentWithKind(C, StringRef());
1013}
1014
1015void CommentASTToXMLConverter::appendParagraphCommentWithKind(
1016 const ParagraphComment *C,
1017 StringRef ParagraphKind) {
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001018 if (C->isWhitespace())
1019 return;
1020
Dmitri Gribenkoaf01bed2013-02-01 20:23:57 +00001021 if (ParagraphKind.empty())
1022 Result << "<Para>";
1023 else
1024 Result << "<Para kind=\"" << ParagraphKind << "\">";
1025
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001026 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
1027 I != E; ++I) {
1028 visit(*I);
1029 }
1030 Result << "</Para>";
1031}
1032
1033void CommentASTToXMLConverter::visitBlockCommandComment(const BlockCommandComment *C) {
Dmitri Gribenkoaf01bed2013-02-01 20:23:57 +00001034 StringRef ParagraphKind;
1035
1036 switch (C->getCommandID()) {
Fariborz Jahanian9db0fe92013-02-26 22:12:16 +00001037 case CommandTraits::KCI_attention:
Dmitri Gribenkoaf01bed2013-02-01 20:23:57 +00001038 case CommandTraits::KCI_author:
1039 case CommandTraits::KCI_authors:
1040 case CommandTraits::KCI_bug:
1041 case CommandTraits::KCI_copyright:
1042 case CommandTraits::KCI_date:
1043 case CommandTraits::KCI_invariant:
1044 case CommandTraits::KCI_note:
1045 case CommandTraits::KCI_post:
1046 case CommandTraits::KCI_pre:
1047 case CommandTraits::KCI_remark:
1048 case CommandTraits::KCI_remarks:
1049 case CommandTraits::KCI_sa:
1050 case CommandTraits::KCI_see:
1051 case CommandTraits::KCI_since:
1052 case CommandTraits::KCI_todo:
1053 case CommandTraits::KCI_version:
1054 case CommandTraits::KCI_warning:
1055 ParagraphKind = C->getCommandName(Traits);
1056 default:
1057 break;
1058 }
1059
1060 appendParagraphCommentWithKind(C->getParagraph(), ParagraphKind);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001061}
1062
1063void CommentASTToXMLConverter::visitParamCommandComment(const ParamCommandComment *C) {
1064 Result << "<Parameter><Name>";
Fariborz Jahanian262e60c2012-10-18 21:42:42 +00001065 appendToResultWithXMLEscaping(C->isParamIndexValid() ? C->getParamName(FC)
1066 : C->getParamNameAsWritten());
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001067 Result << "</Name>";
1068
1069 if (C->isParamIndexValid())
1070 Result << "<Index>" << C->getParamIndex() << "</Index>";
1071
1072 Result << "<Direction isExplicit=\"" << C->isDirectionExplicit() << "\">";
1073 switch (C->getDirection()) {
1074 case ParamCommandComment::In:
1075 Result << "in";
1076 break;
1077 case ParamCommandComment::Out:
1078 Result << "out";
1079 break;
1080 case ParamCommandComment::InOut:
1081 Result << "in,out";
1082 break;
1083 }
1084 Result << "</Direction><Discussion>";
1085 visit(C->getParagraph());
1086 Result << "</Discussion></Parameter>";
1087}
1088
1089void CommentASTToXMLConverter::visitTParamCommandComment(
1090 const TParamCommandComment *C) {
1091 Result << "<Parameter><Name>";
Fariborz Jahanian262e60c2012-10-18 21:42:42 +00001092 appendToResultWithXMLEscaping(C->isPositionValid() ? C->getParamName(FC)
1093 : C->getParamNameAsWritten());
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001094 Result << "</Name>";
1095
1096 if (C->isPositionValid() && C->getDepth() == 1) {
1097 Result << "<Index>" << C->getIndex(0) << "</Index>";
1098 }
1099
1100 Result << "<Discussion>";
1101 visit(C->getParagraph());
1102 Result << "</Discussion></Parameter>";
1103}
1104
1105void CommentASTToXMLConverter::visitVerbatimBlockComment(
1106 const VerbatimBlockComment *C) {
1107 unsigned NumLines = C->getNumLines();
1108 if (NumLines == 0)
1109 return;
1110
Dmitri Gribenko4c494f42013-02-03 17:48:05 +00001111 switch (C->getCommandID()) {
1112 case CommandTraits::KCI_code:
1113 Result << "<Verbatim xml:space=\"preserve\" kind=\"code\">";
1114 break;
1115 default:
1116 Result << "<Verbatim xml:space=\"preserve\" kind=\"verbatim\">";
1117 break;
1118 }
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001119 for (unsigned i = 0; i != NumLines; ++i) {
1120 appendToResultWithXMLEscaping(C->getText(i));
1121 if (i + 1 != NumLines)
1122 Result << '\n';
1123 }
1124 Result << "</Verbatim>";
1125}
1126
1127void CommentASTToXMLConverter::visitVerbatimBlockLineComment(
1128 const VerbatimBlockLineComment *C) {
1129 llvm_unreachable("should not see this AST node");
1130}
1131
1132void CommentASTToXMLConverter::visitVerbatimLineComment(
1133 const VerbatimLineComment *C) {
Dmitri Gribenko6cd44202012-08-08 22:10:24 +00001134 Result << "<Verbatim xml:space=\"preserve\" kind=\"verbatim\">";
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001135 appendToResultWithXMLEscaping(C->getText());
1136 Result << "</Verbatim>";
1137}
1138
1139void CommentASTToXMLConverter::visitFullComment(const FullComment *C) {
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +00001140 FullCommentParts Parts(C, Traits);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001141
1142 const DeclInfo *DI = C->getDeclInfo();
1143 StringRef RootEndTag;
1144 if (DI) {
1145 switch (DI->getKind()) {
1146 case DeclInfo::OtherKind:
1147 RootEndTag = "</Other>";
1148 Result << "<Other";
1149 break;
1150 case DeclInfo::FunctionKind:
1151 RootEndTag = "</Function>";
1152 Result << "<Function";
1153 switch (DI->TemplateKind) {
1154 case DeclInfo::NotTemplate:
1155 break;
1156 case DeclInfo::Template:
1157 Result << " templateKind=\"template\"";
1158 break;
1159 case DeclInfo::TemplateSpecialization:
1160 Result << " templateKind=\"specialization\"";
1161 break;
1162 case DeclInfo::TemplatePartialSpecialization:
1163 llvm_unreachable("partial specializations of functions "
1164 "are not allowed in C++");
1165 }
1166 if (DI->IsInstanceMethod)
1167 Result << " isInstanceMethod=\"1\"";
1168 if (DI->IsClassMethod)
1169 Result << " isClassMethod=\"1\"";
1170 break;
1171 case DeclInfo::ClassKind:
1172 RootEndTag = "</Class>";
1173 Result << "<Class";
1174 switch (DI->TemplateKind) {
1175 case DeclInfo::NotTemplate:
1176 break;
1177 case DeclInfo::Template:
1178 Result << " templateKind=\"template\"";
1179 break;
1180 case DeclInfo::TemplateSpecialization:
1181 Result << " templateKind=\"specialization\"";
1182 break;
1183 case DeclInfo::TemplatePartialSpecialization:
1184 Result << " templateKind=\"partialSpecialization\"";
1185 break;
1186 }
1187 break;
1188 case DeclInfo::VariableKind:
1189 RootEndTag = "</Variable>";
1190 Result << "<Variable";
1191 break;
1192 case DeclInfo::NamespaceKind:
1193 RootEndTag = "</Namespace>";
1194 Result << "<Namespace";
1195 break;
1196 case DeclInfo::TypedefKind:
1197 RootEndTag = "</Typedef>";
1198 Result << "<Typedef";
1199 break;
Dmitri Gribenkocff339a2012-08-07 18:59:04 +00001200 case DeclInfo::EnumKind:
1201 RootEndTag = "</Enum>";
1202 Result << "<Enum";
1203 break;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001204 }
1205
1206 {
1207 // Print line and column number.
Fariborz Jahanian1bfb00d2012-10-17 21:58:03 +00001208 SourceLocation Loc = DI->CurrentDecl->getLocation();
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001209 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
1210 FileID FID = LocInfo.first;
1211 unsigned FileOffset = LocInfo.second;
1212
1213 if (!FID.isInvalid()) {
1214 if (const FileEntry *FE = SM.getFileEntryForID(FID)) {
1215 Result << " file=\"";
1216 appendToResultWithXMLEscaping(FE->getName());
1217 Result << "\"";
1218 }
1219 Result << " line=\"" << SM.getLineNumber(FID, FileOffset)
1220 << "\" column=\"" << SM.getColumnNumber(FID, FileOffset)
1221 << "\"";
1222 }
1223 }
1224
1225 // Finish the root tag.
1226 Result << ">";
1227
1228 bool FoundName = false;
Fariborz Jahanianbf967be2012-10-10 18:34:52 +00001229 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DI->CommentDecl)) {
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001230 if (DeclarationName DeclName = ND->getDeclName()) {
1231 Result << "<Name>";
1232 std::string Name = DeclName.getAsString();
1233 appendToResultWithXMLEscaping(Name);
1234 FoundName = true;
1235 Result << "</Name>";
1236 }
1237 }
1238 if (!FoundName)
1239 Result << "<Name>&lt;anonymous&gt;</Name>";
1240
1241 {
1242 // Print USR.
1243 SmallString<128> USR;
Fariborz Jahanianbf967be2012-10-10 18:34:52 +00001244 cxcursor::getDeclCursorUSR(DI->CommentDecl, USR);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001245 if (!USR.empty()) {
1246 Result << "<USR>";
1247 appendToResultWithXMLEscaping(USR);
1248 Result << "</USR>";
1249 }
1250 }
1251 } else {
1252 // No DeclInfo -- just emit some root tag and name tag.
1253 RootEndTag = "</Other>";
1254 Result << "<Other><Name>unknown</Name>";
1255 }
Fariborz Jahanianf843a582013-01-31 23:12:39 +00001256
1257 if (Parts.Headerfile) {
1258 Result << "<Headerfile>";
1259 visit(Parts.Headerfile);
1260 Result << "</Headerfile>";
1261 }
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001262
Dmitri Gribenko7c984992012-10-25 18:28:26 +00001263 {
1264 // Pretty-print the declaration.
1265 Result << "<Declaration>";
1266 SmallString<128> Declaration;
1267 getSourceTextOfDeclaration(DI, Declaration);
Fariborz Jahanian7c106832012-12-19 00:01:48 +00001268 formatTextOfDeclaration(DI, Declaration);
Dmitri Gribenko7c984992012-10-25 18:28:26 +00001269 appendToResultWithXMLEscaping(Declaration);
Fariborz Jahanian88b95212012-12-18 23:02:59 +00001270
Dmitri Gribenko7c984992012-10-25 18:28:26 +00001271 Result << "</Declaration>";
1272 }
1273
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001274 bool FirstParagraphIsBrief = false;
1275 if (Parts.Brief) {
1276 Result << "<Abstract>";
1277 visit(Parts.Brief);
1278 Result << "</Abstract>";
1279 } else if (Parts.FirstParagraph) {
1280 Result << "<Abstract>";
1281 visit(Parts.FirstParagraph);
1282 Result << "</Abstract>";
1283 FirstParagraphIsBrief = true;
1284 }
Fariborz Jahanianf843a582013-01-31 23:12:39 +00001285
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001286 if (Parts.TParams.size() != 0) {
1287 Result << "<TemplateParameters>";
1288 for (unsigned i = 0, e = Parts.TParams.size(); i != e; ++i)
1289 visit(Parts.TParams[i]);
1290 Result << "</TemplateParameters>";
1291 }
1292
1293 if (Parts.Params.size() != 0) {
1294 Result << "<Parameters>";
1295 for (unsigned i = 0, e = Parts.Params.size(); i != e; ++i)
1296 visit(Parts.Params[i]);
1297 Result << "</Parameters>";
1298 }
1299
1300 if (Parts.Returns) {
1301 Result << "<ResultDiscussion>";
1302 visit(Parts.Returns);
1303 Result << "</ResultDiscussion>";
1304 }
Fariborz Jahanian257e2e82012-09-28 22:35:49 +00001305
Fariborz Jahanianbf967be2012-10-10 18:34:52 +00001306 if (DI->CommentDecl->hasAttrs()) {
1307 const AttrVec &Attrs = DI->CommentDecl->getAttrs();
Fariborz Jahanianfaab5612012-10-01 18:42:25 +00001308 for (unsigned i = 0, e = Attrs.size(); i != e; i++) {
1309 const AvailabilityAttr *AA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Fariborz Jahanian2a465332012-10-02 20:05:47 +00001310 if (!AA) {
Fariborz Jahanian8da68b82012-10-02 23:01:04 +00001311 if (const DeprecatedAttr *DA = dyn_cast<DeprecatedAttr>(Attrs[i])) {
1312 if (DA->getMessage().empty())
1313 Result << "<Deprecated/>";
1314 else {
Dmitri Gribenko7d9c9752012-10-03 09:04:56 +00001315 Result << "<Deprecated>";
1316 appendToResultWithXMLEscaping(DA->getMessage());
1317 Result << "</Deprecated>";
Fariborz Jahanian8da68b82012-10-02 23:01:04 +00001318 }
1319 }
1320 else if (const UnavailableAttr *UA = dyn_cast<UnavailableAttr>(Attrs[i])) {
1321 if (UA->getMessage().empty())
1322 Result << "<Unavailable/>";
1323 else {
Dmitri Gribenko7d9c9752012-10-03 09:04:56 +00001324 Result << "<Unavailable>";
1325 appendToResultWithXMLEscaping(UA->getMessage());
1326 Result << "</Unavailable>";
Fariborz Jahanian8da68b82012-10-02 23:01:04 +00001327 }
1328 }
Fariborz Jahanian257e2e82012-09-28 22:35:49 +00001329 continue;
Fariborz Jahanian2a465332012-10-02 20:05:47 +00001330 }
Fariborz Jahanianfaab5612012-10-01 18:42:25 +00001331
1332 // 'availability' attribute.
Fariborz Jahanian257e2e82012-09-28 22:35:49 +00001333 Result << "<Availability";
Fariborz Jahanianfaab5612012-10-01 18:42:25 +00001334 StringRef Distribution;
Fariborz Jahanian257e2e82012-09-28 22:35:49 +00001335 if (AA->getPlatform()) {
Fariborz Jahanianfaab5612012-10-01 18:42:25 +00001336 Distribution = AvailabilityAttr::getPrettyPlatformName(
1337 AA->getPlatform()->getName());
1338 if (Distribution.empty())
1339 Distribution = AA->getPlatform()->getName();
Fariborz Jahanian257e2e82012-09-28 22:35:49 +00001340 }
Fariborz Jahanianfaab5612012-10-01 18:42:25 +00001341 Result << " distribution=\"" << Distribution << "\">";
Fariborz Jahanian257e2e82012-09-28 22:35:49 +00001342 VersionTuple IntroducedInVersion = AA->getIntroduced();
1343 if (!IntroducedInVersion.empty()) {
Fariborz Jahanianfaab5612012-10-01 18:42:25 +00001344 Result << "<IntroducedInVersion>"
1345 << IntroducedInVersion.getAsString()
1346 << "</IntroducedInVersion>";
Fariborz Jahanian257e2e82012-09-28 22:35:49 +00001347 }
1348 VersionTuple DeprecatedInVersion = AA->getDeprecated();
1349 if (!DeprecatedInVersion.empty()) {
Fariborz Jahanianfaab5612012-10-01 18:42:25 +00001350 Result << "<DeprecatedInVersion>"
1351 << DeprecatedInVersion.getAsString()
1352 << "</DeprecatedInVersion>";
Fariborz Jahanian257e2e82012-09-28 22:35:49 +00001353 }
1354 VersionTuple RemovedAfterVersion = AA->getObsoleted();
1355 if (!RemovedAfterVersion.empty()) {
Fariborz Jahanianfaab5612012-10-01 18:42:25 +00001356 Result << "<RemovedAfterVersion>"
1357 << RemovedAfterVersion.getAsString()
1358 << "</RemovedAfterVersion>";
Fariborz Jahanian257e2e82012-09-28 22:35:49 +00001359 }
1360 StringRef DeprecationSummary = AA->getMessage();
1361 if (!DeprecationSummary.empty()) {
Dmitri Gribenko7d9c9752012-10-03 09:04:56 +00001362 Result << "<DeprecationSummary>";
1363 appendToResultWithXMLEscaping(DeprecationSummary);
1364 Result << "</DeprecationSummary>";
Fariborz Jahanian257e2e82012-09-28 22:35:49 +00001365 }
Fariborz Jahanian257e2e82012-09-28 22:35:49 +00001366 if (AA->getUnavailable())
Fariborz Jahanian8da68b82012-10-02 23:01:04 +00001367 Result << "<Unavailable/>";
Fariborz Jahanianfaab5612012-10-01 18:42:25 +00001368 Result << "</Availability>";
Fariborz Jahanian257e2e82012-09-28 22:35:49 +00001369 }
1370 }
Fariborz Jahanianfaab5612012-10-01 18:42:25 +00001371
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001372 {
1373 bool StartTagEmitted = false;
1374 for (unsigned i = 0, e = Parts.MiscBlocks.size(); i != e; ++i) {
1375 const Comment *C = Parts.MiscBlocks[i];
1376 if (FirstParagraphIsBrief && C == Parts.FirstParagraph)
1377 continue;
1378 if (!StartTagEmitted) {
1379 Result << "<Discussion>";
1380 StartTagEmitted = true;
1381 }
1382 visit(C);
1383 }
1384 if (StartTagEmitted)
1385 Result << "</Discussion>";
1386 }
1387
1388 Result << RootEndTag;
1389
1390 Result.flush();
1391}
1392
1393void CommentASTToXMLConverter::appendToResultWithXMLEscaping(StringRef S) {
1394 for (StringRef::iterator I = S.begin(), E = S.end(); I != E; ++I) {
1395 const char C = *I;
1396 switch (C) {
1397 case '&':
1398 Result << "&amp;";
1399 break;
1400 case '<':
1401 Result << "&lt;";
1402 break;
1403 case '>':
1404 Result << "&gt;";
1405 break;
1406 case '"':
1407 Result << "&quot;";
1408 break;
1409 case '\'':
1410 Result << "&apos;";
1411 break;
1412 default:
1413 Result << C;
1414 break;
1415 }
1416 }
1417}
1418
1419extern "C" {
1420
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +00001421CXString clang_FullComment_getAsXML(CXComment CXC) {
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001422 const FullComment *FC = getASTNodeAs<FullComment>(CXC);
1423 if (!FC)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +00001424 return cxstring::createNull();
Fariborz Jahanian88b95212012-12-18 23:02:59 +00001425 ASTContext &Context = FC->getDeclInfo()->CurrentDecl->getASTContext();
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +00001426 CXTranslationUnit TU = CXC.TranslationUnit;
Dmitri Gribenko5694feb2013-01-26 18:53:38 +00001427 SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager();
Dmitri Gribenko337ee242013-01-26 21:39:50 +00001428
1429 if (!TU->FormatContext) {
1430 TU->FormatContext = new SimpleFormatContext(Context.getLangOpts());
Fariborz Jahanian8a68da12012-12-19 00:35:23 +00001431 } else if ((TU->FormatInMemoryUniqueId % 1000) == 0) {
Fariborz Jahanian7c106832012-12-19 00:01:48 +00001432 // Delete after some number of iterators, so the buffers don't grow
1433 // too large.
Dmitri Gribenko337ee242013-01-26 21:39:50 +00001434 delete TU->FormatContext;
1435 TU->FormatContext = new SimpleFormatContext(Context.getLangOpts());
Fariborz Jahanian88b95212012-12-18 23:02:59 +00001436 }
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001437
1438 SmallString<1024> XML;
Fariborz Jahanian88b95212012-12-18 23:02:59 +00001439 CommentASTToXMLConverter Converter(FC, XML, getCommandTraits(CXC), SM,
Dmitri Gribenko337ee242013-01-26 21:39:50 +00001440 *TU->FormatContext,
1441 TU->FormatInMemoryUniqueId++);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001442 Converter.visit(FC);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +00001443 return cxstring::createDup(XML.str());
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001444}
1445
1446} // end extern "C"
1447