blob: bf2b3f2319b8906582a447ae0a5c6384e246a8b3 [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"
Nico Weberdae86962008-08-09 18:32:11 +000016#include "clang/Basic/Diagnostic.h"
Ted Kremenek54117722007-12-20 00:34:58 +000017#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000020#include "clang/AST/ASTConsumer.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000021#include "clang/AST/ASTContext.h"
Anders Carlsson78762eb2009-09-24 18:54:49 +000022#include "clang/AST/RecordLayout.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000024#include "llvm/Module.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000025#include "llvm/Support/Timer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000027#include "llvm/System/Path.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000028using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000029
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000030//===----------------------------------------------------------------------===//
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000031/// ASTPrinter - Pretty-printer and dumper of ASTs
Chris Lattner6000dac2007-08-08 22:51:59 +000032
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000033namespace {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000034 class ASTPrinter : public ASTConsumer {
35 llvm::raw_ostream &Out;
36 bool Dump;
Mike Stump1eb44332009-09-09 15:08:12 +000037
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000038 public:
Mike Stump1eb44332009-09-09 15:08:12 +000039 ASTPrinter(llvm::raw_ostream* o = NULL, bool Dump = false)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000040 : Out(o? *o : llvm::errs()), Dump(Dump) { }
Mike Stump1eb44332009-09-09 15:08:12 +000041
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000042 virtual void HandleTranslationUnit(ASTContext &Context) {
43 PrintingPolicy Policy = Context.PrintingPolicy;
44 Policy.Dump = Dump;
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000045 Context.getTranslationUnitDecl()->print(Out, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +000046 }
Chris Lattner3d4997d2007-09-15 23:02:28 +000047 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +000048} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +000049
Ted Kremeneka95d3752008-09-13 05:16:45 +000050ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +000051 return new ASTPrinter(out);
52}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000053
54//===----------------------------------------------------------------------===//
Douglas Gregoree75c052009-05-21 20:55:50 +000055/// ASTPrinterXML - XML-printer of ASTs
56
57namespace {
58 class ASTPrinterXML : public ASTConsumer {
59 DocumentXML Doc;
60
61 public:
62 ASTPrinterXML(llvm::raw_ostream& o) : Doc("CLANG_XML", o) {}
Mike Stump1eb44332009-09-09 15:08:12 +000063
Douglas Gregoree75c052009-05-21 20:55:50 +000064 void Initialize(ASTContext &Context) {
65 Doc.initialize(Context);
66 }
67
68 virtual void HandleTranslationUnit(ASTContext &Ctx) {
69 Doc.addSubNode("TranslationUnit");
Mike Stump1eb44332009-09-09 15:08:12 +000070 for (DeclContext::decl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000071 D = Ctx.getTranslationUnitDecl()->decls_begin(),
72 DEnd = Ctx.getTranslationUnitDecl()->decls_end();
Mike Stump1eb44332009-09-09 15:08:12 +000073 D != DEnd;
Douglas Gregoree75c052009-05-21 20:55:50 +000074 ++D)
Douglas Gregoree75c052009-05-21 20:55:50 +000075 Doc.PrintDecl(*D);
Douglas Gregoree75c052009-05-21 20:55:50 +000076 Doc.toParent();
77 Doc.finalize();
78 }
79 };
80} // end anonymous namespace
81
82
83ASTConsumer *clang::CreateASTPrinterXML(llvm::raw_ostream* out) {
84 return new ASTPrinterXML(out ? *out : llvm::outs());
85}
Mike Stump1eb44332009-09-09 15:08:12 +000086
87ASTConsumer *clang::CreateASTDumper() {
88 return new ASTPrinter(0, true);
Douglas Gregor609e72f2009-04-26 02:02:08 +000089}
Chris Lattner3d4997d2007-09-15 23:02:28 +000090
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000091//===----------------------------------------------------------------------===//
92/// ASTViewer - AST Visualization
93
Ted Kremenek80de08f2007-09-19 21:29:43 +000094namespace {
95 class ASTViewer : public ASTConsumer {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000096 ASTContext *Context;
Ted Kremenek80de08f2007-09-19 21:29:43 +000097 public:
Ted Kremenek95041a22007-12-19 22:51:13 +000098 void Initialize(ASTContext &Context) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000099 this->Context = &Context;
Ted Kremenek80de08f2007-09-19 21:29:43 +0000100 }
Chris Lattner682bf922009-03-29 16:50:03 +0000101
102 virtual void HandleTopLevelDecl(DeclGroupRef D) {
103 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
104 HandleTopLevelSingleDecl(*I);
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Chris Lattner682bf922009-03-29 16:50:03 +0000107 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000108 };
109}
110
Chris Lattner682bf922009-03-29 16:50:03 +0000111void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000112 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000113 FD->print(llvm::errs());
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Douglas Gregoraf3280f2009-09-12 00:08:48 +0000115 if (Stmt *Body = FD->getBody()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000116 llvm::errs() << '\n';
Douglas Gregoraf3280f2009-09-12 00:08:48 +0000117 Body->viewAST();
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000118 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000119 }
120 return;
121 }
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000123 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000124 MD->print(llvm::errs());
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000126 if (MD->getBody()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000127 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000128 MD->getBody()->viewAST();
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000129 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000130 }
131 }
132}
133
134
Ted Kremenek80de08f2007-09-19 21:29:43 +0000135ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
136
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000137//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000138/// DeclContextPrinter - Decl and DeclContext Visualization
139
140namespace {
141
142class DeclContextPrinter : public ASTConsumer {
143 llvm::raw_ostream& Out;
144public:
145 DeclContextPrinter() : Out(llvm::errs()) {}
146
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000147 void HandleTranslationUnit(ASTContext &C) {
148 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000149 }
150
151 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
152};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000153} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000154
Mike Stump1eb44332009-09-09 15:08:12 +0000155void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000156 unsigned Indentation) {
157 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000158 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000159 case Decl::TranslationUnit:
160 Out << "[translation unit] " << DC;
161 break;
162 case Decl::Namespace: {
163 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000164 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000165 Out << ND;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000166 break;
167 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000168 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000169 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000170 if (ED->isDefinition())
171 Out << "[enum] ";
172 else
173 Out << "<enum> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000174 Out << ED;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000175 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000176 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000177 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000178 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000179 if (RD->isDefinition())
180 Out << "[struct] ";
181 else
182 Out << "<struct> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000183 Out << RD;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000184 break;
185 }
186 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000187 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000188 if (RD->isDefinition())
189 Out << "[class] ";
190 else
191 Out << "<class> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000192 Out << RD << ' ' << DC;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000193 break;
194 }
195 case Decl::ObjCMethod:
196 Out << "[objc method]";
197 break;
198 case Decl::ObjCInterface:
199 Out << "[objc interface]";
200 break;
201 case Decl::ObjCCategory:
202 Out << "[objc category]";
203 break;
204 case Decl::ObjCProtocol:
205 Out << "[objc protocol]";
206 break;
207 case Decl::ObjCImplementation:
208 Out << "[objc implementation]";
209 break;
210 case Decl::ObjCCategoryImpl:
211 Out << "[objc categoryimpl]";
212 break;
213 case Decl::LinkageSpec:
214 Out << "[linkage spec]";
215 break;
216 case Decl::Block:
217 Out << "[block]";
218 break;
219 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000220 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000221 if (FD->isThisDeclarationADefinition())
222 Out << "[function] ";
223 else
224 Out << "<function> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000225 Out << FD;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000226 // Print the parameters.
227 Out << "(";
228 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000229 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000230 E = FD->param_end(); I != E; ++I) {
231 if (PrintComma)
232 Out << ", ";
233 else
234 PrintComma = true;
Benjamin Kramer900fc632010-04-17 09:33:03 +0000235 Out << *I;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000236 }
237 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000238 break;
239 }
240 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000241 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000242 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000243 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000244 else if (D->isImplicit())
245 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000246 else
247 Out << "<c++ method> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000248 Out << D;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000249 // Print the parameters.
250 Out << "(";
251 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000252 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000253 E = D->param_end(); I != E; ++I) {
254 if (PrintComma)
255 Out << ", ";
256 else
257 PrintComma = true;
Benjamin Kramer900fc632010-04-17 09:33:03 +0000258 Out << *I;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000259 }
260 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000261
262 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000263 const DeclContext* SemaDC = D->getDeclContext();
264 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000265 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000266 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000267
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000268 break;
269 }
270 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000271 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000272 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000273 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000274 else if (D->isImplicit())
275 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000276 else
277 Out << "<c++ ctor> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000278 Out << D;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000279 // Print the parameters.
280 Out << "(";
281 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000282 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000283 E = D->param_end(); I != E; ++I) {
284 if (PrintComma)
285 Out << ", ";
286 else
287 PrintComma = true;
Benjamin Kramer900fc632010-04-17 09:33:03 +0000288 Out << *I;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000289 }
290 Out << ")";
291
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000292 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000293 const DeclContext* SemaDC = D->getDeclContext();
294 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000295 if (SemaDC != LexicalDC)
296 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000297 break;
298 }
299 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000300 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000301 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000302 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000303 else if (D->isImplicit())
304 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000305 else
306 Out << "<c++ dtor> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000307 Out << D;
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000308 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000309 const DeclContext* SemaDC = D->getDeclContext();
310 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000311 if (SemaDC != LexicalDC)
312 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000313 break;
314 }
315 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000316 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000317 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000318 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000319 else if (D->isImplicit())
320 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000321 else
322 Out << "<c++ conversion> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000323 Out << D;
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000324 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000325 const DeclContext* SemaDC = D->getDeclContext();
326 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000327 if (SemaDC != LexicalDC)
328 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000329 break;
330 }
331
332 default:
333 assert(0 && "a decl that inherits DeclContext isn't handled");
334 }
335
336 Out << "\n";
337
338 // Print decls in the DeclContext.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000339 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000340 I != E; ++I) {
341 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000342 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000343
344 Decl::Kind DK = I->getKind();
345 switch (DK) {
346 case Decl::Namespace:
347 case Decl::Enum:
348 case Decl::Record:
349 case Decl::CXXRecord:
350 case Decl::ObjCMethod:
351 case Decl::ObjCInterface:
Mike Stump1eb44332009-09-09 15:08:12 +0000352 case Decl::ObjCCategory:
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000353 case Decl::ObjCProtocol:
354 case Decl::ObjCImplementation:
355 case Decl::ObjCCategoryImpl:
356 case Decl::LinkageSpec:
357 case Decl::Block:
358 case Decl::Function:
359 case Decl::CXXMethod:
360 case Decl::CXXConstructor:
361 case Decl::CXXDestructor:
362 case Decl::CXXConversion:
363 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000364 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000365 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000366 break;
367 }
368 case Decl::Field: {
369 FieldDecl* FD = cast<FieldDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000370 Out << "<field> " << FD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000371 break;
372 }
373 case Decl::Typedef: {
374 TypedefDecl* TD = cast<TypedefDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000375 Out << "<typedef> " << TD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000376 break;
377 }
378 case Decl::EnumConstant: {
379 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000380 Out << "<enum constant> " << ECD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000381 break;
382 }
383 case Decl::Var: {
384 VarDecl* VD = cast<VarDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000385 Out << "<var> " << VD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000386 break;
387 }
388 case Decl::ImplicitParam: {
389 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000390 Out << "<implicit parameter> " << IPD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000391 break;
392 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000393 case Decl::ParmVar: {
394 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000395 Out << "<parameter> " << PVD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000396 break;
397 }
398 case Decl::ObjCProperty: {
399 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000400 Out << "<objc property> " << OPD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000401 break;
402 }
Eli Friedman7ac1c9e2009-12-08 06:22:37 +0000403 case Decl::FunctionTemplate: {
404 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000405 Out << "<function template> " << FTD << '\n';
Eli Friedman7ac1c9e2009-12-08 06:22:37 +0000406 break;
407 }
Eli Friedman368a55d2010-01-03 02:01:11 +0000408 case Decl::FileScopeAsm: {
409 Out << "<file-scope asm>\n";
410 break;
411 }
412 case Decl::UsingDirective: {
413 Out << "<using directive>\n";
414 break;
415 }
416 case Decl::NamespaceAlias: {
417 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000418 Out << "<namespace alias> " << NAD << '\n';
Eli Friedman368a55d2010-01-03 02:01:11 +0000419 break;
420 }
Zhongxing Xu2f3cd9c2010-01-20 03:21:28 +0000421 case Decl::ClassTemplate: {
422 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000423 Out << "<class template> " << CTD << '\n';
Zhongxing Xu2f3cd9c2010-01-20 03:21:28 +0000424 break;
425 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000426 default:
Benjamin Kramer900fc632010-04-17 09:33:03 +0000427 Out << "DeclKind: " << DK << '"' << *I << "\"\n";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000428 assert(0 && "decl unhandled");
429 }
430 }
431}
Mike Stump1eb44332009-09-09 15:08:12 +0000432ASTConsumer *clang::CreateDeclContextPrinter() {
433 return new DeclContextPrinter();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000434}
435
436//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000437/// InheritanceViewer - C++ Inheritance Visualization
438
439namespace {
440class InheritanceViewer : public ASTConsumer {
441 const std::string clsname;
442public:
443 InheritanceViewer(const std::string& cname) : clsname(cname) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000445 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000446 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000447 if (RecordType *T = dyn_cast<RecordType>(*I)) {
448 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
449 // FIXME: This lookup needs to be generalized to handle namespaces and
450 // (when we support them) templates.
451 if (D->getNameAsString() == clsname) {
Mike Stump1eb44332009-09-09 15:08:12 +0000452 D->viewInheritance(C);
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000453 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000454 }
455 }
456 }
Mike Stump1eb44332009-09-09 15:08:12 +0000457};
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000458}
459
460ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
461 return new InheritanceViewer(clsname);
462}