23 #include "llvm/Support/Path.h" 24 #include "llvm/Support/Timer.h" 25 #include "llvm/Support/raw_ostream.h" 26 using namespace clang;
37 enum Kind { DumpFull, Dump, Print,
None };
38 ASTPrinter(std::unique_ptr<raw_ostream> Out,
Kind K, StringRef FilterString,
39 bool DumpLookups =
false)
40 : Out(Out ? *Out :
llvm::outs()), OwnedOut(
std::move(Out)),
41 OutputKind(K), FilterString(FilterString), DumpLookups(DumpLookups) {}
43 void HandleTranslationUnit(
ASTContext &Context)
override {
46 if (FilterString.empty())
52 bool shouldWalkTypesOfTypeLocs()
const {
return false; }
54 bool TraverseDecl(
Decl *D) {
55 if (D && filterMatches(D)) {
56 bool ShowColors = Out.has_colors();
58 Out.changeColor(raw_ostream::BLUE);
59 Out << (OutputKind != Print ?
"Dumping " :
"Printing ") <<
getName(D)
68 return base::TraverseDecl(D);
73 if (isa<NamedDecl>(D))
74 return cast<NamedDecl>(D)->getQualifiedNameAsString();
77 bool filterMatches(
Decl *D) {
78 return getName(D).find(FilterString) != std::string::npos;
83 if (DC == DC->getPrimaryContext())
84 DC->dumpLookups(Out, OutputKind != None, OutputKind == DumpFull);
86 Out <<
"Lookup map is in primary DeclContext " 87 << DC->getPrimaryContext() <<
"\n";
89 Out <<
"Not a DeclContext\n";
90 }
else if (OutputKind == Print) {
92 D->
print(Out, Policy, 0,
true);
93 }
else if (OutputKind != None)
94 D->
dump(Out, OutputKind == DumpFull);
98 std::unique_ptr<raw_ostream> OwnedOut;
104 std::string FilterString;
115 ASTDeclNodeLister(raw_ostream *Out =
nullptr)
116 : Out(Out ? *Out :
llvm::outs()) {}
118 void HandleTranslationUnit(
ASTContext &Context)
override {
122 bool shouldWalkTypesOfTypeLocs()
const {
return false; }
135 std::unique_ptr<ASTConsumer>
137 StringRef FilterString) {
138 return llvm::make_unique<ASTPrinter>(std::move(Out), ASTPrinter::Print,
142 std::unique_ptr<ASTConsumer>
144 StringRef FilterString,
148 assert((DumpDecls || Deserialize || DumpLookups) &&
"nothing to dump");
149 return llvm::make_unique<ASTPrinter>(std::move(Out),
150 Deserialize ? ASTPrinter::DumpFull :
151 DumpDecls ? ASTPrinter::Dump :
153 FilterString, DumpLookups);
157 return llvm::make_unique<ASTDeclNodeLister>(
nullptr);
167 void Initialize(
ASTContext &Context)
override {
168 this->Context = &Context;
173 HandleTopLevelSingleDecl(*I);
177 void HandleTopLevelSingleDecl(
Decl *D);
181 void ASTViewer::HandleTopLevelSingleDecl(
Decl *D) {
182 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
183 D->
print(llvm::errs());
186 llvm::errs() <<
'\n';
188 llvm::errs() <<
'\n';
194 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...
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
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. ...
std::string getName(ArrayRef< StringRef > Parts) const
Get the platform-specific name separator.
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...
std::unique_ptr< ASTConsumer > CreateASTDumper(std::unique_ptr< raw_ostream > OS, StringRef FilterString, bool DumpDecls, bool Deserialize, bool DumpLookups)
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...
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