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/Basic/Diagnostic.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/AST/AST.h"
19 #include "clang/AST/ASTConsumer.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/RecordLayout.h"
22 #include "clang/AST/PrettyPrinter.h"
23 #include "llvm/Module.h"
24 #include "llvm/Support/Timer.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/Path.h"
27 using namespace clang;
29 //===----------------------------------------------------------------------===//
30 /// ASTPrinter - Pretty-printer and dumper of ASTs
33 class ASTPrinter : public ASTConsumer {
38 ASTPrinter(raw_ostream* o = NULL, bool Dump = false)
39 : Out(o? *o : llvm::outs()), Dump(Dump) { }
41 virtual void HandleTranslationUnit(ASTContext &Context) {
42 PrintingPolicy Policy = Context.getPrintingPolicy();
44 Context.getTranslationUnitDecl()->print(Out, Policy, /*Indentation=*/0,
45 /*PrintInstantiation=*/true);
48 } // end anonymous namespace
50 ASTConsumer *clang::CreateASTPrinter(raw_ostream* out) {
51 return new ASTPrinter(out);
54 ASTConsumer *clang::CreateASTDumper() {
55 return new ASTPrinter(0, true);
58 //===----------------------------------------------------------------------===//
59 /// ASTViewer - AST Visualization
62 class ASTViewer : public ASTConsumer {
65 void Initialize(ASTContext &Context) {
66 this->Context = &Context;
69 virtual void HandleTopLevelDecl(DeclGroupRef D) {
70 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
71 HandleTopLevelSingleDecl(*I);
74 void HandleTopLevelSingleDecl(Decl *D);
78 void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
79 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
80 D->print(llvm::errs());
82 if (Stmt *Body = D->getBody()) {
91 ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
93 //===----------------------------------------------------------------------===//
94 /// DeclContextPrinter - Decl and DeclContext Visualization
98 class DeclContextPrinter : public ASTConsumer {
101 DeclContextPrinter() : Out(llvm::errs()) {}
103 void HandleTranslationUnit(ASTContext &C) {
104 PrintDeclContext(C.getTranslationUnitDecl(), 4);
107 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
109 } // end anonymous namespace
111 void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
112 unsigned Indentation) {
113 // Print DeclContext name.
114 switch (DC->getDeclKind()) {
115 case Decl::TranslationUnit:
116 Out << "[translation unit] " << DC;
118 case Decl::Namespace: {
119 Out << "[namespace] ";
120 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
125 const EnumDecl* ED = cast<EnumDecl>(DC);
126 if (ED->isCompleteDefinition())
134 const RecordDecl* RD = cast<RecordDecl>(DC);
135 if (RD->isCompleteDefinition())
142 case Decl::CXXRecord: {
143 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
144 if (RD->isCompleteDefinition())
148 Out << *RD << ' ' << DC;
151 case Decl::ObjCMethod:
152 Out << "[objc method]";
154 case Decl::ObjCInterface:
155 Out << "[objc interface]";
157 case Decl::ObjCCategory:
158 Out << "[objc category]";
160 case Decl::ObjCProtocol:
161 Out << "[objc protocol]";
163 case Decl::ObjCImplementation:
164 Out << "[objc implementation]";
166 case Decl::ObjCCategoryImpl:
167 Out << "[objc categoryimpl]";
169 case Decl::LinkageSpec:
170 Out << "[linkage spec]";
175 case Decl::Function: {
176 const FunctionDecl* FD = cast<FunctionDecl>(DC);
177 if (FD->doesThisDeclarationHaveABody())
178 Out << "[function] ";
180 Out << "<function> ";
182 // Print the parameters.
184 bool PrintComma = false;
185 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
186 E = FD->param_end(); I != E; ++I) {
196 case Decl::CXXMethod: {
197 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
198 if (D->isOutOfLine())
199 Out << "[c++ method] ";
200 else if (D->isImplicit())
201 Out << "(c++ method) ";
203 Out << "<c++ method> ";
205 // Print the parameters.
207 bool PrintComma = false;
208 for (FunctionDecl::param_const_iterator I = D->param_begin(),
209 E = D->param_end(); I != E; ++I) {
218 // Check the semantic DeclContext.
219 const DeclContext* SemaDC = D->getDeclContext();
220 const DeclContext* LexicalDC = D->getLexicalDeclContext();
221 if (SemaDC != LexicalDC)
222 Out << " [[" << SemaDC << "]]";
226 case Decl::CXXConstructor: {
227 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
228 if (D->isOutOfLine())
229 Out << "[c++ ctor] ";
230 else if (D->isImplicit())
231 Out << "(c++ ctor) ";
233 Out << "<c++ ctor> ";
235 // Print the parameters.
237 bool PrintComma = false;
238 for (FunctionDecl::param_const_iterator I = D->param_begin(),
239 E = D->param_end(); I != E; ++I) {
248 // Check the semantic DC.
249 const DeclContext* SemaDC = D->getDeclContext();
250 const DeclContext* LexicalDC = D->getLexicalDeclContext();
251 if (SemaDC != LexicalDC)
252 Out << " [[" << SemaDC << "]]";
255 case Decl::CXXDestructor: {
256 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
257 if (D->isOutOfLine())
258 Out << "[c++ dtor] ";
259 else if (D->isImplicit())
260 Out << "(c++ dtor) ";
262 Out << "<c++ dtor> ";
264 // Check the semantic DC.
265 const DeclContext* SemaDC = D->getDeclContext();
266 const DeclContext* LexicalDC = D->getLexicalDeclContext();
267 if (SemaDC != LexicalDC)
268 Out << " [[" << SemaDC << "]]";
271 case Decl::CXXConversion: {
272 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
273 if (D->isOutOfLine())
274 Out << "[c++ conversion] ";
275 else if (D->isImplicit())
276 Out << "(c++ conversion) ";
278 Out << "<c++ conversion> ";
280 // Check the semantic DC.
281 const DeclContext* SemaDC = D->getDeclContext();
282 const DeclContext* LexicalDC = D->getLexicalDeclContext();
283 if (SemaDC != LexicalDC)
284 Out << " [[" << SemaDC << "]]";
289 llvm_unreachable("a decl that inherits DeclContext isn't handled");
294 // Print decls in the DeclContext.
295 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
297 for (unsigned i = 0; i < Indentation; ++i)
300 Decl::Kind DK = I->getKind();
302 case Decl::Namespace:
305 case Decl::CXXRecord:
306 case Decl::ObjCMethod:
307 case Decl::ObjCInterface:
308 case Decl::ObjCCategory:
309 case Decl::ObjCProtocol:
310 case Decl::ObjCImplementation:
311 case Decl::ObjCCategoryImpl:
312 case Decl::LinkageSpec:
315 case Decl::CXXMethod:
316 case Decl::CXXConstructor:
317 case Decl::CXXDestructor:
318 case Decl::CXXConversion:
320 DeclContext* DC = cast<DeclContext>(*I);
321 PrintDeclContext(DC, Indentation+2);
324 case Decl::IndirectField: {
325 IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(*I);
326 Out << "<IndirectField> " << *IFD << '\n';
330 LabelDecl *LD = cast<LabelDecl>(*I);
331 Out << "<Label> " << *LD << '\n';
335 FieldDecl *FD = cast<FieldDecl>(*I);
336 Out << "<field> " << *FD << '\n';
340 case Decl::TypeAlias: {
341 TypedefNameDecl* TD = cast<TypedefNameDecl>(*I);
342 Out << "<typedef> " << *TD << '\n';
345 case Decl::EnumConstant: {
346 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
347 Out << "<enum constant> " << *ECD << '\n';
351 VarDecl* VD = cast<VarDecl>(*I);
352 Out << "<var> " << *VD << '\n';
355 case Decl::ImplicitParam: {
356 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
357 Out << "<implicit parameter> " << *IPD << '\n';
360 case Decl::ParmVar: {
361 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
362 Out << "<parameter> " << *PVD << '\n';
365 case Decl::ObjCProperty: {
366 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
367 Out << "<objc property> " << *OPD << '\n';
370 case Decl::FunctionTemplate: {
371 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I);
372 Out << "<function template> " << *FTD << '\n';
375 case Decl::FileScopeAsm: {
376 Out << "<file-scope asm>\n";
379 case Decl::UsingDirective: {
380 Out << "<using directive>\n";
383 case Decl::NamespaceAlias: {
384 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I);
385 Out << "<namespace alias> " << *NAD << '\n';
388 case Decl::ClassTemplate: {
389 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(*I);
390 Out << "<class template> " << *CTD << '\n';
394 Out << "DeclKind: " << DK << '"' << *I << "\"\n";
395 llvm_unreachable("decl unhandled");
399 ASTConsumer *clang::CreateDeclContextPrinter() {
400 return new DeclContextPrinter();
403 //===----------------------------------------------------------------------===//
404 /// ASTDumperXML - In-depth XML dumping.
407 class ASTDumpXML : public ASTConsumer {
411 ASTDumpXML(raw_ostream &OS) : OS(OS) {}
413 void HandleTranslationUnit(ASTContext &C) {
414 C.getTranslationUnitDecl()->dumpXML(OS);
419 ASTConsumer *clang::CreateASTDumperXML(raw_ostream &OS) {
420 return new ASTDumpXML(OS);