1 //===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // AST Consumer Implementations.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/ASTConsumers.h"
15 #include "clang/Frontend/DocumentXML.h"
16 #include "clang/Frontend/PathDiagnosticClients.h"
17 #include "clang/Basic/Diagnostic.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Basic/FileManager.h"
20 #include "clang/AST/AST.h"
21 #include "clang/AST/ASTConsumer.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/RecordLayout.h"
24 #include "clang/AST/PrettyPrinter.h"
25 #include "clang/CodeGen/ModuleBuilder.h"
26 #include "llvm/Module.h"
27 #include "llvm/Support/Timer.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/System/Path.h"
30 using namespace clang;
32 //===----------------------------------------------------------------------===//
33 /// ASTPrinter - Pretty-printer and dumper of ASTs
36 class ASTPrinter : public ASTConsumer {
37 llvm::raw_ostream &Out;
41 ASTPrinter(llvm::raw_ostream* o = NULL, bool Dump = false)
42 : Out(o? *o : llvm::errs()), Dump(Dump) { }
44 virtual void HandleTranslationUnit(ASTContext &Context) {
45 PrintingPolicy Policy = Context.PrintingPolicy;
47 Context.getTranslationUnitDecl()->print(Out, Policy);
50 } // end anonymous namespace
52 ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
53 return new ASTPrinter(out);
56 //===----------------------------------------------------------------------===//
57 /// ASTPrinterXML - XML-printer of ASTs
60 class ASTPrinterXML : public ASTConsumer {
64 ASTPrinterXML(llvm::raw_ostream& o) : Doc("CLANG_XML", o) {}
66 void Initialize(ASTContext &Context) {
67 Doc.initialize(Context);
70 virtual void HandleTranslationUnit(ASTContext &Ctx) {
71 Doc.addSubNode("TranslationUnit");
72 for (DeclContext::decl_iterator
73 D = Ctx.getTranslationUnitDecl()->decls_begin(),
74 DEnd = Ctx.getTranslationUnitDecl()->decls_end();
82 } // end anonymous namespace
85 ASTConsumer *clang::CreateASTPrinterXML(llvm::raw_ostream* out) {
86 return new ASTPrinterXML(out ? *out : llvm::outs());
89 ASTConsumer *clang::CreateASTDumper() {
90 return new ASTPrinter(0, true);
93 //===----------------------------------------------------------------------===//
94 /// ASTViewer - AST Visualization
97 class ASTViewer : public ASTConsumer {
100 void Initialize(ASTContext &Context) {
101 this->Context = &Context;
104 virtual void HandleTopLevelDecl(DeclGroupRef D) {
105 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
106 HandleTopLevelSingleDecl(*I);
109 void HandleTopLevelSingleDecl(Decl *D);
113 void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
114 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
115 FD->print(llvm::errs());
117 if (Stmt *Body = FD->getBody()) {
118 llvm::errs() << '\n';
120 llvm::errs() << '\n';
125 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
126 MD->print(llvm::errs());
129 llvm::errs() << '\n';
130 MD->getBody()->viewAST();
131 llvm::errs() << '\n';
137 ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
139 //===----------------------------------------------------------------------===//
140 /// DeclContextPrinter - Decl and DeclContext Visualization
144 class DeclContextPrinter : public ASTConsumer {
145 llvm::raw_ostream& Out;
147 DeclContextPrinter() : Out(llvm::errs()) {}
149 void HandleTranslationUnit(ASTContext &C) {
150 PrintDeclContext(C.getTranslationUnitDecl(), 4);
153 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
155 } // end anonymous namespace
157 void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
158 unsigned Indentation) {
159 // Print DeclContext name.
160 switch (DC->getDeclKind()) {
161 case Decl::TranslationUnit:
162 Out << "[translation unit] " << DC;
164 case Decl::Namespace: {
165 Out << "[namespace] ";
166 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
171 const EnumDecl* ED = cast<EnumDecl>(DC);
172 if (ED->isDefinition())
180 const RecordDecl* RD = cast<RecordDecl>(DC);
181 if (RD->isDefinition())
188 case Decl::CXXRecord: {
189 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
190 if (RD->isDefinition())
194 Out << RD << ' ' << DC;
197 case Decl::ObjCMethod:
198 Out << "[objc method]";
200 case Decl::ObjCInterface:
201 Out << "[objc interface]";
203 case Decl::ObjCCategory:
204 Out << "[objc category]";
206 case Decl::ObjCProtocol:
207 Out << "[objc protocol]";
209 case Decl::ObjCImplementation:
210 Out << "[objc implementation]";
212 case Decl::ObjCCategoryImpl:
213 Out << "[objc categoryimpl]";
215 case Decl::LinkageSpec:
216 Out << "[linkage spec]";
221 case Decl::Function: {
222 const FunctionDecl* FD = cast<FunctionDecl>(DC);
223 if (FD->isThisDeclarationADefinition())
224 Out << "[function] ";
226 Out << "<function> ";
228 // Print the parameters.
230 bool PrintComma = false;
231 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
232 E = FD->param_end(); I != E; ++I) {
242 case Decl::CXXMethod: {
243 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
244 if (D->isOutOfLine())
245 Out << "[c++ method] ";
246 else if (D->isImplicit())
247 Out << "(c++ method) ";
249 Out << "<c++ method> ";
251 // Print the parameters.
253 bool PrintComma = false;
254 for (FunctionDecl::param_const_iterator I = D->param_begin(),
255 E = D->param_end(); I != E; ++I) {
264 // Check the semantic DeclContext.
265 const DeclContext* SemaDC = D->getDeclContext();
266 const DeclContext* LexicalDC = D->getLexicalDeclContext();
267 if (SemaDC != LexicalDC)
268 Out << " [[" << SemaDC << "]]";
272 case Decl::CXXConstructor: {
273 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
274 if (D->isOutOfLine())
275 Out << "[c++ ctor] ";
276 else if (D->isImplicit())
277 Out << "(c++ ctor) ";
279 Out << "<c++ ctor> ";
281 // Print the parameters.
283 bool PrintComma = false;
284 for (FunctionDecl::param_const_iterator I = D->param_begin(),
285 E = D->param_end(); I != E; ++I) {
294 // Check the semantic DC.
295 const DeclContext* SemaDC = D->getDeclContext();
296 const DeclContext* LexicalDC = D->getLexicalDeclContext();
297 if (SemaDC != LexicalDC)
298 Out << " [[" << SemaDC << "]]";
301 case Decl::CXXDestructor: {
302 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
303 if (D->isOutOfLine())
304 Out << "[c++ dtor] ";
305 else if (D->isImplicit())
306 Out << "(c++ dtor) ";
308 Out << "<c++ dtor> ";
310 // Check the semantic DC.
311 const DeclContext* SemaDC = D->getDeclContext();
312 const DeclContext* LexicalDC = D->getLexicalDeclContext();
313 if (SemaDC != LexicalDC)
314 Out << " [[" << SemaDC << "]]";
317 case Decl::CXXConversion: {
318 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
319 if (D->isOutOfLine())
320 Out << "[c++ conversion] ";
321 else if (D->isImplicit())
322 Out << "(c++ conversion) ";
324 Out << "<c++ conversion> ";
326 // Check the semantic DC.
327 const DeclContext* SemaDC = D->getDeclContext();
328 const DeclContext* LexicalDC = D->getLexicalDeclContext();
329 if (SemaDC != LexicalDC)
330 Out << " [[" << SemaDC << "]]";
335 assert(0 && "a decl that inherits DeclContext isn't handled");
340 // Print decls in the DeclContext.
341 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
343 for (unsigned i = 0; i < Indentation; ++i)
346 Decl::Kind DK = I->getKind();
348 case Decl::Namespace:
351 case Decl::CXXRecord:
352 case Decl::ObjCMethod:
353 case Decl::ObjCInterface:
354 case Decl::ObjCCategory:
355 case Decl::ObjCProtocol:
356 case Decl::ObjCImplementation:
357 case Decl::ObjCCategoryImpl:
358 case Decl::LinkageSpec:
361 case Decl::CXXMethod:
362 case Decl::CXXConstructor:
363 case Decl::CXXDestructor:
364 case Decl::CXXConversion:
366 DeclContext* DC = cast<DeclContext>(*I);
367 PrintDeclContext(DC, Indentation+2);
371 FieldDecl* FD = cast<FieldDecl>(*I);
372 Out << "<field> " << FD << '\n';
375 case Decl::Typedef: {
376 TypedefDecl* TD = cast<TypedefDecl>(*I);
377 Out << "<typedef> " << TD << '\n';
380 case Decl::EnumConstant: {
381 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
382 Out << "<enum constant> " << ECD << '\n';
386 VarDecl* VD = cast<VarDecl>(*I);
387 Out << "<var> " << VD << '\n';
390 case Decl::ImplicitParam: {
391 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
392 Out << "<implicit parameter> " << IPD << '\n';
395 case Decl::ParmVar: {
396 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
397 Out << "<parameter> " << PVD << '\n';
400 case Decl::ObjCProperty: {
401 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
402 Out << "<objc property> " << OPD << '\n';
405 case Decl::FunctionTemplate: {
406 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I);
407 Out << "<function template> " << FTD << '\n';
410 case Decl::FileScopeAsm: {
411 Out << "<file-scope asm>\n";
414 case Decl::UsingDirective: {
415 Out << "<using directive>\n";
418 case Decl::NamespaceAlias: {
419 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I);
420 Out << "<namespace alias> " << NAD << '\n';
423 case Decl::ClassTemplate: {
424 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(*I);
425 Out << "<class template> " << CTD << '\n';
429 Out << "DeclKind: " << DK << '"' << *I << "\"\n";
430 assert(0 && "decl unhandled");
434 ASTConsumer *clang::CreateDeclContextPrinter() {
435 return new DeclContextPrinter();
438 //===----------------------------------------------------------------------===//
439 /// InheritanceViewer - C++ Inheritance Visualization
442 class InheritanceViewer : public ASTConsumer {
443 const std::string clsname;
445 InheritanceViewer(const std::string& cname) : clsname(cname) {}
447 void HandleTranslationUnit(ASTContext &C) {
448 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
449 if (RecordType *T = dyn_cast<RecordType>(*I)) {
450 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
451 // FIXME: This lookup needs to be generalized to handle namespaces and
452 // (when we support them) templates.
453 if (D->getNameAsString() == clsname) {
454 D->viewInheritance(C);
462 ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
463 return new InheritanceViewer(clsname);