blob: 390ae09497b06257ffabf11914285d591c9884dc [file] [log] [blame]
Chris Lattner97e8b6f2007-10-07 06:04:32 +00001//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner97e8b6f2007-10-07 06:04:32 +000010// AST Consumer Implementations.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
Eli Friedman39d7c4d2009-05-18 22:50:54 +000014#include "clang/Frontend/ASTConsumers.h"
Nico Weberdae86962008-08-09 18:32:11 +000015#include "clang/Basic/Diagnostic.h"
Ted Kremenek54117722007-12-20 00:34:58 +000016#include "clang/Basic/SourceManager.h"
17#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000019#include "clang/AST/ASTConsumer.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000020#include "clang/AST/ASTContext.h"
Anders Carlsson78762eb2009-09-24 18:54:49 +000021#include "clang/AST/RecordLayout.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000022#include "clang/AST/PrettyPrinter.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000023#include "llvm/Module.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000024#include "llvm/Support/Timer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000025#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000026#include "llvm/Support/Path.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000027using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000028
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000029//===----------------------------------------------------------------------===//
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000030/// ASTPrinter - Pretty-printer and dumper of ASTs
Chris Lattner6000dac2007-08-08 22:51:59 +000031
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000032namespace {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000033 class ASTPrinter : public ASTConsumer {
Chris Lattner5f9e2722011-07-23 10:55:15 +000034 raw_ostream &Out;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000035 bool Dump;
Mike Stump1eb44332009-09-09 15:08:12 +000036
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000037 public:
Chris Lattner5f9e2722011-07-23 10:55:15 +000038 ASTPrinter(raw_ostream* o = NULL, bool Dump = false)
Argyrios Kyrtzidisbc1e1462010-08-03 17:29:57 +000039 : Out(o? *o : llvm::outs()), Dump(Dump) { }
Mike Stump1eb44332009-09-09 15:08:12 +000040
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000041 virtual void HandleTranslationUnit(ASTContext &Context) {
Douglas Gregor30c42402011-09-27 22:38:19 +000042 PrintingPolicy Policy = Context.getPrintingPolicy();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000043 Policy.Dump = Dump;
Richard Trieu5cb3d692011-07-28 00:19:05 +000044 Context.getTranslationUnitDecl()->print(Out, Policy, /*Indentation=*/0,
45 /*PrintInstantiation=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +000046 }
Chris Lattner3d4997d2007-09-15 23:02:28 +000047 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +000048} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +000049
Chris Lattner5f9e2722011-07-23 10:55:15 +000050ASTConsumer *clang::CreateASTPrinter(raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +000051 return new ASTPrinter(out);
52}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000053
Mike Stump1eb44332009-09-09 15:08:12 +000054ASTConsumer *clang::CreateASTDumper() {
55 return new ASTPrinter(0, true);
Douglas Gregor609e72f2009-04-26 02:02:08 +000056}
Chris Lattner3d4997d2007-09-15 23:02:28 +000057
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000058//===----------------------------------------------------------------------===//
59/// ASTViewer - AST Visualization
60
Ted Kremenek80de08f2007-09-19 21:29:43 +000061namespace {
62 class ASTViewer : public ASTConsumer {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000063 ASTContext *Context;
Ted Kremenek80de08f2007-09-19 21:29:43 +000064 public:
Ted Kremenek95041a22007-12-19 22:51:13 +000065 void Initialize(ASTContext &Context) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000066 this->Context = &Context;
Ted Kremenek80de08f2007-09-19 21:29:43 +000067 }
Chris Lattner682bf922009-03-29 16:50:03 +000068
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +000069 virtual bool HandleTopLevelDecl(DeclGroupRef D) {
Chris Lattner682bf922009-03-29 16:50:03 +000070 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
71 HandleTopLevelSingleDecl(*I);
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +000072 return true;
Chris Lattner682bf922009-03-29 16:50:03 +000073 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Chris Lattner682bf922009-03-29 16:50:03 +000075 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +000076 };
77}
78
Chris Lattner682bf922009-03-29 16:50:03 +000079void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Argyrios Kyrtzidis9d96f922010-07-07 11:31:23 +000080 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
81 D->print(llvm::errs());
82
83 if (Stmt *Body = D->getBody()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000084 llvm::errs() << '\n';
Douglas Gregoraf3280f2009-09-12 00:08:48 +000085 Body->viewAST();
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000086 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +000087 }
Chris Lattnerb23ff6b2009-03-28 05:44:17 +000088 }
89}
90
91
Ted Kremenek80de08f2007-09-19 21:29:43 +000092ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
93
Ted Kremenek74bf2c92007-09-07 23:47:56 +000094//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +000095/// DeclContextPrinter - Decl and DeclContext Visualization
96
97namespace {
98
99class DeclContextPrinter : public ASTConsumer {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000100 raw_ostream& Out;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000101public:
102 DeclContextPrinter() : Out(llvm::errs()) {}
103
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000104 void HandleTranslationUnit(ASTContext &C) {
105 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000106 }
107
108 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
109};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000110} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000111
Mike Stump1eb44332009-09-09 15:08:12 +0000112void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000113 unsigned Indentation) {
114 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000115 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000116 case Decl::TranslationUnit:
117 Out << "[translation unit] " << DC;
118 break;
119 case Decl::Namespace: {
120 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000121 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000122 Out << *ND;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000123 break;
124 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000125 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000126 const EnumDecl* ED = cast<EnumDecl>(DC);
John McCall5e1cdac2011-10-07 06:10:15 +0000127 if (ED->isCompleteDefinition())
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000128 Out << "[enum] ";
129 else
130 Out << "<enum> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000131 Out << *ED;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000132 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000133 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000134 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000135 const RecordDecl* RD = cast<RecordDecl>(DC);
John McCall5e1cdac2011-10-07 06:10:15 +0000136 if (RD->isCompleteDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000137 Out << "[struct] ";
138 else
139 Out << "<struct> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000140 Out << *RD;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000141 break;
142 }
143 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000144 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
John McCall5e1cdac2011-10-07 06:10:15 +0000145 if (RD->isCompleteDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000146 Out << "[class] ";
147 else
148 Out << "<class> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000149 Out << *RD << ' ' << DC;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000150 break;
151 }
152 case Decl::ObjCMethod:
153 Out << "[objc method]";
154 break;
155 case Decl::ObjCInterface:
156 Out << "[objc interface]";
157 break;
158 case Decl::ObjCCategory:
159 Out << "[objc category]";
160 break;
161 case Decl::ObjCProtocol:
162 Out << "[objc protocol]";
163 break;
164 case Decl::ObjCImplementation:
165 Out << "[objc implementation]";
166 break;
167 case Decl::ObjCCategoryImpl:
168 Out << "[objc categoryimpl]";
169 break;
170 case Decl::LinkageSpec:
171 Out << "[linkage spec]";
172 break;
173 case Decl::Block:
174 Out << "[block]";
175 break;
176 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000177 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Sean Hunt10620eb2011-05-06 20:44:56 +0000178 if (FD->doesThisDeclarationHaveABody())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000179 Out << "[function] ";
180 else
181 Out << "<function> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000182 Out << *FD;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000183 // Print the parameters.
184 Out << "(";
185 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000186 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000187 E = FD->param_end(); I != E; ++I) {
188 if (PrintComma)
189 Out << ", ";
190 else
191 PrintComma = true;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000192 Out << **I;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000193 }
194 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000195 break;
196 }
197 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000198 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000199 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000200 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000201 else if (D->isImplicit())
202 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000203 else
204 Out << "<c++ method> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000205 Out << *D;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000206 // Print the parameters.
207 Out << "(";
208 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000209 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000210 E = D->param_end(); I != E; ++I) {
211 if (PrintComma)
212 Out << ", ";
213 else
214 PrintComma = true;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000215 Out << **I;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000216 }
217 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000218
219 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000220 const DeclContext* SemaDC = D->getDeclContext();
221 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000222 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000223 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000224
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000225 break;
226 }
227 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000228 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000229 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000230 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000231 else if (D->isImplicit())
232 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000233 else
234 Out << "<c++ ctor> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000235 Out << *D;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000236 // Print the parameters.
237 Out << "(";
238 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000239 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000240 E = D->param_end(); I != E; ++I) {
241 if (PrintComma)
242 Out << ", ";
243 else
244 PrintComma = true;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000245 Out << **I;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000246 }
247 Out << ")";
248
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000249 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000250 const DeclContext* SemaDC = D->getDeclContext();
251 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000252 if (SemaDC != LexicalDC)
253 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000254 break;
255 }
256 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000257 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000258 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000259 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000260 else if (D->isImplicit())
261 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000262 else
263 Out << "<c++ dtor> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000264 Out << *D;
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000265 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000266 const DeclContext* SemaDC = D->getDeclContext();
267 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000268 if (SemaDC != LexicalDC)
269 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000270 break;
271 }
272 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000273 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000274 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000275 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000276 else if (D->isImplicit())
277 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000278 else
279 Out << "<c++ conversion> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000280 Out << *D;
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000281 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000282 const DeclContext* SemaDC = D->getDeclContext();
283 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000284 if (SemaDC != LexicalDC)
285 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000286 break;
287 }
288
289 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000290 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000291 }
292
293 Out << "\n";
294
295 // Print decls in the DeclContext.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000296 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000297 I != E; ++I) {
298 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000299 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000300
301 Decl::Kind DK = I->getKind();
302 switch (DK) {
303 case Decl::Namespace:
304 case Decl::Enum:
305 case Decl::Record:
306 case Decl::CXXRecord:
307 case Decl::ObjCMethod:
308 case Decl::ObjCInterface:
Mike Stump1eb44332009-09-09 15:08:12 +0000309 case Decl::ObjCCategory:
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000310 case Decl::ObjCProtocol:
311 case Decl::ObjCImplementation:
312 case Decl::ObjCCategoryImpl:
313 case Decl::LinkageSpec:
314 case Decl::Block:
315 case Decl::Function:
316 case Decl::CXXMethod:
317 case Decl::CXXConstructor:
318 case Decl::CXXDestructor:
319 case Decl::CXXConversion:
320 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000321 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000322 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000323 break;
324 }
Francois Picheta3d39c02010-12-21 03:08:02 +0000325 case Decl::IndirectField: {
326 IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000327 Out << "<IndirectField> " << *IFD << '\n';
Francois Picheta3d39c02010-12-21 03:08:02 +0000328 break;
329 }
Chris Lattner21ac10d2011-02-18 00:52:55 +0000330 case Decl::Label: {
331 LabelDecl *LD = cast<LabelDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000332 Out << "<Label> " << *LD << '\n';
Chris Lattner21ac10d2011-02-18 00:52:55 +0000333 break;
334 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000335 case Decl::Field: {
Chris Lattner21ac10d2011-02-18 00:52:55 +0000336 FieldDecl *FD = cast<FieldDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000337 Out << "<field> " << *FD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000338 break;
339 }
Richard Smith162e1c12011-04-15 14:24:37 +0000340 case Decl::Typedef:
341 case Decl::TypeAlias: {
342 TypedefNameDecl* TD = cast<TypedefNameDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000343 Out << "<typedef> " << *TD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000344 break;
345 }
346 case Decl::EnumConstant: {
347 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000348 Out << "<enum constant> " << *ECD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000349 break;
350 }
351 case Decl::Var: {
352 VarDecl* VD = cast<VarDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000353 Out << "<var> " << *VD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000354 break;
355 }
356 case Decl::ImplicitParam: {
357 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000358 Out << "<implicit parameter> " << *IPD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000359 break;
360 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000361 case Decl::ParmVar: {
362 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000363 Out << "<parameter> " << *PVD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000364 break;
365 }
366 case Decl::ObjCProperty: {
367 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000368 Out << "<objc property> " << *OPD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000369 break;
370 }
Eli Friedman7ac1c9e2009-12-08 06:22:37 +0000371 case Decl::FunctionTemplate: {
372 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000373 Out << "<function template> " << *FTD << '\n';
Eli Friedman7ac1c9e2009-12-08 06:22:37 +0000374 break;
375 }
Eli Friedman368a55d2010-01-03 02:01:11 +0000376 case Decl::FileScopeAsm: {
377 Out << "<file-scope asm>\n";
378 break;
379 }
380 case Decl::UsingDirective: {
381 Out << "<using directive>\n";
382 break;
383 }
384 case Decl::NamespaceAlias: {
385 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000386 Out << "<namespace alias> " << *NAD << '\n';
Eli Friedman368a55d2010-01-03 02:01:11 +0000387 break;
388 }
Zhongxing Xu2f3cd9c2010-01-20 03:21:28 +0000389 case Decl::ClassTemplate: {
390 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000391 Out << "<class template> " << *CTD << '\n';
Zhongxing Xu2f3cd9c2010-01-20 03:21:28 +0000392 break;
393 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000394 default:
Benjamin Kramer900fc632010-04-17 09:33:03 +0000395 Out << "DeclKind: " << DK << '"' << *I << "\"\n";
David Blaikieb219cfc2011-09-23 05:06:16 +0000396 llvm_unreachable("decl unhandled");
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000397 }
398 }
399}
Mike Stump1eb44332009-09-09 15:08:12 +0000400ASTConsumer *clang::CreateDeclContextPrinter() {
401 return new DeclContextPrinter();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000402}
403
404//===----------------------------------------------------------------------===//
John McCallf3514242010-11-24 11:21:45 +0000405/// ASTDumperXML - In-depth XML dumping.
406
407namespace {
408class ASTDumpXML : public ASTConsumer {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000409 raw_ostream &OS;
John McCallf3514242010-11-24 11:21:45 +0000410
411public:
Chris Lattner5f9e2722011-07-23 10:55:15 +0000412 ASTDumpXML(raw_ostream &OS) : OS(OS) {}
John McCallf3514242010-11-24 11:21:45 +0000413
414 void HandleTranslationUnit(ASTContext &C) {
415 C.getTranslationUnitDecl()->dumpXML(OS);
416 }
417};
418}
419
Chris Lattner5f9e2722011-07-23 10:55:15 +0000420ASTConsumer *clang::CreateASTDumperXML(raw_ostream &OS) {
John McCallf3514242010-11-24 11:21:45 +0000421 return new ASTDumpXML(OS);
422}