22#include "llvm/Support/Error.h"
23#include "llvm/Support/GlobPattern.h"
24#include "llvm/Support/Timer.h"
25#include "llvm/Support/raw_ostream.h"
34 typedef RecursiveASTVisitor<ASTPrinter> base;
37 enum Kind { DumpFull, Dump, Print, None };
38 ASTPrinter(std::unique_ptr<raw_ostream> Out, Kind K,
40 StringRef FilterPath,
bool DumpLookups =
false,
41 bool DumpDeclTypes =
false)
42 : Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)),
43 OutputKind(K), OutputFormat(Format), FilterString(FilterString),
44 FilterPath(FilterPath), DumpLookups(DumpLookups),
45 DumpDeclTypes(DumpDeclTypes) {}
48 StringRef FilterString, StringRef FilterPath,
49 bool DumpLookups =
false,
bool DumpDeclTypes =
false)
50 : Out(Out), OwnedOut(
nullptr), OutputKind(K), OutputFormat(Format),
51 FilterString(FilterString), FilterPath(FilterPath),
52 DumpLookups(DumpLookups), DumpDeclTypes(DumpDeclTypes) {}
54 void HandleTranslationUnit(ASTContext &Context)
override {
57 if (FilterString.empty() && FilterPath.empty())
63 bool shouldWalkTypesOfTypeLocs()
const {
return false; }
65 bool TraverseDecl(Decl *D) {
66 if (D && filterMatches(D)) {
67 bool ShowColors = Out.has_colors();
69 Out.changeColor(raw_ostream::BLUE);
72 Out << (OutputKind != Print ?
"Dumping " :
"Printing ") <<
getName(D)
91 bool filterMatches(Decl *D) {
92 if (!FilterString.empty() &&
93 getName(D).find(FilterString) == std::string::npos)
96 if (!FilterPath.empty()) {
107 llvm::Expected<llvm::GlobPattern> Pattern =
108 llvm::GlobPattern::create(FilterPath);
110 llvm::consumeError(Pattern.takeError());
120 void print(Decl *D) {
122 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
123 if (DC == DC->getPrimaryContext())
124 DC->dumpLookups(Out, OutputKind != None, OutputKind == DumpFull);
126 Out <<
"Lookup map is in primary DeclContext "
127 << DC->getPrimaryContext() <<
"\n";
129 Out <<
"Not a DeclContext\n";
130 }
else if (OutputKind == Print) {
132 Policy.IncludeTagDefinition =
true;
133 Policy.PrettyEnums =
false;
134 D->
print(Out, Policy, 0,
true);
135 }
else if (OutputKind != None) {
136 D->
dump(Out, OutputKind == DumpFull, OutputFormat);
141 if (
auto *TD = dyn_cast<TemplateDecl>(D))
142 if (Decl *TempD = TD->getTemplatedDecl())
147 if (
auto *VD = dyn_cast<ValueDecl>(InnerD))
148 VD->getType().
dump(Out, VD->getASTContext());
149 if (
auto *TD = dyn_cast<TypeDecl>(InnerD)) {
150 const ASTContext &Ctx = TD->getASTContext();
157 std::unique_ptr<raw_ostream> OwnedOut;
166 std::string FilterString;
169 std::string FilterPath;
183 ASTDeclNodeLister(raw_ostream *Out =
nullptr)
184 : Out(Out ? *Out : llvm::outs()) {
185 ShouldWalkTypesOfTypeLocs =
false;
188 void HandleTranslationUnit(ASTContext &Context)
override {
192 bool VisitNamedDecl(NamedDecl *D)
override {
203std::unique_ptr<ASTConsumer>
205 StringRef FilterString) {
206 return std::make_unique<ASTPrinter>(std::move(Out), ASTPrinter::Print,
210std::unique_ptr<ASTConsumer>
212 StringRef FilterPath,
bool DumpDecls,
bool Deserialize,
213 bool DumpLookups,
bool DumpDeclTypes,
215 assert((DumpDecls || Deserialize || DumpLookups) &&
"nothing to dump");
216 return std::make_unique<ASTPrinter>(std::move(Out),
217 Deserialize ? ASTPrinter::DumpFull
218 : DumpDecls ? ASTPrinter::Dump
220 Format, FilterString, FilterPath,
221 DumpLookups, DumpDeclTypes);
224std::unique_ptr<ASTConsumer>
226 StringRef FilterPath,
bool DumpDecls,
bool Deserialize,
227 bool DumpLookups,
bool DumpDeclTypes,
229 assert((DumpDecls || Deserialize || DumpLookups) &&
"nothing to dump");
230 return std::make_unique<ASTPrinter>(Out,
231 Deserialize ? ASTPrinter::DumpFull
232 : DumpDecls ? ASTPrinter::Dump
234 Format, FilterString, FilterPath,
235 DumpLookups, DumpDeclTypes);
239 return std::make_unique<ASTDeclNodeLister>(
nullptr);
250 void Initialize(
ASTContext &Context)
override { this->Context = &Context; }
252 bool HandleTopLevelDecl(DeclGroupRef D)
override {
254 HandleTopLevelSingleDecl(*I);
258 void HandleTopLevelSingleDecl(Decl *D);
262void ASTViewer::HandleTopLevelSingleDecl(
Decl *D) {
264 D->
print(llvm::errs());
266 if (Stmt *Body = D->
getBody()) {
267 llvm::errs() <<
'\n';
269 llvm::errs() <<
'\n';
275 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)
Defines the SourceManager interface.
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 ...
SourceManager & getSourceManager()
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...
SourceLocation getLocation() const
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...
const char * getFilename() const
Return the presumed filename of this location.
bool isInvalid() const
Return true if this object is invalid or uninitialized.
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
bool TraverseDecl(Decl *D)
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
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.
Top level wrappers for InstallAPI frontend operations.
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 > CreateASTPrinter(std::unique_ptr< raw_ostream > OS, StringRef FilterString)
std::unique_ptr< ASTConsumer > CreateASTViewer()
DynamicRecursiveASTVisitorBase< false > DynamicRecursiveASTVisitor
U cast(CodeGen::Address addr)
std::unique_ptr< ASTConsumer > CreateASTDumper(std::unique_ptr< raw_ostream > OS, StringRef FilterString, StringRef FilterPath, bool DumpDecls, bool Deserialize, bool DumpLookups, bool DumpDeclTypes, ASTDumpOutputFormat Format)