1 //===--- CommentDumper.cpp - Dumping implementation for Comment ASTs ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/AST/CommentVisitor.h"
11 #include "llvm/Support/raw_ostream.h"
17 class CommentDumper: public comments::ConstCommentVisitor<CommentDumper> {
19 const CommandTraits *Traits;
20 const SourceManager *SM;
22 /// The \c FullComment parent of the comment being dumped.
23 const FullComment *FC;
28 CommentDumper(raw_ostream &OS,
29 const CommandTraits *Traits,
30 const SourceManager *SM,
31 const FullComment *FC) :
32 OS(OS), Traits(Traits), SM(SM), FC(FC), IndentLevel(0)
35 void dumpIndent() const {
36 for (unsigned i = 1, e = IndentLevel; i < e; ++i)
40 void dumpLocation(SourceLocation Loc) {
45 void dumpSourceRange(const Comment *C);
47 void dumpComment(const Comment *C);
49 void dumpSubtree(const Comment *C);
52 void visitTextComment(const TextComment *C);
53 void visitInlineCommandComment(const InlineCommandComment *C);
54 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
55 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
58 void visitParagraphComment(const ParagraphComment *C);
59 void visitBlockCommandComment(const BlockCommandComment *C);
60 void visitParamCommandComment(const ParamCommandComment *C);
61 void visitTParamCommandComment(const TParamCommandComment *C);
62 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
63 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
64 void visitVerbatimLineComment(const VerbatimLineComment *C);
66 void visitFullComment(const FullComment *C);
68 const char *getCommandName(unsigned CommandID) {
70 return Traits->getCommandInfo(CommandID)->Name;
71 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
74 return "<not a builtin command>";
78 void CommentDumper::dumpSourceRange(const Comment *C) {
82 SourceRange SR = C->getSourceRange();
85 dumpLocation(SR.getBegin());
86 if (SR.getBegin() != SR.getEnd()) {
88 dumpLocation(SR.getEnd());
93 void CommentDumper::dumpComment(const Comment *C) {
95 OS << "(" << C->getCommentKindName()
96 << " " << (const void *) C;
100 void CommentDumper::dumpSubtree(const Comment *C) {
104 for (Comment::child_iterator I = C->child_begin(),
118 void CommentDumper::visitTextComment(const TextComment *C) {
121 OS << " Text=\"" << C->getText() << "\"";
124 void CommentDumper::visitInlineCommandComment(const InlineCommandComment *C) {
127 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
128 switch (C->getRenderKind()) {
129 case InlineCommandComment::RenderNormal:
130 OS << " RenderNormal";
132 case InlineCommandComment::RenderBold:
135 case InlineCommandComment::RenderMonospaced:
136 OS << " RenderMonospaced";
138 case InlineCommandComment::RenderEmphasized:
139 OS << " RenderEmphasized";
143 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
144 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
147 void CommentDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
150 OS << " Name=\"" << C->getTagName() << "\"";
151 if (C->getNumAttrs() != 0) {
153 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
154 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
155 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
158 if (C->isSelfClosing())
159 OS << " SelfClosing";
162 void CommentDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
165 OS << " Name=\"" << C->getTagName() << "\"";
168 void CommentDumper::visitParagraphComment(const ParagraphComment *C) {
172 void CommentDumper::visitBlockCommandComment(const BlockCommandComment *C) {
175 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
176 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
177 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
180 void CommentDumper::visitParamCommandComment(const ParamCommandComment *C) {
183 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
185 if (C->isDirectionExplicit())
190 if (C->hasParamName()) {
191 if (C->isParamIndexValid())
192 OS << " Param=\"" << C->getParamName(FC) << "\"";
194 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
197 if (C->isParamIndexValid())
198 OS << " ParamIndex=" << C->getParamIndex();
201 void CommentDumper::visitTParamCommandComment(const TParamCommandComment *C) {
204 if (C->hasParamName()) {
205 if (C->isPositionValid())
206 OS << " Param=\"" << C->getParamName(FC) << "\"";
208 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
211 if (C->isPositionValid()) {
213 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
214 OS << C->getIndex(i);
222 void CommentDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
225 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
226 " CloseName=\"" << C->getCloseName() << "\"";
229 void CommentDumper::visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C) {
232 OS << " Text=\"" << C->getText() << "\"";
235 void CommentDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
238 OS << " Text=\"" << C->getText() << "\"";
241 void CommentDumper::visitFullComment(const FullComment *C) {
245 } // unnamed namespace
247 void Comment::dump(llvm::raw_ostream &OS, const CommandTraits *Traits,
248 const SourceManager *SM) const {
249 const FullComment *FC = dyn_cast<FullComment>(this);
250 CommentDumper D(llvm::errs(), Traits, SM, FC);
252 llvm::errs() << '\n';
255 } // end namespace comments
256 } // end namespace clang