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