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(std::unique_ptr<raw_ostream> Out = nullptr, bool Dump = false,
39 StringRef FilterString = "", bool DumpLookups = false)
40 : Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)), 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);
97 std::unique_ptr<raw_ostream> OwnedOut;
99 std::string FilterString;
103 class ASTDeclNodeLister : public ASTConsumer,
104 public RecursiveASTVisitor<ASTDeclNodeLister> {
106 ASTDeclNodeLister(raw_ostream *Out = nullptr)
107 : Out(Out ? *Out : llvm::outs()) {}
109 void HandleTranslationUnit(ASTContext &Context) override {
110 TraverseDecl(Context.getTranslationUnitDecl());
113 bool shouldWalkTypesOfTypeLocs() const { return false; }
115 bool VisitNamedDecl(NamedDecl *D) {
116 D->printQualifiedName(Out);
124 } // end anonymous namespace
126 std::unique_ptr<ASTConsumer>
127 clang::CreateASTPrinter(std::unique_ptr<raw_ostream> Out,
128 StringRef FilterString) {
129 return llvm::make_unique<ASTPrinter>(std::move(Out), /*Dump=*/false,
133 std::unique_ptr<ASTConsumer> clang::CreateASTDumper(StringRef FilterString,
136 assert((DumpDecls || DumpLookups) && "nothing to dump");
137 return llvm::make_unique<ASTPrinter>(nullptr, DumpDecls, FilterString,
141 std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
142 return llvm::make_unique<ASTDeclNodeLister>(nullptr);
145 //===----------------------------------------------------------------------===//
146 /// ASTViewer - AST Visualization
149 class ASTViewer : public ASTConsumer {
152 void Initialize(ASTContext &Context) override {
153 this->Context = &Context;
156 bool HandleTopLevelDecl(DeclGroupRef D) override {
157 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
158 HandleTopLevelSingleDecl(*I);
162 void HandleTopLevelSingleDecl(Decl *D);
166 void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
167 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
168 D->print(llvm::errs());
170 if (Stmt *Body = D->getBody()) {
171 llvm::errs() << '\n';
173 llvm::errs() << '\n';
178 std::unique_ptr<ASTConsumer> clang::CreateASTViewer() {
179 return llvm::make_unique<ASTViewer>();
182 //===----------------------------------------------------------------------===//
183 /// DeclContextPrinter - Decl and DeclContext Visualization
187 class DeclContextPrinter : public ASTConsumer {
190 DeclContextPrinter() : Out(llvm::errs()) {}
192 void HandleTranslationUnit(ASTContext &C) override {
193 PrintDeclContext(C.getTranslationUnitDecl(), 4);
196 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
198 } // end anonymous namespace
200 void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
201 unsigned Indentation) {
202 // Print DeclContext name.
203 switch (DC->getDeclKind()) {
204 case Decl::TranslationUnit:
205 Out << "[translation unit] " << DC;
207 case Decl::Namespace: {
208 Out << "[namespace] ";
209 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
214 const EnumDecl* ED = cast<EnumDecl>(DC);
215 if (ED->isCompleteDefinition())
223 const RecordDecl* RD = cast<RecordDecl>(DC);
224 if (RD->isCompleteDefinition())
231 case Decl::CXXRecord: {
232 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
233 if (RD->isCompleteDefinition())
237 Out << *RD << ' ' << DC;
240 case Decl::ObjCMethod:
241 Out << "[objc method]";
243 case Decl::ObjCInterface:
244 Out << "[objc interface]";
246 case Decl::ObjCCategory:
247 Out << "[objc category]";
249 case Decl::ObjCProtocol:
250 Out << "[objc protocol]";
252 case Decl::ObjCImplementation:
253 Out << "[objc implementation]";
255 case Decl::ObjCCategoryImpl:
256 Out << "[objc categoryimpl]";
258 case Decl::LinkageSpec:
259 Out << "[linkage spec]";
264 case Decl::Function: {
265 const FunctionDecl* FD = cast<FunctionDecl>(DC);
266 if (FD->doesThisDeclarationHaveABody())
267 Out << "[function] ";
269 Out << "<function> ";
271 // Print the parameters.
273 bool PrintComma = false;
274 for (auto I : FD->parameters()) {
284 case Decl::CXXMethod: {
285 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
286 if (D->isOutOfLine())
287 Out << "[c++ method] ";
288 else if (D->isImplicit())
289 Out << "(c++ method) ";
291 Out << "<c++ method> ";
293 // Print the parameters.
295 bool PrintComma = false;
296 for (ParmVarDecl *Parameter : D->parameters()) {
305 // Check the semantic DeclContext.
306 const DeclContext* SemaDC = D->getDeclContext();
307 const DeclContext* LexicalDC = D->getLexicalDeclContext();
308 if (SemaDC != LexicalDC)
309 Out << " [[" << SemaDC << "]]";
313 case Decl::CXXConstructor: {
314 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
315 if (D->isOutOfLine())
316 Out << "[c++ ctor] ";
317 else if (D->isImplicit())
318 Out << "(c++ ctor) ";
320 Out << "<c++ ctor> ";
322 // Print the parameters.
324 bool PrintComma = false;
325 for (ParmVarDecl *Parameter : D->parameters()) {
334 // Check the semantic DC.
335 const DeclContext* SemaDC = D->getDeclContext();
336 const DeclContext* LexicalDC = D->getLexicalDeclContext();
337 if (SemaDC != LexicalDC)
338 Out << " [[" << SemaDC << "]]";
341 case Decl::CXXDestructor: {
342 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
343 if (D->isOutOfLine())
344 Out << "[c++ dtor] ";
345 else if (D->isImplicit())
346 Out << "(c++ dtor) ";
348 Out << "<c++ dtor> ";
350 // Check the semantic DC.
351 const DeclContext* SemaDC = D->getDeclContext();
352 const DeclContext* LexicalDC = D->getLexicalDeclContext();
353 if (SemaDC != LexicalDC)
354 Out << " [[" << SemaDC << "]]";
357 case Decl::CXXConversion: {
358 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
359 if (D->isOutOfLine())
360 Out << "[c++ conversion] ";
361 else if (D->isImplicit())
362 Out << "(c++ conversion) ";
364 Out << "<c++ conversion> ";
366 // Check the semantic DC.
367 const DeclContext* SemaDC = D->getDeclContext();
368 const DeclContext* LexicalDC = D->getLexicalDeclContext();
369 if (SemaDC != LexicalDC)
370 Out << " [[" << SemaDC << "]]";
375 llvm_unreachable("a decl that inherits DeclContext isn't handled");
380 // Print decls in the DeclContext.
381 for (auto *I : DC->decls()) {
382 for (unsigned i = 0; i < Indentation; ++i)
385 Decl::Kind DK = I->getKind();
387 case Decl::Namespace:
390 case Decl::CXXRecord:
391 case Decl::ObjCMethod:
392 case Decl::ObjCInterface:
393 case Decl::ObjCCategory:
394 case Decl::ObjCProtocol:
395 case Decl::ObjCImplementation:
396 case Decl::ObjCCategoryImpl:
397 case Decl::LinkageSpec:
400 case Decl::CXXMethod:
401 case Decl::CXXConstructor:
402 case Decl::CXXDestructor:
403 case Decl::CXXConversion:
405 DeclContext* DC = cast<DeclContext>(I);
406 PrintDeclContext(DC, Indentation+2);
409 case Decl::IndirectField: {
410 IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(I);
411 Out << "<IndirectField> " << *IFD << '\n';
415 LabelDecl *LD = cast<LabelDecl>(I);
416 Out << "<Label> " << *LD << '\n';
420 FieldDecl *FD = cast<FieldDecl>(I);
421 Out << "<field> " << *FD << '\n';
425 case Decl::TypeAlias: {
426 TypedefNameDecl* TD = cast<TypedefNameDecl>(I);
427 Out << "<typedef> " << *TD << '\n';
430 case Decl::EnumConstant: {
431 EnumConstantDecl* ECD = cast<EnumConstantDecl>(I);
432 Out << "<enum constant> " << *ECD << '\n';
436 VarDecl* VD = cast<VarDecl>(I);
437 Out << "<var> " << *VD << '\n';
440 case Decl::ImplicitParam: {
441 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(I);
442 Out << "<implicit parameter> " << *IPD << '\n';
445 case Decl::ParmVar: {
446 ParmVarDecl* PVD = cast<ParmVarDecl>(I);
447 Out << "<parameter> " << *PVD << '\n';
450 case Decl::ObjCProperty: {
451 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(I);
452 Out << "<objc property> " << *OPD << '\n';
455 case Decl::FunctionTemplate: {
456 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(I);
457 Out << "<function template> " << *FTD << '\n';
460 case Decl::FileScopeAsm: {
461 Out << "<file-scope asm>\n";
464 case Decl::UsingDirective: {
465 Out << "<using directive>\n";
468 case Decl::NamespaceAlias: {
469 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(I);
470 Out << "<namespace alias> " << *NAD << '\n';
473 case Decl::ClassTemplate: {
474 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(I);
475 Out << "<class template> " << *CTD << '\n';
478 case Decl::OMPThreadPrivate: {
479 Out << "<omp threadprivate> " << '"' << I << "\"\n";
483 Out << "DeclKind: " << DK << '"' << I << "\"\n";
484 llvm_unreachable("decl unhandled");
488 std::unique_ptr<ASTConsumer> clang::CreateDeclContextPrinter() {
489 return llvm::make_unique<DeclContextPrinter>();