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