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 Policy.PrettyEnums =
false;
103 D->
print(Out, Policy, 0,
true);
104 }
else if (OutputKind != None) {
105 D->
dump(Out, OutputKind == DumpFull, OutputFormat);
110 if (
auto *TD = dyn_cast<TemplateDecl>(D))
111 if (Decl *TempD = TD->getTemplatedDecl())
116 if (
auto *VD = dyn_cast<ValueDecl>(InnerD))
117 VD->getType().
dump(Out, VD->getASTContext());
118 if (
auto *TD = dyn_cast<TypeDecl>(InnerD)) {
119 const ASTContext &Ctx = TD->getASTContext();
126 std::unique_ptr<raw_ostream> OwnedOut;
135 std::string FilterString;
149 ASTDeclNodeLister(raw_ostream *Out =
nullptr)
150 : Out(Out ? *Out : llvm::outs()) {
151 ShouldWalkTypesOfTypeLocs =
false;
154 void HandleTranslationUnit(ASTContext &Context)
override {
158 bool VisitNamedDecl(NamedDecl *D)
override {
169std::unique_ptr<ASTConsumer>
171 StringRef FilterString) {
172 return std::make_unique<ASTPrinter>(std::move(Out), ASTPrinter::Print,
176std::unique_ptr<ASTConsumer>
178 bool DumpDecls,
bool Deserialize,
bool DumpLookups,
180 assert((DumpDecls || Deserialize || DumpLookups) &&
"nothing to dump");
181 return std::make_unique<ASTPrinter>(
183 Deserialize ? ASTPrinter::DumpFull
184 : DumpDecls ? ASTPrinter::Dump : ASTPrinter::None,
185 Format, FilterString, DumpLookups, DumpDeclTypes);
188std::unique_ptr<ASTConsumer>
190 bool Deserialize,
bool DumpLookups,
bool DumpDeclTypes,
192 assert((DumpDecls || Deserialize || DumpLookups) &&
"nothing to dump");
193 return std::make_unique<ASTPrinter>(Out,
194 Deserialize ? ASTPrinter::DumpFull
195 : DumpDecls ? ASTPrinter::Dump
197 Format, FilterString, DumpLookups,
202 return std::make_unique<ASTDeclNodeLister>(
nullptr);
213 void Initialize(
ASTContext &Context)
override { this->Context = &Context; }
215 bool HandleTopLevelDecl(DeclGroupRef D)
override {
217 HandleTopLevelSingleDecl(*I);
221 void HandleTopLevelSingleDecl(Decl *D);
225void ASTViewer::HandleTopLevelSingleDecl(
Decl *D) {
227 D->
print(llvm::errs());
229 if (Stmt *Body = D->
getBody()) {
230 llvm::errs() <<
'\n';
232 llvm::errs() <<
'\n';
238 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)