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 = "", bool DumpLookups = false)
41 : Out(Out ? *Out : llvm::outs()), Dump(Dump),
42 FilterString(FilterString), DumpLookups(DumpLookups) {}
44 virtual void HandleTranslationUnit(ASTContext &Context) {
45 TranslationUnitDecl *D = Context.getTranslationUnitDecl();
47 if (FilterString.empty())
53 bool shouldWalkTypesOfTypeLocs() const { return false; }
55 bool TraverseDecl(Decl *D) {
56 if (D != NULL && filterMatches(D)) {
57 bool ShowColors = Out.has_colors();
59 Out.changeColor(raw_ostream::BLUE);
60 Out << (Dump ? "Dumping " : "Printing ") << getName(D) << ":\n";
65 // Don't traverse child nodes to avoid output duplication.
68 return base::TraverseDecl(D);
72 std::string getName(Decl *D) {
73 if (isa<NamedDecl>(D))
74 return cast<NamedDecl>(D)->getQualifiedNameAsString();
77 bool filterMatches(Decl *D) {
78 return getName(D).find(FilterString) != std::string::npos;
82 if (DeclContext *DC = dyn_cast<DeclContext>(D))
85 Out << "Not a DeclContext\n";
89 D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
94 std::string FilterString;
98 class ASTDeclNodeLister : public ASTConsumer,
99 public RecursiveASTVisitor<ASTDeclNodeLister> {
101 ASTDeclNodeLister(raw_ostream *Out = NULL)
102 : Out(Out ? *Out : llvm::outs()) {}
104 virtual void HandleTranslationUnit(ASTContext &Context) {
105 TraverseDecl(Context.getTranslationUnitDecl());
108 bool shouldWalkTypesOfTypeLocs() const { return false; }
110 virtual bool VisitNamedDecl(NamedDecl *D) {
111 D->printQualifiedName(Out);
119 } // end anonymous namespace
121 ASTConsumer *clang::CreateASTPrinter(raw_ostream *Out,
122 StringRef FilterString) {
123 return new ASTPrinter(Out, /*Dump=*/ false, FilterString);
126 ASTConsumer *clang::CreateASTDumper(StringRef FilterString, bool DumpLookups) {
127 return new ASTPrinter(0, /*Dump=*/ true, FilterString, DumpLookups);
130 ASTConsumer *clang::CreateASTDeclNodeLister() {
131 return new ASTDeclNodeLister(0);
134 //===----------------------------------------------------------------------===//
135 /// ASTViewer - AST Visualization
138 class ASTViewer : public ASTConsumer {
141 void Initialize(ASTContext &Context) {
142 this->Context = &Context;
145 virtual bool HandleTopLevelDecl(DeclGroupRef D) {
146 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
147 HandleTopLevelSingleDecl(*I);
151 void HandleTopLevelSingleDecl(Decl *D);
155 void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
156 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
157 D->print(llvm::errs());
159 if (Stmt *Body = D->getBody()) {
160 llvm::errs() << '\n';
162 llvm::errs() << '\n';
168 ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
170 //===----------------------------------------------------------------------===//
171 /// DeclContextPrinter - Decl and DeclContext Visualization
175 class DeclContextPrinter : public ASTConsumer {
178 DeclContextPrinter() : Out(llvm::errs()) {}
180 void HandleTranslationUnit(ASTContext &C) {
181 PrintDeclContext(C.getTranslationUnitDecl(), 4);
184 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
186 } // end anonymous namespace
188 void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
189 unsigned Indentation) {
190 // Print DeclContext name.
191 switch (DC->getDeclKind()) {
192 case Decl::TranslationUnit:
193 Out << "[translation unit] " << DC;
195 case Decl::Namespace: {
196 Out << "[namespace] ";
197 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
202 const EnumDecl* ED = cast<EnumDecl>(DC);
203 if (ED->isCompleteDefinition())
211 const RecordDecl* RD = cast<RecordDecl>(DC);
212 if (RD->isCompleteDefinition())
219 case Decl::CXXRecord: {
220 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
221 if (RD->isCompleteDefinition())
225 Out << *RD << ' ' << DC;
228 case Decl::ObjCMethod:
229 Out << "[objc method]";
231 case Decl::ObjCInterface:
232 Out << "[objc interface]";
234 case Decl::ObjCCategory:
235 Out << "[objc category]";
237 case Decl::ObjCProtocol:
238 Out << "[objc protocol]";
240 case Decl::ObjCImplementation:
241 Out << "[objc implementation]";
243 case Decl::ObjCCategoryImpl:
244 Out << "[objc categoryimpl]";
246 case Decl::LinkageSpec:
247 Out << "[linkage spec]";
252 case Decl::Function: {
253 const FunctionDecl* FD = cast<FunctionDecl>(DC);
254 if (FD->doesThisDeclarationHaveABody())
255 Out << "[function] ";
257 Out << "<function> ";
259 // Print the parameters.
261 bool PrintComma = false;
262 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
263 E = FD->param_end(); I != E; ++I) {
273 case Decl::CXXMethod: {
274 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
275 if (D->isOutOfLine())
276 Out << "[c++ method] ";
277 else if (D->isImplicit())
278 Out << "(c++ method) ";
280 Out << "<c++ method> ";
282 // Print the parameters.
284 bool PrintComma = false;
285 for (FunctionDecl::param_const_iterator I = D->param_begin(),
286 E = D->param_end(); I != E; ++I) {
295 // Check the semantic DeclContext.
296 const DeclContext* SemaDC = D->getDeclContext();
297 const DeclContext* LexicalDC = D->getLexicalDeclContext();
298 if (SemaDC != LexicalDC)
299 Out << " [[" << SemaDC << "]]";
303 case Decl::CXXConstructor: {
304 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
305 if (D->isOutOfLine())
306 Out << "[c++ ctor] ";
307 else if (D->isImplicit())
308 Out << "(c++ ctor) ";
310 Out << "<c++ ctor> ";
312 // Print the parameters.
314 bool PrintComma = false;
315 for (FunctionDecl::param_const_iterator I = D->param_begin(),
316 E = D->param_end(); I != E; ++I) {
325 // Check the semantic DC.
326 const DeclContext* SemaDC = D->getDeclContext();
327 const DeclContext* LexicalDC = D->getLexicalDeclContext();
328 if (SemaDC != LexicalDC)
329 Out << " [[" << SemaDC << "]]";
332 case Decl::CXXDestructor: {
333 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
334 if (D->isOutOfLine())
335 Out << "[c++ dtor] ";
336 else if (D->isImplicit())
337 Out << "(c++ dtor) ";
339 Out << "<c++ dtor> ";
341 // Check the semantic DC.
342 const DeclContext* SemaDC = D->getDeclContext();
343 const DeclContext* LexicalDC = D->getLexicalDeclContext();
344 if (SemaDC != LexicalDC)
345 Out << " [[" << SemaDC << "]]";
348 case Decl::CXXConversion: {
349 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
350 if (D->isOutOfLine())
351 Out << "[c++ conversion] ";
352 else if (D->isImplicit())
353 Out << "(c++ conversion) ";
355 Out << "<c++ conversion> ";
357 // Check the semantic DC.
358 const DeclContext* SemaDC = D->getDeclContext();
359 const DeclContext* LexicalDC = D->getLexicalDeclContext();
360 if (SemaDC != LexicalDC)
361 Out << " [[" << SemaDC << "]]";
366 llvm_unreachable("a decl that inherits DeclContext isn't handled");
371 // Print decls in the DeclContext.
372 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
374 for (unsigned i = 0; i < Indentation; ++i)
377 Decl::Kind DK = I->getKind();
379 case Decl::Namespace:
382 case Decl::CXXRecord:
383 case Decl::ObjCMethod:
384 case Decl::ObjCInterface:
385 case Decl::ObjCCategory:
386 case Decl::ObjCProtocol:
387 case Decl::ObjCImplementation:
388 case Decl::ObjCCategoryImpl:
389 case Decl::LinkageSpec:
392 case Decl::CXXMethod:
393 case Decl::CXXConstructor:
394 case Decl::CXXDestructor:
395 case Decl::CXXConversion:
397 DeclContext* DC = cast<DeclContext>(*I);
398 PrintDeclContext(DC, Indentation+2);
401 case Decl::IndirectField: {
402 IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(*I);
403 Out << "<IndirectField> " << *IFD << '\n';
407 LabelDecl *LD = cast<LabelDecl>(*I);
408 Out << "<Label> " << *LD << '\n';
412 FieldDecl *FD = cast<FieldDecl>(*I);
413 Out << "<field> " << *FD << '\n';
417 case Decl::TypeAlias: {
418 TypedefNameDecl* TD = cast<TypedefNameDecl>(*I);
419 Out << "<typedef> " << *TD << '\n';
422 case Decl::EnumConstant: {
423 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
424 Out << "<enum constant> " << *ECD << '\n';
428 VarDecl* VD = cast<VarDecl>(*I);
429 Out << "<var> " << *VD << '\n';
432 case Decl::ImplicitParam: {
433 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
434 Out << "<implicit parameter> " << *IPD << '\n';
437 case Decl::ParmVar: {
438 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
439 Out << "<parameter> " << *PVD << '\n';
442 case Decl::ObjCProperty: {
443 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
444 Out << "<objc property> " << *OPD << '\n';
447 case Decl::FunctionTemplate: {
448 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I);
449 Out << "<function template> " << *FTD << '\n';
452 case Decl::FileScopeAsm: {
453 Out << "<file-scope asm>\n";
456 case Decl::UsingDirective: {
457 Out << "<using directive>\n";
460 case Decl::NamespaceAlias: {
461 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I);
462 Out << "<namespace alias> " << *NAD << '\n';
465 case Decl::ClassTemplate: {
466 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(*I);
467 Out << "<class template> " << *CTD << '\n';
470 case Decl::OMPThreadPrivate: {
471 Out << "<omp threadprivate> " << '"' << *I << "\"\n";
475 Out << "DeclKind: " << DK << '"' << *I << "\"\n";
476 llvm_unreachable("decl unhandled");
480 ASTConsumer *clang::CreateDeclContextPrinter() {
481 return new DeclContextPrinter();