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/SourceManager.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/Timer.h"
25 #include "llvm/Support/raw_ostream.h"
26 using namespace clang;
28 //===----------------------------------------------------------------------===//
29 /// ASTPrinter - Pretty-printer and dumper of ASTs
32 class ASTPrinter : public ASTConsumer,
33 public RecursiveASTVisitor<ASTPrinter> {
34 typedef RecursiveASTVisitor<ASTPrinter> base;
37 ASTPrinter(std::unique_ptr<raw_ostream> Out = nullptr, bool Dump = false,
38 StringRef FilterString = "", bool DumpLookups = false)
39 : Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)), Dump(Dump),
40 FilterString(FilterString), DumpLookups(DumpLookups) {}
42 void HandleTranslationUnit(ASTContext &Context) override {
43 TranslationUnitDecl *D = Context.getTranslationUnitDecl();
45 if (FilterString.empty())
51 bool shouldWalkTypesOfTypeLocs() const { return false; }
53 bool TraverseDecl(Decl *D) {
54 if (D && filterMatches(D)) {
55 bool ShowColors = Out.has_colors();
57 Out.changeColor(raw_ostream::BLUE);
58 Out << ((Dump || DumpLookups) ? "Dumping " : "Printing ") << getName(D)
64 // Don't traverse child nodes to avoid output duplication.
67 return base::TraverseDecl(D);
71 std::string getName(Decl *D) {
72 if (isa<NamedDecl>(D))
73 return cast<NamedDecl>(D)->getQualifiedNameAsString();
76 bool filterMatches(Decl *D) {
77 return getName(D).find(FilterString) != std::string::npos;
81 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
82 if (DC == DC->getPrimaryContext())
83 DC->dumpLookups(Out, Dump);
85 Out << "Lookup map is in primary DeclContext "
86 << DC->getPrimaryContext() << "\n";
88 Out << "Not a DeclContext\n";
92 D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
96 std::unique_ptr<raw_ostream> OwnedOut;
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>
126 clang::CreateASTPrinter(std::unique_ptr<raw_ostream> Out,
127 StringRef FilterString) {
128 return llvm::make_unique<ASTPrinter>(std::move(Out), /*Dump=*/false,
132 std::unique_ptr<ASTConsumer> clang::CreateASTDumper(StringRef FilterString,
135 assert((DumpDecls || DumpLookups) && "nothing to dump");
136 return llvm::make_unique<ASTPrinter>(nullptr, DumpDecls, FilterString,
140 std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
141 return llvm::make_unique<ASTDeclNodeLister>(nullptr);
144 //===----------------------------------------------------------------------===//
145 /// ASTViewer - AST Visualization
148 class ASTViewer : public ASTConsumer {
151 void Initialize(ASTContext &Context) override {
152 this->Context = &Context;
155 bool HandleTopLevelDecl(DeclGroupRef D) override {
156 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
157 HandleTopLevelSingleDecl(*I);
161 void HandleTopLevelSingleDecl(Decl *D);
165 void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
166 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
167 D->print(llvm::errs());
169 if (Stmt *Body = D->getBody()) {
170 llvm::errs() << '\n';
172 llvm::errs() << '\n';
177 std::unique_ptr<ASTConsumer> clang::CreateASTViewer() {
178 return llvm::make_unique<ASTViewer>();
181 //===----------------------------------------------------------------------===//
182 /// DeclContextPrinter - Decl and DeclContext Visualization
186 class DeclContextPrinter : public ASTConsumer {
189 DeclContextPrinter() : Out(llvm::errs()) {}
191 void HandleTranslationUnit(ASTContext &C) override {
192 PrintDeclContext(C.getTranslationUnitDecl(), 4);
195 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
197 } // end anonymous namespace
199 void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
200 unsigned Indentation) {
201 // Print DeclContext name.
202 switch (DC->getDeclKind()) {
203 case Decl::TranslationUnit:
204 Out << "[translation unit] " << DC;
206 case Decl::Namespace: {
207 Out << "[namespace] ";
208 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
213 const EnumDecl* ED = cast<EnumDecl>(DC);
214 if (ED->isCompleteDefinition())
222 const RecordDecl* RD = cast<RecordDecl>(DC);
223 if (RD->isCompleteDefinition())
230 case Decl::CXXRecord: {
231 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
232 if (RD->isCompleteDefinition())
236 Out << *RD << ' ' << DC;
239 case Decl::ObjCMethod:
240 Out << "[objc method]";
242 case Decl::ObjCInterface:
243 Out << "[objc interface]";
245 case Decl::ObjCCategory:
246 Out << "[objc category]";
248 case Decl::ObjCProtocol:
249 Out << "[objc protocol]";
251 case Decl::ObjCImplementation:
252 Out << "[objc implementation]";
254 case Decl::ObjCCategoryImpl:
255 Out << "[objc categoryimpl]";
257 case Decl::LinkageSpec:
258 Out << "[linkage spec]";
263 case Decl::Function: {
264 const FunctionDecl* FD = cast<FunctionDecl>(DC);
265 if (FD->doesThisDeclarationHaveABody())
266 Out << "[function] ";
268 Out << "<function> ";
270 // Print the parameters.
272 bool PrintComma = false;
273 for (auto I : FD->parameters()) {
283 case Decl::CXXMethod: {
284 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
285 if (D->isOutOfLine())
286 Out << "[c++ method] ";
287 else if (D->isImplicit())
288 Out << "(c++ method) ";
290 Out << "<c++ method> ";
292 // Print the parameters.
294 bool PrintComma = false;
295 for (ParmVarDecl *Parameter : D->parameters()) {
304 // Check the semantic DeclContext.
305 const DeclContext* SemaDC = D->getDeclContext();
306 const DeclContext* LexicalDC = D->getLexicalDeclContext();
307 if (SemaDC != LexicalDC)
308 Out << " [[" << SemaDC << "]]";
312 case Decl::CXXConstructor: {
313 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
314 if (D->isOutOfLine())
315 Out << "[c++ ctor] ";
316 else if (D->isImplicit())
317 Out << "(c++ ctor) ";
319 Out << "<c++ ctor> ";
321 // Print the parameters.
323 bool PrintComma = false;
324 for (ParmVarDecl *Parameter : D->parameters()) {
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>();