blob: 026848bcd5ac5f7cba122e19dcf6c2547823a240 [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"
Ted Kremenekcb330932008-02-18 21:21:23 +000026#include "llvm/Support/Timer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000028#include "llvm/System/Path.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000029using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000030
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000031//===----------------------------------------------------------------------===//
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000032/// ASTPrinter - Pretty-printer and dumper of ASTs
Chris Lattner6000dac2007-08-08 22:51:59 +000033
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000034namespace {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000035 class ASTPrinter : public ASTConsumer {
36 llvm::raw_ostream &Out;
37 bool Dump;
38
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000039 public:
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000040 ASTPrinter(llvm::raw_ostream* o = NULL, bool Dump = false)
41 : Out(o? *o : llvm::errs()), Dump(Dump) { }
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000042
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000043 virtual void HandleTranslationUnit(ASTContext &Context) {
44 PrintingPolicy Policy = Context.PrintingPolicy;
45 Policy.Dump = Dump;
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000046 Context.getTranslationUnitDecl()->print(Out, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +000047 }
Chris Lattner3d4997d2007-09-15 23:02:28 +000048 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +000049} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +000050
Ted Kremeneka95d3752008-09-13 05:16:45 +000051ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +000052 return new ASTPrinter(out);
53}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000054
55//===----------------------------------------------------------------------===//
Douglas Gregoree75c052009-05-21 20:55:50 +000056/// ASTPrinterXML - XML-printer of ASTs
57
58namespace {
59 class ASTPrinterXML : public ASTConsumer {
60 DocumentXML Doc;
61
62 public:
63 ASTPrinterXML(llvm::raw_ostream& o) : Doc("CLANG_XML", o) {}
64
65 void Initialize(ASTContext &Context) {
66 Doc.initialize(Context);
67 }
68
69 virtual void HandleTranslationUnit(ASTContext &Ctx) {
70 Doc.addSubNode("TranslationUnit");
71 for (DeclContext::decl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000072 D = Ctx.getTranslationUnitDecl()->decls_begin(),
73 DEnd = Ctx.getTranslationUnitDecl()->decls_end();
Douglas Gregoree75c052009-05-21 20:55:50 +000074 D != DEnd;
75 ++D)
76 {
77 Doc.PrintDecl(*D);
78 }
79 Doc.toParent();
80 Doc.finalize();
81 }
82 };
83} // end anonymous namespace
84
85
86ASTConsumer *clang::CreateASTPrinterXML(llvm::raw_ostream* out) {
87 return new ASTPrinterXML(out ? *out : llvm::outs());
88}
89
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000090ASTConsumer *clang::CreateASTDumper() {
91 return new ASTPrinter(0, true);
Douglas Gregor609e72f2009-04-26 02:02:08 +000092}
Chris Lattner3d4997d2007-09-15 23:02:28 +000093
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000094//===----------------------------------------------------------------------===//
95/// ASTViewer - AST Visualization
96
Ted Kremenek80de08f2007-09-19 21:29:43 +000097namespace {
98 class ASTViewer : public ASTConsumer {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000099 ASTContext *Context;
Ted Kremenek80de08f2007-09-19 21:29:43 +0000100 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000101 void Initialize(ASTContext &Context) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000102 this->Context = &Context;
Ted Kremenek80de08f2007-09-19 21:29:43 +0000103 }
Chris Lattner682bf922009-03-29 16:50:03 +0000104
105 virtual void HandleTopLevelDecl(DeclGroupRef D) {
106 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
107 HandleTopLevelSingleDecl(*I);
108 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000109
Chris Lattner682bf922009-03-29 16:50:03 +0000110 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000111 };
112}
113
Chris Lattner682bf922009-03-29 16:50:03 +0000114void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000115 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000116 FD->print(llvm::errs());
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000117
Douglas Gregor72971342009-04-18 00:02:19 +0000118 if (FD->getBodyIfAvailable()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000119 llvm::errs() << '\n';
Douglas Gregor72971342009-04-18 00:02:19 +0000120 FD->getBodyIfAvailable()->viewAST();
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000121 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000122 }
123 return;
124 }
125
126 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000127 MD->print(llvm::errs());
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000128
129 if (MD->getBody()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000130 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000131 MD->getBody()->viewAST();
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000132 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000133 }
134 }
135}
136
137
Ted Kremenek80de08f2007-09-19 21:29:43 +0000138ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
139
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000140//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000141/// DeclContextPrinter - Decl and DeclContext Visualization
142
143namespace {
144
145class DeclContextPrinter : public ASTConsumer {
146 llvm::raw_ostream& Out;
147public:
148 DeclContextPrinter() : Out(llvm::errs()) {}
149
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000150 void HandleTranslationUnit(ASTContext &C) {
151 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000152 }
153
154 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
155};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000156} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000157
158void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
159 unsigned Indentation) {
160 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000161 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000162 case Decl::TranslationUnit:
163 Out << "[translation unit] " << DC;
164 break;
165 case Decl::Namespace: {
166 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000167 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000168 Out << ND->getNameAsString();
169 break;
170 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000171 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000172 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000173 if (ED->isDefinition())
174 Out << "[enum] ";
175 else
176 Out << "<enum> ";
177 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000178 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000179 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000180 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000181 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000182 if (RD->isDefinition())
183 Out << "[struct] ";
184 else
185 Out << "<struct> ";
186 Out << RD->getNameAsString();
187 break;
188 }
189 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000190 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000191 if (RD->isDefinition())
192 Out << "[class] ";
193 else
194 Out << "<class> ";
195 Out << RD->getNameAsString() << " " << DC;
196 break;
197 }
198 case Decl::ObjCMethod:
199 Out << "[objc method]";
200 break;
201 case Decl::ObjCInterface:
202 Out << "[objc interface]";
203 break;
204 case Decl::ObjCCategory:
205 Out << "[objc category]";
206 break;
207 case Decl::ObjCProtocol:
208 Out << "[objc protocol]";
209 break;
210 case Decl::ObjCImplementation:
211 Out << "[objc implementation]";
212 break;
213 case Decl::ObjCCategoryImpl:
214 Out << "[objc categoryimpl]";
215 break;
216 case Decl::LinkageSpec:
217 Out << "[linkage spec]";
218 break;
219 case Decl::Block:
220 Out << "[block]";
221 break;
222 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000223 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000224 if (FD->isThisDeclarationADefinition())
225 Out << "[function] ";
226 else
227 Out << "<function> ";
228 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000229 // Print the parameters.
230 Out << "(";
231 bool PrintComma = false;
232 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
233 E = FD->param_end(); I != E; ++I) {
234 if (PrintComma)
235 Out << ", ";
236 else
237 PrintComma = true;
238 Out << (*I)->getNameAsString();
239 }
240 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000241 break;
242 }
243 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000244 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000245 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000246 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000247 else if (D->isImplicit())
248 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000249 else
250 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000251 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000252 // Print the parameters.
253 Out << "(";
254 bool PrintComma = false;
255 for (FunctionDecl::param_const_iterator I = D->param_begin(),
256 E = D->param_end(); I != E; ++I) {
257 if (PrintComma)
258 Out << ", ";
259 else
260 PrintComma = true;
261 Out << (*I)->getNameAsString();
262 }
263 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000264
265 // Check the semantic DeclContext.
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)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000269 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000270
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000271 break;
272 }
273 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000274 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000275 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000276 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000277 else if (D->isImplicit())
278 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000279 else
280 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000281 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000282 // Print the parameters.
283 Out << "(";
284 bool PrintComma = false;
285 for (FunctionDecl::param_const_iterator I = D->param_begin(),
286 E = D->param_end(); I != E; ++I) {
287 if (PrintComma)
288 Out << ", ";
289 else
290 PrintComma = true;
291 Out << (*I)->getNameAsString();
292 }
293 Out << ")";
294
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000295 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000296 const DeclContext* SemaDC = D->getDeclContext();
297 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000298 if (SemaDC != LexicalDC)
299 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000300 break;
301 }
302 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000303 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000304 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000305 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000306 else if (D->isImplicit())
307 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000308 else
309 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000310 Out << D->getNameAsString();
311 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000312 const DeclContext* SemaDC = D->getDeclContext();
313 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000314 if (SemaDC != LexicalDC)
315 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000316 break;
317 }
318 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000319 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000320 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000321 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000322 else if (D->isImplicit())
323 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000324 else
325 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000326 Out << D->getNameAsString();
327 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000328 const DeclContext* SemaDC = D->getDeclContext();
329 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000330 if (SemaDC != LexicalDC)
331 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000332 break;
333 }
334
335 default:
336 assert(0 && "a decl that inherits DeclContext isn't handled");
337 }
338
339 Out << "\n";
340
341 // Print decls in the DeclContext.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000342 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000343 I != E; ++I) {
344 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000345 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000346
347 Decl::Kind DK = I->getKind();
348 switch (DK) {
349 case Decl::Namespace:
350 case Decl::Enum:
351 case Decl::Record:
352 case Decl::CXXRecord:
353 case Decl::ObjCMethod:
354 case Decl::ObjCInterface:
355 case Decl::ObjCCategory:
356 case Decl::ObjCProtocol:
357 case Decl::ObjCImplementation:
358 case Decl::ObjCCategoryImpl:
359 case Decl::LinkageSpec:
360 case Decl::Block:
361 case Decl::Function:
362 case Decl::CXXMethod:
363 case Decl::CXXConstructor:
364 case Decl::CXXDestructor:
365 case Decl::CXXConversion:
366 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000367 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000368 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000369 break;
370 }
371 case Decl::Field: {
372 FieldDecl* FD = cast<FieldDecl>(*I);
373 Out << "<field> " << FD->getNameAsString() << "\n";
374 break;
375 }
376 case Decl::Typedef: {
377 TypedefDecl* TD = cast<TypedefDecl>(*I);
378 Out << "<typedef> " << TD->getNameAsString() << "\n";
379 break;
380 }
381 case Decl::EnumConstant: {
382 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
383 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
384 break;
385 }
386 case Decl::Var: {
387 VarDecl* VD = cast<VarDecl>(*I);
388 Out << "<var> " << VD->getNameAsString() << "\n";
389 break;
390 }
391 case Decl::ImplicitParam: {
392 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
393 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
394 break;
395 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000396 case Decl::ParmVar: {
397 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
398 Out << "<parameter> " << PVD->getNameAsString() << "\n";
399 break;
400 }
Zhongxing Xuba16be92009-04-05 02:04:38 +0000401 case Decl::OriginalParmVar: {
402 OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I);
403 Out << "<original parameter> " << OPVD->getNameAsString() << "\n";
404 break;
405 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000406 case Decl::ObjCProperty: {
407 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
408 Out << "<objc property> " << OPD->getNameAsString() << "\n";
409 break;
410 }
411 default:
Zhongxing Xuba16be92009-04-05 02:04:38 +0000412 fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName());
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000413 assert(0 && "decl unhandled");
414 }
415 }
416}
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000417ASTConsumer *clang::CreateDeclContextPrinter() {
418 return new DeclContextPrinter();
419}
420
421//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000422/// InheritanceViewer - C++ Inheritance Visualization
423
424namespace {
425class InheritanceViewer : public ASTConsumer {
426 const std::string clsname;
427public:
428 InheritanceViewer(const std::string& cname) : clsname(cname) {}
429
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000430 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000431 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000432 if (RecordType *T = dyn_cast<RecordType>(*I)) {
433 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
434 // FIXME: This lookup needs to be generalized to handle namespaces and
435 // (when we support them) templates.
436 if (D->getNameAsString() == clsname) {
437 D->viewInheritance(C);
438 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000439 }
440 }
441 }
442};
443}
444
445ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
446 return new InheritanceViewer(clsname);
447}