blob: fd7a3942a43868f8c56ec78c515407a290b324fa [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);
46 void visitHTMLOpenTagComment(const HTMLOpenTagComment *C);
47 void visitHTMLCloseTagComment(const HTMLCloseTagComment *C);
48
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
109 for (unsigned i = 0, e = C->getArgCount(); i != e; ++i)
110 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
111}
112
113void CommentDumper::visitHTMLOpenTagComment(const HTMLOpenTagComment *C) {
114 dumpComment(C);
115
116 OS << " Name=\"" << C->getTagName() << "\"";
117 if (C->getAttrCount() != 0) {
118 OS << " Attrs: ";
119 for (unsigned i = 0, e = C->getAttrCount(); i != e; ++i) {
120 const HTMLOpenTagComment::Attribute &Attr = C->getAttr(i);
121 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
122 }
123 }
124}
125
126void CommentDumper::visitHTMLCloseTagComment(const HTMLCloseTagComment *C) {
127 dumpComment(C);
128
129 OS << " Name=\"" << C->getTagName() << "\"";
130}
131
132void CommentDumper::visitParagraphComment(const ParagraphComment *C) {
133 dumpComment(C);
134}
135
136void CommentDumper::visitBlockCommandComment(const BlockCommandComment *C) {
137 dumpComment(C);
138
139 OS << " Name=\"" << C->getCommandName() << "\"";
140}
141
142void CommentDumper::visitParamCommandComment(const ParamCommandComment *C) {
143 dumpComment(C);
144
145 switch (C->getDirection()) {
146 case ParamCommandComment::In:
147 OS << " [in]";
148 break;
149 case ParamCommandComment::Out:
150 OS << " [out]";
151 break;
152 case ParamCommandComment::InOut:
153 OS << " [in,out]";
154 break;
155 }
156
157 if (C->isDirectionExplicit())
158 OS << " explicitly";
159 else
160 OS << " implicitly";
161
162 if (C->hasParamName()) {
163 OS << " Param=\"" << C->getParamName() << "\"";
164 }
165}
166
167void CommentDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
168 dumpComment(C);
169
170 OS << " Name=\"" << C->getCommandName() << "\""
171 " CloseName=\"" << C->getCloseName() << "\"";
172}
173
174void CommentDumper::visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C) {
175 dumpComment(C);
176
177 OS << " Text=\"" << C->getText() << "\"";
178}
179
180void CommentDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
181 dumpComment(C);
182
183 OS << " Text=\"" << C->getText() << "\"";
184}
185
186void CommentDumper::visitFullComment(const FullComment *C) {
187 dumpComment(C);
188}
189
190} // unnamed namespace
191
192void Comment::dump() const {
193 CommentDumper D(llvm::errs(), NULL);
194 D.dumpSubtree(this);
195 llvm::errs() << '\n';
196}
197
198void Comment::dump(SourceManager &SM) const {
199 CommentDumper D(llvm::errs(), &SM);
200 D.dumpSubtree(this);
201 llvm::errs() << '\n';
202}
203
204} // end namespace comments
205} // end namespace clang
206