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