blob: 027e3a9800439181d6c969964ea00507d34bb944 [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);
53 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
54 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
55 void visitVerbatimLineComment(const VerbatimLineComment *C);
56
57 void visitFullComment(const FullComment *C);
58};
59
60void CommentDumper::dumpSourceRange(const Comment *C) {
61 if (!SM)
62 return;
63
64 SourceRange SR = C->getSourceRange();
65
66 OS << " <";
67 dumpLocation(SR.getBegin());
68 if (SR.getBegin() != SR.getEnd()) {
69 OS << ", ";
70 dumpLocation(SR.getEnd());
71 }
72 OS << ">";
73}
74
75void CommentDumper::dumpComment(const Comment *C) {
76 dumpIndent();
77 OS << "(" << C->getCommentKindName()
78 << " " << (void *) C;
79 dumpSourceRange(C);
80}
81
82void CommentDumper::dumpSubtree(const Comment *C) {
83 ++IndentLevel;
84 if (C) {
85 visit(C);
86 for (Comment::child_iterator I = C->child_begin(),
87 E = C->child_end();
88 I != E; ++I) {
89 OS << '\n';
90 dumpSubtree(*I);
91 }
92 OS << ')';
93 } else {
94 dumpIndent();
95 OS << "<<<NULL>>>";
96 }
97 --IndentLevel;
98}
99
100void CommentDumper::visitTextComment(const TextComment *C) {
101 dumpComment(C);
102
103 OS << " Text=\"" << C->getText() << "\"";
104}
105
106void CommentDumper::visitInlineCommandComment(const InlineCommandComment *C) {
107 dumpComment(C);
108
Dmitri Gribenkocdd1b372012-07-18 23:20:23 +0000109 OS << " Name=\"" << C->getCommandName() << "\"";
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000110 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000111 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
112}
113
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000114void CommentDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000115 dumpComment(C);
116
117 OS << " Name=\"" << C->getTagName() << "\"";
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000118 if (C->getNumAttrs() != 0) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000119 OS << " Attrs: ";
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000120 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000121 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000122 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
123 }
124 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000125 if (C->isSelfClosing())
126 OS << " SelfClosing";
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000127}
128
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000129void CommentDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000130 dumpComment(C);
131
132 OS << " Name=\"" << C->getTagName() << "\"";
133}
134
135void CommentDumper::visitParagraphComment(const ParagraphComment *C) {
136 dumpComment(C);
137}
138
139void CommentDumper::visitBlockCommandComment(const BlockCommandComment *C) {
140 dumpComment(C);
141
142 OS << " Name=\"" << C->getCommandName() << "\"";
143}
144
145void CommentDumper::visitParamCommandComment(const ParamCommandComment *C) {
146 dumpComment(C);
147
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000148 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000149
150 if (C->isDirectionExplicit())
151 OS << " explicitly";
152 else
153 OS << " implicitly";
154
155 if (C->hasParamName()) {
156 OS << " Param=\"" << C->getParamName() << "\"";
157 }
158}
159
160void CommentDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
161 dumpComment(C);
162
163 OS << " Name=\"" << C->getCommandName() << "\""
164 " CloseName=\"" << C->getCloseName() << "\"";
165}
166
167void CommentDumper::visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C) {
168 dumpComment(C);
169
170 OS << " Text=\"" << C->getText() << "\"";
171}
172
173void CommentDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
174 dumpComment(C);
175
176 OS << " Text=\"" << C->getText() << "\"";
177}
178
179void CommentDumper::visitFullComment(const FullComment *C) {
180 dumpComment(C);
181}
182
183} // unnamed namespace
184
Dmitri Gribenkofb3643a2012-07-18 16:30:42 +0000185void Comment::dump(llvm::raw_ostream &OS, SourceManager *SM) const {
186 CommentDumper D(llvm::errs(), SM);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000187 D.dumpSubtree(this);
188 llvm::errs() << '\n';
189}
190
191} // end namespace comments
192} // end namespace clang
193