22#include "llvm/Support/Path.h"
23#include "llvm/Support/Timer.h"
24#include "llvm/Support/raw_ostream.h"
37 ASTPrinter(std::unique_ptr<raw_ostream> Out, Kind K,
39 bool DumpLookups =
false,
bool DumpDeclTypes =
false)
40 : Out(Out ? *Out :
llvm::outs()), OwnedOut(
std::move(Out)),
41 OutputKind(K), OutputFormat(Format), FilterString(FilterString),
42 DumpLookups(DumpLookups), DumpDeclTypes(DumpDeclTypes) {}
47 if (FilterString.empty())
56 if (
D && filterMatches(
D)) {
59 Out.changeColor(raw_ostream::BLUE);
62 Out << (OutputKind != Print ?
"Dumping " :
"Printing ") <<
getName(
D)
77 if (isa<NamedDecl>(
D))
78 return cast<NamedDecl>(
D)->getQualifiedNameAsString();
81 bool filterMatches(
Decl *
D) {
82 return getName(
D).find(FilterString) != std::string::npos;
87 if (DC == DC->getPrimaryContext())
88 DC->dumpLookups(Out, OutputKind != None, OutputKind == DumpFull);
90 Out <<
"Lookup map is in primary DeclContext "
91 << DC->getPrimaryContext() <<
"\n";
93 Out <<
"Not a DeclContext\n";
94 }
else if (OutputKind == Print) {
96 D->
print(Out, Policy, 0,
true);
97 }
else if (OutputKind != None) {
98 D->
dump(Out, OutputKind == DumpFull, OutputFormat);
103 if (
auto *TD = dyn_cast<TemplateDecl>(
D))
104 InnerD = TD->getTemplatedDecl();
108 if (
auto *VD = dyn_cast<ValueDecl>(InnerD))
109 VD->getType().
dump(Out, VD->getASTContext());
110 if (
auto *TD = dyn_cast<TypeDecl>(InnerD))
111 TD->getTypeForDecl()->dump(Out, TD->getASTContext());
116 std::unique_ptr<raw_ostream> OwnedOut;
125 std::string FilterString;
139 ASTDeclNodeLister(raw_ostream *Out =
nullptr)
140 : Out(Out ? *Out :
llvm::outs()) {}
149 D->printQualifiedName(Out);
159std::unique_ptr<ASTConsumer>
161 StringRef FilterString) {
162 return std::make_unique<ASTPrinter>(std::move(Out), ASTPrinter::Print,
166std::unique_ptr<ASTConsumer>
168 bool DumpDecls,
bool Deserialize,
bool DumpLookups,
170 assert((DumpDecls || Deserialize || DumpLookups) &&
"nothing to dump");
171 return std::make_unique<ASTPrinter>(
173 Deserialize ? ASTPrinter::DumpFull
174 : DumpDecls ? ASTPrinter::Dump : ASTPrinter::None,
175 Format, FilterString, DumpLookups, DumpDeclTypes);
179 return std::make_unique<ASTDeclNodeLister>(
nullptr);
194 HandleTopLevelSingleDecl(*I);
198 void HandleTopLevelSingleDecl(
Decl *
D);
202void ASTViewer::HandleTopLevelSingleDecl(
Decl *
D) {
203 if (isa<FunctionDecl>(
D) || isa<ObjCMethodDecl>(
D)) {
207 llvm::errs() <<
'\n';
209 llvm::errs() <<
'\n';
215 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)
static std::string getName(const CallEvent &Call)
Defines the SourceManager interface.
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
virtual void HandleTranslationUnit(ASTContext &Ctx)
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
virtual bool HandleTopLevelDecl(DeclGroupRef D)
HandleTopLevelDecl - Handle the specified top-level declaration.
virtual void Initialize(ASTContext &Context)
Initialize - This is called to initialize the consumer, providing the ASTContext.
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
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
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
This represents a decl that may have a name.
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
bool TraverseDecl(Decl *D)
Recursively visit a declaration, by dispatching to Traverse*Decl() based on the argument's dynamic ty...
bool shouldWalkTypesOfTypeLocs() const
Return whether this visitor should recurse into the types of TypeLocs.
Stmt - This represents one statement.
The top declaration context.
constexpr XRayInstrMask None
bool Dump(InterpState &S, CodePtr OpPC)
The JSON file list parser is used to communicate input to InstallAPI.
ASTDumpOutputFormat
Used to specify the format for printing AST dump information.
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()
Diagnostic wrappers for TextAPI types for error reporting.
Describes how types, statements, expressions, and declarations should be printed.