21#include "llvm/Support/Timer.h"
22#include "llvm/Support/raw_ostream.h"
31 typedef RecursiveASTVisitor<ASTPrinter> base;
34 enum Kind { DumpFull, Dump, Print, None };
35 ASTPrinter(std::unique_ptr<raw_ostream> Out, Kind K,
37 bool DumpLookups =
false,
bool DumpDeclTypes =
false)
38 : Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)),
39 OutputKind(K), OutputFormat(Format), FilterString(FilterString),
40 DumpLookups(DumpLookups), DumpDeclTypes(DumpDeclTypes) {}
43 StringRef FilterString,
bool DumpLookups =
false,
44 bool DumpDeclTypes =
false)
45 : Out(Out), OwnedOut(
nullptr), OutputKind(K), OutputFormat(Format),
46 FilterString(FilterString), DumpLookups(DumpLookups),
47 DumpDeclTypes(DumpDeclTypes) {}
49 void HandleTranslationUnit(ASTContext &Context)
override {
52 if (FilterString.empty())
58 bool shouldWalkTypesOfTypeLocs()
const {
return false; }
60 bool TraverseDecl(Decl *D) {
61 if (D && filterMatches(D)) {
62 bool ShowColors = Out.has_colors();
64 Out.changeColor(raw_ostream::BLUE);
67 Out << (OutputKind != Print ?
"Dumping " :
"Printing ") <<
getName(D)
86 bool filterMatches(Decl *D) {
87 return getName(D).find(FilterString) != std::string::npos;
91 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
92 if (DC == DC->getPrimaryContext())
93 DC->dumpLookups(Out, OutputKind != None, OutputKind == DumpFull);
95 Out <<
"Lookup map is in primary DeclContext "
96 << DC->getPrimaryContext() <<
"\n";
98 Out <<
"Not a DeclContext\n";
99 }
else if (OutputKind == Print) {
101 Policy.IncludeTagDefinition =
true;
102 D->
print(Out, Policy, 0,
true);
103 }
else if (OutputKind != None) {
104 D->
dump(Out, OutputKind == DumpFull, OutputFormat);
109 if (
auto *TD = dyn_cast<TemplateDecl>(D))
110 if (Decl *TempD = TD->getTemplatedDecl())
115 if (
auto *VD = dyn_cast<ValueDecl>(InnerD))
116 VD->getType().
dump(Out, VD->getASTContext());
117 if (
auto *TD = dyn_cast<TypeDecl>(InnerD)) {
118 const ASTContext &Ctx = TD->getASTContext();
125 std::unique_ptr<raw_ostream> OwnedOut;
134 std::string FilterString;
148 ASTDeclNodeLister(raw_ostream *Out =
nullptr)
149 : Out(Out ? *Out : llvm::outs()) {
150 ShouldWalkTypesOfTypeLocs =
false;
153 void HandleTranslationUnit(ASTContext &Context)
override {
157 bool VisitNamedDecl(NamedDecl *D)
override {
168std::unique_ptr<ASTConsumer>
170 StringRef FilterString) {
171 return std::make_unique<ASTPrinter>(std::move(Out), ASTPrinter::Print,
175std::unique_ptr<ASTConsumer>
177 bool DumpDecls,
bool Deserialize,
bool DumpLookups,
179 assert((DumpDecls || Deserialize || DumpLookups) &&
"nothing to dump");
180 return std::make_unique<ASTPrinter>(
182 Deserialize ? ASTPrinter::DumpFull
183 : DumpDecls ? ASTPrinter::Dump : ASTPrinter::None,
184 Format, FilterString, DumpLookups, DumpDeclTypes);
187std::unique_ptr<ASTConsumer>
189 bool Deserialize,
bool DumpLookups,
bool DumpDeclTypes,
191 assert((DumpDecls || Deserialize || DumpLookups) &&
"nothing to dump");
192 return std::make_unique<ASTPrinter>(Out,
193 Deserialize ? ASTPrinter::DumpFull
194 : DumpDecls ? ASTPrinter::Dump
196 Format, FilterString, DumpLookups,
201 return std::make_unique<ASTDeclNodeLister>(
nullptr);
212 void Initialize(
ASTContext &Context)
override { this->Context = &Context; }
214 bool HandleTopLevelDecl(DeclGroupRef D)
override {
216 HandleTopLevelSingleDecl(*I);
220 void HandleTopLevelSingleDecl(Decl *D);
224void ASTViewer::HandleTopLevelSingleDecl(
Decl *D) {
226 D->
print(llvm::errs());
228 if (Stmt *Body = D->
getBody()) {
229 llvm::errs() <<
'\n';
231 llvm::errs() <<
'\n';
237 return std::make_unique<ASTViewer>();
Defines the clang::ASTContext interface.
Defines the Diagnostic-related interfaces.
static void print(llvm::raw_ostream &OS, const T &V, const Context &Ctx, QualType Ty)
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
TranslationUnitDecl * getTranslationUnitDecl() const
const LangOptions & getLangOpts() const
QualType getTypeDeclType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier Qualifier, const TypeDecl *Decl) const
Decl - This represents one declaration (or definition), e.g.
ASTContext & getASTContext() const LLVM_READONLY
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
void print(raw_ostream &Out, unsigned Indentation=0, bool PrintInstantiation=false) const
void printQualifiedName(raw_ostream &OS) const
Returns a human-readable qualified name for this declaration, like A::B::i, for i being member of nam...
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
bool TraverseDecl(Decl *D)
StringRef getName(const HeaderType T)
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
The JSON file list parser is used to communicate input to InstallAPI.
ASTDumpOutputFormat
Used to specify the format for printing AST dump information.
bool isa(CodeGen::Address addr)
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
std::unique_ptr< ASTConsumer > CreateASTDeclNodeLister()
std::unique_ptr< ASTConsumer > CreateASTDumper(std::unique_ptr< raw_ostream > OS, StringRef FilterString, bool DumpDecls, bool Deserialize, bool DumpLookups, bool DumpDeclTypes, ASTDumpOutputFormat Format)
std::unique_ptr< ASTConsumer > CreateASTPrinter(std::unique_ptr< raw_ostream > OS, StringRef FilterString)
std::unique_ptr< ASTConsumer > CreateASTViewer()
DynamicRecursiveASTVisitorBase< false > DynamicRecursiveASTVisitor
U cast(CodeGen::Address addr)