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