blob: e90e7fcf7e77ade2ad85b1d72b1c2aec5f99b654 [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 }
406 default:
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000407 Out << "DeclKind: " << DK << '"' << I->getDeclKindName() << "\"\n";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000408 assert(0 && "decl unhandled");
409 }
410 }
411}
Mike Stump1eb44332009-09-09 15:08:12 +0000412ASTConsumer *clang::CreateDeclContextPrinter() {
413 return new DeclContextPrinter();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000414}
415
416//===----------------------------------------------------------------------===//
Anders Carlsson78762eb2009-09-24 18:54:49 +0000417/// RecordLayoutDumper - C++ Record Layout Dumping.
418namespace {
419class RecordLayoutDumper : public ASTConsumer {
420 llvm::raw_ostream& Out;
421
Anders Carlsson8fdccd92009-09-24 23:50:42 +0000422 void PrintOffset(uint64_t Offset, unsigned IndentLevel) {
423 Out << llvm::format("%4d | ", Offset);
424 for (unsigned I = 0; I < IndentLevel * 2; ++I) Out << ' ';
425 }
426
427 void DumpRecordLayoutOffsets(const CXXRecordDecl *RD, ASTContext &C,
428 uint64_t Offset,
429 unsigned IndentLevel, const char* Description,
430 bool IncludeVirtualBases) {
431 const ASTRecordLayout &Info = C.getASTRecordLayout(RD);
432
433 PrintOffset(Offset, IndentLevel);
434 Out << C.getTypeDeclType((CXXRecordDecl *)RD).getAsString();
435 if (Description)
436 Out << ' ' << Description;
437 if (RD->isEmpty())
438 Out << " (empty)";
439 Out << '\n';
440
441 IndentLevel++;
442
443 const CXXRecordDecl *PrimaryBase = Info.getPrimaryBase();
444
445 // Vtable pointer.
446 if (RD->isDynamicClass() && !PrimaryBase) {
447 PrintOffset(Offset, IndentLevel);
448 Out << '(' << RD->getNameAsString() << " vtable pointer)\n";
449 }
450 // Dump (non-virtual) bases
451 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
452 E = RD->bases_end(); I != E; ++I) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000453 assert(!I->getType()->isDependentType() &&
454 "Cannot layout class with dependent bases.");
Anders Carlsson8fdccd92009-09-24 23:50:42 +0000455 if (I->isVirtual())
456 continue;
457
458 const CXXRecordDecl *Base =
459 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
460
461 uint64_t BaseOffset = Offset + Info.getBaseClassOffset(Base) / 8;
462
463 DumpRecordLayoutOffsets(Base, C, BaseOffset, IndentLevel,
464 Base == PrimaryBase ? "(primary base)" : "(base)",
465 /*IncludeVirtualBases=*/false);
466 }
467
468 // Dump fields.
469 uint64_t FieldNo = 0;
470 for (CXXRecordDecl::field_iterator I = RD->field_begin(),
471 E = RD->field_end(); I != E; ++I, ++FieldNo) {
472 const FieldDecl *Field = *I;
473 uint64_t FieldOffset = Offset + Info.getFieldOffset(FieldNo) / 8;
474
475 if (const RecordType *RT = Field->getType()->getAs<RecordType>()) {
476 if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
477 DumpRecordLayoutOffsets(D, C, FieldOffset, IndentLevel,
478 Field->getNameAsCString(),
479 /*IncludeVirtualBases=*/true);
480 continue;
481 }
482 }
483
484 PrintOffset(FieldOffset, IndentLevel);
485 Out << Field->getType().getAsString() << ' ';
486 Out << Field->getNameAsString() << '\n';
487 }
488
489 if (!IncludeVirtualBases)
490 return;
491
492 // Dump virtual bases.
493 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
494 E = RD->vbases_end(); I != E; ++I) {
495 assert(I->isVirtual() && "Found non-virtual class!");
496 const CXXRecordDecl *VBase =
497 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
498
499 uint64_t VBaseOffset = Offset + Info.getVBaseClassOffset(VBase) / 8;
500 DumpRecordLayoutOffsets(VBase, C, VBaseOffset, IndentLevel,
501 VBase == PrimaryBase ?
502 "(primary virtual base)" : "(virtual base)",
503 /*IncludeVirtualBases=*/false);
504 }
505 }
506
507 // FIXME: Maybe this could be useful in ASTContext.cpp.
Anders Carlsson78762eb2009-09-24 18:54:49 +0000508 void DumpRecordLayout(const CXXRecordDecl *RD, ASTContext &C) {
509 const ASTRecordLayout &Info = C.getASTRecordLayout(RD);
510
Anders Carlsson8fdccd92009-09-24 23:50:42 +0000511 DumpRecordLayoutOffsets(RD, C, 0, 0, 0,
512 /*IncludeVirtualBases=*/true);
Anders Carlsson78762eb2009-09-24 18:54:49 +0000513 Out << " sizeof=" << Info.getSize() / 8;
514 Out << ", dsize=" << Info.getDataSize() / 8;
515 Out << ", align=" << Info.getAlignment() / 8 << '\n';
516 Out << " nvsize=" << Info.getNonVirtualSize() / 8;
517 Out << ", nvalign=" << Info.getNonVirtualAlign() / 8 << '\n';
518 Out << '\n';
519 }
520
521public:
522 RecordLayoutDumper() : Out(llvm::errs()) {}
523
524 void HandleTranslationUnit(ASTContext &C) {
525 for (ASTContext::type_iterator I = C.types_begin(), E = C.types_end();
526 I != E; ++I) {
527 const RecordType *RT = dyn_cast<RecordType>(*I);
528 if (!RT)
529 continue;
530
531 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
532 if (!RD)
533 continue;
534
535 if (RD->isImplicit())
536 continue;
537
Anders Carlssona4c60812009-09-25 01:54:38 +0000538 if (RD->isDependentType())
539 continue;
540
Anders Carlsson4f5bf3b2009-09-26 01:02:04 +0000541 if (RD->isInvalidDecl())
542 continue;
543
544 if (!RD->getDefinition(C))
545 continue;
546
Anders Carlsson78762eb2009-09-24 18:54:49 +0000547 // FIXME: Do we really need to hard code this?
548 if (RD->getQualifiedNameAsString() == "__va_list_tag")
549 continue;
550
551 DumpRecordLayout(RD, C);
552 }
553 }
554};
555} // end anonymous namespace
556ASTConsumer *clang::CreateRecordLayoutDumper() {
557 return new RecordLayoutDumper();
558}
559
560//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000561/// InheritanceViewer - C++ Inheritance Visualization
562
563namespace {
564class InheritanceViewer : public ASTConsumer {
565 const std::string clsname;
566public:
567 InheritanceViewer(const std::string& cname) : clsname(cname) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000569 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000570 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000571 if (RecordType *T = dyn_cast<RecordType>(*I)) {
572 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
573 // FIXME: This lookup needs to be generalized to handle namespaces and
574 // (when we support them) templates.
575 if (D->getNameAsString() == clsname) {
Mike Stump1eb44332009-09-09 15:08:12 +0000576 D->viewInheritance(C);
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000577 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000578 }
579 }
580 }
Mike Stump1eb44332009-09-09 15:08:12 +0000581};
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000582}
583
584ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
585 return new InheritanceViewer(clsname);
586}