blob: 9186e4572ffb260e2c49ab71a62d47e02ce28213 [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"
Mike Stump1eb44332009-09-09 15:08:12 +000015#include "clang/Frontend/DocumentXML.h"
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000016#include "clang/Frontend/PathDiagnosticClients.h"
Nico Weberdae86962008-08-09 18:32:11 +000017#include "clang/Basic/Diagnostic.h"
Ted Kremenek54117722007-12-20 00:34:58 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000021#include "clang/AST/ASTConsumer.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000022#include "clang/AST/ASTContext.h"
Anders Carlsson78762eb2009-09-24 18:54:49 +000023#include "clang/AST/RecordLayout.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000024#include "clang/AST/PrettyPrinter.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000025#include "clang/CodeGen/ModuleBuilder.h"
26#include "llvm/Module.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000027#include "llvm/Support/Timer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000029#include "llvm/System/Path.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000030using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000031
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000032//===----------------------------------------------------------------------===//
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000033/// ASTPrinter - Pretty-printer and dumper of ASTs
Chris Lattner6000dac2007-08-08 22:51:59 +000034
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000035namespace {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000036 class ASTPrinter : public ASTConsumer {
37 llvm::raw_ostream &Out;
38 bool Dump;
Mike Stump1eb44332009-09-09 15:08:12 +000039
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000040 public:
Mike Stump1eb44332009-09-09 15:08:12 +000041 ASTPrinter(llvm::raw_ostream* o = NULL, bool Dump = false)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000042 : Out(o? *o : llvm::errs()), Dump(Dump) { }
Mike Stump1eb44332009-09-09 15:08:12 +000043
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000044 virtual void HandleTranslationUnit(ASTContext &Context) {
45 PrintingPolicy Policy = Context.PrintingPolicy;
46 Policy.Dump = Dump;
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000047 Context.getTranslationUnitDecl()->print(Out, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +000048 }
Chris Lattner3d4997d2007-09-15 23:02:28 +000049 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +000050} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +000051
Ted Kremeneka95d3752008-09-13 05:16:45 +000052ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +000053 return new ASTPrinter(out);
54}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000055
56//===----------------------------------------------------------------------===//
Douglas Gregoree75c052009-05-21 20:55:50 +000057/// ASTPrinterXML - XML-printer of ASTs
58
59namespace {
60 class ASTPrinterXML : public ASTConsumer {
61 DocumentXML Doc;
62
63 public:
64 ASTPrinterXML(llvm::raw_ostream& o) : Doc("CLANG_XML", o) {}
Mike Stump1eb44332009-09-09 15:08:12 +000065
Douglas Gregoree75c052009-05-21 20:55:50 +000066 void Initialize(ASTContext &Context) {
67 Doc.initialize(Context);
68 }
69
70 virtual void HandleTranslationUnit(ASTContext &Ctx) {
71 Doc.addSubNode("TranslationUnit");
Mike Stump1eb44332009-09-09 15:08:12 +000072 for (DeclContext::decl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000073 D = Ctx.getTranslationUnitDecl()->decls_begin(),
74 DEnd = Ctx.getTranslationUnitDecl()->decls_end();
Mike Stump1eb44332009-09-09 15:08:12 +000075 D != DEnd;
Douglas Gregoree75c052009-05-21 20:55:50 +000076 ++D)
Douglas Gregoree75c052009-05-21 20:55:50 +000077 Doc.PrintDecl(*D);
Douglas Gregoree75c052009-05-21 20:55:50 +000078 Doc.toParent();
79 Doc.finalize();
80 }
81 };
82} // end anonymous namespace
83
84
85ASTConsumer *clang::CreateASTPrinterXML(llvm::raw_ostream* out) {
86 return new ASTPrinterXML(out ? *out : llvm::outs());
87}
Mike Stump1eb44332009-09-09 15:08:12 +000088
89ASTConsumer *clang::CreateASTDumper() {
90 return new ASTPrinter(0, true);
Douglas Gregor609e72f2009-04-26 02:02:08 +000091}
Chris Lattner3d4997d2007-09-15 23:02:28 +000092
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000093//===----------------------------------------------------------------------===//
94/// ASTViewer - AST Visualization
95
Ted Kremenek80de08f2007-09-19 21:29:43 +000096namespace {
97 class ASTViewer : public ASTConsumer {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000098 ASTContext *Context;
Ted Kremenek80de08f2007-09-19 21:29:43 +000099 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000100 void Initialize(ASTContext &Context) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000101 this->Context = &Context;
Ted Kremenek80de08f2007-09-19 21:29:43 +0000102 }
Chris Lattner682bf922009-03-29 16:50:03 +0000103
104 virtual void HandleTopLevelDecl(DeclGroupRef D) {
105 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
106 HandleTopLevelSingleDecl(*I);
107 }
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner682bf922009-03-29 16:50:03 +0000109 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000110 };
111}
112
Chris Lattner682bf922009-03-29 16:50:03 +0000113void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000114 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000115 FD->print(llvm::errs());
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Douglas Gregoraf3280f2009-09-12 00:08:48 +0000117 if (Stmt *Body = FD->getBody()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000118 llvm::errs() << '\n';
Douglas Gregoraf3280f2009-09-12 00:08:48 +0000119 Body->viewAST();
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000120 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000121 }
122 return;
123 }
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000125 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000126 MD->print(llvm::errs());
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000128 if (MD->getBody()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000129 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000130 MD->getBody()->viewAST();
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000131 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000132 }
133 }
134}
135
136
Ted Kremenek80de08f2007-09-19 21:29:43 +0000137ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
138
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000139//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000140/// DeclContextPrinter - Decl and DeclContext Visualization
141
142namespace {
143
144class DeclContextPrinter : public ASTConsumer {
145 llvm::raw_ostream& Out;
146public:
147 DeclContextPrinter() : Out(llvm::errs()) {}
148
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000149 void HandleTranslationUnit(ASTContext &C) {
150 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000151 }
152
153 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
154};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000155} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000156
Mike Stump1eb44332009-09-09 15:08:12 +0000157void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000158 unsigned Indentation) {
159 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000160 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000161 case Decl::TranslationUnit:
162 Out << "[translation unit] " << DC;
163 break;
164 case Decl::Namespace: {
165 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000166 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000167 Out << ND->getNameAsString();
168 break;
169 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000170 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000171 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000172 if (ED->isDefinition())
173 Out << "[enum] ";
174 else
175 Out << "<enum> ";
176 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000177 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000178 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000179 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000180 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000181 if (RD->isDefinition())
182 Out << "[struct] ";
183 else
184 Out << "<struct> ";
185 Out << RD->getNameAsString();
186 break;
187 }
188 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000189 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000190 if (RD->isDefinition())
191 Out << "[class] ";
192 else
193 Out << "<class> ";
194 Out << RD->getNameAsString() << " " << DC;
195 break;
196 }
197 case Decl::ObjCMethod:
198 Out << "[objc method]";
199 break;
200 case Decl::ObjCInterface:
201 Out << "[objc interface]";
202 break;
203 case Decl::ObjCCategory:
204 Out << "[objc category]";
205 break;
206 case Decl::ObjCProtocol:
207 Out << "[objc protocol]";
208 break;
209 case Decl::ObjCImplementation:
210 Out << "[objc implementation]";
211 break;
212 case Decl::ObjCCategoryImpl:
213 Out << "[objc categoryimpl]";
214 break;
215 case Decl::LinkageSpec:
216 Out << "[linkage spec]";
217 break;
218 case Decl::Block:
219 Out << "[block]";
220 break;
221 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000222 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000223 if (FD->isThisDeclarationADefinition())
224 Out << "[function] ";
225 else
226 Out << "<function> ";
227 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000228 // Print the parameters.
229 Out << "(";
230 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000231 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000232 E = FD->param_end(); I != E; ++I) {
233 if (PrintComma)
234 Out << ", ";
235 else
236 PrintComma = true;
237 Out << (*I)->getNameAsString();
238 }
239 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000240 break;
241 }
242 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000243 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000244 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000245 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000246 else if (D->isImplicit())
247 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000248 else
249 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000250 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000251 // Print the parameters.
252 Out << "(";
253 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000254 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000255 E = D->param_end(); I != E; ++I) {
256 if (PrintComma)
257 Out << ", ";
258 else
259 PrintComma = true;
260 Out << (*I)->getNameAsString();
261 }
262 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000263
264 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000265 const DeclContext* SemaDC = D->getDeclContext();
266 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000267 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000268 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000269
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000270 break;
271 }
272 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000273 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000274 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000275 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000276 else if (D->isImplicit())
277 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000278 else
279 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000280 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000281 // Print the parameters.
282 Out << "(";
283 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000284 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000285 E = D->param_end(); I != E; ++I) {
286 if (PrintComma)
287 Out << ", ";
288 else
289 PrintComma = true;
290 Out << (*I)->getNameAsString();
291 }
292 Out << ")";
293
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000294 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000295 const DeclContext* SemaDC = D->getDeclContext();
296 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000297 if (SemaDC != LexicalDC)
298 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000299 break;
300 }
301 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000302 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000303 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000304 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000305 else if (D->isImplicit())
306 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000307 else
308 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000309 Out << D->getNameAsString();
310 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000311 const DeclContext* SemaDC = D->getDeclContext();
312 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000313 if (SemaDC != LexicalDC)
314 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000315 break;
316 }
317 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000318 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000319 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000320 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000321 else if (D->isImplicit())
322 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000323 else
324 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000325 Out << D->getNameAsString();
326 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000327 const DeclContext* SemaDC = D->getDeclContext();
328 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000329 if (SemaDC != LexicalDC)
330 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000331 break;
332 }
333
334 default:
335 assert(0 && "a decl that inherits DeclContext isn't handled");
336 }
337
338 Out << "\n";
339
340 // Print decls in the DeclContext.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000341 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000342 I != E; ++I) {
343 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000344 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000345
346 Decl::Kind DK = I->getKind();
347 switch (DK) {
348 case Decl::Namespace:
349 case Decl::Enum:
350 case Decl::Record:
351 case Decl::CXXRecord:
352 case Decl::ObjCMethod:
353 case Decl::ObjCInterface:
Mike Stump1eb44332009-09-09 15:08:12 +0000354 case Decl::ObjCCategory:
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000355 case Decl::ObjCProtocol:
356 case Decl::ObjCImplementation:
357 case Decl::ObjCCategoryImpl:
358 case Decl::LinkageSpec:
359 case Decl::Block:
360 case Decl::Function:
361 case Decl::CXXMethod:
362 case Decl::CXXConstructor:
363 case Decl::CXXDestructor:
364 case Decl::CXXConversion:
365 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000366 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000367 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000368 break;
369 }
370 case Decl::Field: {
371 FieldDecl* FD = cast<FieldDecl>(*I);
372 Out << "<field> " << FD->getNameAsString() << "\n";
373 break;
374 }
375 case Decl::Typedef: {
376 TypedefDecl* TD = cast<TypedefDecl>(*I);
377 Out << "<typedef> " << TD->getNameAsString() << "\n";
378 break;
379 }
380 case Decl::EnumConstant: {
381 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
382 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
383 break;
384 }
385 case Decl::Var: {
386 VarDecl* VD = cast<VarDecl>(*I);
387 Out << "<var> " << VD->getNameAsString() << "\n";
388 break;
389 }
390 case Decl::ImplicitParam: {
391 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
392 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
393 break;
394 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000395 case Decl::ParmVar: {
396 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
397 Out << "<parameter> " << PVD->getNameAsString() << "\n";
398 break;
399 }
400 case Decl::ObjCProperty: {
401 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
402 Out << "<objc property> " << OPD->getNameAsString() << "\n";
403 break;
404 }
Eli Friedman7ac1c9e2009-12-08 06:22:37 +0000405 case Decl::FunctionTemplate: {
406 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I);
407 Out << "<function template> " << FTD->getNameAsString() << "\n";
408 break;
409 }
Eli Friedman368a55d2010-01-03 02:01:11 +0000410 case Decl::FileScopeAsm: {
411 Out << "<file-scope asm>\n";
412 break;
413 }
414 case Decl::UsingDirective: {
415 Out << "<using directive>\n";
416 break;
417 }
418 case Decl::NamespaceAlias: {
419 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I);
420 Out << "<namespace alias> " << NAD->getNameAsString() << "\n";
421 break;
422 }
Zhongxing Xu2f3cd9c2010-01-20 03:21:28 +0000423 case Decl::ClassTemplate: {
424 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(*I);
425 Out << "<class template> " << CTD->getNameAsString() << '\n';
426 break;
427 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000428 default:
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000429 Out << "DeclKind: " << DK << '"' << I->getDeclKindName() << "\"\n";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000430 assert(0 && "decl unhandled");
431 }
432 }
433}
Mike Stump1eb44332009-09-09 15:08:12 +0000434ASTConsumer *clang::CreateDeclContextPrinter() {
435 return new DeclContextPrinter();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000436}
437
438//===----------------------------------------------------------------------===//
Anders Carlsson78762eb2009-09-24 18:54:49 +0000439/// RecordLayoutDumper - C++ Record Layout Dumping.
440namespace {
441class RecordLayoutDumper : public ASTConsumer {
Anders Carlsson78762eb2009-09-24 18:54:49 +0000442public:
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000443 RecordLayoutDumper() {}
444
Anders Carlsson78762eb2009-09-24 18:54:49 +0000445 void HandleTranslationUnit(ASTContext &C) {
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000446 for (ASTContext::type_iterator I = C.types_begin(), E = C.types_end();
Anders Carlsson78762eb2009-09-24 18:54:49 +0000447 I != E; ++I) {
448 const RecordType *RT = dyn_cast<RecordType>(*I);
449 if (!RT)
450 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000451
Anders Carlsson78762eb2009-09-24 18:54:49 +0000452 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000453 if (!RD || RD->isImplicit() || RD->isDependentType() ||
454 RD->isInvalidDecl() || !RD->getDefinition())
Anders Carlsson78762eb2009-09-24 18:54:49 +0000455 continue;
456
457 // FIXME: Do we really need to hard code this?
458 if (RD->getQualifiedNameAsString() == "__va_list_tag")
459 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000460
461 C.DumpRecordLayout(RD, llvm::errs());
Anders Carlsson78762eb2009-09-24 18:54:49 +0000462 }
463 }
464};
465} // end anonymous namespace
466ASTConsumer *clang::CreateRecordLayoutDumper() {
467 return new RecordLayoutDumper();
468}
469
470//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000471/// InheritanceViewer - C++ Inheritance Visualization
472
473namespace {
474class InheritanceViewer : public ASTConsumer {
475 const std::string clsname;
476public:
477 InheritanceViewer(const std::string& cname) : clsname(cname) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000479 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000480 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000481 if (RecordType *T = dyn_cast<RecordType>(*I)) {
482 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
483 // FIXME: This lookup needs to be generalized to handle namespaces and
484 // (when we support them) templates.
485 if (D->getNameAsString() == clsname) {
Mike Stump1eb44332009-09-09 15:08:12 +0000486 D->viewInheritance(C);
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000487 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000488 }
489 }
490 }
Mike Stump1eb44332009-09-09 15:08:12 +0000491};
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000492}
493
494ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
495 return new InheritanceViewer(clsname);
496}