blob: 178f1bf8e1ef690ffbf8872a4050a0bf0ce6015c [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"
Torok Edwinf42e4a62009-08-24 13:25:12 +000029#include <cstdio>
30
Chris Lattner6000dac2007-08-08 22:51:59 +000031using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000032
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000033//===----------------------------------------------------------------------===//
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000034/// ASTPrinter - Pretty-printer and dumper of ASTs
Chris Lattner6000dac2007-08-08 22:51:59 +000035
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000036namespace {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000037 class ASTPrinter : public ASTConsumer {
38 llvm::raw_ostream &Out;
39 bool Dump;
40
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000041 public:
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000042 ASTPrinter(llvm::raw_ostream* o = NULL, bool Dump = false)
43 : Out(o? *o : llvm::errs()), Dump(Dump) { }
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000044
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000045 virtual void HandleTranslationUnit(ASTContext &Context) {
46 PrintingPolicy Policy = Context.PrintingPolicy;
47 Policy.Dump = Dump;
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000048 Context.getTranslationUnitDecl()->print(Out, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +000049 }
Chris Lattner3d4997d2007-09-15 23:02:28 +000050 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +000051} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +000052
Ted Kremeneka95d3752008-09-13 05:16:45 +000053ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +000054 return new ASTPrinter(out);
55}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000056
57//===----------------------------------------------------------------------===//
Douglas Gregoree75c052009-05-21 20:55:50 +000058/// ASTPrinterXML - XML-printer of ASTs
59
60namespace {
61 class ASTPrinterXML : public ASTConsumer {
62 DocumentXML Doc;
63
64 public:
65 ASTPrinterXML(llvm::raw_ostream& o) : Doc("CLANG_XML", o) {}
66
67 void Initialize(ASTContext &Context) {
68 Doc.initialize(Context);
69 }
70
71 virtual void HandleTranslationUnit(ASTContext &Ctx) {
72 Doc.addSubNode("TranslationUnit");
73 for (DeclContext::decl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000074 D = Ctx.getTranslationUnitDecl()->decls_begin(),
75 DEnd = Ctx.getTranslationUnitDecl()->decls_end();
Douglas Gregoree75c052009-05-21 20:55:50 +000076 D != DEnd;
77 ++D)
78 {
79 Doc.PrintDecl(*D);
80 }
81 Doc.toParent();
82 Doc.finalize();
83 }
84 };
85} // end anonymous namespace
86
87
88ASTConsumer *clang::CreateASTPrinterXML(llvm::raw_ostream* out) {
89 return new ASTPrinterXML(out ? *out : llvm::outs());
90}
91
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000092ASTConsumer *clang::CreateASTDumper() {
93 return new ASTPrinter(0, true);
Douglas Gregor609e72f2009-04-26 02:02:08 +000094}
Chris Lattner3d4997d2007-09-15 23:02:28 +000095
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000096//===----------------------------------------------------------------------===//
97/// ASTViewer - AST Visualization
98
Ted Kremenek80de08f2007-09-19 21:29:43 +000099namespace {
100 class ASTViewer : public ASTConsumer {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000101 ASTContext *Context;
Ted Kremenek80de08f2007-09-19 21:29:43 +0000102 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000103 void Initialize(ASTContext &Context) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000104 this->Context = &Context;
Ted Kremenek80de08f2007-09-19 21:29:43 +0000105 }
Chris Lattner682bf922009-03-29 16:50:03 +0000106
107 virtual void HandleTopLevelDecl(DeclGroupRef D) {
108 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
109 HandleTopLevelSingleDecl(*I);
110 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000111
Chris Lattner682bf922009-03-29 16:50:03 +0000112 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000113 };
114}
115
Chris Lattner682bf922009-03-29 16:50:03 +0000116void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000117 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000118 FD->print(llvm::errs());
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000119
Douglas Gregor72971342009-04-18 00:02:19 +0000120 if (FD->getBodyIfAvailable()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000121 llvm::errs() << '\n';
Douglas Gregor72971342009-04-18 00:02:19 +0000122 FD->getBodyIfAvailable()->viewAST();
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000123 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000124 }
125 return;
126 }
127
128 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000129 MD->print(llvm::errs());
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000130
131 if (MD->getBody()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000132 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000133 MD->getBody()->viewAST();
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000134 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000135 }
136 }
137}
138
139
Ted Kremenek80de08f2007-09-19 21:29:43 +0000140ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
141
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000142//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000143/// DeclContextPrinter - Decl and DeclContext Visualization
144
145namespace {
146
147class DeclContextPrinter : public ASTConsumer {
148 llvm::raw_ostream& Out;
149public:
150 DeclContextPrinter() : Out(llvm::errs()) {}
151
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000152 void HandleTranslationUnit(ASTContext &C) {
153 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000154 }
155
156 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
157};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000158} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000159
160void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
161 unsigned Indentation) {
162 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000163 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000164 case Decl::TranslationUnit:
165 Out << "[translation unit] " << DC;
166 break;
167 case Decl::Namespace: {
168 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000169 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000170 Out << ND->getNameAsString();
171 break;
172 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000173 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000174 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000175 if (ED->isDefinition())
176 Out << "[enum] ";
177 else
178 Out << "<enum> ";
179 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000180 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000181 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000182 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000183 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000184 if (RD->isDefinition())
185 Out << "[struct] ";
186 else
187 Out << "<struct> ";
188 Out << RD->getNameAsString();
189 break;
190 }
191 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000192 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000193 if (RD->isDefinition())
194 Out << "[class] ";
195 else
196 Out << "<class> ";
197 Out << RD->getNameAsString() << " " << DC;
198 break;
199 }
200 case Decl::ObjCMethod:
201 Out << "[objc method]";
202 break;
203 case Decl::ObjCInterface:
204 Out << "[objc interface]";
205 break;
206 case Decl::ObjCCategory:
207 Out << "[objc category]";
208 break;
209 case Decl::ObjCProtocol:
210 Out << "[objc protocol]";
211 break;
212 case Decl::ObjCImplementation:
213 Out << "[objc implementation]";
214 break;
215 case Decl::ObjCCategoryImpl:
216 Out << "[objc categoryimpl]";
217 break;
218 case Decl::LinkageSpec:
219 Out << "[linkage spec]";
220 break;
221 case Decl::Block:
222 Out << "[block]";
223 break;
224 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000225 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000226 if (FD->isThisDeclarationADefinition())
227 Out << "[function] ";
228 else
229 Out << "<function> ";
230 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000231 // Print the parameters.
232 Out << "(";
233 bool PrintComma = false;
234 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
235 E = FD->param_end(); I != E; ++I) {
236 if (PrintComma)
237 Out << ", ";
238 else
239 PrintComma = true;
240 Out << (*I)->getNameAsString();
241 }
242 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000243 break;
244 }
245 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000246 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000247 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000248 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000249 else if (D->isImplicit())
250 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000251 else
252 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000253 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000254 // Print the parameters.
255 Out << "(";
256 bool PrintComma = false;
257 for (FunctionDecl::param_const_iterator I = D->param_begin(),
258 E = D->param_end(); I != E; ++I) {
259 if (PrintComma)
260 Out << ", ";
261 else
262 PrintComma = true;
263 Out << (*I)->getNameAsString();
264 }
265 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000266
267 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000268 const DeclContext* SemaDC = D->getDeclContext();
269 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000270 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000271 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000272
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000273 break;
274 }
275 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000276 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000277 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000278 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000279 else if (D->isImplicit())
280 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000281 else
282 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000283 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000284 // Print the parameters.
285 Out << "(";
286 bool PrintComma = false;
287 for (FunctionDecl::param_const_iterator I = D->param_begin(),
288 E = D->param_end(); I != E; ++I) {
289 if (PrintComma)
290 Out << ", ";
291 else
292 PrintComma = true;
293 Out << (*I)->getNameAsString();
294 }
295 Out << ")";
296
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000297 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000298 const DeclContext* SemaDC = D->getDeclContext();
299 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000300 if (SemaDC != LexicalDC)
301 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000302 break;
303 }
304 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000305 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000306 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000307 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000308 else if (D->isImplicit())
309 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000310 else
311 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000312 Out << D->getNameAsString();
313 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000314 const DeclContext* SemaDC = D->getDeclContext();
315 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000316 if (SemaDC != LexicalDC)
317 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000318 break;
319 }
320 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000321 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000322 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000323 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000324 else if (D->isImplicit())
325 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000326 else
327 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000328 Out << D->getNameAsString();
329 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000330 const DeclContext* SemaDC = D->getDeclContext();
331 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000332 if (SemaDC != LexicalDC)
333 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000334 break;
335 }
336
337 default:
338 assert(0 && "a decl that inherits DeclContext isn't handled");
339 }
340
341 Out << "\n";
342
343 // Print decls in the DeclContext.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000344 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000345 I != E; ++I) {
346 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000347 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000348
349 Decl::Kind DK = I->getKind();
350 switch (DK) {
351 case Decl::Namespace:
352 case Decl::Enum:
353 case Decl::Record:
354 case Decl::CXXRecord:
355 case Decl::ObjCMethod:
356 case Decl::ObjCInterface:
357 case Decl::ObjCCategory:
358 case Decl::ObjCProtocol:
359 case Decl::ObjCImplementation:
360 case Decl::ObjCCategoryImpl:
361 case Decl::LinkageSpec:
362 case Decl::Block:
363 case Decl::Function:
364 case Decl::CXXMethod:
365 case Decl::CXXConstructor:
366 case Decl::CXXDestructor:
367 case Decl::CXXConversion:
368 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000369 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000370 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000371 break;
372 }
373 case Decl::Field: {
374 FieldDecl* FD = cast<FieldDecl>(*I);
375 Out << "<field> " << FD->getNameAsString() << "\n";
376 break;
377 }
378 case Decl::Typedef: {
379 TypedefDecl* TD = cast<TypedefDecl>(*I);
380 Out << "<typedef> " << TD->getNameAsString() << "\n";
381 break;
382 }
383 case Decl::EnumConstant: {
384 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
385 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
386 break;
387 }
388 case Decl::Var: {
389 VarDecl* VD = cast<VarDecl>(*I);
390 Out << "<var> " << VD->getNameAsString() << "\n";
391 break;
392 }
393 case Decl::ImplicitParam: {
394 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
395 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
396 break;
397 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000398 case Decl::ParmVar: {
399 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
400 Out << "<parameter> " << PVD->getNameAsString() << "\n";
401 break;
402 }
Zhongxing Xuba16be92009-04-05 02:04:38 +0000403 case Decl::OriginalParmVar: {
404 OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I);
405 Out << "<original parameter> " << OPVD->getNameAsString() << "\n";
406 break;
407 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000408 case Decl::ObjCProperty: {
409 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
410 Out << "<objc property> " << OPD->getNameAsString() << "\n";
411 break;
412 }
413 default:
Zhongxing Xuba16be92009-04-05 02:04:38 +0000414 fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName());
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000415 assert(0 && "decl unhandled");
416 }
417 }
418}
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000419ASTConsumer *clang::CreateDeclContextPrinter() {
420 return new DeclContextPrinter();
421}
422
423//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000424/// InheritanceViewer - C++ Inheritance Visualization
425
426namespace {
427class InheritanceViewer : public ASTConsumer {
428 const std::string clsname;
429public:
430 InheritanceViewer(const std::string& cname) : clsname(cname) {}
431
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000432 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000433 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000434 if (RecordType *T = dyn_cast<RecordType>(*I)) {
435 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
436 // FIXME: This lookup needs to be generalized to handle namespaces and
437 // (when we support them) templates.
438 if (D->getNameAsString() == clsname) {
439 D->viewInheritance(C);
440 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000441 }
442 }
443 }
444};
445}
446
447ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
448 return new InheritanceViewer(clsname);
449}