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/Support/Path.h"
25 #include "llvm/Support/Timer.h"
26 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
29 //===----------------------------------------------------------------------===//
30 /// ASTPrinter - Pretty-printer and dumper of ASTs
33 class ASTPrinter : public ASTConsumer,
34 public RecursiveASTVisitor<ASTPrinter> {
35 typedef RecursiveASTVisitor<ASTPrinter> base;
38 ASTPrinter(raw_ostream *Out = nullptr, bool Dump = false,
39 StringRef FilterString = "", bool DumpLookups = false)
40 : Out(Out ? *Out : llvm::outs()), Dump(Dump),
41 FilterString(FilterString), DumpLookups(DumpLookups) {}
43 void HandleTranslationUnit(ASTContext &Context) override {
44 TranslationUnitDecl *D = Context.getTranslationUnitDecl();
46 if (FilterString.empty())
52 bool shouldWalkTypesOfTypeLocs() const { return false; }
54 bool TraverseDecl(Decl *D) {
55 if (D && filterMatches(D)) {
56 bool ShowColors = Out.has_colors();
58 Out.changeColor(raw_ostream::BLUE);
59 Out << ((Dump || DumpLookups) ? "Dumping " : "Printing ") << getName(D)
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)) {
83 if (DC == DC->getPrimaryContext())
84 DC->dumpLookups(Out, Dump);
86 Out << "Lookup map is in primary DeclContext "
87 << DC->getPrimaryContext() << "\n";
89 Out << "Not a DeclContext\n";
93 D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
98 std::string FilterString;
102 class ASTDeclNodeLister : public ASTConsumer,
103 public RecursiveASTVisitor<ASTDeclNodeLister> {
105 ASTDeclNodeLister(raw_ostream *Out = nullptr)
106 : Out(Out ? *Out : llvm::outs()) {}
108 void HandleTranslationUnit(ASTContext &Context) override {
109 TraverseDecl(Context.getTranslationUnitDecl());
112 bool shouldWalkTypesOfTypeLocs() const { return false; }
114 bool VisitNamedDecl(NamedDecl *D) {
115 D->printQualifiedName(Out);
123 } // end anonymous namespace
125 std::unique_ptr<ASTConsumer> clang::CreateASTPrinter(raw_ostream *Out,
126 StringRef FilterString) {
127 return llvm::make_unique<ASTPrinter>(Out, /*Dump=*/false, FilterString);
130 std::unique_ptr<ASTConsumer> clang::CreateASTDumper(StringRef FilterString,
133 assert((DumpDecls || DumpLookups) && "nothing to dump");
134 return llvm::make_unique<ASTPrinter>(nullptr, DumpDecls, FilterString,
138 std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
139 return llvm::make_unique<ASTDeclNodeLister>(nullptr);
142 //===----------------------------------------------------------------------===//
143 /// ASTViewer - AST Visualization
146 class ASTViewer : public ASTConsumer {
149 void Initialize(ASTContext &Context) override {
150 this->Context = &Context;
153 bool HandleTopLevelDecl(DeclGroupRef D) override {
154 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
155 HandleTopLevelSingleDecl(*I);
159 void HandleTopLevelSingleDecl(Decl *D);
163 void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
164 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
165 D->print(llvm::errs());
167 if (Stmt *Body = D->getBody()) {
168 llvm::errs() << '\n';
170 llvm::errs() << '\n';
175 std::unique_ptr<ASTConsumer> clang::CreateASTViewer() {
176 return llvm::make_unique<ASTViewer>();
179 //===----------------------------------------------------------------------===//
180 /// DeclContextPrinter - Decl and DeclContext Visualization
184 class DeclContextPrinter : public ASTConsumer {
187 DeclContextPrinter() : Out(llvm::errs()) {}
189 void HandleTranslationUnit(ASTContext &C) override {
190 PrintDeclContext(C.getTranslationUnitDecl(), 4);
193 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
195 } // end anonymous namespace
197 void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
198 unsigned Indentation) {
199 // Print DeclContext name.
200 switch (DC->getDeclKind()) {
201 case Decl::TranslationUnit:
202 Out << "[translation unit] " << DC;
204 case Decl::Namespace: {
205 Out << "[namespace] ";
206 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
211 const EnumDecl* ED = cast<EnumDecl>(DC);
212 if (ED->isCompleteDefinition())
220 const RecordDecl* RD = cast<RecordDecl>(DC);
221 if (RD->isCompleteDefinition())
228 case Decl::CXXRecord: {
229 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
230 if (RD->isCompleteDefinition())
234 Out << *RD << ' ' << DC;
237 case Decl::ObjCMethod:
238 Out << "[objc method]";
240 case Decl::ObjCInterface:
241 Out << "[objc interface]";
243 case Decl::ObjCCategory:
244 Out << "[objc category]";
246 case Decl::ObjCProtocol:
247 Out << "[objc protocol]";
249 case Decl::ObjCImplementation:
250 Out << "[objc implementation]";
252 case Decl::ObjCCategoryImpl:
253 Out << "[objc categoryimpl]";
255 case Decl::LinkageSpec:
256 Out << "[linkage spec]";
261 case Decl::Function: {
262 const FunctionDecl* FD = cast<FunctionDecl>(DC);
263 if (FD->doesThisDeclarationHaveABody())
264 Out << "[function] ";
266 Out << "<function> ";
268 // Print the parameters.
270 bool PrintComma = false;
271 for (auto I : FD->params()) {
281 case Decl::CXXMethod: {
282 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
283 if (D->isOutOfLine())
284 Out << "[c++ method] ";
285 else if (D->isImplicit())
286 Out << "(c++ method) ";
288 Out << "<c++ method> ";
290 // Print the parameters.
292 bool PrintComma = false;
293 for (FunctionDecl::param_const_iterator I = D->param_begin(),
294 E = D->param_end(); I != E; ++I) {
303 // Check the semantic DeclContext.
304 const DeclContext* SemaDC = D->getDeclContext();
305 const DeclContext* LexicalDC = D->getLexicalDeclContext();
306 if (SemaDC != LexicalDC)
307 Out << " [[" << SemaDC << "]]";
311 case Decl::CXXConstructor: {
312 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
313 if (D->isOutOfLine())
314 Out << "[c++ ctor] ";
315 else if (D->isImplicit())
316 Out << "(c++ ctor) ";
318 Out << "<c++ ctor> ";
320 // Print the parameters.
322 bool PrintComma = false;
323 for (FunctionDecl::param_const_iterator I = D->param_begin(),
324 E = D->param_end(); I != E; ++I) {
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::CXXDestructor: {
341 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
342 if (D->isOutOfLine())
343 Out << "[c++ dtor] ";
344 else if (D->isImplicit())
345 Out << "(c++ dtor) ";
347 Out << "<c++ dtor> ";
349 // Check the semantic DC.
350 const DeclContext* SemaDC = D->getDeclContext();
351 const DeclContext* LexicalDC = D->getLexicalDeclContext();
352 if (SemaDC != LexicalDC)
353 Out << " [[" << SemaDC << "]]";
356 case Decl::CXXConversion: {
357 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
358 if (D->isOutOfLine())
359 Out << "[c++ conversion] ";
360 else if (D->isImplicit())
361 Out << "(c++ conversion) ";
363 Out << "<c++ conversion> ";
365 // Check the semantic DC.
366 const DeclContext* SemaDC = D->getDeclContext();
367 const DeclContext* LexicalDC = D->getLexicalDeclContext();
368 if (SemaDC != LexicalDC)
369 Out << " [[" << SemaDC << "]]";
374 llvm_unreachable("a decl that inherits DeclContext isn't handled");
379 // Print decls in the DeclContext.
380 for (auto *I : DC->decls()) {
381 for (unsigned i = 0; i < Indentation; ++i)
384 Decl::Kind DK = I->getKind();
386 case Decl::Namespace:
389 case Decl::CXXRecord:
390 case Decl::ObjCMethod:
391 case Decl::ObjCInterface:
392 case Decl::ObjCCategory:
393 case Decl::ObjCProtocol:
394 case Decl::ObjCImplementation:
395 case Decl::ObjCCategoryImpl:
396 case Decl::LinkageSpec:
399 case Decl::CXXMethod:
400 case Decl::CXXConstructor:
401 case Decl::CXXDestructor:
402 case Decl::CXXConversion:
404 DeclContext* DC = cast<DeclContext>(I);
405 PrintDeclContext(DC, Indentation+2);
408 case Decl::IndirectField: {
409 IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(I);
410 Out << "<IndirectField> " << *IFD << '\n';
414 LabelDecl *LD = cast<LabelDecl>(I);
415 Out << "<Label> " << *LD << '\n';
419 FieldDecl *FD = cast<FieldDecl>(I);
420 Out << "<field> " << *FD << '\n';
424 case Decl::TypeAlias: {
425 TypedefNameDecl* TD = cast<TypedefNameDecl>(I);
426 Out << "<typedef> " << *TD << '\n';
429 case Decl::EnumConstant: {
430 EnumConstantDecl* ECD = cast<EnumConstantDecl>(I);
431 Out << "<enum constant> " << *ECD << '\n';
435 VarDecl* VD = cast<VarDecl>(I);
436 Out << "<var> " << *VD << '\n';
439 case Decl::ImplicitParam: {
440 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(I);
441 Out << "<implicit parameter> " << *IPD << '\n';
444 case Decl::ParmVar: {
445 ParmVarDecl* PVD = cast<ParmVarDecl>(I);
446 Out << "<parameter> " << *PVD << '\n';
449 case Decl::ObjCProperty: {
450 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(I);
451 Out << "<objc property> " << *OPD << '\n';
454 case Decl::FunctionTemplate: {
455 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(I);
456 Out << "<function template> " << *FTD << '\n';
459 case Decl::FileScopeAsm: {
460 Out << "<file-scope asm>\n";
463 case Decl::UsingDirective: {
464 Out << "<using directive>\n";
467 case Decl::NamespaceAlias: {
468 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(I);
469 Out << "<namespace alias> " << *NAD << '\n';
472 case Decl::ClassTemplate: {
473 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(I);
474 Out << "<class template> " << *CTD << '\n';
477 case Decl::OMPThreadPrivate: {
478 Out << "<omp threadprivate> " << '"' << I << "\"\n";
482 Out << "DeclKind: " << DK << '"' << I << "\"\n";
483 llvm_unreachable("decl unhandled");
487 std::unique_ptr<ASTConsumer> clang::CreateDeclContextPrinter() {
488 return llvm::make_unique<DeclContextPrinter>();