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/FileManager.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/AST/AST.h"
19 #include "clang/AST/ASTConsumer.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/PrettyPrinter.h"
22 #include "clang/AST/RecordLayout.h"
23 #include "clang/AST/RecursiveASTVisitor.h"
24 #include "llvm/Module.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Support/Timer.h"
28 using namespace clang;
30 //===----------------------------------------------------------------------===//
31 /// ASTPrinter - Pretty-printer and dumper of ASTs
34 class ASTPrinter : public ASTConsumer,
35 public RecursiveASTVisitor<ASTPrinter> {
36 typedef RecursiveASTVisitor<ASTPrinter> base;
39 ASTPrinter(raw_ostream *Out = NULL, bool Dump = false,
40 StringRef FilterString = "")
41 : Out(Out ? *Out : llvm::outs()), Dump(Dump),
42 FilterString(FilterString) {}
44 virtual void HandleTranslationUnit(ASTContext &Context) {
45 TranslationUnitDecl *D = Context.getTranslationUnitDecl();
47 if (FilterString.empty()) {
51 D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
58 bool shouldWalkTypesOfTypeLocs() const { return false; }
60 bool TraverseDecl(Decl *D) {
61 if (D != NULL && filterMatches(D)) {
62 Out.changeColor(llvm::raw_ostream::BLUE) <<
63 (Dump ? "Dumping " : "Printing ") << getName(D) << ":\n";
68 D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
70 // Don't traverse child nodes to avoid output duplication.
73 return base::TraverseDecl(D);
77 std::string getName(Decl *D) {
78 if (isa<NamedDecl>(D))
79 return cast<NamedDecl>(D)->getQualifiedNameAsString();
82 bool filterMatches(Decl *D) {
83 return getName(D).find(FilterString) != std::string::npos;
88 std::string FilterString;
91 class ASTDeclNodeLister : public ASTConsumer,
92 public RecursiveASTVisitor<ASTDeclNodeLister> {
94 ASTDeclNodeLister(raw_ostream *Out = NULL)
95 : Out(Out ? *Out : llvm::outs()) {}
97 virtual void HandleTranslationUnit(ASTContext &Context) {
98 TraverseDecl(Context.getTranslationUnitDecl());
101 bool shouldWalkTypesOfTypeLocs() const { return false; }
103 virtual bool VisitNamedDecl(NamedDecl *D) {
104 Out << D->getQualifiedNameAsString() << "\n";
111 } // end anonymous namespace
113 ASTConsumer *clang::CreateASTPrinter(raw_ostream *Out,
114 StringRef FilterString) {
115 return new ASTPrinter(Out, /*Dump=*/ false, FilterString);
118 ASTConsumer *clang::CreateASTDumper(StringRef FilterString) {
119 return new ASTPrinter(0, /*Dump=*/ true, FilterString);
122 ASTConsumer *clang::CreateASTDeclNodeLister() {
123 return new ASTDeclNodeLister(0);
126 //===----------------------------------------------------------------------===//
127 /// ASTViewer - AST Visualization
130 class ASTViewer : public ASTConsumer {
133 void Initialize(ASTContext &Context) {
134 this->Context = &Context;
137 virtual bool HandleTopLevelDecl(DeclGroupRef D) {
138 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
139 HandleTopLevelSingleDecl(*I);
143 void HandleTopLevelSingleDecl(Decl *D);
147 void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
148 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
149 D->print(llvm::errs());
151 if (Stmt *Body = D->getBody()) {
152 llvm::errs() << '\n';
154 llvm::errs() << '\n';
160 ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
162 //===----------------------------------------------------------------------===//
163 /// DeclContextPrinter - Decl and DeclContext Visualization
167 class DeclContextPrinter : public ASTConsumer {
170 DeclContextPrinter() : Out(llvm::errs()) {}
172 void HandleTranslationUnit(ASTContext &C) {
173 PrintDeclContext(C.getTranslationUnitDecl(), 4);
176 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
178 } // end anonymous namespace
180 void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
181 unsigned Indentation) {
182 // Print DeclContext name.
183 switch (DC->getDeclKind()) {
184 case Decl::TranslationUnit:
185 Out << "[translation unit] " << DC;
187 case Decl::Namespace: {
188 Out << "[namespace] ";
189 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
194 const EnumDecl* ED = cast<EnumDecl>(DC);
195 if (ED->isCompleteDefinition())
203 const RecordDecl* RD = cast<RecordDecl>(DC);
204 if (RD->isCompleteDefinition())
211 case Decl::CXXRecord: {
212 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
213 if (RD->isCompleteDefinition())
217 Out << *RD << ' ' << DC;
220 case Decl::ObjCMethod:
221 Out << "[objc method]";
223 case Decl::ObjCInterface:
224 Out << "[objc interface]";
226 case Decl::ObjCCategory:
227 Out << "[objc category]";
229 case Decl::ObjCProtocol:
230 Out << "[objc protocol]";
232 case Decl::ObjCImplementation:
233 Out << "[objc implementation]";
235 case Decl::ObjCCategoryImpl:
236 Out << "[objc categoryimpl]";
238 case Decl::LinkageSpec:
239 Out << "[linkage spec]";
244 case Decl::Function: {
245 const FunctionDecl* FD = cast<FunctionDecl>(DC);
246 if (FD->doesThisDeclarationHaveABody())
247 Out << "[function] ";
249 Out << "<function> ";
251 // Print the parameters.
253 bool PrintComma = false;
254 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
255 E = FD->param_end(); I != E; ++I) {
265 case Decl::CXXMethod: {
266 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
267 if (D->isOutOfLine())
268 Out << "[c++ method] ";
269 else if (D->isImplicit())
270 Out << "(c++ method) ";
272 Out << "<c++ method> ";
274 // Print the parameters.
276 bool PrintComma = false;
277 for (FunctionDecl::param_const_iterator I = D->param_begin(),
278 E = D->param_end(); I != E; ++I) {
287 // Check the semantic DeclContext.
288 const DeclContext* SemaDC = D->getDeclContext();
289 const DeclContext* LexicalDC = D->getLexicalDeclContext();
290 if (SemaDC != LexicalDC)
291 Out << " [[" << SemaDC << "]]";
295 case Decl::CXXConstructor: {
296 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
297 if (D->isOutOfLine())
298 Out << "[c++ ctor] ";
299 else if (D->isImplicit())
300 Out << "(c++ ctor) ";
302 Out << "<c++ ctor> ";
304 // Print the parameters.
306 bool PrintComma = false;
307 for (FunctionDecl::param_const_iterator I = D->param_begin(),
308 E = D->param_end(); I != E; ++I) {
317 // Check the semantic DC.
318 const DeclContext* SemaDC = D->getDeclContext();
319 const DeclContext* LexicalDC = D->getLexicalDeclContext();
320 if (SemaDC != LexicalDC)
321 Out << " [[" << SemaDC << "]]";
324 case Decl::CXXDestructor: {
325 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
326 if (D->isOutOfLine())
327 Out << "[c++ dtor] ";
328 else if (D->isImplicit())
329 Out << "(c++ dtor) ";
331 Out << "<c++ dtor> ";
333 // Check the semantic DC.
334 const DeclContext* SemaDC = D->getDeclContext();
335 const DeclContext* LexicalDC = D->getLexicalDeclContext();
336 if (SemaDC != LexicalDC)
337 Out << " [[" << SemaDC << "]]";
340 case Decl::CXXConversion: {
341 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
342 if (D->isOutOfLine())
343 Out << "[c++ conversion] ";
344 else if (D->isImplicit())
345 Out << "(c++ conversion) ";
347 Out << "<c++ conversion> ";
349 // Check the semantic DC.
350 const DeclContext* SemaDC = D->getDeclContext();
351 const DeclContext* LexicalDC = D->getLexicalDeclContext();
352 if (SemaDC != LexicalDC)
353 Out << " [[" << SemaDC << "]]";
358 llvm_unreachable("a decl that inherits DeclContext isn't handled");
363 // Print decls in the DeclContext.
364 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
366 for (unsigned i = 0; i < Indentation; ++i)
369 Decl::Kind DK = I->getKind();
371 case Decl::Namespace:
374 case Decl::CXXRecord:
375 case Decl::ObjCMethod:
376 case Decl::ObjCInterface:
377 case Decl::ObjCCategory:
378 case Decl::ObjCProtocol:
379 case Decl::ObjCImplementation:
380 case Decl::ObjCCategoryImpl:
381 case Decl::LinkageSpec:
384 case Decl::CXXMethod:
385 case Decl::CXXConstructor:
386 case Decl::CXXDestructor:
387 case Decl::CXXConversion:
389 DeclContext* DC = cast<DeclContext>(*I);
390 PrintDeclContext(DC, Indentation+2);
393 case Decl::IndirectField: {
394 IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(*I);
395 Out << "<IndirectField> " << *IFD << '\n';
399 LabelDecl *LD = cast<LabelDecl>(*I);
400 Out << "<Label> " << *LD << '\n';
404 FieldDecl *FD = cast<FieldDecl>(*I);
405 Out << "<field> " << *FD << '\n';
409 case Decl::TypeAlias: {
410 TypedefNameDecl* TD = cast<TypedefNameDecl>(*I);
411 Out << "<typedef> " << *TD << '\n';
414 case Decl::EnumConstant: {
415 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
416 Out << "<enum constant> " << *ECD << '\n';
420 VarDecl* VD = cast<VarDecl>(*I);
421 Out << "<var> " << *VD << '\n';
424 case Decl::ImplicitParam: {
425 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
426 Out << "<implicit parameter> " << *IPD << '\n';
429 case Decl::ParmVar: {
430 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
431 Out << "<parameter> " << *PVD << '\n';
434 case Decl::ObjCProperty: {
435 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
436 Out << "<objc property> " << *OPD << '\n';
439 case Decl::FunctionTemplate: {
440 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I);
441 Out << "<function template> " << *FTD << '\n';
444 case Decl::FileScopeAsm: {
445 Out << "<file-scope asm>\n";
448 case Decl::UsingDirective: {
449 Out << "<using directive>\n";
452 case Decl::NamespaceAlias: {
453 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I);
454 Out << "<namespace alias> " << *NAD << '\n';
457 case Decl::ClassTemplate: {
458 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(*I);
459 Out << "<class template> " << *CTD << '\n';
463 Out << "DeclKind: " << DK << '"' << *I << "\"\n";
464 llvm_unreachable("decl unhandled");
468 ASTConsumer *clang::CreateDeclContextPrinter() {
469 return new DeclContextPrinter();
472 //===----------------------------------------------------------------------===//
473 /// ASTDumperXML - In-depth XML dumping.
476 class ASTDumpXML : public ASTConsumer {
480 ASTDumpXML(raw_ostream &OS) : OS(OS) {}
482 void HandleTranslationUnit(ASTContext &C) {
483 C.getTranslationUnitDecl()->dumpXML(OS);
488 ASTConsumer *clang::CreateASTDumperXML(raw_ostream &OS) {
489 return new ASTDumpXML(OS);