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/AST/AST.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/PrettyPrinter.h"
19 #include "clang/AST/RecordLayout.h"
20 #include "clang/AST/RecursiveASTVisitor.h"
21 #include "clang/Basic/Diagnostic.h"
22 #include "clang/Basic/FileManager.h"
23 #include "clang/Basic/SourceManager.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/Timer.h"
27 #include "llvm/Support/raw_ostream.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 bool ShowColors = Out.has_colors();
64 Out.changeColor(raw_ostream::BLUE);
65 Out << (Dump ? "Dumping " : "Printing ") << getName(D) << ":\n";
71 D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
73 // Don't traverse child nodes to avoid output duplication.
76 return base::TraverseDecl(D);
80 std::string getName(Decl *D) {
81 if (isa<NamedDecl>(D))
82 return cast<NamedDecl>(D)->getQualifiedNameAsString();
85 bool filterMatches(Decl *D) {
86 return getName(D).find(FilterString) != std::string::npos;
91 std::string FilterString;
94 class ASTDeclNodeLister : public ASTConsumer,
95 public RecursiveASTVisitor<ASTDeclNodeLister> {
97 ASTDeclNodeLister(raw_ostream *Out = NULL)
98 : Out(Out ? *Out : llvm::outs()) {}
100 virtual void HandleTranslationUnit(ASTContext &Context) {
101 TraverseDecl(Context.getTranslationUnitDecl());
104 bool shouldWalkTypesOfTypeLocs() const { return false; }
106 virtual bool VisitNamedDecl(NamedDecl *D) {
107 D->printQualifiedName(Out);
115 } // end anonymous namespace
117 ASTConsumer *clang::CreateASTPrinter(raw_ostream *Out,
118 StringRef FilterString) {
119 return new ASTPrinter(Out, /*Dump=*/ false, FilterString);
122 ASTConsumer *clang::CreateASTDumper(StringRef FilterString) {
123 return new ASTPrinter(0, /*Dump=*/ true, FilterString);
126 ASTConsumer *clang::CreateASTDeclNodeLister() {
127 return new ASTDeclNodeLister(0);
130 //===----------------------------------------------------------------------===//
131 /// ASTViewer - AST Visualization
134 class ASTViewer : public ASTConsumer {
137 void Initialize(ASTContext &Context) {
138 this->Context = &Context;
141 virtual bool HandleTopLevelDecl(DeclGroupRef D) {
142 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
143 HandleTopLevelSingleDecl(*I);
147 void HandleTopLevelSingleDecl(Decl *D);
151 void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
152 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
153 D->print(llvm::errs());
155 if (Stmt *Body = D->getBody()) {
156 llvm::errs() << '\n';
158 llvm::errs() << '\n';
164 ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
166 //===----------------------------------------------------------------------===//
167 /// DeclContextPrinter - Decl and DeclContext Visualization
171 class DeclContextPrinter : public ASTConsumer {
174 DeclContextPrinter() : Out(llvm::errs()) {}
176 void HandleTranslationUnit(ASTContext &C) {
177 PrintDeclContext(C.getTranslationUnitDecl(), 4);
180 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
182 } // end anonymous namespace
184 void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
185 unsigned Indentation) {
186 // Print DeclContext name.
187 switch (DC->getDeclKind()) {
188 case Decl::TranslationUnit:
189 Out << "[translation unit] " << DC;
191 case Decl::Namespace: {
192 Out << "[namespace] ";
193 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
198 const EnumDecl* ED = cast<EnumDecl>(DC);
199 if (ED->isCompleteDefinition())
207 const RecordDecl* RD = cast<RecordDecl>(DC);
208 if (RD->isCompleteDefinition())
215 case Decl::CXXRecord: {
216 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
217 if (RD->isCompleteDefinition())
221 Out << *RD << ' ' << DC;
224 case Decl::ObjCMethod:
225 Out << "[objc method]";
227 case Decl::ObjCInterface:
228 Out << "[objc interface]";
230 case Decl::ObjCCategory:
231 Out << "[objc category]";
233 case Decl::ObjCProtocol:
234 Out << "[objc protocol]";
236 case Decl::ObjCImplementation:
237 Out << "[objc implementation]";
239 case Decl::ObjCCategoryImpl:
240 Out << "[objc categoryimpl]";
242 case Decl::LinkageSpec:
243 Out << "[linkage spec]";
248 case Decl::Function: {
249 const FunctionDecl* FD = cast<FunctionDecl>(DC);
250 if (FD->doesThisDeclarationHaveABody())
251 Out << "[function] ";
253 Out << "<function> ";
255 // Print the parameters.
257 bool PrintComma = false;
258 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
259 E = FD->param_end(); I != E; ++I) {
269 case Decl::CXXMethod: {
270 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
271 if (D->isOutOfLine())
272 Out << "[c++ method] ";
273 else if (D->isImplicit())
274 Out << "(c++ method) ";
276 Out << "<c++ method> ";
278 // Print the parameters.
280 bool PrintComma = false;
281 for (FunctionDecl::param_const_iterator I = D->param_begin(),
282 E = D->param_end(); I != E; ++I) {
291 // Check the semantic DeclContext.
292 const DeclContext* SemaDC = D->getDeclContext();
293 const DeclContext* LexicalDC = D->getLexicalDeclContext();
294 if (SemaDC != LexicalDC)
295 Out << " [[" << SemaDC << "]]";
299 case Decl::CXXConstructor: {
300 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
301 if (D->isOutOfLine())
302 Out << "[c++ ctor] ";
303 else if (D->isImplicit())
304 Out << "(c++ ctor) ";
306 Out << "<c++ ctor> ";
308 // Print the parameters.
310 bool PrintComma = false;
311 for (FunctionDecl::param_const_iterator I = D->param_begin(),
312 E = D->param_end(); I != E; ++I) {
321 // Check the semantic DC.
322 const DeclContext* SemaDC = D->getDeclContext();
323 const DeclContext* LexicalDC = D->getLexicalDeclContext();
324 if (SemaDC != LexicalDC)
325 Out << " [[" << SemaDC << "]]";
328 case Decl::CXXDestructor: {
329 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
330 if (D->isOutOfLine())
331 Out << "[c++ dtor] ";
332 else if (D->isImplicit())
333 Out << "(c++ dtor) ";
335 Out << "<c++ dtor> ";
337 // Check the semantic DC.
338 const DeclContext* SemaDC = D->getDeclContext();
339 const DeclContext* LexicalDC = D->getLexicalDeclContext();
340 if (SemaDC != LexicalDC)
341 Out << " [[" << SemaDC << "]]";
344 case Decl::CXXConversion: {
345 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
346 if (D->isOutOfLine())
347 Out << "[c++ conversion] ";
348 else if (D->isImplicit())
349 Out << "(c++ conversion) ";
351 Out << "<c++ conversion> ";
353 // Check the semantic DC.
354 const DeclContext* SemaDC = D->getDeclContext();
355 const DeclContext* LexicalDC = D->getLexicalDeclContext();
356 if (SemaDC != LexicalDC)
357 Out << " [[" << SemaDC << "]]";
362 llvm_unreachable("a decl that inherits DeclContext isn't handled");
367 // Print decls in the DeclContext.
368 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
370 for (unsigned i = 0; i < Indentation; ++i)
373 Decl::Kind DK = I->getKind();
375 case Decl::Namespace:
378 case Decl::CXXRecord:
379 case Decl::ObjCMethod:
380 case Decl::ObjCInterface:
381 case Decl::ObjCCategory:
382 case Decl::ObjCProtocol:
383 case Decl::ObjCImplementation:
384 case Decl::ObjCCategoryImpl:
385 case Decl::LinkageSpec:
388 case Decl::CXXMethod:
389 case Decl::CXXConstructor:
390 case Decl::CXXDestructor:
391 case Decl::CXXConversion:
393 DeclContext* DC = cast<DeclContext>(*I);
394 PrintDeclContext(DC, Indentation+2);
397 case Decl::IndirectField: {
398 IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(*I);
399 Out << "<IndirectField> " << *IFD << '\n';
403 LabelDecl *LD = cast<LabelDecl>(*I);
404 Out << "<Label> " << *LD << '\n';
408 FieldDecl *FD = cast<FieldDecl>(*I);
409 Out << "<field> " << *FD << '\n';
413 case Decl::TypeAlias: {
414 TypedefNameDecl* TD = cast<TypedefNameDecl>(*I);
415 Out << "<typedef> " << *TD << '\n';
418 case Decl::EnumConstant: {
419 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
420 Out << "<enum constant> " << *ECD << '\n';
424 VarDecl* VD = cast<VarDecl>(*I);
425 Out << "<var> " << *VD << '\n';
428 case Decl::ImplicitParam: {
429 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
430 Out << "<implicit parameter> " << *IPD << '\n';
433 case Decl::ParmVar: {
434 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
435 Out << "<parameter> " << *PVD << '\n';
438 case Decl::ObjCProperty: {
439 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
440 Out << "<objc property> " << *OPD << '\n';
443 case Decl::FunctionTemplate: {
444 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I);
445 Out << "<function template> " << *FTD << '\n';
448 case Decl::FileScopeAsm: {
449 Out << "<file-scope asm>\n";
452 case Decl::UsingDirective: {
453 Out << "<using directive>\n";
456 case Decl::NamespaceAlias: {
457 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I);
458 Out << "<namespace alias> " << *NAD << '\n';
461 case Decl::ClassTemplate: {
462 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(*I);
463 Out << "<class template> " << *CTD << '\n';
466 case Decl::OMPThreadPrivate: {
467 Out << "<omp threadprivate> " << '"' << *I << "\"\n";
471 Out << "DeclKind: " << DK << '"' << *I << "\"\n";
472 llvm_unreachable("decl unhandled");
476 ASTConsumer *clang::CreateDeclContextPrinter() {
477 return new DeclContextPrinter();
480 //===----------------------------------------------------------------------===//
481 /// ASTDumperXML - In-depth XML dumping.
484 class ASTDumpXML : public ASTConsumer {
488 ASTDumpXML(raw_ostream &OS) : OS(OS) {}
490 void HandleTranslationUnit(ASTContext &C) {
491 C.getTranslationUnitDecl()->dumpXML(OS);
496 ASTConsumer *clang::CreateASTDumperXML(raw_ostream &OS) {
497 return new ASTDumpXML(OS);