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 {
34 llvm::raw_ostream &Out;
38 ASTPrinter(llvm::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.PrintingPolicy;
44 Context.getTranslationUnitDecl()->print(Out, Policy);
47 } // end anonymous namespace
49 ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
50 return new ASTPrinter(out);
53 ASTConsumer *clang::CreateASTDumper() {
54 return new ASTPrinter(0, true);
57 //===----------------------------------------------------------------------===//
58 /// ASTViewer - AST Visualization
61 class ASTViewer : public ASTConsumer {
64 void Initialize(ASTContext &Context) {
65 this->Context = &Context;
68 virtual void HandleTopLevelDecl(DeclGroupRef D) {
69 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
70 HandleTopLevelSingleDecl(*I);
73 void HandleTopLevelSingleDecl(Decl *D);
77 void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
78 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
79 D->print(llvm::errs());
81 if (Stmt *Body = D->getBody()) {
90 ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
92 //===----------------------------------------------------------------------===//
93 /// DeclContextPrinter - Decl and DeclContext Visualization
97 class DeclContextPrinter : public ASTConsumer {
98 llvm::raw_ostream& Out;
100 DeclContextPrinter() : Out(llvm::errs()) {}
102 void HandleTranslationUnit(ASTContext &C) {
103 PrintDeclContext(C.getTranslationUnitDecl(), 4);
106 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
108 } // end anonymous namespace
110 void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
111 unsigned Indentation) {
112 // Print DeclContext name.
113 switch (DC->getDeclKind()) {
114 case Decl::TranslationUnit:
115 Out << "[translation unit] " << DC;
117 case Decl::Namespace: {
118 Out << "[namespace] ";
119 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
124 const EnumDecl* ED = cast<EnumDecl>(DC);
125 if (ED->isDefinition())
133 const RecordDecl* RD = cast<RecordDecl>(DC);
134 if (RD->isDefinition())
141 case Decl::CXXRecord: {
142 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
143 if (RD->isDefinition())
147 Out << RD << ' ' << DC;
150 case Decl::ObjCMethod:
151 Out << "[objc method]";
153 case Decl::ObjCInterface:
154 Out << "[objc interface]";
156 case Decl::ObjCCategory:
157 Out << "[objc category]";
159 case Decl::ObjCProtocol:
160 Out << "[objc protocol]";
162 case Decl::ObjCImplementation:
163 Out << "[objc implementation]";
165 case Decl::ObjCCategoryImpl:
166 Out << "[objc categoryimpl]";
168 case Decl::LinkageSpec:
169 Out << "[linkage spec]";
174 case Decl::Function: {
175 const FunctionDecl* FD = cast<FunctionDecl>(DC);
176 if (FD->doesThisDeclarationHaveABody())
177 Out << "[function] ";
179 Out << "<function> ";
181 // Print the parameters.
183 bool PrintComma = false;
184 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
185 E = FD->param_end(); I != E; ++I) {
195 case Decl::CXXMethod: {
196 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
197 if (D->isOutOfLine())
198 Out << "[c++ method] ";
199 else if (D->isImplicit())
200 Out << "(c++ method) ";
202 Out << "<c++ method> ";
204 // Print the parameters.
206 bool PrintComma = false;
207 for (FunctionDecl::param_const_iterator I = D->param_begin(),
208 E = D->param_end(); I != E; ++I) {
217 // Check the semantic DeclContext.
218 const DeclContext* SemaDC = D->getDeclContext();
219 const DeclContext* LexicalDC = D->getLexicalDeclContext();
220 if (SemaDC != LexicalDC)
221 Out << " [[" << SemaDC << "]]";
225 case Decl::CXXConstructor: {
226 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
227 if (D->isOutOfLine())
228 Out << "[c++ ctor] ";
229 else if (D->isImplicit())
230 Out << "(c++ ctor) ";
232 Out << "<c++ ctor> ";
234 // Print the parameters.
236 bool PrintComma = false;
237 for (FunctionDecl::param_const_iterator I = D->param_begin(),
238 E = D->param_end(); I != E; ++I) {
247 // Check the semantic DC.
248 const DeclContext* SemaDC = D->getDeclContext();
249 const DeclContext* LexicalDC = D->getLexicalDeclContext();
250 if (SemaDC != LexicalDC)
251 Out << " [[" << SemaDC << "]]";
254 case Decl::CXXDestructor: {
255 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
256 if (D->isOutOfLine())
257 Out << "[c++ dtor] ";
258 else if (D->isImplicit())
259 Out << "(c++ dtor) ";
261 Out << "<c++ dtor> ";
263 // Check the semantic DC.
264 const DeclContext* SemaDC = D->getDeclContext();
265 const DeclContext* LexicalDC = D->getLexicalDeclContext();
266 if (SemaDC != LexicalDC)
267 Out << " [[" << SemaDC << "]]";
270 case Decl::CXXConversion: {
271 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
272 if (D->isOutOfLine())
273 Out << "[c++ conversion] ";
274 else if (D->isImplicit())
275 Out << "(c++ conversion) ";
277 Out << "<c++ conversion> ";
279 // Check the semantic DC.
280 const DeclContext* SemaDC = D->getDeclContext();
281 const DeclContext* LexicalDC = D->getLexicalDeclContext();
282 if (SemaDC != LexicalDC)
283 Out << " [[" << SemaDC << "]]";
288 assert(0 && "a decl that inherits DeclContext isn't handled");
293 // Print decls in the DeclContext.
294 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
296 for (unsigned i = 0; i < Indentation; ++i)
299 Decl::Kind DK = I->getKind();
301 case Decl::Namespace:
304 case Decl::CXXRecord:
305 case Decl::ObjCMethod:
306 case Decl::ObjCInterface:
307 case Decl::ObjCCategory:
308 case Decl::ObjCProtocol:
309 case Decl::ObjCImplementation:
310 case Decl::ObjCCategoryImpl:
311 case Decl::LinkageSpec:
314 case Decl::CXXMethod:
315 case Decl::CXXConstructor:
316 case Decl::CXXDestructor:
317 case Decl::CXXConversion:
319 DeclContext* DC = cast<DeclContext>(*I);
320 PrintDeclContext(DC, Indentation+2);
323 case Decl::IndirectField: {
324 IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(*I);
325 Out << "<IndirectField> " << IFD << '\n';
329 LabelDecl *LD = cast<LabelDecl>(*I);
330 Out << "<Label> " << LD << '\n';
334 FieldDecl *FD = cast<FieldDecl>(*I);
335 Out << "<field> " << FD << '\n';
339 case Decl::TypeAlias: {
340 TypedefNameDecl* TD = cast<TypedefNameDecl>(*I);
341 Out << "<typedef> " << TD << '\n';
344 case Decl::EnumConstant: {
345 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
346 Out << "<enum constant> " << ECD << '\n';
350 VarDecl* VD = cast<VarDecl>(*I);
351 Out << "<var> " << VD << '\n';
354 case Decl::ImplicitParam: {
355 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
356 Out << "<implicit parameter> " << IPD << '\n';
359 case Decl::ParmVar: {
360 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
361 Out << "<parameter> " << PVD << '\n';
364 case Decl::ObjCProperty: {
365 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
366 Out << "<objc property> " << OPD << '\n';
369 case Decl::FunctionTemplate: {
370 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I);
371 Out << "<function template> " << FTD << '\n';
374 case Decl::FileScopeAsm: {
375 Out << "<file-scope asm>\n";
378 case Decl::UsingDirective: {
379 Out << "<using directive>\n";
382 case Decl::NamespaceAlias: {
383 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I);
384 Out << "<namespace alias> " << NAD << '\n';
387 case Decl::ClassTemplate: {
388 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(*I);
389 Out << "<class template> " << CTD << '\n';
393 Out << "DeclKind: " << DK << '"' << *I << "\"\n";
394 assert(0 && "decl unhandled");
398 ASTConsumer *clang::CreateDeclContextPrinter() {
399 return new DeclContextPrinter();
402 //===----------------------------------------------------------------------===//
403 /// ASTDumperXML - In-depth XML dumping.
406 class ASTDumpXML : public ASTConsumer {
407 llvm::raw_ostream &OS;
410 ASTDumpXML(llvm::raw_ostream &OS) : OS(OS) {}
412 void HandleTranslationUnit(ASTContext &C) {
413 C.getTranslationUnitDecl()->dumpXML(OS);
418 ASTConsumer *clang::CreateASTDumperXML(llvm::raw_ostream &OS) {
419 return new ASTDumpXML(OS);