20#include "llvm/Support/Timer.h"
21#include "llvm/Support/raw_ostream.h"
30 typedef RecursiveASTVisitor<ASTPrinter> base;
33 enum Kind { DumpFull, Dump, Print, None };
34 ASTPrinter(std::unique_ptr<raw_ostream> Out, Kind K,
36 bool DumpLookups =
false,
bool DumpDeclTypes =
false)
37 : Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)),
38 OutputKind(K), OutputFormat(Format), FilterString(FilterString),
39 DumpLookups(DumpLookups), DumpDeclTypes(DumpDeclTypes) {}
42 StringRef FilterString,
bool DumpLookups =
false,
43 bool DumpDeclTypes =
false)
44 : Out(Out), OwnedOut(
nullptr), OutputKind(K), OutputFormat(Format),
45 FilterString(FilterString), DumpLookups(DumpLookups),
46 DumpDeclTypes(DumpDeclTypes) {}
48 void HandleTranslationUnit(ASTContext &Context)
override {
51 if (FilterString.empty())
57 bool shouldWalkTypesOfTypeLocs()
const {
return false; }
59 bool TraverseDecl(Decl *D) {
60 if (D && filterMatches(D)) {
61 bool ShowColors = Out.has_colors();
63 Out.changeColor(raw_ostream::BLUE);
66 Out << (OutputKind != Print ?
"Dumping " :
"Printing ") <<
getName(D)
85 bool filterMatches(Decl *D) {
86 return getName(D).find(FilterString) != std::string::npos;
90 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
91 if (DC == DC->getPrimaryContext())
92 DC->dumpLookups(Out, OutputKind != None, OutputKind == DumpFull);
94 Out <<
"Lookup map is in primary DeclContext "
95 << DC->getPrimaryContext() <<
"\n";
97 Out <<
"Not a DeclContext\n";
98 }
else if (OutputKind == Print) {
100 Policy.IncludeTagDefinition =
true;
101 D->
print(Out, Policy, 0,
true);
102 }
else if (OutputKind != None) {
103 D->
dump(Out, OutputKind == DumpFull, OutputFormat);
108 if (
auto *TD = dyn_cast<TemplateDecl>(D))
109 if (Decl *TempD = TD->getTemplatedDecl())
114 if (
auto *VD = dyn_cast<ValueDecl>(InnerD))
115 VD->getType().
dump(Out, VD->getASTContext());
116 if (
auto *TD = dyn_cast<TypeDecl>(InnerD)) {
117 const ASTContext &Ctx = TD->getASTContext();
124 std::unique_ptr<raw_ostream> OwnedOut;
133 std::string FilterString;
147 ASTDeclNodeLister(raw_ostream *Out =
nullptr)
148 : Out(Out ? *Out : llvm::outs()) {}
150 void HandleTranslationUnit(ASTContext &Context)
override {
154 bool shouldWalkTypesOfTypeLocs()
const {
return false; }
156 bool VisitNamedDecl(NamedDecl *D) {
167std::unique_ptr<ASTConsumer>
169 StringRef FilterString) {
170 return std::make_unique<ASTPrinter>(std::move(Out), ASTPrinter::Print,
174std::unique_ptr<ASTConsumer>
176 bool DumpDecls,
bool Deserialize,
bool DumpLookups,
178 assert((DumpDecls || Deserialize || DumpLookups) &&
"nothing to dump");
179 return std::make_unique<ASTPrinter>(
181 Deserialize ? ASTPrinter::DumpFull
182 : DumpDecls ? ASTPrinter::Dump : ASTPrinter::None,
183 Format, FilterString, DumpLookups, DumpDeclTypes);
186std::unique_ptr<ASTConsumer>
188 bool Deserialize,
bool DumpLookups,
bool DumpDeclTypes,
190 assert((DumpDecls || Deserialize || DumpLookups) &&
"nothing to dump");
191 return std::make_unique<ASTPrinter>(Out,
192 Deserialize ? ASTPrinter::DumpFull
193 : DumpDecls ? ASTPrinter::Dump
195 Format, FilterString, DumpLookups,
200 return std::make_unique<ASTDeclNodeLister>(
nullptr);
211 void Initialize(
ASTContext &Context)
override { this->Context = &Context; }
213 bool HandleTopLevelDecl(DeclGroupRef D)
override {
215 HandleTopLevelSingleDecl(*I);
219 void HandleTopLevelSingleDecl(Decl *D);
223void ASTViewer::HandleTopLevelSingleDecl(
Decl *D) {
225 D->
print(llvm::errs());
227 if (Stmt *Body = D->
getBody()) {
228 llvm::errs() <<
'\n';
230 llvm::errs() <<
'\n';
236 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, ASTContext &ASTCtx, 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()
U cast(CodeGen::Address addr)