1 //===--- CommentToXML.cpp - Convert comments to XML representation --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Index/CommentToXML.h"
11 #include "SimpleFormatContext.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/AST/Attr.h"
14 #include "clang/AST/Comment.h"
15 #include "clang/AST/CommentVisitor.h"
16 #include "clang/Format/Format.h"
17 #include "clang/Index/USRGeneration.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/TinyPtrVector.h"
20 #include "llvm/Support/raw_ostream.h"
22 using namespace clang;
23 using namespace clang::comments;
24 using namespace clang::index;
28 /// This comparison will sort parameters with valid index by index, then vararg
29 /// parameters, and invalid (unresolved) parameters last.
30 class ParamCommandCommentCompareIndex {
32 bool operator()(const ParamCommandComment *LHS,
33 const ParamCommandComment *RHS) const {
34 unsigned LHSIndex = UINT_MAX;
35 unsigned RHSIndex = UINT_MAX;
37 if (LHS->isParamIndexValid()) {
38 if (LHS->isVarArgParam())
39 LHSIndex = UINT_MAX - 1;
41 LHSIndex = LHS->getParamIndex();
43 if (RHS->isParamIndexValid()) {
44 if (RHS->isVarArgParam())
45 RHSIndex = UINT_MAX - 1;
47 RHSIndex = RHS->getParamIndex();
49 return LHSIndex < RHSIndex;
53 /// This comparison will sort template parameters in the following order:
54 /// \li real template parameters (depth = 1) in index order;
55 /// \li all other names (depth > 1);
56 /// \li unresolved names.
57 class TParamCommandCommentComparePosition {
59 bool operator()(const TParamCommandComment *LHS,
60 const TParamCommandComment *RHS) const {
61 // Sort unresolved names last.
62 if (!LHS->isPositionValid())
64 if (!RHS->isPositionValid())
67 if (LHS->getDepth() > 1)
69 if (RHS->getDepth() > 1)
72 // Sort template parameters in index order.
73 if (LHS->getDepth() == 1 && RHS->getDepth() == 1)
74 return LHS->getIndex(0) < RHS->getIndex(0);
76 // Leave all other names in source order.
81 /// Separate parts of a FullComment.
82 struct FullCommentParts {
83 /// Take a full comment apart and initialize members accordingly.
84 FullCommentParts(const FullComment *C,
85 const CommandTraits &Traits);
87 const BlockContentComment *Brief;
88 const BlockContentComment *Headerfile;
89 const ParagraphComment *FirstParagraph;
90 SmallVector<const BlockCommandComment *, 4> Returns;
91 SmallVector<const ParamCommandComment *, 8> Params;
92 SmallVector<const TParamCommandComment *, 4> TParams;
93 llvm::TinyPtrVector<const BlockCommandComment *> Exceptions;
94 SmallVector<const BlockContentComment *, 8> MiscBlocks;
97 FullCommentParts::FullCommentParts(const FullComment *C,
98 const CommandTraits &Traits) :
99 Brief(nullptr), Headerfile(nullptr), FirstParagraph(nullptr) {
100 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
102 const Comment *Child = *I;
105 switch (Child->getCommentKind()) {
106 case Comment::NoCommentKind:
109 case Comment::ParagraphCommentKind: {
110 const ParagraphComment *PC = cast<ParagraphComment>(Child);
111 if (PC->isWhitespace())
116 MiscBlocks.push_back(PC);
120 case Comment::BlockCommandCommentKind: {
121 const BlockCommandComment *BCC = cast<BlockCommandComment>(Child);
122 const CommandInfo *Info = Traits.getCommandInfo(BCC->getCommandID());
123 if (!Brief && Info->IsBriefCommand) {
127 if (!Headerfile && Info->IsHeaderfileCommand) {
131 if (Info->IsReturnsCommand) {
132 Returns.push_back(BCC);
135 if (Info->IsThrowsCommand) {
136 Exceptions.push_back(BCC);
139 MiscBlocks.push_back(BCC);
143 case Comment::ParamCommandCommentKind: {
144 const ParamCommandComment *PCC = cast<ParamCommandComment>(Child);
145 if (!PCC->hasParamName())
148 if (!PCC->isDirectionExplicit() && !PCC->hasNonWhitespaceParagraph())
151 Params.push_back(PCC);
155 case Comment::TParamCommandCommentKind: {
156 const TParamCommandComment *TPCC = cast<TParamCommandComment>(Child);
157 if (!TPCC->hasParamName())
160 if (!TPCC->hasNonWhitespaceParagraph())
163 TParams.push_back(TPCC);
167 case Comment::VerbatimBlockCommentKind:
168 MiscBlocks.push_back(cast<BlockCommandComment>(Child));
171 case Comment::VerbatimLineCommentKind: {
172 const VerbatimLineComment *VLC = cast<VerbatimLineComment>(Child);
173 const CommandInfo *Info = Traits.getCommandInfo(VLC->getCommandID());
174 if (!Info->IsDeclarationCommand)
175 MiscBlocks.push_back(VLC);
179 case Comment::TextCommentKind:
180 case Comment::InlineCommandCommentKind:
181 case Comment::HTMLStartTagCommentKind:
182 case Comment::HTMLEndTagCommentKind:
183 case Comment::VerbatimBlockLineCommentKind:
184 case Comment::FullCommentKind:
185 llvm_unreachable("AST node of this kind can't be a child of "
190 // Sort params in order they are declared in the function prototype.
191 // Unresolved parameters are put at the end of the list in the same order
192 // they were seen in the comment.
193 std::stable_sort(Params.begin(), Params.end(),
194 ParamCommandCommentCompareIndex());
196 std::stable_sort(TParams.begin(), TParams.end(),
197 TParamCommandCommentComparePosition());
200 void printHTMLStartTagComment(const HTMLStartTagComment *C,
201 llvm::raw_svector_ostream &Result) {
202 Result << "<" << C->getTagName();
204 if (C->getNumAttrs() != 0) {
205 for (unsigned i = 0, e = C->getNumAttrs(); i != e; i++) {
207 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
209 if (!Attr.Value.empty())
210 Result << "=\"" << Attr.Value << "\"";
214 if (!C->isSelfClosing())
220 class CommentASTToHTMLConverter :
221 public ConstCommentVisitor<CommentASTToHTMLConverter> {
223 /// \param Str accumulator for HTML.
224 CommentASTToHTMLConverter(const FullComment *FC,
225 SmallVectorImpl<char> &Str,
226 const CommandTraits &Traits) :
227 FC(FC), Result(Str), Traits(Traits)
231 void visitTextComment(const TextComment *C);
232 void visitInlineCommandComment(const InlineCommandComment *C);
233 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
234 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
237 void visitParagraphComment(const ParagraphComment *C);
238 void visitBlockCommandComment(const BlockCommandComment *C);
239 void visitParamCommandComment(const ParamCommandComment *C);
240 void visitTParamCommandComment(const TParamCommandComment *C);
241 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
242 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
243 void visitVerbatimLineComment(const VerbatimLineComment *C);
245 void visitFullComment(const FullComment *C);
249 /// Convert a paragraph that is not a block by itself (an argument to some
251 void visitNonStandaloneParagraphComment(const ParagraphComment *C);
253 void appendToResultWithHTMLEscaping(StringRef S);
256 const FullComment *FC;
257 /// Output stream for HTML.
258 llvm::raw_svector_ostream Result;
260 const CommandTraits &Traits;
262 } // end unnamed namespace
264 void CommentASTToHTMLConverter::visitTextComment(const TextComment *C) {
265 appendToResultWithHTMLEscaping(C->getText());
268 void CommentASTToHTMLConverter::visitInlineCommandComment(
269 const InlineCommandComment *C) {
270 // Nothing to render if no arguments supplied.
271 if (C->getNumArgs() == 0)
274 // Nothing to render if argument is empty.
275 StringRef Arg0 = C->getArgText(0);
279 switch (C->getRenderKind()) {
280 case InlineCommandComment::RenderNormal:
281 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i) {
282 appendToResultWithHTMLEscaping(C->getArgText(i));
287 case InlineCommandComment::RenderBold:
288 assert(C->getNumArgs() == 1);
290 appendToResultWithHTMLEscaping(Arg0);
293 case InlineCommandComment::RenderMonospaced:
294 assert(C->getNumArgs() == 1);
296 appendToResultWithHTMLEscaping(Arg0);
299 case InlineCommandComment::RenderEmphasized:
300 assert(C->getNumArgs() == 1);
302 appendToResultWithHTMLEscaping(Arg0);
308 void CommentASTToHTMLConverter::visitHTMLStartTagComment(
309 const HTMLStartTagComment *C) {
310 printHTMLStartTagComment(C, Result);
313 void CommentASTToHTMLConverter::visitHTMLEndTagComment(
314 const HTMLEndTagComment *C) {
315 Result << "</" << C->getTagName() << ">";
318 void CommentASTToHTMLConverter::visitParagraphComment(
319 const ParagraphComment *C) {
320 if (C->isWhitespace())
324 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
331 void CommentASTToHTMLConverter::visitBlockCommandComment(
332 const BlockCommandComment *C) {
333 const CommandInfo *Info = Traits.getCommandInfo(C->getCommandID());
334 if (Info->IsBriefCommand) {
335 Result << "<p class=\"para-brief\">";
336 visitNonStandaloneParagraphComment(C->getParagraph());
340 if (Info->IsReturnsCommand) {
341 Result << "<p class=\"para-returns\">"
342 "<span class=\"word-returns\">Returns</span> ";
343 visitNonStandaloneParagraphComment(C->getParagraph());
347 // We don't know anything about this command. Just render the paragraph.
348 visit(C->getParagraph());
351 void CommentASTToHTMLConverter::visitParamCommandComment(
352 const ParamCommandComment *C) {
353 if (C->isParamIndexValid()) {
354 if (C->isVarArgParam()) {
355 Result << "<dt class=\"param-name-index-vararg\">";
356 appendToResultWithHTMLEscaping(C->getParamNameAsWritten());
358 Result << "<dt class=\"param-name-index-"
359 << C->getParamIndex()
361 appendToResultWithHTMLEscaping(C->getParamName(FC));
364 Result << "<dt class=\"param-name-index-invalid\">";
365 appendToResultWithHTMLEscaping(C->getParamNameAsWritten());
369 if (C->isParamIndexValid()) {
370 if (C->isVarArgParam())
371 Result << "<dd class=\"param-descr-index-vararg\">";
373 Result << "<dd class=\"param-descr-index-"
374 << C->getParamIndex()
377 Result << "<dd class=\"param-descr-index-invalid\">";
379 visitNonStandaloneParagraphComment(C->getParagraph());
383 void CommentASTToHTMLConverter::visitTParamCommandComment(
384 const TParamCommandComment *C) {
385 if (C->isPositionValid()) {
386 if (C->getDepth() == 1)
387 Result << "<dt class=\"tparam-name-index-"
391 Result << "<dt class=\"tparam-name-index-other\">";
392 appendToResultWithHTMLEscaping(C->getParamName(FC));
394 Result << "<dt class=\"tparam-name-index-invalid\">";
395 appendToResultWithHTMLEscaping(C->getParamNameAsWritten());
400 if (C->isPositionValid()) {
401 if (C->getDepth() == 1)
402 Result << "<dd class=\"tparam-descr-index-"
406 Result << "<dd class=\"tparam-descr-index-other\">";
408 Result << "<dd class=\"tparam-descr-index-invalid\">";
410 visitNonStandaloneParagraphComment(C->getParagraph());
414 void CommentASTToHTMLConverter::visitVerbatimBlockComment(
415 const VerbatimBlockComment *C) {
416 unsigned NumLines = C->getNumLines();
421 for (unsigned i = 0; i != NumLines; ++i) {
422 appendToResultWithHTMLEscaping(C->getText(i));
423 if (i + 1 != NumLines)
429 void CommentASTToHTMLConverter::visitVerbatimBlockLineComment(
430 const VerbatimBlockLineComment *C) {
431 llvm_unreachable("should not see this AST node");
434 void CommentASTToHTMLConverter::visitVerbatimLineComment(
435 const VerbatimLineComment *C) {
437 appendToResultWithHTMLEscaping(C->getText());
441 void CommentASTToHTMLConverter::visitFullComment(const FullComment *C) {
442 FullCommentParts Parts(C, Traits);
444 bool FirstParagraphIsBrief = false;
445 if (Parts.Headerfile)
446 visit(Parts.Headerfile);
449 else if (Parts.FirstParagraph) {
450 Result << "<p class=\"para-brief\">";
451 visitNonStandaloneParagraphComment(Parts.FirstParagraph);
453 FirstParagraphIsBrief = true;
456 for (unsigned i = 0, e = Parts.MiscBlocks.size(); i != e; ++i) {
457 const Comment *C = Parts.MiscBlocks[i];
458 if (FirstParagraphIsBrief && C == Parts.FirstParagraph)
463 if (Parts.TParams.size() != 0) {
465 for (unsigned i = 0, e = Parts.TParams.size(); i != e; ++i)
466 visit(Parts.TParams[i]);
470 if (Parts.Params.size() != 0) {
472 for (unsigned i = 0, e = Parts.Params.size(); i != e; ++i)
473 visit(Parts.Params[i]);
477 if (Parts.Returns.size() != 0) {
478 Result << "<div class=\"result-discussion\">";
479 for (unsigned i = 0, e = Parts.Returns.size(); i != e; ++i)
480 visit(Parts.Returns[i]);
486 void CommentASTToHTMLConverter::visitNonStandaloneParagraphComment(
487 const ParagraphComment *C) {
491 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
497 void CommentASTToHTMLConverter::appendToResultWithHTMLEscaping(StringRef S) {
498 for (StringRef::iterator I = S.begin(), E = S.end(); I != E; ++I) {
527 class CommentASTToXMLConverter :
528 public ConstCommentVisitor<CommentASTToXMLConverter> {
530 /// \param Str accumulator for XML.
531 CommentASTToXMLConverter(const FullComment *FC,
532 SmallVectorImpl<char> &Str,
533 const CommandTraits &Traits,
534 const SourceManager &SM,
535 SimpleFormatContext &SFC,
537 FC(FC), Result(Str), Traits(Traits), SM(SM),
538 FormatRewriterContext(SFC),
539 FormatInMemoryUniqueId(FUID) { }
542 void visitTextComment(const TextComment *C);
543 void visitInlineCommandComment(const InlineCommandComment *C);
544 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
545 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
548 void visitParagraphComment(const ParagraphComment *C);
550 void appendParagraphCommentWithKind(const ParagraphComment *C,
553 void visitBlockCommandComment(const BlockCommandComment *C);
554 void visitParamCommandComment(const ParamCommandComment *C);
555 void visitTParamCommandComment(const TParamCommandComment *C);
556 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
557 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
558 void visitVerbatimLineComment(const VerbatimLineComment *C);
560 void visitFullComment(const FullComment *C);
563 void appendToResultWithXMLEscaping(StringRef S);
564 void appendToResultWithCDATAEscaping(StringRef S);
566 void formatTextOfDeclaration(const DeclInfo *DI,
567 SmallString<128> &Declaration);
570 const FullComment *FC;
572 /// Output stream for XML.
573 llvm::raw_svector_ostream Result;
575 const CommandTraits &Traits;
576 const SourceManager &SM;
577 SimpleFormatContext &FormatRewriterContext;
578 unsigned FormatInMemoryUniqueId;
581 void getSourceTextOfDeclaration(const DeclInfo *ThisDecl,
582 SmallVectorImpl<char> &Str) {
583 ASTContext &Context = ThisDecl->CurrentDecl->getASTContext();
584 const LangOptions &LangOpts = Context.getLangOpts();
585 llvm::raw_svector_ostream OS(Str);
586 PrintingPolicy PPolicy(LangOpts);
587 PPolicy.PolishForDeclaration = true;
588 PPolicy.TerseOutput = true;
589 ThisDecl->CurrentDecl->print(OS, PPolicy,
590 /*Indentation*/0, /*PrintInstantiation*/false);
593 void CommentASTToXMLConverter::formatTextOfDeclaration(
594 const DeclInfo *DI, SmallString<128> &Declaration) {
595 // Formatting API expects null terminated input string.
596 StringRef StringDecl(Declaration.c_str(), Declaration.size());
598 // Formatter specific code.
599 // Form a unique in memory buffer name.
600 SmallString<128> filename;
601 filename += "xmldecl";
602 filename += llvm::utostr(FormatInMemoryUniqueId);
604 FileID ID = FormatRewriterContext.createInMemoryFile(filename, StringDecl);
605 SourceLocation Start = FormatRewriterContext.Sources.getLocForStartOfFile(ID)
606 .getLocWithOffset(0);
607 unsigned Length = Declaration.size();
609 tooling::Replacements Replace = reformat(
610 format::getLLVMStyle(), FormatRewriterContext.Sources, ID,
611 CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));
612 applyAllReplacements(Replace, FormatRewriterContext.Rewrite);
613 Declaration = FormatRewriterContext.getRewrittenText(ID);
616 } // end unnamed namespace
618 void CommentASTToXMLConverter::visitTextComment(const TextComment *C) {
619 appendToResultWithXMLEscaping(C->getText());
622 void CommentASTToXMLConverter::visitInlineCommandComment(
623 const InlineCommandComment *C) {
624 // Nothing to render if no arguments supplied.
625 if (C->getNumArgs() == 0)
628 // Nothing to render if argument is empty.
629 StringRef Arg0 = C->getArgText(0);
633 switch (C->getRenderKind()) {
634 case InlineCommandComment::RenderNormal:
635 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i) {
636 appendToResultWithXMLEscaping(C->getArgText(i));
640 case InlineCommandComment::RenderBold:
641 assert(C->getNumArgs() == 1);
643 appendToResultWithXMLEscaping(Arg0);
646 case InlineCommandComment::RenderMonospaced:
647 assert(C->getNumArgs() == 1);
648 Result << "<monospaced>";
649 appendToResultWithXMLEscaping(Arg0);
650 Result << "</monospaced>";
652 case InlineCommandComment::RenderEmphasized:
653 assert(C->getNumArgs() == 1);
654 Result << "<emphasized>";
655 appendToResultWithXMLEscaping(Arg0);
656 Result << "</emphasized>";
661 void CommentASTToXMLConverter::visitHTMLStartTagComment(
662 const HTMLStartTagComment *C) {
663 Result << "<rawHTML";
664 if (C->isMalformed())
665 Result << " isMalformed=\"1\"";
670 llvm::raw_svector_ostream TagOS(Tag);
671 printHTMLStartTagComment(C, TagOS);
673 appendToResultWithCDATAEscaping(Tag);
675 Result << "</rawHTML>";
679 CommentASTToXMLConverter::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
680 Result << "<rawHTML";
681 if (C->isMalformed())
682 Result << " isMalformed=\"1\"";
683 Result << "></" << C->getTagName() << "></rawHTML>";
687 CommentASTToXMLConverter::visitParagraphComment(const ParagraphComment *C) {
688 appendParagraphCommentWithKind(C, StringRef());
691 void CommentASTToXMLConverter::appendParagraphCommentWithKind(
692 const ParagraphComment *C,
693 StringRef ParagraphKind) {
694 if (C->isWhitespace())
697 if (ParagraphKind.empty())
700 Result << "<Para kind=\"" << ParagraphKind << "\">";
702 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
709 void CommentASTToXMLConverter::visitBlockCommandComment(
710 const BlockCommandComment *C) {
711 StringRef ParagraphKind;
713 switch (C->getCommandID()) {
714 case CommandTraits::KCI_attention:
715 case CommandTraits::KCI_author:
716 case CommandTraits::KCI_authors:
717 case CommandTraits::KCI_bug:
718 case CommandTraits::KCI_copyright:
719 case CommandTraits::KCI_date:
720 case CommandTraits::KCI_invariant:
721 case CommandTraits::KCI_note:
722 case CommandTraits::KCI_post:
723 case CommandTraits::KCI_pre:
724 case CommandTraits::KCI_remark:
725 case CommandTraits::KCI_remarks:
726 case CommandTraits::KCI_sa:
727 case CommandTraits::KCI_see:
728 case CommandTraits::KCI_since:
729 case CommandTraits::KCI_todo:
730 case CommandTraits::KCI_version:
731 case CommandTraits::KCI_warning:
732 ParagraphKind = C->getCommandName(Traits);
737 appendParagraphCommentWithKind(C->getParagraph(), ParagraphKind);
740 void CommentASTToXMLConverter::visitParamCommandComment(
741 const ParamCommandComment *C) {
742 Result << "<Parameter><Name>";
743 appendToResultWithXMLEscaping(C->isParamIndexValid()
744 ? C->getParamName(FC)
745 : C->getParamNameAsWritten());
748 if (C->isParamIndexValid()) {
749 if (C->isVarArgParam())
750 Result << "<IsVarArg />";
752 Result << "<Index>" << C->getParamIndex() << "</Index>";
755 Result << "<Direction isExplicit=\"" << C->isDirectionExplicit() << "\">";
756 switch (C->getDirection()) {
757 case ParamCommandComment::In:
760 case ParamCommandComment::Out:
763 case ParamCommandComment::InOut:
767 Result << "</Direction><Discussion>";
768 visit(C->getParagraph());
769 Result << "</Discussion></Parameter>";
772 void CommentASTToXMLConverter::visitTParamCommandComment(
773 const TParamCommandComment *C) {
774 Result << "<Parameter><Name>";
775 appendToResultWithXMLEscaping(C->isPositionValid() ? C->getParamName(FC)
776 : C->getParamNameAsWritten());
779 if (C->isPositionValid() && C->getDepth() == 1) {
780 Result << "<Index>" << C->getIndex(0) << "</Index>";
783 Result << "<Discussion>";
784 visit(C->getParagraph());
785 Result << "</Discussion></Parameter>";
788 void CommentASTToXMLConverter::visitVerbatimBlockComment(
789 const VerbatimBlockComment *C) {
790 unsigned NumLines = C->getNumLines();
794 switch (C->getCommandID()) {
795 case CommandTraits::KCI_code:
796 Result << "<Verbatim xml:space=\"preserve\" kind=\"code\">";
799 Result << "<Verbatim xml:space=\"preserve\" kind=\"verbatim\">";
802 for (unsigned i = 0; i != NumLines; ++i) {
803 appendToResultWithXMLEscaping(C->getText(i));
804 if (i + 1 != NumLines)
807 Result << "</Verbatim>";
810 void CommentASTToXMLConverter::visitVerbatimBlockLineComment(
811 const VerbatimBlockLineComment *C) {
812 llvm_unreachable("should not see this AST node");
815 void CommentASTToXMLConverter::visitVerbatimLineComment(
816 const VerbatimLineComment *C) {
817 Result << "<Verbatim xml:space=\"preserve\" kind=\"verbatim\">";
818 appendToResultWithXMLEscaping(C->getText());
819 Result << "</Verbatim>";
822 void CommentASTToXMLConverter::visitFullComment(const FullComment *C) {
823 FullCommentParts Parts(C, Traits);
825 const DeclInfo *DI = C->getDeclInfo();
826 StringRef RootEndTag;
828 switch (DI->getKind()) {
829 case DeclInfo::OtherKind:
830 RootEndTag = "</Other>";
833 case DeclInfo::FunctionKind:
834 RootEndTag = "</Function>";
835 Result << "<Function";
836 switch (DI->TemplateKind) {
837 case DeclInfo::NotTemplate:
839 case DeclInfo::Template:
840 Result << " templateKind=\"template\"";
842 case DeclInfo::TemplateSpecialization:
843 Result << " templateKind=\"specialization\"";
845 case DeclInfo::TemplatePartialSpecialization:
846 llvm_unreachable("partial specializations of functions "
847 "are not allowed in C++");
849 if (DI->IsInstanceMethod)
850 Result << " isInstanceMethod=\"1\"";
851 if (DI->IsClassMethod)
852 Result << " isClassMethod=\"1\"";
854 case DeclInfo::ClassKind:
855 RootEndTag = "</Class>";
857 switch (DI->TemplateKind) {
858 case DeclInfo::NotTemplate:
860 case DeclInfo::Template:
861 Result << " templateKind=\"template\"";
863 case DeclInfo::TemplateSpecialization:
864 Result << " templateKind=\"specialization\"";
866 case DeclInfo::TemplatePartialSpecialization:
867 Result << " templateKind=\"partialSpecialization\"";
871 case DeclInfo::VariableKind:
872 RootEndTag = "</Variable>";
873 Result << "<Variable";
875 case DeclInfo::NamespaceKind:
876 RootEndTag = "</Namespace>";
877 Result << "<Namespace";
879 case DeclInfo::TypedefKind:
880 RootEndTag = "</Typedef>";
881 Result << "<Typedef";
883 case DeclInfo::EnumKind:
884 RootEndTag = "</Enum>";
890 // Print line and column number.
891 SourceLocation Loc = DI->CurrentDecl->getLocation();
892 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
893 FileID FID = LocInfo.first;
894 unsigned FileOffset = LocInfo.second;
897 if (const FileEntry *FE = SM.getFileEntryForID(FID)) {
898 Result << " file=\"";
899 appendToResultWithXMLEscaping(FE->getName());
902 Result << " line=\"" << SM.getLineNumber(FID, FileOffset)
903 << "\" column=\"" << SM.getColumnNumber(FID, FileOffset)
908 // Finish the root tag.
911 bool FoundName = false;
912 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DI->CommentDecl)) {
913 if (DeclarationName DeclName = ND->getDeclName()) {
915 std::string Name = DeclName.getAsString();
916 appendToResultWithXMLEscaping(Name);
922 Result << "<Name><anonymous></Name>";
926 SmallString<128> USR;
927 generateUSRForDecl(DI->CommentDecl, USR);
930 appendToResultWithXMLEscaping(USR);
935 // No DeclInfo -- just emit some root tag and name tag.
936 RootEndTag = "</Other>";
937 Result << "<Other><Name>unknown</Name>";
940 if (Parts.Headerfile) {
941 Result << "<Headerfile>";
942 visit(Parts.Headerfile);
943 Result << "</Headerfile>";
947 // Pretty-print the declaration.
948 Result << "<Declaration>";
949 SmallString<128> Declaration;
950 getSourceTextOfDeclaration(DI, Declaration);
951 formatTextOfDeclaration(DI, Declaration);
952 appendToResultWithXMLEscaping(Declaration);
953 Result << "</Declaration>";
956 bool FirstParagraphIsBrief = false;
958 Result << "<Abstract>";
960 Result << "</Abstract>";
961 } else if (Parts.FirstParagraph) {
962 Result << "<Abstract>";
963 visit(Parts.FirstParagraph);
964 Result << "</Abstract>";
965 FirstParagraphIsBrief = true;
968 if (Parts.TParams.size() != 0) {
969 Result << "<TemplateParameters>";
970 for (unsigned i = 0, e = Parts.TParams.size(); i != e; ++i)
971 visit(Parts.TParams[i]);
972 Result << "</TemplateParameters>";
975 if (Parts.Params.size() != 0) {
976 Result << "<Parameters>";
977 for (unsigned i = 0, e = Parts.Params.size(); i != e; ++i)
978 visit(Parts.Params[i]);
979 Result << "</Parameters>";
982 if (Parts.Exceptions.size() != 0) {
983 Result << "<Exceptions>";
984 for (unsigned i = 0, e = Parts.Exceptions.size(); i != e; ++i)
985 visit(Parts.Exceptions[i]);
986 Result << "</Exceptions>";
989 if (Parts.Returns.size() != 0) {
990 Result << "<ResultDiscussion>";
991 for (unsigned i = 0, e = Parts.Returns.size(); i != e; ++i)
992 visit(Parts.Returns[i]);
993 Result << "</ResultDiscussion>";
996 if (DI->CommentDecl->hasAttrs()) {
997 const AttrVec &Attrs = DI->CommentDecl->getAttrs();
998 for (unsigned i = 0, e = Attrs.size(); i != e; i++) {
999 const AvailabilityAttr *AA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1001 if (const DeprecatedAttr *DA = dyn_cast<DeprecatedAttr>(Attrs[i])) {
1002 if (DA->getMessage().empty())
1003 Result << "<Deprecated/>";
1005 Result << "<Deprecated>";
1006 appendToResultWithXMLEscaping(DA->getMessage());
1007 Result << "</Deprecated>";
1010 else if (const UnavailableAttr *UA = dyn_cast<UnavailableAttr>(Attrs[i])) {
1011 if (UA->getMessage().empty())
1012 Result << "<Unavailable/>";
1014 Result << "<Unavailable>";
1015 appendToResultWithXMLEscaping(UA->getMessage());
1016 Result << "</Unavailable>";
1022 // 'availability' attribute.
1023 Result << "<Availability";
1024 StringRef Distribution;
1025 if (AA->getPlatform()) {
1026 Distribution = AvailabilityAttr::getPrettyPlatformName(
1027 AA->getPlatform()->getName());
1028 if (Distribution.empty())
1029 Distribution = AA->getPlatform()->getName();
1031 Result << " distribution=\"" << Distribution << "\">";
1032 VersionTuple IntroducedInVersion = AA->getIntroduced();
1033 if (!IntroducedInVersion.empty()) {
1034 Result << "<IntroducedInVersion>"
1035 << IntroducedInVersion.getAsString()
1036 << "</IntroducedInVersion>";
1038 VersionTuple DeprecatedInVersion = AA->getDeprecated();
1039 if (!DeprecatedInVersion.empty()) {
1040 Result << "<DeprecatedInVersion>"
1041 << DeprecatedInVersion.getAsString()
1042 << "</DeprecatedInVersion>";
1044 VersionTuple RemovedAfterVersion = AA->getObsoleted();
1045 if (!RemovedAfterVersion.empty()) {
1046 Result << "<RemovedAfterVersion>"
1047 << RemovedAfterVersion.getAsString()
1048 << "</RemovedAfterVersion>";
1050 StringRef DeprecationSummary = AA->getMessage();
1051 if (!DeprecationSummary.empty()) {
1052 Result << "<DeprecationSummary>";
1053 appendToResultWithXMLEscaping(DeprecationSummary);
1054 Result << "</DeprecationSummary>";
1056 if (AA->getUnavailable())
1057 Result << "<Unavailable/>";
1058 Result << "</Availability>";
1063 bool StartTagEmitted = false;
1064 for (unsigned i = 0, e = Parts.MiscBlocks.size(); i != e; ++i) {
1065 const Comment *C = Parts.MiscBlocks[i];
1066 if (FirstParagraphIsBrief && C == Parts.FirstParagraph)
1068 if (!StartTagEmitted) {
1069 Result << "<Discussion>";
1070 StartTagEmitted = true;
1074 if (StartTagEmitted)
1075 Result << "</Discussion>";
1078 Result << RootEndTag;
1081 void CommentASTToXMLConverter::appendToResultWithXMLEscaping(StringRef S) {
1082 for (StringRef::iterator I = S.begin(), E = S.end(); I != E; ++I) {
1107 void CommentASTToXMLConverter::appendToResultWithCDATAEscaping(StringRef S) {
1111 Result << "<![CDATA[";
1112 while (!S.empty()) {
1113 size_t Pos = S.find("]]>");
1115 Result << "]]]]><![CDATA[>";
1116 S = S.drop_front(3);
1119 if (Pos == StringRef::npos)
1122 Result << S.substr(0, Pos);
1124 S = S.drop_front(Pos);
1129 CommentToXMLConverter::CommentToXMLConverter() : FormatInMemoryUniqueId(0) {}
1130 CommentToXMLConverter::~CommentToXMLConverter() {}
1132 void CommentToXMLConverter::convertCommentToHTML(const FullComment *FC,
1133 SmallVectorImpl<char> &HTML,
1134 const ASTContext &Context) {
1135 CommentASTToHTMLConverter Converter(FC, HTML,
1136 Context.getCommentCommandTraits());
1137 Converter.visit(FC);
1140 void CommentToXMLConverter::convertHTMLTagNodeToText(
1141 const comments::HTMLTagComment *HTC, SmallVectorImpl<char> &Text,
1142 const ASTContext &Context) {
1143 CommentASTToHTMLConverter Converter(nullptr, Text,
1144 Context.getCommentCommandTraits());
1145 Converter.visit(HTC);
1148 void CommentToXMLConverter::convertCommentToXML(const FullComment *FC,
1149 SmallVectorImpl<char> &XML,
1150 const ASTContext &Context) {
1151 if (!FormatContext || (FormatInMemoryUniqueId % 1000) == 0) {
1152 // Create a new format context, or re-create it after some number of
1153 // iterations, so the buffers don't grow too large.
1154 FormatContext.reset(new SimpleFormatContext(Context.getLangOpts()));
1157 CommentASTToXMLConverter Converter(FC, XML, Context.getCommentCommandTraits(),
1158 Context.getSourceManager(), *FormatContext,
1159 FormatInMemoryUniqueId++);
1160 Converter.visit(FC);