blob: aec68f3e112a08e64f107c7d8b76eb19d5926d55 [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"
Torok Edwinf42e4a62009-08-24 13:25:12 +000030#include <cstdio>
31
Chris Lattner6000dac2007-08-08 22:51:59 +000032using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000033
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000034//===----------------------------------------------------------------------===//
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000035/// ASTPrinter - Pretty-printer and dumper of ASTs
Chris Lattner6000dac2007-08-08 22:51:59 +000036
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000037namespace {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000038 class ASTPrinter : public ASTConsumer {
39 llvm::raw_ostream &Out;
40 bool Dump;
Mike Stump1eb44332009-09-09 15:08:12 +000041
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000042 public:
Mike Stump1eb44332009-09-09 15:08:12 +000043 ASTPrinter(llvm::raw_ostream* o = NULL, bool Dump = false)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000044 : Out(o? *o : llvm::errs()), Dump(Dump) { }
Mike Stump1eb44332009-09-09 15:08:12 +000045
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000046 virtual void HandleTranslationUnit(ASTContext &Context) {
47 PrintingPolicy Policy = Context.PrintingPolicy;
48 Policy.Dump = Dump;
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000049 Context.getTranslationUnitDecl()->print(Out, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +000050 }
Chris Lattner3d4997d2007-09-15 23:02:28 +000051 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +000052} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +000053
Ted Kremeneka95d3752008-09-13 05:16:45 +000054ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +000055 return new ASTPrinter(out);
56}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000057
58//===----------------------------------------------------------------------===//
Douglas Gregoree75c052009-05-21 20:55:50 +000059/// ASTPrinterXML - XML-printer of ASTs
60
61namespace {
62 class ASTPrinterXML : public ASTConsumer {
63 DocumentXML Doc;
64
65 public:
66 ASTPrinterXML(llvm::raw_ostream& o) : Doc("CLANG_XML", o) {}
Mike Stump1eb44332009-09-09 15:08:12 +000067
Douglas Gregoree75c052009-05-21 20:55:50 +000068 void Initialize(ASTContext &Context) {
69 Doc.initialize(Context);
70 }
71
72 virtual void HandleTranslationUnit(ASTContext &Ctx) {
73 Doc.addSubNode("TranslationUnit");
Mike Stump1eb44332009-09-09 15:08:12 +000074 for (DeclContext::decl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000075 D = Ctx.getTranslationUnitDecl()->decls_begin(),
76 DEnd = Ctx.getTranslationUnitDecl()->decls_end();
Mike Stump1eb44332009-09-09 15:08:12 +000077 D != DEnd;
Douglas Gregoree75c052009-05-21 20:55:50 +000078 ++D)
Douglas Gregoree75c052009-05-21 20:55:50 +000079 Doc.PrintDecl(*D);
Douglas Gregoree75c052009-05-21 20:55:50 +000080 Doc.toParent();
81 Doc.finalize();
82 }
83 };
84} // end anonymous namespace
85
86
87ASTConsumer *clang::CreateASTPrinterXML(llvm::raw_ostream* out) {
88 return new ASTPrinterXML(out ? *out : llvm::outs());
89}
Mike Stump1eb44332009-09-09 15:08:12 +000090
91ASTConsumer *clang::CreateASTDumper() {
92 return new ASTPrinter(0, true);
Douglas Gregor609e72f2009-04-26 02:02:08 +000093}
Chris Lattner3d4997d2007-09-15 23:02:28 +000094
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000095//===----------------------------------------------------------------------===//
96/// ASTViewer - AST Visualization
97
Ted Kremenek80de08f2007-09-19 21:29:43 +000098namespace {
99 class ASTViewer : public ASTConsumer {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000100 ASTContext *Context;
Ted Kremenek80de08f2007-09-19 21:29:43 +0000101 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000102 void Initialize(ASTContext &Context) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000103 this->Context = &Context;
Ted Kremenek80de08f2007-09-19 21:29:43 +0000104 }
Chris Lattner682bf922009-03-29 16:50:03 +0000105
106 virtual void HandleTopLevelDecl(DeclGroupRef D) {
107 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
108 HandleTopLevelSingleDecl(*I);
109 }
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Chris Lattner682bf922009-03-29 16:50:03 +0000111 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000112 };
113}
114
Chris Lattner682bf922009-03-29 16:50:03 +0000115void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000116 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000117 FD->print(llvm::errs());
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Douglas Gregoraf3280f2009-09-12 00:08:48 +0000119 if (Stmt *Body = FD->getBody()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000120 llvm::errs() << '\n';
Douglas Gregoraf3280f2009-09-12 00:08:48 +0000121 Body->viewAST();
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000122 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000123 }
124 return;
125 }
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000127 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000128 MD->print(llvm::errs());
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000130 if (MD->getBody()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000131 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000132 MD->getBody()->viewAST();
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000133 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000134 }
135 }
136}
137
138
Ted Kremenek80de08f2007-09-19 21:29:43 +0000139ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
140
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000141//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000142/// DeclContextPrinter - Decl and DeclContext Visualization
143
144namespace {
145
146class DeclContextPrinter : public ASTConsumer {
147 llvm::raw_ostream& Out;
148public:
149 DeclContextPrinter() : Out(llvm::errs()) {}
150
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000151 void HandleTranslationUnit(ASTContext &C) {
152 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000153 }
154
155 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
156};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000157} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000158
Mike Stump1eb44332009-09-09 15:08:12 +0000159void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000160 unsigned Indentation) {
161 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000162 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000163 case Decl::TranslationUnit:
164 Out << "[translation unit] " << DC;
165 break;
166 case Decl::Namespace: {
167 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000168 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000169 Out << ND->getNameAsString();
170 break;
171 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000172 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000173 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000174 if (ED->isDefinition())
175 Out << "[enum] ";
176 else
177 Out << "<enum> ";
178 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000179 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000180 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000181 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000182 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000183 if (RD->isDefinition())
184 Out << "[struct] ";
185 else
186 Out << "<struct> ";
187 Out << RD->getNameAsString();
188 break;
189 }
190 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000191 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000192 if (RD->isDefinition())
193 Out << "[class] ";
194 else
195 Out << "<class> ";
196 Out << RD->getNameAsString() << " " << DC;
197 break;
198 }
199 case Decl::ObjCMethod:
200 Out << "[objc method]";
201 break;
202 case Decl::ObjCInterface:
203 Out << "[objc interface]";
204 break;
205 case Decl::ObjCCategory:
206 Out << "[objc category]";
207 break;
208 case Decl::ObjCProtocol:
209 Out << "[objc protocol]";
210 break;
211 case Decl::ObjCImplementation:
212 Out << "[objc implementation]";
213 break;
214 case Decl::ObjCCategoryImpl:
215 Out << "[objc categoryimpl]";
216 break;
217 case Decl::LinkageSpec:
218 Out << "[linkage spec]";
219 break;
220 case Decl::Block:
221 Out << "[block]";
222 break;
223 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000224 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000225 if (FD->isThisDeclarationADefinition())
226 Out << "[function] ";
227 else
228 Out << "<function> ";
229 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000230 // Print the parameters.
231 Out << "(";
232 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000233 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000234 E = FD->param_end(); I != E; ++I) {
235 if (PrintComma)
236 Out << ", ";
237 else
238 PrintComma = true;
239 Out << (*I)->getNameAsString();
240 }
241 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000242 break;
243 }
244 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000245 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000246 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000247 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000248 else if (D->isImplicit())
249 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000250 else
251 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000252 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000253 // Print the parameters.
254 Out << "(";
255 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000256 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000257 E = D->param_end(); I != E; ++I) {
258 if (PrintComma)
259 Out << ", ";
260 else
261 PrintComma = true;
262 Out << (*I)->getNameAsString();
263 }
264 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000265
266 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000267 const DeclContext* SemaDC = D->getDeclContext();
268 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000269 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000270 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000271
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000272 break;
273 }
274 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000275 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000276 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000277 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000278 else if (D->isImplicit())
279 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000280 else
281 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000282 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000283 // Print the parameters.
284 Out << "(";
285 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000286 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000287 E = D->param_end(); I != E; ++I) {
288 if (PrintComma)
289 Out << ", ";
290 else
291 PrintComma = true;
292 Out << (*I)->getNameAsString();
293 }
294 Out << ")";
295
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000296 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000297 const DeclContext* SemaDC = D->getDeclContext();
298 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000299 if (SemaDC != LexicalDC)
300 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000301 break;
302 }
303 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000304 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000305 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000306 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000307 else if (D->isImplicit())
308 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000309 else
310 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000311 Out << D->getNameAsString();
312 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000313 const DeclContext* SemaDC = D->getDeclContext();
314 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000315 if (SemaDC != LexicalDC)
316 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000317 break;
318 }
319 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000320 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000321 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000322 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000323 else if (D->isImplicit())
324 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000325 else
326 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000327 Out << D->getNameAsString();
328 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000329 const DeclContext* SemaDC = D->getDeclContext();
330 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000331 if (SemaDC != LexicalDC)
332 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000333 break;
334 }
335
336 default:
337 assert(0 && "a decl that inherits DeclContext isn't handled");
338 }
339
340 Out << "\n";
341
342 // Print decls in the DeclContext.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000343 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000344 I != E; ++I) {
345 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000346 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000347
348 Decl::Kind DK = I->getKind();
349 switch (DK) {
350 case Decl::Namespace:
351 case Decl::Enum:
352 case Decl::Record:
353 case Decl::CXXRecord:
354 case Decl::ObjCMethod:
355 case Decl::ObjCInterface:
Mike Stump1eb44332009-09-09 15:08:12 +0000356 case Decl::ObjCCategory:
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000357 case Decl::ObjCProtocol:
358 case Decl::ObjCImplementation:
359 case Decl::ObjCCategoryImpl:
360 case Decl::LinkageSpec:
361 case Decl::Block:
362 case Decl::Function:
363 case Decl::CXXMethod:
364 case Decl::CXXConstructor:
365 case Decl::CXXDestructor:
366 case Decl::CXXConversion:
367 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000368 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000369 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000370 break;
371 }
372 case Decl::Field: {
373 FieldDecl* FD = cast<FieldDecl>(*I);
374 Out << "<field> " << FD->getNameAsString() << "\n";
375 break;
376 }
377 case Decl::Typedef: {
378 TypedefDecl* TD = cast<TypedefDecl>(*I);
379 Out << "<typedef> " << TD->getNameAsString() << "\n";
380 break;
381 }
382 case Decl::EnumConstant: {
383 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
384 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
385 break;
386 }
387 case Decl::Var: {
388 VarDecl* VD = cast<VarDecl>(*I);
389 Out << "<var> " << VD->getNameAsString() << "\n";
390 break;
391 }
392 case Decl::ImplicitParam: {
393 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
394 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
395 break;
396 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000397 case Decl::ParmVar: {
398 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
399 Out << "<parameter> " << PVD->getNameAsString() << "\n";
400 break;
401 }
Zhongxing Xuba16be92009-04-05 02:04:38 +0000402 case Decl::OriginalParmVar: {
403 OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I);
404 Out << "<original parameter> " << OPVD->getNameAsString() << "\n";
405 break;
406 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000407 case Decl::ObjCProperty: {
408 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
409 Out << "<objc property> " << OPD->getNameAsString() << "\n";
410 break;
411 }
412 default:
Zhongxing Xuba16be92009-04-05 02:04:38 +0000413 fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName());
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000414 assert(0 && "decl unhandled");
415 }
416 }
417}
Mike Stump1eb44332009-09-09 15:08:12 +0000418ASTConsumer *clang::CreateDeclContextPrinter() {
419 return new DeclContextPrinter();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000420}
421
422//===----------------------------------------------------------------------===//
Anders Carlsson78762eb2009-09-24 18:54:49 +0000423/// RecordLayoutDumper - C++ Record Layout Dumping.
424namespace {
425class RecordLayoutDumper : public ASTConsumer {
426 llvm::raw_ostream& Out;
427
428 // FIXME: Maybe this be useful in ASTContext.cpp.
429 void DumpRecordLayout(const CXXRecordDecl *RD, ASTContext &C) {
430 const ASTRecordLayout &Info = C.getASTRecordLayout(RD);
431
432 Out << RD->getKindName() << ' ' << RD->getQualifiedNameAsString() << '\n';
433 Out << " sizeof=" << Info.getSize() / 8;
434 Out << ", dsize=" << Info.getDataSize() / 8;
435 Out << ", align=" << Info.getAlignment() / 8 << '\n';
436 Out << " nvsize=" << Info.getNonVirtualSize() / 8;
437 Out << ", nvalign=" << Info.getNonVirtualAlign() / 8 << '\n';
438 Out << '\n';
439 }
440
441public:
442 RecordLayoutDumper() : Out(llvm::errs()) {}
443
444 void HandleTranslationUnit(ASTContext &C) {
445 for (ASTContext::type_iterator I = C.types_begin(), E = C.types_end();
446 I != E; ++I) {
447 const RecordType *RT = dyn_cast<RecordType>(*I);
448 if (!RT)
449 continue;
450
451 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
452 if (!RD)
453 continue;
454
455 if (RD->isImplicit())
456 continue;
457
458 // FIXME: Do we really need to hard code this?
459 if (RD->getQualifiedNameAsString() == "__va_list_tag")
460 continue;
461
462 DumpRecordLayout(RD, C);
463 }
464 }
465};
466} // end anonymous namespace
467ASTConsumer *clang::CreateRecordLayoutDumper() {
468 return new RecordLayoutDumper();
469}
470
471//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000472/// InheritanceViewer - C++ Inheritance Visualization
473
474namespace {
475class InheritanceViewer : public ASTConsumer {
476 const std::string clsname;
477public:
478 InheritanceViewer(const std::string& cname) : clsname(cname) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000480 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000481 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000482 if (RecordType *T = dyn_cast<RecordType>(*I)) {
483 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
484 // FIXME: This lookup needs to be generalized to handle namespaces and
485 // (when we support them) templates.
486 if (D->getNameAsString() == clsname) {
Mike Stump1eb44332009-09-09 15:08:12 +0000487 D->viewInheritance(C);
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000488 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000489 }
490 }
491 }
Mike Stump1eb44332009-09-09 15:08:12 +0000492};
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000493}
494
495ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
496 return new InheritanceViewer(clsname);
497}