blob: dffc8233a0cafb9ee9192613126b3bc23491665e [file] [log] [blame]
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001//===--- 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
13namespace clang {
14namespace comments {
15
16namespace {
17class CommentDumper: public comments::ConstCommentVisitor<CommentDumper> {
18 raw_ostream &OS;
19 SourceManager *SM;
20 unsigned IndentLevel;
21
22public:
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 Gribenko3f38bf22012-07-13 00:44:24 +000046 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
47 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000048
49 // Block content.
50 void visitParagraphComment(const ParagraphComment *C);
51 void visitBlockCommandComment(const BlockCommandComment *C);
52 void visitParamCommandComment(const ParamCommandComment *C);
Dmitri Gribenko96b09862012-07-31 22:37:06 +000053 void visitTParamCommandComment(const TParamCommandComment *C);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000054 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
55 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
56 void visitVerbatimLineComment(const VerbatimLineComment *C);
57
58 void visitFullComment(const FullComment *C);
59};
60
61void CommentDumper::dumpSourceRange(const Comment *C) {
62 if (!SM)
63 return;
64
65 SourceRange SR = C->getSourceRange();
66
67 OS << " <";
68 dumpLocation(SR.getBegin());
69 if (SR.getBegin() != SR.getEnd()) {
70 OS << ", ";
71 dumpLocation(SR.getEnd());
72 }
73 OS << ">";
74}
75
76void CommentDumper::dumpComment(const Comment *C) {
77 dumpIndent();
78 OS << "(" << C->getCommentKindName()
Dmitri Gribenkoc1093612012-07-30 17:52:50 +000079 << " " << (const void *) C;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000080 dumpSourceRange(C);
81}
82
83void CommentDumper::dumpSubtree(const Comment *C) {
84 ++IndentLevel;
85 if (C) {
86 visit(C);
87 for (Comment::child_iterator I = C->child_begin(),
88 E = C->child_end();
89 I != E; ++I) {
90 OS << '\n';
91 dumpSubtree(*I);
92 }
93 OS << ')';
94 } else {
95 dumpIndent();
96 OS << "<<<NULL>>>";
97 }
98 --IndentLevel;
99}
100
101void CommentDumper::visitTextComment(const TextComment *C) {
102 dumpComment(C);
103
104 OS << " Text=\"" << C->getText() << "\"";
105}
106
107void CommentDumper::visitInlineCommandComment(const InlineCommandComment *C) {
108 dumpComment(C);
109
Dmitri Gribenkocdd1b372012-07-18 23:20:23 +0000110 OS << " Name=\"" << C->getCommandName() << "\"";
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000111 switch (C->getRenderKind()) {
112 case InlineCommandComment::RenderNormal:
113 OS << " RenderNormal";
114 break;
115 case InlineCommandComment::RenderBold:
116 OS << " RenderBold";
117 break;
118 case InlineCommandComment::RenderMonospaced:
119 OS << " RenderMonospaced";
120 break;
121 case InlineCommandComment::RenderEmphasized:
122 OS << " RenderEmphasized";
123 break;
124 }
125
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000126 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000127 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
128}
129
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000130void CommentDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000131 dumpComment(C);
132
133 OS << " Name=\"" << C->getTagName() << "\"";
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000134 if (C->getNumAttrs() != 0) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000135 OS << " Attrs: ";
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000136 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000137 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000138 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
139 }
140 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000141 if (C->isSelfClosing())
142 OS << " SelfClosing";
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000143}
144
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000145void CommentDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000146 dumpComment(C);
147
148 OS << " Name=\"" << C->getTagName() << "\"";
149}
150
151void CommentDumper::visitParagraphComment(const ParagraphComment *C) {
152 dumpComment(C);
153}
154
155void CommentDumper::visitBlockCommandComment(const BlockCommandComment *C) {
156 dumpComment(C);
157
158 OS << " Name=\"" << C->getCommandName() << "\"";
Dmitri Gribenkoad29b2b2012-07-19 18:43:24 +0000159 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
160 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000161}
162
163void CommentDumper::visitParamCommandComment(const ParamCommandComment *C) {
164 dumpComment(C);
165
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000166 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000167
168 if (C->isDirectionExplicit())
169 OS << " explicitly";
170 else
171 OS << " implicitly";
172
Dmitri Gribenko72b57cc2012-07-28 00:35:48 +0000173 if (C->hasParamName())
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000174 OS << " Param=\"" << C->getParamName() << "\"";
Dmitri Gribenko72b57cc2012-07-28 00:35:48 +0000175
176 if (C->isParamIndexValid())
177 OS << " ParamIndex=" << C->getParamIndex();
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000178}
179
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000180void CommentDumper::visitTParamCommandComment(const TParamCommandComment *C) {
181 dumpComment(C);
182
183 if (C->hasParamName()) {
184 OS << " Param=\"" << C->getParamName() << "\"";
185 }
186
187 if (C->isPositionValid()) {
188 OS << " Position=<";
189 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
190 OS << C->getIndex(i);
191 if (i != e - 1)
192 OS << ", ";
193 }
194 OS << ">";
195 }
196}
197
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000198void CommentDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
199 dumpComment(C);
200
201 OS << " Name=\"" << C->getCommandName() << "\""
202 " CloseName=\"" << C->getCloseName() << "\"";
203}
204
205void CommentDumper::visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C) {
206 dumpComment(C);
207
208 OS << " Text=\"" << C->getText() << "\"";
209}
210
211void CommentDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
212 dumpComment(C);
213
214 OS << " Text=\"" << C->getText() << "\"";
215}
216
217void CommentDumper::visitFullComment(const FullComment *C) {
218 dumpComment(C);
219}
220
221} // unnamed namespace
222
Dmitri Gribenkofb3643a2012-07-18 16:30:42 +0000223void Comment::dump(llvm::raw_ostream &OS, SourceManager *SM) const {
224 CommentDumper D(llvm::errs(), SM);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000225 D.dumpSubtree(this);
226 llvm::errs() << '\n';
227}
228
229} // end namespace comments
230} // end namespace clang
231