blob: 87b01d4a6a2ae857247afbf41d1bef855c367201 [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) {
Argyrios Kyrtzidis9d96f922010-07-07 11:31:23 +0000112 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
113 D->print(llvm::errs());
114
115 if (Stmt *Body = D->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 }
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000120 }
121}
122
123
Ted Kremenek80de08f2007-09-19 21:29:43 +0000124ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
125
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000126//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000127/// DeclContextPrinter - Decl and DeclContext Visualization
128
129namespace {
130
131class DeclContextPrinter : public ASTConsumer {
132 llvm::raw_ostream& Out;
133public:
134 DeclContextPrinter() : Out(llvm::errs()) {}
135
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000136 void HandleTranslationUnit(ASTContext &C) {
137 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000138 }
139
140 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
141};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000142} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000143
Mike Stump1eb44332009-09-09 15:08:12 +0000144void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000145 unsigned Indentation) {
146 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000147 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000148 case Decl::TranslationUnit:
149 Out << "[translation unit] " << DC;
150 break;
151 case Decl::Namespace: {
152 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000153 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000154 Out << ND;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000155 break;
156 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000157 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000158 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000159 if (ED->isDefinition())
160 Out << "[enum] ";
161 else
162 Out << "<enum> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000163 Out << ED;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000164 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000165 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000166 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000167 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000168 if (RD->isDefinition())
169 Out << "[struct] ";
170 else
171 Out << "<struct> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000172 Out << RD;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000173 break;
174 }
175 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000176 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000177 if (RD->isDefinition())
178 Out << "[class] ";
179 else
180 Out << "<class> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000181 Out << RD << ' ' << DC;
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000182 break;
183 }
184 case Decl::ObjCMethod:
185 Out << "[objc method]";
186 break;
187 case Decl::ObjCInterface:
188 Out << "[objc interface]";
189 break;
190 case Decl::ObjCCategory:
191 Out << "[objc category]";
192 break;
193 case Decl::ObjCProtocol:
194 Out << "[objc protocol]";
195 break;
196 case Decl::ObjCImplementation:
197 Out << "[objc implementation]";
198 break;
199 case Decl::ObjCCategoryImpl:
200 Out << "[objc categoryimpl]";
201 break;
202 case Decl::LinkageSpec:
203 Out << "[linkage spec]";
204 break;
205 case Decl::Block:
206 Out << "[block]";
207 break;
208 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000209 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000210 if (FD->isThisDeclarationADefinition())
211 Out << "[function] ";
212 else
213 Out << "<function> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000214 Out << FD;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000215 // Print the parameters.
216 Out << "(";
217 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000218 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000219 E = FD->param_end(); I != E; ++I) {
220 if (PrintComma)
221 Out << ", ";
222 else
223 PrintComma = true;
Benjamin Kramer900fc632010-04-17 09:33:03 +0000224 Out << *I;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000225 }
226 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000227 break;
228 }
229 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000230 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000231 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000232 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000233 else if (D->isImplicit())
234 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000235 else
236 Out << "<c++ method> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000237 Out << D;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000238 // Print the parameters.
239 Out << "(";
240 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000241 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000242 E = D->param_end(); I != E; ++I) {
243 if (PrintComma)
244 Out << ", ";
245 else
246 PrintComma = true;
Benjamin Kramer900fc632010-04-17 09:33:03 +0000247 Out << *I;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000248 }
249 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000250
251 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000252 const DeclContext* SemaDC = D->getDeclContext();
253 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000254 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000255 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000256
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000257 break;
258 }
259 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000260 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000261 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000262 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000263 else if (D->isImplicit())
264 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000265 else
266 Out << "<c++ ctor> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000267 Out << D;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000268 // Print the parameters.
269 Out << "(";
270 bool PrintComma = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000271 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000272 E = D->param_end(); I != E; ++I) {
273 if (PrintComma)
274 Out << ", ";
275 else
276 PrintComma = true;
Benjamin Kramer900fc632010-04-17 09:33:03 +0000277 Out << *I;
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000278 }
279 Out << ")";
280
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000281 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000282 const DeclContext* SemaDC = D->getDeclContext();
283 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000284 if (SemaDC != LexicalDC)
285 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000286 break;
287 }
288 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000289 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000290 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000291 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000292 else if (D->isImplicit())
293 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000294 else
295 Out << "<c++ dtor> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000296 Out << D;
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::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000305 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000306 if (D->isOutOfLine())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000307 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000308 else if (D->isImplicit())
309 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000310 else
311 Out << "<c++ conversion> ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000312 Out << D;
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000313 // 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
321 default:
322 assert(0 && "a decl that inherits DeclContext isn't handled");
323 }
324
325 Out << "\n";
326
327 // Print decls in the DeclContext.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000328 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000329 I != E; ++I) {
330 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000331 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000332
333 Decl::Kind DK = I->getKind();
334 switch (DK) {
335 case Decl::Namespace:
336 case Decl::Enum:
337 case Decl::Record:
338 case Decl::CXXRecord:
339 case Decl::ObjCMethod:
340 case Decl::ObjCInterface:
Mike Stump1eb44332009-09-09 15:08:12 +0000341 case Decl::ObjCCategory:
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000342 case Decl::ObjCProtocol:
343 case Decl::ObjCImplementation:
344 case Decl::ObjCCategoryImpl:
345 case Decl::LinkageSpec:
346 case Decl::Block:
347 case Decl::Function:
348 case Decl::CXXMethod:
349 case Decl::CXXConstructor:
350 case Decl::CXXDestructor:
351 case Decl::CXXConversion:
352 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000353 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000354 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000355 break;
356 }
357 case Decl::Field: {
358 FieldDecl* FD = cast<FieldDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000359 Out << "<field> " << FD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000360 break;
361 }
362 case Decl::Typedef: {
363 TypedefDecl* TD = cast<TypedefDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000364 Out << "<typedef> " << TD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000365 break;
366 }
367 case Decl::EnumConstant: {
368 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000369 Out << "<enum constant> " << ECD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000370 break;
371 }
372 case Decl::Var: {
373 VarDecl* VD = cast<VarDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000374 Out << "<var> " << VD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000375 break;
376 }
377 case Decl::ImplicitParam: {
378 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000379 Out << "<implicit parameter> " << IPD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000380 break;
381 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000382 case Decl::ParmVar: {
383 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000384 Out << "<parameter> " << PVD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000385 break;
386 }
387 case Decl::ObjCProperty: {
388 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000389 Out << "<objc property> " << OPD << '\n';
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000390 break;
391 }
Eli Friedman7ac1c9e2009-12-08 06:22:37 +0000392 case Decl::FunctionTemplate: {
393 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000394 Out << "<function template> " << FTD << '\n';
Eli Friedman7ac1c9e2009-12-08 06:22:37 +0000395 break;
396 }
Eli Friedman368a55d2010-01-03 02:01:11 +0000397 case Decl::FileScopeAsm: {
398 Out << "<file-scope asm>\n";
399 break;
400 }
401 case Decl::UsingDirective: {
402 Out << "<using directive>\n";
403 break;
404 }
405 case Decl::NamespaceAlias: {
406 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000407 Out << "<namespace alias> " << NAD << '\n';
Eli Friedman368a55d2010-01-03 02:01:11 +0000408 break;
409 }
Zhongxing Xu2f3cd9c2010-01-20 03:21:28 +0000410 case Decl::ClassTemplate: {
411 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(*I);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000412 Out << "<class template> " << CTD << '\n';
Zhongxing Xu2f3cd9c2010-01-20 03:21:28 +0000413 break;
414 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000415 default:
Benjamin Kramer900fc632010-04-17 09:33:03 +0000416 Out << "DeclKind: " << DK << '"' << *I << "\"\n";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000417 assert(0 && "decl unhandled");
418 }
419 }
420}
Mike Stump1eb44332009-09-09 15:08:12 +0000421ASTConsumer *clang::CreateDeclContextPrinter() {
422 return new DeclContextPrinter();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000423}
424
425//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000426/// InheritanceViewer - C++ Inheritance Visualization
427
428namespace {
429class InheritanceViewer : public ASTConsumer {
430 const std::string clsname;
431public:
432 InheritanceViewer(const std::string& cname) : clsname(cname) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000434 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000435 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000436 if (RecordType *T = dyn_cast<RecordType>(*I)) {
437 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
438 // FIXME: This lookup needs to be generalized to handle namespaces and
439 // (when we support them) templates.
440 if (D->getNameAsString() == clsname) {
Mike Stump1eb44332009-09-09 15:08:12 +0000441 D->viewInheritance(C);
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000442 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000443 }
444 }
445 }
Mike Stump1eb44332009-09-09 15:08:12 +0000446};
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000447}
448
449ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
450 return new InheritanceViewer(clsname);
451}