]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/AST/CommentDumper.cpp
Merge ATF 0.16 from vendor/atf/dist.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / AST / CommentDumper.cpp
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;
19   SourceManager *SM;
20   unsigned IndentLevel;
21
22 public:
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 visitHTMLStartTagComment(const HTMLStartTagComment *C);
47   void visitHTMLEndTagComment(const HTMLEndTagComment *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 visitTParamCommandComment(const TParamCommandComment *C);
54   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
61 void 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
76 void CommentDumper::dumpComment(const Comment *C) {
77   dumpIndent();
78   OS << "(" << C->getCommentKindName()
79      << " " << (const void *) C;
80   dumpSourceRange(C);
81 }
82
83 void 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
101 void CommentDumper::visitTextComment(const TextComment *C) {
102   dumpComment(C);
103
104   OS << " Text=\"" << C->getText() << "\"";
105 }
106
107 void CommentDumper::visitInlineCommandComment(const InlineCommandComment *C) {
108   dumpComment(C);
109
110   OS << " Name=\"" << C->getCommandName() << "\"";
111   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
126   for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
127     OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
128 }
129
130 void CommentDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
131   dumpComment(C);
132
133   OS << " Name=\"" << C->getTagName() << "\"";
134   if (C->getNumAttrs() != 0) {
135     OS << " Attrs: ";
136     for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
137       const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
138       OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
139     }
140   }
141   if (C->isSelfClosing())
142     OS << " SelfClosing";
143 }
144
145 void CommentDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
146   dumpComment(C);
147
148   OS << " Name=\"" << C->getTagName() << "\"";
149 }
150
151 void CommentDumper::visitParagraphComment(const ParagraphComment *C) {
152   dumpComment(C);
153 }
154
155 void CommentDumper::visitBlockCommandComment(const BlockCommandComment *C) {
156   dumpComment(C);
157
158   OS << " Name=\"" << C->getCommandName() << "\"";
159   for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
160     OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
161 }
162
163 void CommentDumper::visitParamCommandComment(const ParamCommandComment *C) {
164   dumpComment(C);
165
166   OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
167
168   if (C->isDirectionExplicit())
169     OS << " explicitly";
170   else
171     OS << " implicitly";
172
173   if (C->hasParamName())
174     OS << " Param=\"" << C->getParamName() << "\"";
175
176   if (C->isParamIndexValid())
177     OS << " ParamIndex=" << C->getParamIndex();
178 }
179
180 void 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
198 void CommentDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
199   dumpComment(C);
200
201   OS << " Name=\"" << C->getCommandName() << "\""
202         " CloseName=\"" << C->getCloseName() << "\"";
203 }
204
205 void CommentDumper::visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C) {
206   dumpComment(C);
207
208   OS << " Text=\"" << C->getText() << "\"";
209 }
210
211 void CommentDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
212   dumpComment(C);
213
214   OS << " Text=\"" << C->getText() << "\"";
215 }
216
217 void CommentDumper::visitFullComment(const FullComment *C) {
218   dumpComment(C);
219 }
220
221 } // unnamed namespace
222
223 void Comment::dump(llvm::raw_ostream &OS, SourceManager *SM) const {
224   CommentDumper D(llvm::errs(), SM);
225   D.dumpSubtree(this);
226   llvm::errs() << '\n';
227 }
228
229 } // end namespace comments
230 } // end namespace clang
231