22 #include "llvm/Support/Path.h" 23 #include "llvm/Support/Timer.h" 24 #include "llvm/Support/raw_ostream.h" 25 using namespace clang;
36 enum Kind { DumpFull, Dump, Print,
None };
37 ASTPrinter(std::unique_ptr<raw_ostream> Out,
Kind K,
39 bool DumpLookups =
false)
40 : Out(Out ? *Out :
llvm::outs()), OwnedOut(
std::move(Out)),
41 OutputKind(K), OutputFormat(Format), FilterString(FilterString),
42 DumpLookups(DumpLookups) {}
44 void HandleTranslationUnit(
ASTContext &Context)
override {
47 if (FilterString.empty())
53 bool shouldWalkTypesOfTypeLocs()
const {
return false; }
55 bool TraverseDecl(
Decl *D) {
56 if (D && filterMatches(D)) {
57 bool ShowColors = Out.has_colors();
59 Out.changeColor(raw_ostream::BLUE);
60 Out << (OutputKind != Print ?
"Dumping " :
"Printing ") <<
getName(D)
69 return base::TraverseDecl(D);
74 if (isa<NamedDecl>(D))
75 return cast<NamedDecl>(D)->getQualifiedNameAsString();
78 bool filterMatches(
Decl *D) {
79 return getName(D).find(FilterString) != std::string::npos;
84 if (DC == DC->getPrimaryContext())
85 DC->dumpLookups(Out, OutputKind != None, OutputKind == DumpFull);
87 Out <<
"Lookup map is in primary DeclContext " 88 << DC->getPrimaryContext() <<
"\n";
90 Out <<
"Not a DeclContext\n";
91 }
else if (OutputKind == Print) {
93 D->
print(Out, Policy, 0,
true);
94 }
else if (OutputKind != None)
95 D->
dump(Out, OutputKind == DumpFull, OutputFormat);
99 std::unique_ptr<raw_ostream> OwnedOut;
108 std::string FilterString;
119 ASTDeclNodeLister(raw_ostream *Out =
nullptr)
120 : Out(Out ? *Out :
llvm::outs()) {}
122 void HandleTranslationUnit(
ASTContext &Context)
override {
126 bool shouldWalkTypesOfTypeLocs()
const {
return false; }
139 std::unique_ptr<ASTConsumer>
141 StringRef FilterString) {
142 return llvm::make_unique<ASTPrinter>(std::move(Out), ASTPrinter::Print,
146 std::unique_ptr<ASTConsumer>
148 bool DumpDecls,
bool Deserialize,
bool DumpLookups,
150 assert((DumpDecls || Deserialize || DumpLookups) &&
"nothing to dump");
151 return llvm::make_unique<ASTPrinter>(std::move(Out),
152 Deserialize ? ASTPrinter::DumpFull :
153 DumpDecls ? ASTPrinter::Dump :
155 FilterString, DumpLookups);
159 return llvm::make_unique<ASTDeclNodeLister>(
nullptr);
169 void Initialize(
ASTContext &Context)
override {
170 this->Context = &Context;
175 HandleTopLevelSingleDecl(*I);
179 void HandleTopLevelSingleDecl(
Decl *D);
183 void ASTViewer::HandleTopLevelSingleDecl(
Decl *D) {
184 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
185 D->
print(llvm::errs());
188 llvm::errs() <<
'\n';
190 llvm::errs() <<
'\n';
196 return llvm::make_unique<ASTViewer>();
Defines the clang::ASTContext interface.
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs...
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Stmt - This represents one statement.
Defines the SourceManager interface.
Decl - This represents one declaration (or definition), e.g.
Describes how types, statements, expressions, and declarations should be printed. ...
void print(raw_ostream &Out, unsigned Indentation=0, bool PrintInstantiation=false) const
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Defines the Diagnostic-related interfaces.
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
ASTDumpOutputFormat
Used to specify the format for printing AST dump information.
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...
ASTContext & getASTContext() const LLVM_READONLY
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
static std::string getName(const CallEvent &Call)
std::unique_ptr< ASTConsumer > CreateASTViewer()
std::unique_ptr< ASTConsumer > CreateASTDeclNodeLister()
TranslationUnitDecl * getTranslationUnitDecl() const
The top declaration context.
std::unique_ptr< ASTConsumer > CreateASTPrinter(std::unique_ptr< raw_ostream > OS, StringRef FilterString)
This represents a decl that may have a name.
const LangOptions & getLangOpts() const
std::unique_ptr< ASTConsumer > CreateASTDumper(std::unique_ptr< raw_ostream > OS, StringRef FilterString, bool DumpDecls, bool Deserialize, bool DumpLookups, ASTDumpOutputFormat Format)