blob: 51e376ffe061d857874e3d3be35c0ea447285141 [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"
Douglas Gregoree75c052009-05-21 20:55:50 +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"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000024#include "clang/CodeGen/ModuleBuilder.h"
25#include "llvm/Module.h"
Daniel Dunbard46075f2008-10-21 23:54:00 +000026#include "llvm/Support/Streams.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;
39
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000040 public:
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000041 ASTPrinter(llvm::raw_ostream* o = NULL, bool Dump = false)
42 : Out(o? *o : llvm::errs()), Dump(Dump) { }
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +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) {}
65
66 void Initialize(ASTContext &Context) {
67 Doc.initialize(Context);
68 }
69
70 virtual void HandleTranslationUnit(ASTContext &Ctx) {
71 Doc.addSubNode("TranslationUnit");
72 for (DeclContext::decl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000073 D = Ctx.getTranslationUnitDecl()->decls_begin(),
74 DEnd = Ctx.getTranslationUnitDecl()->decls_end();
Douglas Gregoree75c052009-05-21 20:55:50 +000075 D != DEnd;
76 ++D)
77 {
78 Doc.PrintDecl(*D);
79 }
80 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}
90
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000091ASTConsumer *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 }
Ted Kremenek80de08f2007-09-19 21:29:43 +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());
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000118
Douglas Gregor72971342009-04-18 00:02:19 +0000119 if (FD->getBodyIfAvailable()) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000120 llvm::cerr << '\n';
Douglas Gregor72971342009-04-18 00:02:19 +0000121 FD->getBodyIfAvailable()->viewAST();
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000122 llvm::cerr << '\n';
123 }
124 return;
125 }
126
127 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000128 MD->print(llvm::errs());
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000129
130 if (MD->getBody()) {
131 llvm::cerr << '\n';
132 MD->getBody()->viewAST();
133 llvm::cerr << '\n';
134 }
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
159void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
160 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;
233 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
234 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;
256 for (FunctionDecl::param_const_iterator I = D->param_begin(),
257 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;
286 for (FunctionDecl::param_const_iterator I = D->param_begin(),
287 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.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000343 // FIXME: Should not use a NULL DeclContext!
344 ASTContext *Context = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000345 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000346 I != E; ++I) {
347 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000348 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000349
350 Decl::Kind DK = I->getKind();
351 switch (DK) {
352 case Decl::Namespace:
353 case Decl::Enum:
354 case Decl::Record:
355 case Decl::CXXRecord:
356 case Decl::ObjCMethod:
357 case Decl::ObjCInterface:
358 case Decl::ObjCCategory:
359 case Decl::ObjCProtocol:
360 case Decl::ObjCImplementation:
361 case Decl::ObjCCategoryImpl:
362 case Decl::LinkageSpec:
363 case Decl::Block:
364 case Decl::Function:
365 case Decl::CXXMethod:
366 case Decl::CXXConstructor:
367 case Decl::CXXDestructor:
368 case Decl::CXXConversion:
369 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000370 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000371 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000372 break;
373 }
374 case Decl::Field: {
375 FieldDecl* FD = cast<FieldDecl>(*I);
376 Out << "<field> " << FD->getNameAsString() << "\n";
377 break;
378 }
379 case Decl::Typedef: {
380 TypedefDecl* TD = cast<TypedefDecl>(*I);
381 Out << "<typedef> " << TD->getNameAsString() << "\n";
382 break;
383 }
384 case Decl::EnumConstant: {
385 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
386 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
387 break;
388 }
389 case Decl::Var: {
390 VarDecl* VD = cast<VarDecl>(*I);
391 Out << "<var> " << VD->getNameAsString() << "\n";
392 break;
393 }
394 case Decl::ImplicitParam: {
395 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
396 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
397 break;
398 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000399 case Decl::ParmVar: {
400 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
401 Out << "<parameter> " << PVD->getNameAsString() << "\n";
402 break;
403 }
Zhongxing Xuba16be92009-04-05 02:04:38 +0000404 case Decl::OriginalParmVar: {
405 OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I);
406 Out << "<original parameter> " << OPVD->getNameAsString() << "\n";
407 break;
408 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000409 case Decl::ObjCProperty: {
410 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
411 Out << "<objc property> " << OPD->getNameAsString() << "\n";
412 break;
413 }
414 default:
Zhongxing Xuba16be92009-04-05 02:04:38 +0000415 fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName());
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000416 assert(0 && "decl unhandled");
417 }
418 }
419}
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000420ASTConsumer *clang::CreateDeclContextPrinter() {
421 return new DeclContextPrinter();
422}
423
424//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000425/// InheritanceViewer - C++ Inheritance Visualization
426
427namespace {
428class InheritanceViewer : public ASTConsumer {
429 const std::string clsname;
430public:
431 InheritanceViewer(const std::string& cname) : clsname(cname) {}
432
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000433 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000434 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000435 if (RecordType *T = dyn_cast<RecordType>(*I)) {
436 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
437 // FIXME: This lookup needs to be generalized to handle namespaces and
438 // (when we support them) templates.
439 if (D->getNameAsString() == clsname) {
440 D->viewInheritance(C);
441 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000442 }
443 }
444 }
445};
446}
447
448ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
449 return new InheritanceViewer(clsname);
450}