Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 1 | //===- 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 Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 15 | #include "CXComment.h" |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 16 | #include "CXCursor.h" |
Chandler Carruth | f59edb9 | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 17 | #include "CXString.h" |
Dmitri Gribenko | d1db125 | 2012-08-09 17:33:20 +0000 | [diff] [blame] | 18 | #include "clang/AST/CommentCommandTraits.h" |
Chandler Carruth | f59edb9 | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 19 | #include "clang/AST/CommentVisitor.h" |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Chandler Carruth | f59edb9 | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 21 | #include "clang/AST/PrettyPrinter.h" |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringSwitch.h" |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Dmitri Gribenko | 221a6d7 | 2012-07-30 17:49:32 +0000 | [diff] [blame] | 25 | #include <climits> |
| 26 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | using namespace clang::cxstring; |
| 29 | using namespace clang::comments; |
| 30 | using namespace clang::cxcomment; |
| 31 | |
| 32 | extern "C" { |
| 33 | |
| 34 | enum CXCommentKind clang_Comment_getKind(CXComment CXC) { |
| 35 | const Comment *C = getASTNode(CXC); |
| 36 | if (!C) |
| 37 | return CXComment_Null; |
| 38 | |
| 39 | switch (C->getCommentKind()) { |
| 40 | case Comment::NoCommentKind: |
| 41 | return CXComment_Null; |
| 42 | |
| 43 | case Comment::TextCommentKind: |
| 44 | return CXComment_Text; |
| 45 | |
| 46 | case Comment::InlineCommandCommentKind: |
| 47 | return CXComment_InlineCommand; |
| 48 | |
| 49 | case Comment::HTMLStartTagCommentKind: |
| 50 | return CXComment_HTMLStartTag; |
| 51 | |
| 52 | case Comment::HTMLEndTagCommentKind: |
| 53 | return CXComment_HTMLEndTag; |
| 54 | |
| 55 | case Comment::ParagraphCommentKind: |
| 56 | return CXComment_Paragraph; |
| 57 | |
| 58 | case Comment::BlockCommandCommentKind: |
| 59 | return CXComment_BlockCommand; |
| 60 | |
| 61 | case Comment::ParamCommandCommentKind: |
| 62 | return CXComment_ParamCommand; |
| 63 | |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 64 | case Comment::TParamCommandCommentKind: |
| 65 | return CXComment_TParamCommand; |
| 66 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 67 | case Comment::VerbatimBlockCommentKind: |
| 68 | return CXComment_VerbatimBlockCommand; |
| 69 | |
| 70 | case Comment::VerbatimBlockLineCommentKind: |
| 71 | return CXComment_VerbatimBlockLine; |
| 72 | |
| 73 | case Comment::VerbatimLineCommentKind: |
| 74 | return CXComment_VerbatimLine; |
| 75 | |
| 76 | case Comment::FullCommentKind: |
| 77 | return CXComment_FullComment; |
| 78 | } |
| 79 | llvm_unreachable("unknown CommentKind"); |
| 80 | } |
| 81 | |
| 82 | unsigned clang_Comment_getNumChildren(CXComment CXC) { |
| 83 | const Comment *C = getASTNode(CXC); |
| 84 | if (!C) |
| 85 | return 0; |
| 86 | |
| 87 | return C->child_count(); |
| 88 | } |
| 89 | |
| 90 | CXComment clang_Comment_getChild(CXComment CXC, unsigned ChildIdx) { |
| 91 | const Comment *C = getASTNode(CXC); |
| 92 | if (!C || ChildIdx >= C->child_count()) |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 93 | return createCXComment(NULL, NULL); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 94 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 95 | return createCXComment(*(C->child_begin() + ChildIdx), CXC.TranslationUnit); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | unsigned clang_Comment_isWhitespace(CXComment CXC) { |
| 99 | const Comment *C = getASTNode(CXC); |
| 100 | if (!C) |
| 101 | return false; |
| 102 | |
| 103 | if (const TextComment *TC = dyn_cast<TextComment>(C)) |
| 104 | return TC->isWhitespace(); |
| 105 | |
| 106 | if (const ParagraphComment *PC = dyn_cast<ParagraphComment>(C)) |
| 107 | return PC->isWhitespace(); |
| 108 | |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | unsigned clang_InlineContentComment_hasTrailingNewline(CXComment CXC) { |
| 113 | const InlineContentComment *ICC = getASTNodeAs<InlineContentComment>(CXC); |
| 114 | if (!ICC) |
| 115 | return false; |
| 116 | |
| 117 | return ICC->hasTrailingNewline(); |
| 118 | } |
| 119 | |
| 120 | CXString clang_TextComment_getText(CXComment CXC) { |
| 121 | const TextComment *TC = getASTNodeAs<TextComment>(CXC); |
| 122 | if (!TC) |
| 123 | return createCXString((const char *) 0); |
| 124 | |
| 125 | return createCXString(TC->getText(), /*DupString=*/ false); |
| 126 | } |
| 127 | |
| 128 | CXString clang_InlineCommandComment_getCommandName(CXComment CXC) { |
| 129 | const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC); |
| 130 | if (!ICC) |
| 131 | return createCXString((const char *) 0); |
| 132 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 133 | const CommandTraits &Traits = getCommandTraits(CXC); |
| 134 | return createCXString(ICC->getCommandName(Traits), /*DupString=*/ false); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 137 | enum CXCommentInlineCommandRenderKind |
| 138 | clang_InlineCommandComment_getRenderKind(CXComment CXC) { |
| 139 | const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC); |
| 140 | if (!ICC) |
| 141 | return CXCommentInlineCommandRenderKind_Normal; |
| 142 | |
| 143 | switch (ICC->getRenderKind()) { |
| 144 | case InlineCommandComment::RenderNormal: |
| 145 | return CXCommentInlineCommandRenderKind_Normal; |
| 146 | |
| 147 | case InlineCommandComment::RenderBold: |
| 148 | return CXCommentInlineCommandRenderKind_Bold; |
| 149 | |
| 150 | case InlineCommandComment::RenderMonospaced: |
| 151 | return CXCommentInlineCommandRenderKind_Monospaced; |
| 152 | |
| 153 | case InlineCommandComment::RenderEmphasized: |
| 154 | return CXCommentInlineCommandRenderKind_Emphasized; |
| 155 | } |
| 156 | llvm_unreachable("unknown InlineCommandComment::RenderKind"); |
| 157 | } |
| 158 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 159 | unsigned clang_InlineCommandComment_getNumArgs(CXComment CXC) { |
| 160 | const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC); |
| 161 | if (!ICC) |
| 162 | return 0; |
| 163 | |
| 164 | return ICC->getNumArgs(); |
| 165 | } |
| 166 | |
| 167 | CXString clang_InlineCommandComment_getArgText(CXComment CXC, |
| 168 | unsigned ArgIdx) { |
| 169 | const InlineCommandComment *ICC = getASTNodeAs<InlineCommandComment>(CXC); |
| 170 | if (!ICC || ArgIdx >= ICC->getNumArgs()) |
| 171 | return createCXString((const char *) 0); |
| 172 | |
| 173 | return createCXString(ICC->getArgText(ArgIdx), /*DupString=*/ false); |
| 174 | } |
| 175 | |
| 176 | CXString clang_HTMLTagComment_getTagName(CXComment CXC) { |
| 177 | const HTMLTagComment *HTC = getASTNodeAs<HTMLTagComment>(CXC); |
| 178 | if (!HTC) |
| 179 | return createCXString((const char *) 0); |
| 180 | |
| 181 | return createCXString(HTC->getTagName(), /*DupString=*/ false); |
| 182 | } |
| 183 | |
| 184 | unsigned clang_HTMLStartTagComment_isSelfClosing(CXComment CXC) { |
| 185 | const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC); |
| 186 | if (!HST) |
| 187 | return false; |
| 188 | |
| 189 | return HST->isSelfClosing(); |
| 190 | } |
| 191 | |
| 192 | unsigned clang_HTMLStartTag_getNumAttrs(CXComment CXC) { |
| 193 | const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC); |
| 194 | if (!HST) |
| 195 | return 0; |
| 196 | |
| 197 | return HST->getNumAttrs(); |
| 198 | } |
| 199 | |
| 200 | CXString clang_HTMLStartTag_getAttrName(CXComment CXC, unsigned AttrIdx) { |
| 201 | const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC); |
| 202 | if (!HST || AttrIdx >= HST->getNumAttrs()) |
| 203 | return createCXString((const char *) 0); |
| 204 | |
| 205 | return createCXString(HST->getAttr(AttrIdx).Name, /*DupString=*/ false); |
| 206 | } |
| 207 | |
| 208 | CXString clang_HTMLStartTag_getAttrValue(CXComment CXC, unsigned AttrIdx) { |
| 209 | const HTMLStartTagComment *HST = getASTNodeAs<HTMLStartTagComment>(CXC); |
| 210 | if (!HST || AttrIdx >= HST->getNumAttrs()) |
| 211 | return createCXString((const char *) 0); |
| 212 | |
| 213 | return createCXString(HST->getAttr(AttrIdx).Value, /*DupString=*/ false); |
| 214 | } |
| 215 | |
| 216 | CXString clang_BlockCommandComment_getCommandName(CXComment CXC) { |
| 217 | const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC); |
| 218 | if (!BCC) |
| 219 | return createCXString((const char *) 0); |
| 220 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 221 | const CommandTraits &Traits = getCommandTraits(CXC); |
| 222 | return createCXString(BCC->getCommandName(Traits), /*DupString=*/ false); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | unsigned clang_BlockCommandComment_getNumArgs(CXComment CXC) { |
| 226 | const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC); |
| 227 | if (!BCC) |
| 228 | return 0; |
| 229 | |
| 230 | return BCC->getNumArgs(); |
| 231 | } |
| 232 | |
| 233 | CXString clang_BlockCommandComment_getArgText(CXComment CXC, |
| 234 | unsigned ArgIdx) { |
| 235 | const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC); |
| 236 | if (!BCC || ArgIdx >= BCC->getNumArgs()) |
| 237 | return createCXString((const char *) 0); |
| 238 | |
| 239 | return createCXString(BCC->getArgText(ArgIdx), /*DupString=*/ false); |
| 240 | } |
| 241 | |
| 242 | CXComment clang_BlockCommandComment_getParagraph(CXComment CXC) { |
| 243 | const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC); |
| 244 | if (!BCC) |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 245 | return createCXComment(NULL, NULL); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 246 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 247 | return createCXComment(BCC->getParagraph(), CXC.TranslationUnit); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | CXString clang_ParamCommandComment_getParamName(CXComment CXC) { |
| 251 | const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC); |
Dmitri Gribenko | 1f8c529 | 2012-07-23 19:41:49 +0000 | [diff] [blame] | 252 | if (!PCC || !PCC->hasParamName()) |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 253 | return createCXString((const char *) 0); |
| 254 | |
Fariborz Jahanian | 262e60c | 2012-10-18 21:42:42 +0000 | [diff] [blame] | 255 | return createCXString(PCC->getParamNameAsWritten(), /*DupString=*/ false); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | unsigned clang_ParamCommandComment_isParamIndexValid(CXComment CXC) { |
| 259 | const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC); |
| 260 | if (!PCC) |
| 261 | return false; |
| 262 | |
| 263 | return PCC->isParamIndexValid(); |
| 264 | } |
| 265 | |
| 266 | unsigned clang_ParamCommandComment_getParamIndex(CXComment CXC) { |
| 267 | const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC); |
Dmitri Gribenko | b740316 | 2012-07-30 17:38:19 +0000 | [diff] [blame] | 268 | if (!PCC || !PCC->isParamIndexValid()) |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 269 | return ParamCommandComment::InvalidParamIndex; |
| 270 | |
| 271 | return PCC->getParamIndex(); |
| 272 | } |
| 273 | |
| 274 | unsigned clang_ParamCommandComment_isDirectionExplicit(CXComment CXC) { |
| 275 | const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC); |
| 276 | if (!PCC) |
| 277 | return false; |
| 278 | |
| 279 | return PCC->isDirectionExplicit(); |
| 280 | } |
| 281 | |
| 282 | enum CXCommentParamPassDirection clang_ParamCommandComment_getDirection( |
| 283 | CXComment CXC) { |
| 284 | const ParamCommandComment *PCC = getASTNodeAs<ParamCommandComment>(CXC); |
| 285 | if (!PCC) |
| 286 | return CXCommentParamPassDirection_In; |
| 287 | |
| 288 | switch (PCC->getDirection()) { |
| 289 | case ParamCommandComment::In: |
| 290 | return CXCommentParamPassDirection_In; |
| 291 | |
| 292 | case ParamCommandComment::Out: |
| 293 | return CXCommentParamPassDirection_Out; |
| 294 | |
| 295 | case ParamCommandComment::InOut: |
| 296 | return CXCommentParamPassDirection_InOut; |
| 297 | } |
| 298 | llvm_unreachable("unknown ParamCommandComment::PassDirection"); |
| 299 | } |
| 300 | |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 301 | CXString clang_TParamCommandComment_getParamName(CXComment CXC) { |
| 302 | const TParamCommandComment *TPCC = getASTNodeAs<TParamCommandComment>(CXC); |
| 303 | if (!TPCC || !TPCC->hasParamName()) |
| 304 | return createCXString((const char *) 0); |
| 305 | |
Fariborz Jahanian | 262e60c | 2012-10-18 21:42:42 +0000 | [diff] [blame] | 306 | return createCXString(TPCC->getParamNameAsWritten(), /*DupString=*/ false); |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | unsigned clang_TParamCommandComment_isParamPositionValid(CXComment CXC) { |
| 310 | const TParamCommandComment *TPCC = getASTNodeAs<TParamCommandComment>(CXC); |
| 311 | if (!TPCC) |
| 312 | return false; |
| 313 | |
| 314 | return TPCC->isPositionValid(); |
| 315 | } |
| 316 | |
| 317 | unsigned clang_TParamCommandComment_getDepth(CXComment CXC) { |
| 318 | const TParamCommandComment *TPCC = getASTNodeAs<TParamCommandComment>(CXC); |
| 319 | if (!TPCC || !TPCC->isPositionValid()) |
| 320 | return 0; |
| 321 | |
| 322 | return TPCC->getDepth(); |
| 323 | } |
| 324 | |
| 325 | unsigned clang_TParamCommandComment_getIndex(CXComment CXC, unsigned Depth) { |
| 326 | const TParamCommandComment *TPCC = getASTNodeAs<TParamCommandComment>(CXC); |
| 327 | if (!TPCC || !TPCC->isPositionValid() || Depth >= TPCC->getDepth()) |
| 328 | return 0; |
| 329 | |
| 330 | return TPCC->getIndex(Depth); |
| 331 | } |
| 332 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 333 | CXString clang_VerbatimBlockLineComment_getText(CXComment CXC) { |
| 334 | const VerbatimBlockLineComment *VBL = |
| 335 | getASTNodeAs<VerbatimBlockLineComment>(CXC); |
| 336 | if (!VBL) |
| 337 | return createCXString((const char *) 0); |
| 338 | |
| 339 | return createCXString(VBL->getText(), /*DupString=*/ false); |
| 340 | } |
| 341 | |
| 342 | CXString clang_VerbatimLineComment_getText(CXComment CXC) { |
| 343 | const VerbatimLineComment *VLC = getASTNodeAs<VerbatimLineComment>(CXC); |
| 344 | if (!VLC) |
| 345 | return createCXString((const char *) 0); |
| 346 | |
| 347 | return createCXString(VLC->getText(), /*DupString=*/ false); |
| 348 | } |
| 349 | |
| 350 | } // end extern "C" |
| 351 | |
| 352 | //===----------------------------------------------------------------------===// |
| 353 | // Helpers for converting comment AST to HTML. |
| 354 | //===----------------------------------------------------------------------===// |
| 355 | |
| 356 | namespace { |
| 357 | |
Dmitri Gribenko | e5db09c | 2012-07-30 19:47:34 +0000 | [diff] [blame] | 358 | /// This comparison will sort parameters with valid index by index and |
| 359 | /// invalid (unresolved) parameters last. |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 360 | class ParamCommandCommentCompareIndex { |
| 361 | public: |
| 362 | bool operator()(const ParamCommandComment *LHS, |
| 363 | const ParamCommandComment *RHS) const { |
Dmitri Gribenko | b740316 | 2012-07-30 17:38:19 +0000 | [diff] [blame] | 364 | unsigned LHSIndex = UINT_MAX; |
| 365 | unsigned RHSIndex = UINT_MAX; |
| 366 | if (LHS->isParamIndexValid()) |
| 367 | LHSIndex = LHS->getParamIndex(); |
| 368 | if (RHS->isParamIndexValid()) |
| 369 | RHSIndex = RHS->getParamIndex(); |
| 370 | |
| 371 | return LHSIndex < RHSIndex; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 372 | } |
| 373 | }; |
| 374 | |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 375 | /// This comparison will sort template parameters in the following order: |
| 376 | /// \li real template parameters (depth = 1) in index order; |
| 377 | /// \li all other names (depth > 1); |
| 378 | /// \li unresolved names. |
| 379 | class TParamCommandCommentComparePosition { |
| 380 | public: |
| 381 | bool operator()(const TParamCommandComment *LHS, |
| 382 | const TParamCommandComment *RHS) const { |
| 383 | // Sort unresolved names last. |
| 384 | if (!LHS->isPositionValid()) |
| 385 | return false; |
| 386 | if (!RHS->isPositionValid()) |
| 387 | return true; |
| 388 | |
| 389 | if (LHS->getDepth() > 1) |
| 390 | return false; |
| 391 | if (RHS->getDepth() > 1) |
| 392 | return true; |
| 393 | |
| 394 | // Sort template parameters in index order. |
| 395 | if (LHS->getDepth() == 1 && RHS->getDepth() == 1) |
| 396 | return LHS->getIndex(0) < RHS->getIndex(0); |
| 397 | |
| 398 | // Leave all other names in source order. |
| 399 | return true; |
| 400 | } |
| 401 | }; |
| 402 | |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 403 | /// Separate parts of a FullComment. |
| 404 | struct FullCommentParts { |
| 405 | /// Take a full comment apart and initialize members accordingly. |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 406 | FullCommentParts(const FullComment *C, |
| 407 | const CommandTraits &Traits); |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 408 | |
| 409 | const BlockContentComment *Brief; |
| 410 | const ParagraphComment *FirstParagraph; |
| 411 | const BlockCommandComment *Returns; |
| 412 | SmallVector<const ParamCommandComment *, 8> Params; |
| 413 | SmallVector<const TParamCommandComment *, 4> TParams; |
| 414 | SmallVector<const BlockContentComment *, 8> MiscBlocks; |
| 415 | }; |
| 416 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 417 | FullCommentParts::FullCommentParts(const FullComment *C, |
| 418 | const CommandTraits &Traits) : |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 419 | Brief(NULL), FirstParagraph(NULL), Returns(NULL) { |
| 420 | for (Comment::child_iterator I = C->child_begin(), E = C->child_end(); |
| 421 | I != E; ++I) { |
| 422 | const Comment *Child = *I; |
| 423 | if (!Child) |
| 424 | continue; |
| 425 | switch (Child->getCommentKind()) { |
| 426 | case Comment::NoCommentKind: |
| 427 | continue; |
| 428 | |
| 429 | case Comment::ParagraphCommentKind: { |
| 430 | const ParagraphComment *PC = cast<ParagraphComment>(Child); |
| 431 | if (PC->isWhitespace()) |
| 432 | break; |
| 433 | if (!FirstParagraph) |
| 434 | FirstParagraph = PC; |
| 435 | |
| 436 | MiscBlocks.push_back(PC); |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | case Comment::BlockCommandCommentKind: { |
| 441 | const BlockCommandComment *BCC = cast<BlockCommandComment>(Child); |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 442 | const CommandInfo *Info = Traits.getCommandInfo(BCC->getCommandID()); |
| 443 | if (!Brief && Info->IsBriefCommand) { |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 444 | Brief = BCC; |
| 445 | break; |
| 446 | } |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 447 | if (!Returns && Info->IsReturnsCommand) { |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 448 | Returns = BCC; |
| 449 | break; |
| 450 | } |
| 451 | MiscBlocks.push_back(BCC); |
| 452 | break; |
| 453 | } |
| 454 | |
| 455 | case Comment::ParamCommandCommentKind: { |
| 456 | const ParamCommandComment *PCC = cast<ParamCommandComment>(Child); |
| 457 | if (!PCC->hasParamName()) |
| 458 | break; |
| 459 | |
| 460 | if (!PCC->isDirectionExplicit() && !PCC->hasNonWhitespaceParagraph()) |
| 461 | break; |
| 462 | |
| 463 | Params.push_back(PCC); |
| 464 | break; |
| 465 | } |
| 466 | |
| 467 | case Comment::TParamCommandCommentKind: { |
| 468 | const TParamCommandComment *TPCC = cast<TParamCommandComment>(Child); |
| 469 | if (!TPCC->hasParamName()) |
| 470 | break; |
| 471 | |
| 472 | if (!TPCC->hasNonWhitespaceParagraph()) |
| 473 | break; |
| 474 | |
| 475 | TParams.push_back(TPCC); |
| 476 | break; |
| 477 | } |
| 478 | |
| 479 | case Comment::VerbatimBlockCommentKind: |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 480 | MiscBlocks.push_back(cast<BlockCommandComment>(Child)); |
| 481 | break; |
| 482 | |
Dmitri Gribenko | 62290ae | 2012-08-09 18:20:29 +0000 | [diff] [blame] | 483 | case Comment::VerbatimLineCommentKind: { |
| 484 | const VerbatimLineComment *VLC = cast<VerbatimLineComment>(Child); |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 485 | const CommandInfo *Info = Traits.getCommandInfo(VLC->getCommandID()); |
| 486 | if (!Info->IsDeclarationCommand) |
Dmitri Gribenko | 62290ae | 2012-08-09 18:20:29 +0000 | [diff] [blame] | 487 | MiscBlocks.push_back(VLC); |
| 488 | break; |
| 489 | } |
| 490 | |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 491 | case Comment::TextCommentKind: |
| 492 | case Comment::InlineCommandCommentKind: |
| 493 | case Comment::HTMLStartTagCommentKind: |
| 494 | case Comment::HTMLEndTagCommentKind: |
| 495 | case Comment::VerbatimBlockLineCommentKind: |
| 496 | case Comment::FullCommentKind: |
| 497 | llvm_unreachable("AST node of this kind can't be a child of " |
| 498 | "a FullComment"); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | // Sort params in order they are declared in the function prototype. |
| 503 | // Unresolved parameters are put at the end of the list in the same order |
| 504 | // they were seen in the comment. |
| 505 | std::stable_sort(Params.begin(), Params.end(), |
| 506 | ParamCommandCommentCompareIndex()); |
| 507 | |
| 508 | std::stable_sort(TParams.begin(), TParams.end(), |
| 509 | TParamCommandCommentComparePosition()); |
| 510 | } |
| 511 | |
| 512 | void PrintHTMLStartTagComment(const HTMLStartTagComment *C, |
| 513 | llvm::raw_svector_ostream &Result) { |
| 514 | Result << "<" << C->getTagName(); |
| 515 | |
| 516 | if (C->getNumAttrs() != 0) { |
| 517 | for (unsigned i = 0, e = C->getNumAttrs(); i != e; i++) { |
| 518 | Result << " "; |
| 519 | const HTMLStartTagComment::Attribute &Attr = C->getAttr(i); |
| 520 | Result << Attr.Name; |
| 521 | if (!Attr.Value.empty()) |
| 522 | Result << "=\"" << Attr.Value << "\""; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (!C->isSelfClosing()) |
| 527 | Result << ">"; |
| 528 | else |
| 529 | Result << "/>"; |
| 530 | } |
| 531 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 532 | class CommentASTToHTMLConverter : |
| 533 | public ConstCommentVisitor<CommentASTToHTMLConverter> { |
| 534 | public: |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 535 | /// \param Str accumulator for HTML. |
Dmitri Gribenko | 8cfabf2 | 2012-10-19 16:51:38 +0000 | [diff] [blame] | 536 | CommentASTToHTMLConverter(const FullComment *FC, |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 537 | SmallVectorImpl<char> &Str, |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 538 | const CommandTraits &Traits) : |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 539 | FC(FC), Result(Str), Traits(Traits) |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 540 | { } |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 541 | |
| 542 | // Inline content. |
| 543 | void visitTextComment(const TextComment *C); |
| 544 | void visitInlineCommandComment(const InlineCommandComment *C); |
| 545 | void visitHTMLStartTagComment(const HTMLStartTagComment *C); |
| 546 | void visitHTMLEndTagComment(const HTMLEndTagComment *C); |
| 547 | |
| 548 | // Block content. |
| 549 | void visitParagraphComment(const ParagraphComment *C); |
| 550 | void visitBlockCommandComment(const BlockCommandComment *C); |
| 551 | void visitParamCommandComment(const ParamCommandComment *C); |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 552 | void visitTParamCommandComment(const TParamCommandComment *C); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 553 | void visitVerbatimBlockComment(const VerbatimBlockComment *C); |
| 554 | void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C); |
| 555 | void visitVerbatimLineComment(const VerbatimLineComment *C); |
| 556 | |
| 557 | void visitFullComment(const FullComment *C); |
| 558 | |
| 559 | // Helpers. |
| 560 | |
| 561 | /// Convert a paragraph that is not a block by itself (an argument to some |
| 562 | /// command). |
| 563 | void visitNonStandaloneParagraphComment(const ParagraphComment *C); |
| 564 | |
| 565 | void appendToResultWithHTMLEscaping(StringRef S); |
| 566 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 567 | private: |
Dmitri Gribenko | 8cfabf2 | 2012-10-19 16:51:38 +0000 | [diff] [blame] | 568 | const FullComment *FC; |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 569 | /// Output stream for HTML. |
| 570 | llvm::raw_svector_ostream Result; |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 571 | |
| 572 | const CommandTraits &Traits; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 573 | }; |
| 574 | } // end unnamed namespace |
| 575 | |
| 576 | void CommentASTToHTMLConverter::visitTextComment(const TextComment *C) { |
| 577 | appendToResultWithHTMLEscaping(C->getText()); |
| 578 | } |
| 579 | |
| 580 | void CommentASTToHTMLConverter::visitInlineCommandComment( |
| 581 | const InlineCommandComment *C) { |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 582 | // Nothing to render if no arguments supplied. |
| 583 | if (C->getNumArgs() == 0) |
| 584 | return; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 585 | |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 586 | // Nothing to render if argument is empty. |
| 587 | StringRef Arg0 = C->getArgText(0); |
| 588 | if (Arg0.empty()) |
| 589 | return; |
| 590 | |
| 591 | switch (C->getRenderKind()) { |
| 592 | case InlineCommandComment::RenderNormal: |
Dmitri Gribenko | 59500fe | 2012-08-01 00:21:12 +0000 | [diff] [blame] | 593 | for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i) { |
| 594 | appendToResultWithHTMLEscaping(C->getArgText(i)); |
| 595 | Result << " "; |
| 596 | } |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 597 | return; |
| 598 | |
| 599 | case InlineCommandComment::RenderBold: |
| 600 | assert(C->getNumArgs() == 1); |
Dmitri Gribenko | 59500fe | 2012-08-01 00:21:12 +0000 | [diff] [blame] | 601 | Result << "<b>"; |
| 602 | appendToResultWithHTMLEscaping(Arg0); |
| 603 | Result << "</b>"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 604 | return; |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 605 | case InlineCommandComment::RenderMonospaced: |
| 606 | assert(C->getNumArgs() == 1); |
Dmitri Gribenko | 59500fe | 2012-08-01 00:21:12 +0000 | [diff] [blame] | 607 | Result << "<tt>"; |
| 608 | appendToResultWithHTMLEscaping(Arg0); |
| 609 | Result<< "</tt>"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 610 | return; |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 611 | case InlineCommandComment::RenderEmphasized: |
| 612 | assert(C->getNumArgs() == 1); |
Dmitri Gribenko | 59500fe | 2012-08-01 00:21:12 +0000 | [diff] [blame] | 613 | Result << "<em>"; |
| 614 | appendToResultWithHTMLEscaping(Arg0); |
| 615 | Result << "</em>"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 616 | return; |
| 617 | } |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | void CommentASTToHTMLConverter::visitHTMLStartTagComment( |
| 621 | const HTMLStartTagComment *C) { |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 622 | PrintHTMLStartTagComment(C, Result); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | void CommentASTToHTMLConverter::visitHTMLEndTagComment( |
| 626 | const HTMLEndTagComment *C) { |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 627 | Result << "</" << C->getTagName() << ">"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void CommentASTToHTMLConverter::visitParagraphComment( |
| 631 | const ParagraphComment *C) { |
| 632 | if (C->isWhitespace()) |
| 633 | return; |
| 634 | |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 635 | Result << "<p>"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 636 | for (Comment::child_iterator I = C->child_begin(), E = C->child_end(); |
| 637 | I != E; ++I) { |
| 638 | visit(*I); |
| 639 | } |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 640 | Result << "</p>"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | void CommentASTToHTMLConverter::visitBlockCommandComment( |
| 644 | const BlockCommandComment *C) { |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 645 | const CommandInfo *Info = Traits.getCommandInfo(C->getCommandID()); |
| 646 | if (Info->IsBriefCommand) { |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 647 | Result << "<p class=\"para-brief\">"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 648 | visitNonStandaloneParagraphComment(C->getParagraph()); |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 649 | Result << "</p>"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 650 | return; |
| 651 | } |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 652 | if (Info->IsReturnsCommand) { |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 653 | Result << "<p class=\"para-returns\">" |
| 654 | "<span class=\"word-returns\">Returns</span> "; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 655 | visitNonStandaloneParagraphComment(C->getParagraph()); |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 656 | Result << "</p>"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 657 | return; |
| 658 | } |
| 659 | // We don't know anything about this command. Just render the paragraph. |
| 660 | visit(C->getParagraph()); |
| 661 | } |
| 662 | |
| 663 | void CommentASTToHTMLConverter::visitParamCommandComment( |
| 664 | const ParamCommandComment *C) { |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 665 | if (C->isParamIndexValid()) { |
| 666 | Result << "<dt class=\"param-name-index-" |
| 667 | << C->getParamIndex() |
| 668 | << "\">"; |
Fariborz Jahanian | 262e60c | 2012-10-18 21:42:42 +0000 | [diff] [blame] | 669 | appendToResultWithHTMLEscaping(C->getParamName(FC)); |
| 670 | } else { |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 671 | Result << "<dt class=\"param-name-index-invalid\">"; |
Fariborz Jahanian | 262e60c | 2012-10-18 21:42:42 +0000 | [diff] [blame] | 672 | appendToResultWithHTMLEscaping(C->getParamNameAsWritten()); |
| 673 | } |
Dmitri Gribenko | 59500fe | 2012-08-01 00:21:12 +0000 | [diff] [blame] | 674 | Result << "</dt>"; |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 675 | |
| 676 | if (C->isParamIndexValid()) { |
| 677 | Result << "<dd class=\"param-descr-index-" |
| 678 | << C->getParamIndex() |
| 679 | << "\">"; |
| 680 | } else |
| 681 | Result << "<dd class=\"param-descr-index-invalid\">"; |
| 682 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 683 | visitNonStandaloneParagraphComment(C->getParagraph()); |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 684 | Result << "</dd>"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 687 | void CommentASTToHTMLConverter::visitTParamCommandComment( |
| 688 | const TParamCommandComment *C) { |
| 689 | if (C->isPositionValid()) { |
| 690 | if (C->getDepth() == 1) |
Dmitri Gribenko | 6a42552 | 2012-08-01 23:47:30 +0000 | [diff] [blame] | 691 | Result << "<dt class=\"tparam-name-index-" |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 692 | << C->getIndex(0) |
| 693 | << "\">"; |
| 694 | else |
Dmitri Gribenko | 6a42552 | 2012-08-01 23:47:30 +0000 | [diff] [blame] | 695 | Result << "<dt class=\"tparam-name-index-other\">"; |
Fariborz Jahanian | 262e60c | 2012-10-18 21:42:42 +0000 | [diff] [blame] | 696 | appendToResultWithHTMLEscaping(C->getParamName(FC)); |
| 697 | } else { |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 698 | Result << "<dt class=\"tparam-name-index-invalid\">"; |
Fariborz Jahanian | 262e60c | 2012-10-18 21:42:42 +0000 | [diff] [blame] | 699 | appendToResultWithHTMLEscaping(C->getParamNameAsWritten()); |
| 700 | } |
| 701 | |
Dmitri Gribenko | 59500fe | 2012-08-01 00:21:12 +0000 | [diff] [blame] | 702 | Result << "</dt>"; |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 703 | |
| 704 | if (C->isPositionValid()) { |
| 705 | if (C->getDepth() == 1) |
| 706 | Result << "<dd class=\"tparam-descr-index-" |
| 707 | << C->getIndex(0) |
| 708 | << "\">"; |
| 709 | else |
| 710 | Result << "<dd class=\"tparam-descr-index-other\">"; |
| 711 | } else |
| 712 | Result << "<dd class=\"tparam-descr-index-invalid\">"; |
| 713 | |
| 714 | visitNonStandaloneParagraphComment(C->getParagraph()); |
| 715 | Result << "</dd>"; |
| 716 | } |
| 717 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 718 | void CommentASTToHTMLConverter::visitVerbatimBlockComment( |
| 719 | const VerbatimBlockComment *C) { |
| 720 | unsigned NumLines = C->getNumLines(); |
| 721 | if (NumLines == 0) |
| 722 | return; |
| 723 | |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 724 | Result << "<pre>"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 725 | for (unsigned i = 0; i != NumLines; ++i) { |
| 726 | appendToResultWithHTMLEscaping(C->getText(i)); |
| 727 | if (i + 1 != NumLines) |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 728 | Result << '\n'; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 729 | } |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 730 | Result << "</pre>"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | void CommentASTToHTMLConverter::visitVerbatimBlockLineComment( |
| 734 | const VerbatimBlockLineComment *C) { |
| 735 | llvm_unreachable("should not see this AST node"); |
| 736 | } |
| 737 | |
| 738 | void CommentASTToHTMLConverter::visitVerbatimLineComment( |
| 739 | const VerbatimLineComment *C) { |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 740 | Result << "<pre>"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 741 | appendToResultWithHTMLEscaping(C->getText()); |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 742 | Result << "</pre>"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | void CommentASTToHTMLConverter::visitFullComment(const FullComment *C) { |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 746 | FullCommentParts Parts(C, Traits); |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 747 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 748 | bool FirstParagraphIsBrief = false; |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 749 | if (Parts.Brief) |
| 750 | visit(Parts.Brief); |
| 751 | else if (Parts.FirstParagraph) { |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 752 | Result << "<p class=\"para-brief\">"; |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 753 | visitNonStandaloneParagraphComment(Parts.FirstParagraph); |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 754 | Result << "</p>"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 755 | FirstParagraphIsBrief = true; |
| 756 | } |
| 757 | |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 758 | for (unsigned i = 0, e = Parts.MiscBlocks.size(); i != e; ++i) { |
| 759 | const Comment *C = Parts.MiscBlocks[i]; |
| 760 | if (FirstParagraphIsBrief && C == Parts.FirstParagraph) |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 761 | continue; |
| 762 | visit(C); |
| 763 | } |
| 764 | |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 765 | if (Parts.TParams.size() != 0) { |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 766 | Result << "<dl>"; |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 767 | for (unsigned i = 0, e = Parts.TParams.size(); i != e; ++i) |
| 768 | visit(Parts.TParams[i]); |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 769 | Result << "</dl>"; |
| 770 | } |
| 771 | |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 772 | if (Parts.Params.size() != 0) { |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 773 | Result << "<dl>"; |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 774 | for (unsigned i = 0, e = Parts.Params.size(); i != e; ++i) |
| 775 | visit(Parts.Params[i]); |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 776 | Result << "</dl>"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Dmitri Gribenko | 2ff84b5 | 2012-08-01 22:48:16 +0000 | [diff] [blame] | 779 | if (Parts.Returns) |
| 780 | visit(Parts.Returns); |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 781 | |
| 782 | Result.flush(); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | void CommentASTToHTMLConverter::visitNonStandaloneParagraphComment( |
| 786 | const ParagraphComment *C) { |
| 787 | if (!C) |
| 788 | return; |
| 789 | |
| 790 | for (Comment::child_iterator I = C->child_begin(), E = C->child_end(); |
| 791 | I != E; ++I) { |
| 792 | visit(*I); |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | void CommentASTToHTMLConverter::appendToResultWithHTMLEscaping(StringRef S) { |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 797 | for (StringRef::iterator I = S.begin(), E = S.end(); I != E; ++I) { |
| 798 | const char C = *I; |
| 799 | switch (C) { |
| 800 | case '&': |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 801 | Result << "&"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 802 | break; |
| 803 | case '<': |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 804 | Result << "<"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 805 | break; |
| 806 | case '>': |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 807 | Result << ">"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 808 | break; |
| 809 | case '"': |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 810 | Result << """; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 811 | break; |
| 812 | case '\'': |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 813 | Result << "'"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 814 | break; |
| 815 | case '/': |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 816 | Result << "/"; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 817 | break; |
| 818 | default: |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 819 | Result << C; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 820 | break; |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | extern "C" { |
| 826 | |
| 827 | CXString clang_HTMLTagComment_getAsString(CXComment CXC) { |
| 828 | const HTMLTagComment *HTC = getASTNodeAs<HTMLTagComment>(CXC); |
| 829 | if (!HTC) |
| 830 | return createCXString((const char *) 0); |
| 831 | |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 832 | SmallString<128> HTML; |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 833 | CommentASTToHTMLConverter Converter(0, HTML, getCommandTraits(CXC)); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 834 | Converter.visit(HTC); |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 835 | return createCXString(HTML.str(), /* DupString = */ true); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | CXString clang_FullComment_getAsHTML(CXComment CXC) { |
| 839 | const FullComment *FC = getASTNodeAs<FullComment>(CXC); |
| 840 | if (!FC) |
| 841 | return createCXString((const char *) 0); |
| 842 | |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 843 | SmallString<1024> HTML; |
Dmitri Gribenko | 8cfabf2 | 2012-10-19 16:51:38 +0000 | [diff] [blame] | 844 | CommentASTToHTMLConverter Converter(FC, HTML, getCommandTraits(CXC)); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 845 | Converter.visit(FC); |
Dmitri Gribenko | 3e63d33 | 2012-07-21 01:47:43 +0000 | [diff] [blame] | 846 | return createCXString(HTML.str(), /* DupString = */ true); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | } // end extern "C" |
| 850 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 851 | namespace { |
| 852 | class CommentASTToXMLConverter : |
| 853 | public ConstCommentVisitor<CommentASTToXMLConverter> { |
| 854 | public: |
| 855 | /// \param Str accumulator for XML. |
Dmitri Gribenko | 8cfabf2 | 2012-10-19 16:51:38 +0000 | [diff] [blame] | 856 | CommentASTToXMLConverter(const FullComment *FC, |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 857 | SmallVectorImpl<char> &Str, |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 858 | const CommandTraits &Traits, |
| 859 | const SourceManager &SM) : |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 860 | FC(FC), Result(Str), Traits(Traits), SM(SM) { } |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 861 | |
| 862 | // Inline content. |
| 863 | void visitTextComment(const TextComment *C); |
| 864 | void visitInlineCommandComment(const InlineCommandComment *C); |
| 865 | void visitHTMLStartTagComment(const HTMLStartTagComment *C); |
| 866 | void visitHTMLEndTagComment(const HTMLEndTagComment *C); |
| 867 | |
| 868 | // Block content. |
| 869 | void visitParagraphComment(const ParagraphComment *C); |
| 870 | void visitBlockCommandComment(const BlockCommandComment *C); |
| 871 | void visitParamCommandComment(const ParamCommandComment *C); |
| 872 | void visitTParamCommandComment(const TParamCommandComment *C); |
| 873 | void visitVerbatimBlockComment(const VerbatimBlockComment *C); |
| 874 | void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C); |
| 875 | void visitVerbatimLineComment(const VerbatimLineComment *C); |
| 876 | |
| 877 | void visitFullComment(const FullComment *C); |
| 878 | |
| 879 | // Helpers. |
| 880 | void appendToResultWithXMLEscaping(StringRef S); |
| 881 | |
| 882 | private: |
Dmitri Gribenko | 8cfabf2 | 2012-10-19 16:51:38 +0000 | [diff] [blame] | 883 | const FullComment *FC; |
| 884 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 885 | /// Output stream for XML. |
| 886 | llvm::raw_svector_ostream Result; |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 887 | |
| 888 | const CommandTraits &Traits; |
| 889 | const SourceManager &SM; |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 890 | }; |
Dmitri Gribenko | 7c98499 | 2012-10-25 18:28:26 +0000 | [diff] [blame] | 891 | |
| 892 | void getSourceTextOfDeclaration(const DeclInfo *ThisDecl, |
| 893 | SmallVectorImpl<char> &Str) { |
| 894 | ASTContext &Context = ThisDecl->CurrentDecl->getASTContext(); |
| 895 | const LangOptions &LangOpts = Context.getLangOpts(); |
| 896 | llvm::raw_svector_ostream OS(Str); |
| 897 | PrintingPolicy PPolicy(LangOpts); |
| 898 | PPolicy.SuppressAttributes = true; |
| 899 | PPolicy.TerseOutput = true; |
| 900 | ThisDecl->CurrentDecl->print(OS, PPolicy, |
| 901 | /*Indentation*/0, /*PrintInstantiation*/true); |
| 902 | } |
| 903 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 904 | } // end unnamed namespace |
| 905 | |
| 906 | void CommentASTToXMLConverter::visitTextComment(const TextComment *C) { |
| 907 | appendToResultWithXMLEscaping(C->getText()); |
| 908 | } |
| 909 | |
| 910 | void CommentASTToXMLConverter::visitInlineCommandComment(const InlineCommandComment *C) { |
| 911 | // Nothing to render if no arguments supplied. |
| 912 | if (C->getNumArgs() == 0) |
| 913 | return; |
| 914 | |
| 915 | // Nothing to render if argument is empty. |
| 916 | StringRef Arg0 = C->getArgText(0); |
| 917 | if (Arg0.empty()) |
| 918 | return; |
| 919 | |
| 920 | switch (C->getRenderKind()) { |
| 921 | case InlineCommandComment::RenderNormal: |
| 922 | for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i) { |
| 923 | appendToResultWithXMLEscaping(C->getArgText(i)); |
| 924 | Result << " "; |
| 925 | } |
| 926 | return; |
| 927 | case InlineCommandComment::RenderBold: |
| 928 | assert(C->getNumArgs() == 1); |
| 929 | Result << "<bold>"; |
| 930 | appendToResultWithXMLEscaping(Arg0); |
| 931 | Result << "</bold>"; |
| 932 | return; |
| 933 | case InlineCommandComment::RenderMonospaced: |
| 934 | assert(C->getNumArgs() == 1); |
| 935 | Result << "<monospaced>"; |
| 936 | appendToResultWithXMLEscaping(Arg0); |
| 937 | Result << "</monospaced>"; |
| 938 | return; |
| 939 | case InlineCommandComment::RenderEmphasized: |
| 940 | assert(C->getNumArgs() == 1); |
| 941 | Result << "<emphasized>"; |
| 942 | appendToResultWithXMLEscaping(Arg0); |
| 943 | Result << "</emphasized>"; |
| 944 | return; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | void CommentASTToXMLConverter::visitHTMLStartTagComment(const HTMLStartTagComment *C) { |
| 949 | Result << "<rawHTML><![CDATA["; |
| 950 | PrintHTMLStartTagComment(C, Result); |
| 951 | Result << "]]></rawHTML>"; |
| 952 | } |
| 953 | |
| 954 | void CommentASTToXMLConverter::visitHTMLEndTagComment(const HTMLEndTagComment *C) { |
| 955 | Result << "<rawHTML></" << C->getTagName() << "></rawHTML>"; |
| 956 | } |
| 957 | |
| 958 | void CommentASTToXMLConverter::visitParagraphComment(const ParagraphComment *C) { |
| 959 | if (C->isWhitespace()) |
| 960 | return; |
| 961 | |
| 962 | Result << "<Para>"; |
| 963 | for (Comment::child_iterator I = C->child_begin(), E = C->child_end(); |
| 964 | I != E; ++I) { |
| 965 | visit(*I); |
| 966 | } |
| 967 | Result << "</Para>"; |
| 968 | } |
| 969 | |
| 970 | void CommentASTToXMLConverter::visitBlockCommandComment(const BlockCommandComment *C) { |
| 971 | visit(C->getParagraph()); |
| 972 | } |
| 973 | |
| 974 | void CommentASTToXMLConverter::visitParamCommandComment(const ParamCommandComment *C) { |
| 975 | Result << "<Parameter><Name>"; |
Fariborz Jahanian | 262e60c | 2012-10-18 21:42:42 +0000 | [diff] [blame] | 976 | appendToResultWithXMLEscaping(C->isParamIndexValid() ? C->getParamName(FC) |
| 977 | : C->getParamNameAsWritten()); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 978 | Result << "</Name>"; |
| 979 | |
| 980 | if (C->isParamIndexValid()) |
| 981 | Result << "<Index>" << C->getParamIndex() << "</Index>"; |
| 982 | |
| 983 | Result << "<Direction isExplicit=\"" << C->isDirectionExplicit() << "\">"; |
| 984 | switch (C->getDirection()) { |
| 985 | case ParamCommandComment::In: |
| 986 | Result << "in"; |
| 987 | break; |
| 988 | case ParamCommandComment::Out: |
| 989 | Result << "out"; |
| 990 | break; |
| 991 | case ParamCommandComment::InOut: |
| 992 | Result << "in,out"; |
| 993 | break; |
| 994 | } |
| 995 | Result << "</Direction><Discussion>"; |
| 996 | visit(C->getParagraph()); |
| 997 | Result << "</Discussion></Parameter>"; |
| 998 | } |
| 999 | |
| 1000 | void CommentASTToXMLConverter::visitTParamCommandComment( |
| 1001 | const TParamCommandComment *C) { |
| 1002 | Result << "<Parameter><Name>"; |
Fariborz Jahanian | 262e60c | 2012-10-18 21:42:42 +0000 | [diff] [blame] | 1003 | appendToResultWithXMLEscaping(C->isPositionValid() ? C->getParamName(FC) |
| 1004 | : C->getParamNameAsWritten()); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1005 | Result << "</Name>"; |
| 1006 | |
| 1007 | if (C->isPositionValid() && C->getDepth() == 1) { |
| 1008 | Result << "<Index>" << C->getIndex(0) << "</Index>"; |
| 1009 | } |
| 1010 | |
| 1011 | Result << "<Discussion>"; |
| 1012 | visit(C->getParagraph()); |
| 1013 | Result << "</Discussion></Parameter>"; |
| 1014 | } |
| 1015 | |
| 1016 | void CommentASTToXMLConverter::visitVerbatimBlockComment( |
| 1017 | const VerbatimBlockComment *C) { |
| 1018 | unsigned NumLines = C->getNumLines(); |
| 1019 | if (NumLines == 0) |
| 1020 | return; |
| 1021 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 1022 | Result << llvm::StringSwitch<const char *>(C->getCommandName(Traits)) |
Dmitri Gribenko | 6cd4420 | 2012-08-08 22:10:24 +0000 | [diff] [blame] | 1023 | .Case("code", "<Verbatim xml:space=\"preserve\" kind=\"code\">") |
| 1024 | .Default("<Verbatim xml:space=\"preserve\" kind=\"verbatim\">"); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1025 | for (unsigned i = 0; i != NumLines; ++i) { |
| 1026 | appendToResultWithXMLEscaping(C->getText(i)); |
| 1027 | if (i + 1 != NumLines) |
| 1028 | Result << '\n'; |
| 1029 | } |
| 1030 | Result << "</Verbatim>"; |
| 1031 | } |
| 1032 | |
| 1033 | void CommentASTToXMLConverter::visitVerbatimBlockLineComment( |
| 1034 | const VerbatimBlockLineComment *C) { |
| 1035 | llvm_unreachable("should not see this AST node"); |
| 1036 | } |
| 1037 | |
| 1038 | void CommentASTToXMLConverter::visitVerbatimLineComment( |
| 1039 | const VerbatimLineComment *C) { |
Dmitri Gribenko | 6cd4420 | 2012-08-08 22:10:24 +0000 | [diff] [blame] | 1040 | Result << "<Verbatim xml:space=\"preserve\" kind=\"verbatim\">"; |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1041 | appendToResultWithXMLEscaping(C->getText()); |
| 1042 | Result << "</Verbatim>"; |
| 1043 | } |
| 1044 | |
| 1045 | void CommentASTToXMLConverter::visitFullComment(const FullComment *C) { |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 1046 | FullCommentParts Parts(C, Traits); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1047 | |
| 1048 | const DeclInfo *DI = C->getDeclInfo(); |
| 1049 | StringRef RootEndTag; |
| 1050 | if (DI) { |
| 1051 | switch (DI->getKind()) { |
| 1052 | case DeclInfo::OtherKind: |
| 1053 | RootEndTag = "</Other>"; |
| 1054 | Result << "<Other"; |
| 1055 | break; |
| 1056 | case DeclInfo::FunctionKind: |
| 1057 | RootEndTag = "</Function>"; |
| 1058 | Result << "<Function"; |
| 1059 | switch (DI->TemplateKind) { |
| 1060 | case DeclInfo::NotTemplate: |
| 1061 | break; |
| 1062 | case DeclInfo::Template: |
| 1063 | Result << " templateKind=\"template\""; |
| 1064 | break; |
| 1065 | case DeclInfo::TemplateSpecialization: |
| 1066 | Result << " templateKind=\"specialization\""; |
| 1067 | break; |
| 1068 | case DeclInfo::TemplatePartialSpecialization: |
| 1069 | llvm_unreachable("partial specializations of functions " |
| 1070 | "are not allowed in C++"); |
| 1071 | } |
| 1072 | if (DI->IsInstanceMethod) |
| 1073 | Result << " isInstanceMethod=\"1\""; |
| 1074 | if (DI->IsClassMethod) |
| 1075 | Result << " isClassMethod=\"1\""; |
| 1076 | break; |
| 1077 | case DeclInfo::ClassKind: |
| 1078 | RootEndTag = "</Class>"; |
| 1079 | Result << "<Class"; |
| 1080 | switch (DI->TemplateKind) { |
| 1081 | case DeclInfo::NotTemplate: |
| 1082 | break; |
| 1083 | case DeclInfo::Template: |
| 1084 | Result << " templateKind=\"template\""; |
| 1085 | break; |
| 1086 | case DeclInfo::TemplateSpecialization: |
| 1087 | Result << " templateKind=\"specialization\""; |
| 1088 | break; |
| 1089 | case DeclInfo::TemplatePartialSpecialization: |
| 1090 | Result << " templateKind=\"partialSpecialization\""; |
| 1091 | break; |
| 1092 | } |
| 1093 | break; |
| 1094 | case DeclInfo::VariableKind: |
| 1095 | RootEndTag = "</Variable>"; |
| 1096 | Result << "<Variable"; |
| 1097 | break; |
| 1098 | case DeclInfo::NamespaceKind: |
| 1099 | RootEndTag = "</Namespace>"; |
| 1100 | Result << "<Namespace"; |
| 1101 | break; |
| 1102 | case DeclInfo::TypedefKind: |
| 1103 | RootEndTag = "</Typedef>"; |
| 1104 | Result << "<Typedef"; |
| 1105 | break; |
Dmitri Gribenko | cff339a | 2012-08-07 18:59:04 +0000 | [diff] [blame] | 1106 | case DeclInfo::EnumKind: |
| 1107 | RootEndTag = "</Enum>"; |
| 1108 | Result << "<Enum"; |
| 1109 | break; |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | { |
| 1113 | // Print line and column number. |
Fariborz Jahanian | 1bfb00d | 2012-10-17 21:58:03 +0000 | [diff] [blame] | 1114 | SourceLocation Loc = DI->CurrentDecl->getLocation(); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1115 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
| 1116 | FileID FID = LocInfo.first; |
| 1117 | unsigned FileOffset = LocInfo.second; |
| 1118 | |
| 1119 | if (!FID.isInvalid()) { |
| 1120 | if (const FileEntry *FE = SM.getFileEntryForID(FID)) { |
| 1121 | Result << " file=\""; |
| 1122 | appendToResultWithXMLEscaping(FE->getName()); |
| 1123 | Result << "\""; |
| 1124 | } |
| 1125 | Result << " line=\"" << SM.getLineNumber(FID, FileOffset) |
| 1126 | << "\" column=\"" << SM.getColumnNumber(FID, FileOffset) |
| 1127 | << "\""; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | // Finish the root tag. |
| 1132 | Result << ">"; |
| 1133 | |
| 1134 | bool FoundName = false; |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 1135 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(DI->CommentDecl)) { |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1136 | if (DeclarationName DeclName = ND->getDeclName()) { |
| 1137 | Result << "<Name>"; |
| 1138 | std::string Name = DeclName.getAsString(); |
| 1139 | appendToResultWithXMLEscaping(Name); |
| 1140 | FoundName = true; |
| 1141 | Result << "</Name>"; |
| 1142 | } |
| 1143 | } |
| 1144 | if (!FoundName) |
| 1145 | Result << "<Name><anonymous></Name>"; |
| 1146 | |
| 1147 | { |
| 1148 | // Print USR. |
| 1149 | SmallString<128> USR; |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 1150 | cxcursor::getDeclCursorUSR(DI->CommentDecl, USR); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1151 | if (!USR.empty()) { |
| 1152 | Result << "<USR>"; |
| 1153 | appendToResultWithXMLEscaping(USR); |
| 1154 | Result << "</USR>"; |
| 1155 | } |
| 1156 | } |
| 1157 | } else { |
| 1158 | // No DeclInfo -- just emit some root tag and name tag. |
| 1159 | RootEndTag = "</Other>"; |
| 1160 | Result << "<Other><Name>unknown</Name>"; |
| 1161 | } |
| 1162 | |
Dmitri Gribenko | 7c98499 | 2012-10-25 18:28:26 +0000 | [diff] [blame] | 1163 | { |
| 1164 | // Pretty-print the declaration. |
| 1165 | Result << "<Declaration>"; |
| 1166 | SmallString<128> Declaration; |
| 1167 | getSourceTextOfDeclaration(DI, Declaration); |
| 1168 | appendToResultWithXMLEscaping(Declaration); |
| 1169 | Result << "</Declaration>"; |
| 1170 | } |
| 1171 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1172 | bool FirstParagraphIsBrief = false; |
| 1173 | if (Parts.Brief) { |
| 1174 | Result << "<Abstract>"; |
| 1175 | visit(Parts.Brief); |
| 1176 | Result << "</Abstract>"; |
| 1177 | } else if (Parts.FirstParagraph) { |
| 1178 | Result << "<Abstract>"; |
| 1179 | visit(Parts.FirstParagraph); |
| 1180 | Result << "</Abstract>"; |
| 1181 | FirstParagraphIsBrief = true; |
| 1182 | } |
| 1183 | |
| 1184 | if (Parts.TParams.size() != 0) { |
| 1185 | Result << "<TemplateParameters>"; |
| 1186 | for (unsigned i = 0, e = Parts.TParams.size(); i != e; ++i) |
| 1187 | visit(Parts.TParams[i]); |
| 1188 | Result << "</TemplateParameters>"; |
| 1189 | } |
| 1190 | |
| 1191 | if (Parts.Params.size() != 0) { |
| 1192 | Result << "<Parameters>"; |
| 1193 | for (unsigned i = 0, e = Parts.Params.size(); i != e; ++i) |
| 1194 | visit(Parts.Params[i]); |
| 1195 | Result << "</Parameters>"; |
| 1196 | } |
| 1197 | |
| 1198 | if (Parts.Returns) { |
| 1199 | Result << "<ResultDiscussion>"; |
| 1200 | visit(Parts.Returns); |
| 1201 | Result << "</ResultDiscussion>"; |
| 1202 | } |
Fariborz Jahanian | 257e2e8 | 2012-09-28 22:35:49 +0000 | [diff] [blame] | 1203 | |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 1204 | if (DI->CommentDecl->hasAttrs()) { |
| 1205 | const AttrVec &Attrs = DI->CommentDecl->getAttrs(); |
Fariborz Jahanian | faab561 | 2012-10-01 18:42:25 +0000 | [diff] [blame] | 1206 | for (unsigned i = 0, e = Attrs.size(); i != e; i++) { |
| 1207 | const AvailabilityAttr *AA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
Fariborz Jahanian | 2a46533 | 2012-10-02 20:05:47 +0000 | [diff] [blame] | 1208 | if (!AA) { |
Fariborz Jahanian | 8da68b8 | 2012-10-02 23:01:04 +0000 | [diff] [blame] | 1209 | if (const DeprecatedAttr *DA = dyn_cast<DeprecatedAttr>(Attrs[i])) { |
| 1210 | if (DA->getMessage().empty()) |
| 1211 | Result << "<Deprecated/>"; |
| 1212 | else { |
Dmitri Gribenko | 7d9c975 | 2012-10-03 09:04:56 +0000 | [diff] [blame] | 1213 | Result << "<Deprecated>"; |
| 1214 | appendToResultWithXMLEscaping(DA->getMessage()); |
| 1215 | Result << "</Deprecated>"; |
Fariborz Jahanian | 8da68b8 | 2012-10-02 23:01:04 +0000 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | else if (const UnavailableAttr *UA = dyn_cast<UnavailableAttr>(Attrs[i])) { |
| 1219 | if (UA->getMessage().empty()) |
| 1220 | Result << "<Unavailable/>"; |
| 1221 | else { |
Dmitri Gribenko | 7d9c975 | 2012-10-03 09:04:56 +0000 | [diff] [blame] | 1222 | Result << "<Unavailable>"; |
| 1223 | appendToResultWithXMLEscaping(UA->getMessage()); |
| 1224 | Result << "</Unavailable>"; |
Fariborz Jahanian | 8da68b8 | 2012-10-02 23:01:04 +0000 | [diff] [blame] | 1225 | } |
| 1226 | } |
Fariborz Jahanian | 257e2e8 | 2012-09-28 22:35:49 +0000 | [diff] [blame] | 1227 | continue; |
Fariborz Jahanian | 2a46533 | 2012-10-02 20:05:47 +0000 | [diff] [blame] | 1228 | } |
Fariborz Jahanian | faab561 | 2012-10-01 18:42:25 +0000 | [diff] [blame] | 1229 | |
| 1230 | // 'availability' attribute. |
Fariborz Jahanian | 257e2e8 | 2012-09-28 22:35:49 +0000 | [diff] [blame] | 1231 | Result << "<Availability"; |
Fariborz Jahanian | faab561 | 2012-10-01 18:42:25 +0000 | [diff] [blame] | 1232 | StringRef Distribution; |
Fariborz Jahanian | 257e2e8 | 2012-09-28 22:35:49 +0000 | [diff] [blame] | 1233 | if (AA->getPlatform()) { |
Fariborz Jahanian | faab561 | 2012-10-01 18:42:25 +0000 | [diff] [blame] | 1234 | Distribution = AvailabilityAttr::getPrettyPlatformName( |
| 1235 | AA->getPlatform()->getName()); |
| 1236 | if (Distribution.empty()) |
| 1237 | Distribution = AA->getPlatform()->getName(); |
Fariborz Jahanian | 257e2e8 | 2012-09-28 22:35:49 +0000 | [diff] [blame] | 1238 | } |
Fariborz Jahanian | faab561 | 2012-10-01 18:42:25 +0000 | [diff] [blame] | 1239 | Result << " distribution=\"" << Distribution << "\">"; |
Fariborz Jahanian | 257e2e8 | 2012-09-28 22:35:49 +0000 | [diff] [blame] | 1240 | VersionTuple IntroducedInVersion = AA->getIntroduced(); |
| 1241 | if (!IntroducedInVersion.empty()) { |
Fariborz Jahanian | faab561 | 2012-10-01 18:42:25 +0000 | [diff] [blame] | 1242 | Result << "<IntroducedInVersion>" |
| 1243 | << IntroducedInVersion.getAsString() |
| 1244 | << "</IntroducedInVersion>"; |
Fariborz Jahanian | 257e2e8 | 2012-09-28 22:35:49 +0000 | [diff] [blame] | 1245 | } |
| 1246 | VersionTuple DeprecatedInVersion = AA->getDeprecated(); |
| 1247 | if (!DeprecatedInVersion.empty()) { |
Fariborz Jahanian | faab561 | 2012-10-01 18:42:25 +0000 | [diff] [blame] | 1248 | Result << "<DeprecatedInVersion>" |
| 1249 | << DeprecatedInVersion.getAsString() |
| 1250 | << "</DeprecatedInVersion>"; |
Fariborz Jahanian | 257e2e8 | 2012-09-28 22:35:49 +0000 | [diff] [blame] | 1251 | } |
| 1252 | VersionTuple RemovedAfterVersion = AA->getObsoleted(); |
| 1253 | if (!RemovedAfterVersion.empty()) { |
Fariborz Jahanian | faab561 | 2012-10-01 18:42:25 +0000 | [diff] [blame] | 1254 | Result << "<RemovedAfterVersion>" |
| 1255 | << RemovedAfterVersion.getAsString() |
| 1256 | << "</RemovedAfterVersion>"; |
Fariborz Jahanian | 257e2e8 | 2012-09-28 22:35:49 +0000 | [diff] [blame] | 1257 | } |
| 1258 | StringRef DeprecationSummary = AA->getMessage(); |
| 1259 | if (!DeprecationSummary.empty()) { |
Dmitri Gribenko | 7d9c975 | 2012-10-03 09:04:56 +0000 | [diff] [blame] | 1260 | Result << "<DeprecationSummary>"; |
| 1261 | appendToResultWithXMLEscaping(DeprecationSummary); |
| 1262 | Result << "</DeprecationSummary>"; |
Fariborz Jahanian | 257e2e8 | 2012-09-28 22:35:49 +0000 | [diff] [blame] | 1263 | } |
Fariborz Jahanian | 257e2e8 | 2012-09-28 22:35:49 +0000 | [diff] [blame] | 1264 | if (AA->getUnavailable()) |
Fariborz Jahanian | 8da68b8 | 2012-10-02 23:01:04 +0000 | [diff] [blame] | 1265 | Result << "<Unavailable/>"; |
Fariborz Jahanian | faab561 | 2012-10-01 18:42:25 +0000 | [diff] [blame] | 1266 | Result << "</Availability>"; |
Fariborz Jahanian | 257e2e8 | 2012-09-28 22:35:49 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
Fariborz Jahanian | faab561 | 2012-10-01 18:42:25 +0000 | [diff] [blame] | 1269 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1270 | { |
| 1271 | bool StartTagEmitted = false; |
| 1272 | for (unsigned i = 0, e = Parts.MiscBlocks.size(); i != e; ++i) { |
| 1273 | const Comment *C = Parts.MiscBlocks[i]; |
| 1274 | if (FirstParagraphIsBrief && C == Parts.FirstParagraph) |
| 1275 | continue; |
| 1276 | if (!StartTagEmitted) { |
| 1277 | Result << "<Discussion>"; |
| 1278 | StartTagEmitted = true; |
| 1279 | } |
| 1280 | visit(C); |
| 1281 | } |
| 1282 | if (StartTagEmitted) |
| 1283 | Result << "</Discussion>"; |
| 1284 | } |
| 1285 | |
| 1286 | Result << RootEndTag; |
| 1287 | |
| 1288 | Result.flush(); |
| 1289 | } |
| 1290 | |
| 1291 | void CommentASTToXMLConverter::appendToResultWithXMLEscaping(StringRef S) { |
| 1292 | for (StringRef::iterator I = S.begin(), E = S.end(); I != E; ++I) { |
| 1293 | const char C = *I; |
| 1294 | switch (C) { |
| 1295 | case '&': |
| 1296 | Result << "&"; |
| 1297 | break; |
| 1298 | case '<': |
| 1299 | Result << "<"; |
| 1300 | break; |
| 1301 | case '>': |
| 1302 | Result << ">"; |
| 1303 | break; |
| 1304 | case '"': |
| 1305 | Result << """; |
| 1306 | break; |
| 1307 | case '\'': |
| 1308 | Result << "'"; |
| 1309 | break; |
| 1310 | default: |
| 1311 | Result << C; |
| 1312 | break; |
| 1313 | } |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | extern "C" { |
| 1318 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 1319 | CXString clang_FullComment_getAsXML(CXComment CXC) { |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1320 | const FullComment *FC = getASTNodeAs<FullComment>(CXC); |
| 1321 | if (!FC) |
| 1322 | return createCXString((const char *) 0); |
| 1323 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 1324 | CXTranslationUnit TU = CXC.TranslationUnit; |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1325 | SourceManager &SM = static_cast<ASTUnit *>(TU->TUData)->getSourceManager(); |
| 1326 | |
| 1327 | SmallString<1024> XML; |
Dmitri Gribenko | 8cfabf2 | 2012-10-19 16:51:38 +0000 | [diff] [blame] | 1328 | CommentASTToXMLConverter Converter(FC, XML, getCommandTraits(CXC), SM); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1329 | Converter.visit(FC); |
| 1330 | return createCXString(XML.str(), /* DupString = */ true); |
| 1331 | } |
| 1332 | |
| 1333 | } // end extern "C" |
| 1334 | |