blob: 52b597e8d273df2cdb17cde45c45e73d6c18c80b [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"
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"
Anders Carlsson78762eb2009-09-24 18:54:49 +000023#include "clang/AST/RecordLayout.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000024#include "clang/AST/PrettyPrinter.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000025#include "clang/CodeGen/ModuleBuilder.h"
26#include "llvm/Module.h"
Anders Carlsson8fdccd92009-09-24 23:50:42 +000027#include "llvm/Support/Format.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000028#include "llvm/Support/Timer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000029#include "llvm/Support/raw_ostream.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000030#include "llvm/System/Path.h"
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;
Mike Stump1eb44332009-09-09 15:08:12 +000040
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000041 public:
Mike Stump1eb44332009-09-09 15:08:12 +000042 ASTPrinter(llvm::raw_ostream* o = NULL, bool Dump = false)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000043 : Out(o? *o : llvm::errs()), Dump(Dump) { }
Mike Stump1eb44332009-09-09 15:08:12 +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) {}
Mike Stump1eb44332009-09-09 15:08:12 +000066
Douglas Gregoree75c052009-05-21 20:55:50 +000067 void Initialize(ASTContext &Context) {
68 Doc.initialize(Context);
69 }
70
71 virtual void HandleTranslationUnit(ASTContext &Ctx) {
72 Doc.addSubNode("TranslationUnit");
Mike Stump1eb44332009-09-09 15:08:12 +000073 for (DeclContext::decl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000074 D = Ctx.getTranslationUnitDecl()->decls_begin(),
75 DEnd = Ctx.getTranslationUnitDecl()->decls_end();
Mike Stump1eb44332009-09-09 15:08:12 +000076 D != DEnd;
Douglas Gregoree75c052009-05-21 20:55:50 +000077 ++D)
Douglas Gregoree75c052009-05-21 20:55:50 +000078 Doc.PrintDecl(*D);
Douglas Gregoree75c052009-05-21 20:55:50 +000079 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}
Mike Stump1eb44332009-09-09 15:08:12 +000089
90ASTConsumer *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 }
Mike Stump1eb44332009-09-09 15:08:12 +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());
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregoraf3280f2009-09-12 00:08:48 +0000118 if (Stmt *Body = FD->getBody()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000119 llvm::errs() << '\n';
Douglas Gregoraf3280f2009-09-12 00:08:48 +0000120 Body->viewAST();
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000121 llvm::errs() << '\n';
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000122 }
123 return;
124 }
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000126 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000127 MD->print(llvm::errs());
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000129 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
Mike Stump1eb44332009-09-09 15:08:12 +0000158void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000159 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;
Mike Stump1eb44332009-09-09 15:08:12 +0000232 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000233 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;
Mike Stump1eb44332009-09-09 15:08:12 +0000255 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000256 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;
Mike Stump1eb44332009-09-09 15:08:12 +0000285 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000286 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:
Mike Stump1eb44332009-09-09 15:08:12 +0000355 case Decl::ObjCCategory:
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000356 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 }
401 case Decl::ObjCProperty: {
402 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
403 Out << "<objc property> " << OPD->getNameAsString() << "\n";
404 break;
405 }
Eli Friedman7ac1c9e2009-12-08 06:22:37 +0000406 case Decl::FunctionTemplate: {
407 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I);
408 Out << "<function template> " << FTD->getNameAsString() << "\n";
409 break;
410 }
Eli Friedman368a55d2010-01-03 02:01:11 +0000411 case Decl::FileScopeAsm: {
412 Out << "<file-scope asm>\n";
413 break;
414 }
415 case Decl::UsingDirective: {
416 Out << "<using directive>\n";
417 break;
418 }
419 case Decl::NamespaceAlias: {
420 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I);
421 Out << "<namespace alias> " << NAD->getNameAsString() << "\n";
422 break;
423 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000424 default:
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000425 Out << "DeclKind: " << DK << '"' << I->getDeclKindName() << "\"\n";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000426 assert(0 && "decl unhandled");
427 }
428 }
429}
Mike Stump1eb44332009-09-09 15:08:12 +0000430ASTConsumer *clang::CreateDeclContextPrinter() {
431 return new DeclContextPrinter();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000432}
433
434//===----------------------------------------------------------------------===//
Anders Carlsson78762eb2009-09-24 18:54:49 +0000435/// RecordLayoutDumper - C++ Record Layout Dumping.
436namespace {
437class RecordLayoutDumper : public ASTConsumer {
438 llvm::raw_ostream& Out;
439
Anders Carlsson8fdccd92009-09-24 23:50:42 +0000440 void PrintOffset(uint64_t Offset, unsigned IndentLevel) {
441 Out << llvm::format("%4d | ", Offset);
442 for (unsigned I = 0; I < IndentLevel * 2; ++I) Out << ' ';
443 }
444
445 void DumpRecordLayoutOffsets(const CXXRecordDecl *RD, ASTContext &C,
446 uint64_t Offset,
447 unsigned IndentLevel, const char* Description,
448 bool IncludeVirtualBases) {
449 const ASTRecordLayout &Info = C.getASTRecordLayout(RD);
450
451 PrintOffset(Offset, IndentLevel);
452 Out << C.getTypeDeclType((CXXRecordDecl *)RD).getAsString();
453 if (Description)
454 Out << ' ' << Description;
455 if (RD->isEmpty())
456 Out << " (empty)";
457 Out << '\n';
458
459 IndentLevel++;
460
461 const CXXRecordDecl *PrimaryBase = Info.getPrimaryBase();
462
463 // Vtable pointer.
464 if (RD->isDynamicClass() && !PrimaryBase) {
465 PrintOffset(Offset, IndentLevel);
466 Out << '(' << RD->getNameAsString() << " vtable pointer)\n";
467 }
468 // Dump (non-virtual) bases
469 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
470 E = RD->bases_end(); I != E; ++I) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000471 assert(!I->getType()->isDependentType() &&
472 "Cannot layout class with dependent bases.");
Anders Carlsson8fdccd92009-09-24 23:50:42 +0000473 if (I->isVirtual())
474 continue;
475
476 const CXXRecordDecl *Base =
477 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
478
479 uint64_t BaseOffset = Offset + Info.getBaseClassOffset(Base) / 8;
480
481 DumpRecordLayoutOffsets(Base, C, BaseOffset, IndentLevel,
482 Base == PrimaryBase ? "(primary base)" : "(base)",
483 /*IncludeVirtualBases=*/false);
484 }
485
486 // Dump fields.
487 uint64_t FieldNo = 0;
488 for (CXXRecordDecl::field_iterator I = RD->field_begin(),
489 E = RD->field_end(); I != E; ++I, ++FieldNo) {
490 const FieldDecl *Field = *I;
491 uint64_t FieldOffset = Offset + Info.getFieldOffset(FieldNo) / 8;
492
493 if (const RecordType *RT = Field->getType()->getAs<RecordType>()) {
494 if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
495 DumpRecordLayoutOffsets(D, C, FieldOffset, IndentLevel,
496 Field->getNameAsCString(),
497 /*IncludeVirtualBases=*/true);
498 continue;
499 }
500 }
501
502 PrintOffset(FieldOffset, IndentLevel);
503 Out << Field->getType().getAsString() << ' ';
504 Out << Field->getNameAsString() << '\n';
505 }
506
507 if (!IncludeVirtualBases)
508 return;
509
510 // Dump virtual bases.
511 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
512 E = RD->vbases_end(); I != E; ++I) {
513 assert(I->isVirtual() && "Found non-virtual class!");
514 const CXXRecordDecl *VBase =
515 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
516
517 uint64_t VBaseOffset = Offset + Info.getVBaseClassOffset(VBase) / 8;
518 DumpRecordLayoutOffsets(VBase, C, VBaseOffset, IndentLevel,
519 VBase == PrimaryBase ?
520 "(primary virtual base)" : "(virtual base)",
521 /*IncludeVirtualBases=*/false);
522 }
523 }
524
525 // FIXME: Maybe this could be useful in ASTContext.cpp.
Anders Carlsson78762eb2009-09-24 18:54:49 +0000526 void DumpRecordLayout(const CXXRecordDecl *RD, ASTContext &C) {
527 const ASTRecordLayout &Info = C.getASTRecordLayout(RD);
528
Anders Carlsson8fdccd92009-09-24 23:50:42 +0000529 DumpRecordLayoutOffsets(RD, C, 0, 0, 0,
530 /*IncludeVirtualBases=*/true);
Anders Carlsson78762eb2009-09-24 18:54:49 +0000531 Out << " sizeof=" << Info.getSize() / 8;
532 Out << ", dsize=" << Info.getDataSize() / 8;
533 Out << ", align=" << Info.getAlignment() / 8 << '\n';
534 Out << " nvsize=" << Info.getNonVirtualSize() / 8;
535 Out << ", nvalign=" << Info.getNonVirtualAlign() / 8 << '\n';
536 Out << '\n';
537 }
538
539public:
540 RecordLayoutDumper() : Out(llvm::errs()) {}
541
542 void HandleTranslationUnit(ASTContext &C) {
543 for (ASTContext::type_iterator I = C.types_begin(), E = C.types_end();
544 I != E; ++I) {
545 const RecordType *RT = dyn_cast<RecordType>(*I);
546 if (!RT)
547 continue;
548
549 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
550 if (!RD)
551 continue;
552
553 if (RD->isImplicit())
554 continue;
555
Anders Carlssona4c60812009-09-25 01:54:38 +0000556 if (RD->isDependentType())
557 continue;
558
Anders Carlsson4f5bf3b2009-09-26 01:02:04 +0000559 if (RD->isInvalidDecl())
560 continue;
561
562 if (!RD->getDefinition(C))
563 continue;
564
Anders Carlsson78762eb2009-09-24 18:54:49 +0000565 // FIXME: Do we really need to hard code this?
566 if (RD->getQualifiedNameAsString() == "__va_list_tag")
567 continue;
568
569 DumpRecordLayout(RD, C);
570 }
571 }
572};
573} // end anonymous namespace
574ASTConsumer *clang::CreateRecordLayoutDumper() {
575 return new RecordLayoutDumper();
576}
577
578//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000579/// InheritanceViewer - C++ Inheritance Visualization
580
581namespace {
582class InheritanceViewer : public ASTConsumer {
583 const std::string clsname;
584public:
585 InheritanceViewer(const std::string& cname) : clsname(cname) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000587 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000588 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000589 if (RecordType *T = dyn_cast<RecordType>(*I)) {
590 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
591 // FIXME: This lookup needs to be generalized to handle namespaces and
592 // (when we support them) templates.
593 if (D->getNameAsString() == clsname) {
Mike Stump1eb44332009-09-09 15:08:12 +0000594 D->viewInheritance(C);
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000595 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000596 }
597 }
598 }
Mike Stump1eb44332009-09-09 15:08:12 +0000599};
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000600}
601
602ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
603 return new InheritanceViewer(clsname);
604}