Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 1 | //===--- CommentDumper.cpp - Dumping implementation for Comment ASTs ------===// |
| 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 | #include "clang/AST/CommentVisitor.h" |
| 11 | #include "llvm/Support/raw_ostream.h" |
| 12 | |
| 13 | namespace clang { |
| 14 | namespace comments { |
| 15 | |
| 16 | namespace { |
| 17 | class CommentDumper: public comments::ConstCommentVisitor<CommentDumper> { |
| 18 | raw_ostream &OS; |
| 19 | SourceManager *SM; |
| 20 | unsigned IndentLevel; |
| 21 | |
| 22 | public: |
| 23 | CommentDumper(raw_ostream &OS, SourceManager *SM) : |
| 24 | OS(OS), SM(SM), IndentLevel(0) |
| 25 | { } |
| 26 | |
| 27 | void dumpIndent() const { |
| 28 | for (unsigned i = 1, e = IndentLevel; i < e; ++i) |
| 29 | OS << " "; |
| 30 | } |
| 31 | |
| 32 | void dumpLocation(SourceLocation Loc) { |
| 33 | if (SM) |
| 34 | Loc.print(OS, *SM); |
| 35 | } |
| 36 | |
| 37 | void dumpSourceRange(const Comment *C); |
| 38 | |
| 39 | void dumpComment(const Comment *C); |
| 40 | |
| 41 | void dumpSubtree(const Comment *C); |
| 42 | |
| 43 | // Inline content. |
| 44 | void visitTextComment(const TextComment *C); |
| 45 | void visitInlineCommandComment(const InlineCommandComment *C); |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 46 | void visitHTMLStartTagComment(const HTMLStartTagComment *C); |
| 47 | void visitHTMLEndTagComment(const HTMLEndTagComment *C); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 48 | |
| 49 | // Block content. |
| 50 | void visitParagraphComment(const ParagraphComment *C); |
| 51 | void visitBlockCommandComment(const BlockCommandComment *C); |
| 52 | void visitParamCommandComment(const ParamCommandComment *C); |
| 53 | void visitVerbatimBlockComment(const VerbatimBlockComment *C); |
| 54 | void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C); |
| 55 | void visitVerbatimLineComment(const VerbatimLineComment *C); |
| 56 | |
| 57 | void visitFullComment(const FullComment *C); |
| 58 | }; |
| 59 | |
| 60 | void CommentDumper::dumpSourceRange(const Comment *C) { |
| 61 | if (!SM) |
| 62 | return; |
| 63 | |
| 64 | SourceRange SR = C->getSourceRange(); |
| 65 | |
| 66 | OS << " <"; |
| 67 | dumpLocation(SR.getBegin()); |
| 68 | if (SR.getBegin() != SR.getEnd()) { |
| 69 | OS << ", "; |
| 70 | dumpLocation(SR.getEnd()); |
| 71 | } |
| 72 | OS << ">"; |
| 73 | } |
| 74 | |
| 75 | void CommentDumper::dumpComment(const Comment *C) { |
| 76 | dumpIndent(); |
| 77 | OS << "(" << C->getCommentKindName() |
| 78 | << " " << (void *) C; |
| 79 | dumpSourceRange(C); |
| 80 | } |
| 81 | |
| 82 | void CommentDumper::dumpSubtree(const Comment *C) { |
| 83 | ++IndentLevel; |
| 84 | if (C) { |
| 85 | visit(C); |
| 86 | for (Comment::child_iterator I = C->child_begin(), |
| 87 | E = C->child_end(); |
| 88 | I != E; ++I) { |
| 89 | OS << '\n'; |
| 90 | dumpSubtree(*I); |
| 91 | } |
| 92 | OS << ')'; |
| 93 | } else { |
| 94 | dumpIndent(); |
| 95 | OS << "<<<NULL>>>"; |
| 96 | } |
| 97 | --IndentLevel; |
| 98 | } |
| 99 | |
| 100 | void CommentDumper::visitTextComment(const TextComment *C) { |
| 101 | dumpComment(C); |
| 102 | |
| 103 | OS << " Text=\"" << C->getText() << "\""; |
| 104 | } |
| 105 | |
| 106 | void CommentDumper::visitInlineCommandComment(const InlineCommandComment *C) { |
| 107 | dumpComment(C); |
| 108 | |
Dmitri Gribenko | cdd1b37 | 2012-07-18 23:20:23 +0000 | [diff] [blame] | 109 | OS << " Name=\"" << C->getCommandName() << "\""; |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 110 | switch (C->getRenderKind()) { |
| 111 | case InlineCommandComment::RenderNormal: |
| 112 | OS << " RenderNormal"; |
| 113 | break; |
| 114 | case InlineCommandComment::RenderBold: |
| 115 | OS << " RenderBold"; |
| 116 | break; |
| 117 | case InlineCommandComment::RenderMonospaced: |
| 118 | OS << " RenderMonospaced"; |
| 119 | break; |
| 120 | case InlineCommandComment::RenderEmphasized: |
| 121 | OS << " RenderEmphasized"; |
| 122 | break; |
| 123 | } |
| 124 | |
Dmitri Gribenko | 0eaf69d | 2012-07-13 19:02:42 +0000 | [diff] [blame] | 125 | for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i) |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 126 | OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\""; |
| 127 | } |
| 128 | |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 129 | void CommentDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) { |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 130 | dumpComment(C); |
| 131 | |
| 132 | OS << " Name=\"" << C->getTagName() << "\""; |
Dmitri Gribenko | 0eaf69d | 2012-07-13 19:02:42 +0000 | [diff] [blame] | 133 | if (C->getNumAttrs() != 0) { |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 134 | OS << " Attrs: "; |
Dmitri Gribenko | 0eaf69d | 2012-07-13 19:02:42 +0000 | [diff] [blame] | 135 | for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) { |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 136 | const HTMLStartTagComment::Attribute &Attr = C->getAttr(i); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 137 | OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\""; |
| 138 | } |
| 139 | } |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 140 | if (C->isSelfClosing()) |
| 141 | OS << " SelfClosing"; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 144 | void CommentDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) { |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 145 | dumpComment(C); |
| 146 | |
| 147 | OS << " Name=\"" << C->getTagName() << "\""; |
| 148 | } |
| 149 | |
| 150 | void CommentDumper::visitParagraphComment(const ParagraphComment *C) { |
| 151 | dumpComment(C); |
| 152 | } |
| 153 | |
| 154 | void CommentDumper::visitBlockCommandComment(const BlockCommandComment *C) { |
| 155 | dumpComment(C); |
| 156 | |
| 157 | OS << " Name=\"" << C->getCommandName() << "\""; |
Dmitri Gribenko | ad29b2b | 2012-07-19 18:43:24 +0000 | [diff] [blame] | 158 | for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i) |
| 159 | OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\""; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void CommentDumper::visitParamCommandComment(const ParamCommandComment *C) { |
| 163 | dumpComment(C); |
| 164 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 165 | OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection()); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 166 | |
| 167 | if (C->isDirectionExplicit()) |
| 168 | OS << " explicitly"; |
| 169 | else |
| 170 | OS << " implicitly"; |
| 171 | |
Dmitri Gribenko | 72b57cc | 2012-07-28 00:35:48 +0000 | [diff] [blame] | 172 | if (C->hasParamName()) |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 173 | OS << " Param=\"" << C->getParamName() << "\""; |
Dmitri Gribenko | 72b57cc | 2012-07-28 00:35:48 +0000 | [diff] [blame] | 174 | |
| 175 | if (C->isParamIndexValid()) |
| 176 | OS << " ParamIndex=" << C->getParamIndex(); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void CommentDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) { |
| 180 | dumpComment(C); |
| 181 | |
| 182 | OS << " Name=\"" << C->getCommandName() << "\"" |
| 183 | " CloseName=\"" << C->getCloseName() << "\""; |
| 184 | } |
| 185 | |
| 186 | void CommentDumper::visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C) { |
| 187 | dumpComment(C); |
| 188 | |
| 189 | OS << " Text=\"" << C->getText() << "\""; |
| 190 | } |
| 191 | |
| 192 | void CommentDumper::visitVerbatimLineComment(const VerbatimLineComment *C) { |
| 193 | dumpComment(C); |
| 194 | |
| 195 | OS << " Text=\"" << C->getText() << "\""; |
| 196 | } |
| 197 | |
| 198 | void CommentDumper::visitFullComment(const FullComment *C) { |
| 199 | dumpComment(C); |
| 200 | } |
| 201 | |
| 202 | } // unnamed namespace |
| 203 | |
Dmitri Gribenko | fb3643a | 2012-07-18 16:30:42 +0000 | [diff] [blame] | 204 | void Comment::dump(llvm::raw_ostream &OS, SourceManager *SM) const { |
| 205 | CommentDumper D(llvm::errs(), SM); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 206 | D.dumpSubtree(this); |
| 207 | llvm::errs() << '\n'; |
| 208 | } |
| 209 | |
| 210 | } // end namespace comments |
| 211 | } // end namespace clang |
| 212 | |