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