blob: 80f94397aa88b126a23fe0b8bb7c952b832bc51a [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"
Alexander Kornienkoe34a0522012-07-26 16:01:23 +000015#include "clang/Basic/FileManager.h"
Nico Weberdae86962008-08-09 18:32:11 +000016#include "clang/Basic/Diagnostic.h"
Ted Kremenek54117722007-12-20 00:34:58 +000017#include "clang/Basic/SourceManager.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"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000021#include "clang/AST/PrettyPrinter.h"
Alexander Kornienkoe34a0522012-07-26 16:01:23 +000022#include "clang/AST/RecordLayout.h"
23#include "clang/AST/RecursiveASTVisitor.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000024#include "llvm/Module.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000025#include "llvm/Support/Path.h"
Alexander Kornienkoe34a0522012-07-26 16:01:23 +000026#include "llvm/Support/raw_ostream.h"
27#include "llvm/Support/Timer.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000028using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000029
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000030//===----------------------------------------------------------------------===//
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000031/// ASTPrinter - Pretty-printer and dumper of ASTs
Chris Lattner6000dac2007-08-08 22:51:59 +000032
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000033namespace {
Alexander Kornienkoe34a0522012-07-26 16:01:23 +000034 class ASTPrinter : public ASTConsumer,
35 public RecursiveASTVisitor<ASTPrinter> {
36 typedef RecursiveASTVisitor<ASTPrinter> base;
Mike Stump1eb44332009-09-09 15:08:12 +000037
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000038 public:
Alexander Kornienkoe34a0522012-07-26 16:01:23 +000039 ASTPrinter(raw_ostream *Out = NULL, bool Dump = false,
40 StringRef FilterString = "")
41 : Out(Out ? *Out : llvm::outs()), Dump(Dump),
42 FilterString(FilterString) {}
Mike Stump1eb44332009-09-09 15:08:12 +000043
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000044 virtual void HandleTranslationUnit(ASTContext &Context) {
Alexander Kornienkoe34a0522012-07-26 16:01:23 +000045 TranslationUnitDecl *D = Context.getTranslationUnitDecl();
46
47 if (FilterString.empty()) {
48 if (Dump)
49 D->dump(Out);
50 else
51 D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
52 return;
53 }
54
55 TraverseDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +000056 }
Alexander Kornienkoe34a0522012-07-26 16:01:23 +000057
58 bool shouldWalkTypesOfTypeLocs() const { return false; }
59
60 bool TraverseDecl(Decl *D) {
61 if (filterMatches(D)) {
62 Out.changeColor(llvm::raw_ostream::BLUE) <<
63 (Dump ? "Dumping " : "Printing ") << getName(D) << ":\n";
64 Out.resetColor();
65 if (Dump)
66 D->dump(Out);
67 else
68 D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
69 // Don't traverse child nodes to avoid output duplication.
70 return true;
71 }
72 return base::TraverseDecl(D);
73 }
74
75 private:
76 std::string getName(Decl *D) {
77 if (isa<NamedDecl>(D))
78 return cast<NamedDecl>(D)->getQualifiedNameAsString();
79 return "";
80 }
81 bool filterMatches(Decl *D) {
82 return getName(D).find(FilterString) != std::string::npos;
83 }
84
85 raw_ostream &Out;
86 bool Dump;
87 std::string FilterString;
Chris Lattner3d4997d2007-09-15 23:02:28 +000088 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +000089} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +000090
Alexander Kornienkoe34a0522012-07-26 16:01:23 +000091ASTConsumer *clang::CreateASTPrinter(raw_ostream *Out,
92 StringRef FilterString) {
93 return new ASTPrinter(Out, /*Dump=*/ false, FilterString);
Ted Kremenekea75c552007-11-28 21:32:21 +000094}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000095
Alexander Kornienkoe34a0522012-07-26 16:01:23 +000096ASTConsumer *clang::CreateASTDumper(StringRef FilterString) {
97 return new ASTPrinter(0, /*Dump=*/ true, FilterString);
Douglas Gregor609e72f2009-04-26 02:02:08 +000098}
Chris Lattner3d4997d2007-09-15 23:02:28 +000099
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000100//===----------------------------------------------------------------------===//
101/// ASTViewer - AST Visualization
102
Ted Kremenek80de08f2007-09-19 21:29:43 +0000103namespace {
104 class ASTViewer : public ASTConsumer {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000105 ASTContext *Context;
Ted Kremenek80de08f2007-09-19 21:29:43 +0000106 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000107 void Initialize(ASTContext &Context) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000108 this->Context = &Context;
Ted Kremenek80de08f2007-09-19 21:29:43 +0000109 }
Chris Lattner682bf922009-03-29 16:50:03 +0000110
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +0000111 virtual bool HandleTopLevelDecl(DeclGroupRef D) {
Chris Lattner682bf922009-03-29 16:50:03 +0000112 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
113 HandleTopLevelSingleDecl(*I);
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +0000114 return true;
Chris Lattner682bf922009-03-29 16:50:03 +0000115 }
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattner682bf922009-03-29 16:50:03 +0000117 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000118 };
119}
120
Chris Lattner682bf922009-03-29 16:50:03 +0000121void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Argyrios Kyrtzidis9d96f922010-07-07 11:31:23 +0000122 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
123 D->print(llvm::errs());
124
125 if (Stmt *Body = D->getBody()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000126 llvm::errs() << '\n';
Douglas Gregoraf3280f2009-09-12 00:08:48 +0000127 Body->viewAST();
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000128 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000129 }
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000130 }
131}
132
133
Ted Kremenek80de08f2007-09-19 21:29:43 +0000134ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
135
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000136//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000137/// DeclContextPrinter - Decl and DeclContext Visualization
138
139namespace {
140
141class DeclContextPrinter : public ASTConsumer {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000142 raw_ostream& Out;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000143public:
144 DeclContextPrinter() : Out(llvm::errs()) {}
145
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000146 void HandleTranslationUnit(ASTContext &C) {
147 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000148 }
149
150 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
151};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000152} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000153
Mike Stump1eb44332009-09-09 15:08:12 +0000154void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000155 unsigned Indentation) {
156 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000157 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000158 case Decl::TranslationUnit:
159 Out << "[translation unit] " << DC;
160 break;
161 case Decl::Namespace: {
162 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000163 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000164 Out << *ND;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000165 break;
166 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000167 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000168 const EnumDecl* ED = cast<EnumDecl>(DC);
John McCall5e1cdac2011-10-07 06:10:15 +0000169 if (ED->isCompleteDefinition())
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000170 Out << "[enum] ";
171 else
172 Out << "<enum> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000173 Out << *ED;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000174 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000175 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000176 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000177 const RecordDecl* RD = cast<RecordDecl>(DC);
John McCall5e1cdac2011-10-07 06:10:15 +0000178 if (RD->isCompleteDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000179 Out << "[struct] ";
180 else
181 Out << "<struct> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000182 Out << *RD;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000183 break;
184 }
185 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000186 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
John McCall5e1cdac2011-10-07 06:10:15 +0000187 if (RD->isCompleteDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000188 Out << "[class] ";
189 else
190 Out << "<class> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000191 Out << *RD << ' ' << DC;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000192 break;
193 }
194 case Decl::ObjCMethod:
195 Out << "[objc method]";
196 break;
197 case Decl::ObjCInterface:
198 Out << "[objc interface]";
199 break;
200 case Decl::ObjCCategory:
201 Out << "[objc category]";
202 break;
203 case Decl::ObjCProtocol:
204 Out << "[objc protocol]";
205 break;
206 case Decl::ObjCImplementation:
207 Out << "[objc implementation]";
208 break;
209 case Decl::ObjCCategoryImpl:
210 Out << "[objc categoryimpl]";
211 break;
212 case Decl::LinkageSpec:
213 Out << "[linkage spec]";
214 break;
215 case Decl::Block:
216 Out << "[block]";
217 break;
218 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000219 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Sean Hunt10620eb2011-05-06 20:44:56 +0000220 if (FD->doesThisDeclarationHaveABody())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000221 Out << "[function] ";
222 else
223 Out << "<function> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000224 Out << *FD;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000225 // Print the parameters.
226 Out << "(";
227 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000228 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000229 E = FD->param_end(); I != E; ++I) {
230 if (PrintComma)
231 Out << ", ";
232 else
233 PrintComma = true;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000234 Out << **I;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000235 }
236 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000237 break;
238 }
239 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000240 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000241 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000242 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000243 else if (D->isImplicit())
244 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000245 else
246 Out << "<c++ method> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000247 Out << *D;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000248 // Print the parameters.
249 Out << "(";
250 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000251 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000252 E = D->param_end(); I != E; ++I) {
253 if (PrintComma)
254 Out << ", ";
255 else
256 PrintComma = true;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000257 Out << **I;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000258 }
259 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000260
261 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000262 const DeclContext* SemaDC = D->getDeclContext();
263 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000264 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000265 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000266
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000267 break;
268 }
269 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000270 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000271 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000272 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000273 else if (D->isImplicit())
274 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000275 else
276 Out << "<c++ ctor> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000277 Out << *D;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000278 // Print the parameters.
279 Out << "(";
280 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000281 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000282 E = D->param_end(); I != E; ++I) {
283 if (PrintComma)
284 Out << ", ";
285 else
286 PrintComma = true;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000287 Out << **I;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000288 }
289 Out << ")";
290
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000291 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000292 const DeclContext* SemaDC = D->getDeclContext();
293 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000294 if (SemaDC != LexicalDC)
295 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000296 break;
297 }
298 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000299 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000300 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000301 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000302 else if (D->isImplicit())
303 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000304 else
305 Out << "<c++ dtor> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000306 Out << *D;
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000307 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000308 const DeclContext* SemaDC = D->getDeclContext();
309 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000310 if (SemaDC != LexicalDC)
311 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000312 break;
313 }
314 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000315 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000316 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000317 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000318 else if (D->isImplicit())
319 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000320 else
321 Out << "<c++ conversion> ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000322 Out << *D;
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000323 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000324 const DeclContext* SemaDC = D->getDeclContext();
325 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000326 if (SemaDC != LexicalDC)
327 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000328 break;
329 }
330
331 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000332 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000333 }
334
335 Out << "\n";
336
337 // Print decls in the DeclContext.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000338 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000339 I != E; ++I) {
340 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000341 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000342
343 Decl::Kind DK = I->getKind();
344 switch (DK) {
345 case Decl::Namespace:
346 case Decl::Enum:
347 case Decl::Record:
348 case Decl::CXXRecord:
349 case Decl::ObjCMethod:
350 case Decl::ObjCInterface:
Mike Stump1eb44332009-09-09 15:08:12 +0000351 case Decl::ObjCCategory:
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000352 case Decl::ObjCProtocol:
353 case Decl::ObjCImplementation:
354 case Decl::ObjCCategoryImpl:
355 case Decl::LinkageSpec:
356 case Decl::Block:
357 case Decl::Function:
358 case Decl::CXXMethod:
359 case Decl::CXXConstructor:
360 case Decl::CXXDestructor:
361 case Decl::CXXConversion:
362 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000363 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000364 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000365 break;
366 }
Francois Picheta3d39c02010-12-21 03:08:02 +0000367 case Decl::IndirectField: {
368 IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000369 Out << "<IndirectField> " << *IFD << '\n';
Francois Picheta3d39c02010-12-21 03:08:02 +0000370 break;
371 }
Chris Lattner21ac10d2011-02-18 00:52:55 +0000372 case Decl::Label: {
373 LabelDecl *LD = cast<LabelDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000374 Out << "<Label> " << *LD << '\n';
Chris Lattner21ac10d2011-02-18 00:52:55 +0000375 break;
376 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000377 case Decl::Field: {
Chris Lattner21ac10d2011-02-18 00:52:55 +0000378 FieldDecl *FD = cast<FieldDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000379 Out << "<field> " << *FD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000380 break;
381 }
Richard Smith162e1c12011-04-15 14:24:37 +0000382 case Decl::Typedef:
383 case Decl::TypeAlias: {
384 TypedefNameDecl* TD = cast<TypedefNameDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000385 Out << "<typedef> " << *TD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000386 break;
387 }
388 case Decl::EnumConstant: {
389 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000390 Out << "<enum constant> " << *ECD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000391 break;
392 }
393 case Decl::Var: {
394 VarDecl* VD = cast<VarDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000395 Out << "<var> " << *VD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000396 break;
397 }
398 case Decl::ImplicitParam: {
399 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000400 Out << "<implicit parameter> " << *IPD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000401 break;
402 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000403 case Decl::ParmVar: {
404 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000405 Out << "<parameter> " << *PVD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000406 break;
407 }
408 case Decl::ObjCProperty: {
409 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000410 Out << "<objc property> " << *OPD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000411 break;
412 }
Eli Friedman7ac1c9e2009-12-08 06:22:37 +0000413 case Decl::FunctionTemplate: {
414 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000415 Out << "<function template> " << *FTD << '\n';
Eli Friedman7ac1c9e2009-12-08 06:22:37 +0000416 break;
417 }
Eli Friedman368a55d2010-01-03 02:01:11 +0000418 case Decl::FileScopeAsm: {
419 Out << "<file-scope asm>\n";
420 break;
421 }
422 case Decl::UsingDirective: {
423 Out << "<using directive>\n";
424 break;
425 }
426 case Decl::NamespaceAlias: {
427 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000428 Out << "<namespace alias> " << *NAD << '\n';
Eli Friedman368a55d2010-01-03 02:01:11 +0000429 break;
430 }
Zhongxing Xu2f3cd9c2010-01-20 03:21:28 +0000431 case Decl::ClassTemplate: {
432 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(*I);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000433 Out << "<class template> " << *CTD << '\n';
Zhongxing Xu2f3cd9c2010-01-20 03:21:28 +0000434 break;
435 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000436 default:
Benjamin Kramer900fc632010-04-17 09:33:03 +0000437 Out << "DeclKind: " << DK << '"' << *I << "\"\n";
David Blaikieb219cfc2011-09-23 05:06:16 +0000438 llvm_unreachable("decl unhandled");
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000439 }
440 }
441}
Mike Stump1eb44332009-09-09 15:08:12 +0000442ASTConsumer *clang::CreateDeclContextPrinter() {
443 return new DeclContextPrinter();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000444}
445
446//===----------------------------------------------------------------------===//
John McCallf3514242010-11-24 11:21:45 +0000447/// ASTDumperXML - In-depth XML dumping.
448
449namespace {
450class ASTDumpXML : public ASTConsumer {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000451 raw_ostream &OS;
John McCallf3514242010-11-24 11:21:45 +0000452
453public:
Chris Lattner5f9e2722011-07-23 10:55:15 +0000454 ASTDumpXML(raw_ostream &OS) : OS(OS) {}
John McCallf3514242010-11-24 11:21:45 +0000455
456 void HandleTranslationUnit(ASTContext &C) {
457 C.getTranslationUnitDecl()->dumpXML(OS);
458 }
459};
460}
461
Chris Lattner5f9e2722011-07-23 10:55:15 +0000462ASTConsumer *clang::CreateASTDumperXML(raw_ostream &OS) {
John McCallf3514242010-11-24 11:21:45 +0000463 return new ASTDumpXML(OS);
464}