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 if (
Decl *TempD = TD->getTemplatedDecl())
109 if (
auto *VD = dyn_cast<ValueDecl>(InnerD))
110 VD->getType().
dump(Out, VD->getASTContext());
111 if (
auto *TD = dyn_cast<TypeDecl>(InnerD))
112 TD->getTypeForDecl()->dump(Out, TD->getASTContext());
117 std::unique_ptr<raw_ostream> OwnedOut;
126 std::string FilterString;
140 ASTDeclNodeLister(raw_ostream *Out =
nullptr)
141 : Out(Out ? *Out :
llvm::outs()) {}
150 D->printQualifiedName(Out);
160std::unique_ptr<ASTConsumer>
162 StringRef FilterString) {
163 return std::make_unique<ASTPrinter>(std::move(Out), ASTPrinter::Print,
167std::unique_ptr<ASTConsumer>
169 bool DumpDecls,
bool Deserialize,
bool DumpLookups,
171 assert((DumpDecls || Deserialize || DumpLookups) &&
"nothing to dump");
172 return std::make_unique<ASTPrinter>(
174 Deserialize ? ASTPrinter::DumpFull
175 : DumpDecls ? ASTPrinter::Dump : ASTPrinter::None,
176 Format, FilterString, DumpLookups, DumpDeclTypes);
180 return std::make_unique<ASTDeclNodeLister>(
nullptr);
195 HandleTopLevelSingleDecl(*I);
199 void HandleTopLevelSingleDecl(
Decl *
D);
203void ASTViewer::HandleTopLevelSingleDecl(
Decl *
D) {
204 if (isa<FunctionDecl>(
D) || isa<ObjCMethodDecl>(
D)) {
208 llvm::errs() <<
'\n';
210 llvm::errs() <<
'\n';
216 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.