diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/AST/DeclPrinter.cpp | 6 | ||||
-rw-r--r-- | lib/AST/DumpXML.cpp | 5 | ||||
-rw-r--r-- | lib/Frontend/ASTConsumers.cpp | 76 | ||||
-rw-r--r-- | lib/Frontend/CompilerInvocation.cpp | 3 | ||||
-rw-r--r-- | lib/Frontend/FrontendActions.cpp | 4 |
5 files changed, 68 insertions, 26 deletions
diff --git a/lib/AST/DeclPrinter.cpp b/lib/AST/DeclPrinter.cpp index 10f38942a5..fe8f8298be 100644 --- a/lib/AST/DeclPrinter.cpp +++ b/lib/AST/DeclPrinter.cpp @@ -173,8 +173,10 @@ void DeclContext::dumpDeclContext() const { Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false); } -void Decl::dump() const { - print(llvm::errs()); +void Decl::dump(raw_ostream &Out) const { + PrintingPolicy Policy = getASTContext().getPrintingPolicy(); + Policy.Dump = true; + print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ true); } raw_ostream& DeclPrinter::Indent(unsigned Indentation) { diff --git a/lib/AST/DumpXML.cpp b/lib/AST/DumpXML.cpp index c5b3c6875b..ad551ccf0a 100644 --- a/lib/AST/DumpXML.cpp +++ b/lib/AST/DumpXML.cpp @@ -1022,17 +1022,12 @@ struct XMLDumper : public XMLDeclVisitor<XMLDumper>, }; } -void Decl::dumpXML() const { - dumpXML(llvm::errs()); -} - void Decl::dumpXML(raw_ostream &out) const { XMLDumper(out, getASTContext()).dispatch(const_cast<Decl*>(this)); } #else /* ifndef NDEBUG */ -void Decl::dumpXML() const {} void Decl::dumpXML(raw_ostream &out) const {} #endif diff --git a/lib/Frontend/ASTConsumers.cpp b/lib/Frontend/ASTConsumers.cpp index 390ae09497..80f94397aa 100644 --- a/lib/Frontend/ASTConsumers.cpp +++ b/lib/Frontend/ASTConsumers.cpp @@ -12,47 +12,89 @@ //===----------------------------------------------------------------------===// #include "clang/Frontend/ASTConsumers.h" +#include "clang/Basic/FileManager.h" #include "clang/Basic/Diagnostic.h" #include "clang/Basic/SourceManager.h" -#include "clang/Basic/FileManager.h" #include "clang/AST/AST.h" #include "clang/AST/ASTConsumer.h" #include "clang/AST/ASTContext.h" -#include "clang/AST/RecordLayout.h" #include "clang/AST/PrettyPrinter.h" +#include "clang/AST/RecordLayout.h" +#include "clang/AST/RecursiveASTVisitor.h" #include "llvm/Module.h" -#include "llvm/Support/Timer.h" -#include "llvm/Support/raw_ostream.h" #include "llvm/Support/Path.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Timer.h" using namespace clang; //===----------------------------------------------------------------------===// /// ASTPrinter - Pretty-printer and dumper of ASTs namespace { - class ASTPrinter : public ASTConsumer { - raw_ostream &Out; - bool Dump; + class ASTPrinter : public ASTConsumer, + public RecursiveASTVisitor<ASTPrinter> { + typedef RecursiveASTVisitor<ASTPrinter> base; public: - ASTPrinter(raw_ostream* o = NULL, bool Dump = false) - : Out(o? *o : llvm::outs()), Dump(Dump) { } + ASTPrinter(raw_ostream *Out = NULL, bool Dump = false, + StringRef FilterString = "") + : Out(Out ? *Out : llvm::outs()), Dump(Dump), + FilterString(FilterString) {} virtual void HandleTranslationUnit(ASTContext &Context) { - PrintingPolicy Policy = Context.getPrintingPolicy(); - Policy.Dump = Dump; - Context.getTranslationUnitDecl()->print(Out, Policy, /*Indentation=*/0, - /*PrintInstantiation=*/true); + TranslationUnitDecl *D = Context.getTranslationUnitDecl(); + + if (FilterString.empty()) { + if (Dump) + D->dump(Out); + else + D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true); + return; + } + + TraverseDecl(D); + } + + bool shouldWalkTypesOfTypeLocs() const { return false; } + + bool TraverseDecl(Decl *D) { + if (filterMatches(D)) { + Out.changeColor(llvm::raw_ostream::BLUE) << + (Dump ? "Dumping " : "Printing ") << getName(D) << ":\n"; + Out.resetColor(); + if (Dump) + D->dump(Out); + else + D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true); + // Don't traverse child nodes to avoid output duplication. + return true; + } + return base::TraverseDecl(D); + } + + private: + std::string getName(Decl *D) { + if (isa<NamedDecl>(D)) + return cast<NamedDecl>(D)->getQualifiedNameAsString(); + return ""; + } + bool filterMatches(Decl *D) { + return getName(D).find(FilterString) != std::string::npos; } + + raw_ostream &Out; + bool Dump; + std::string FilterString; }; } // end anonymous namespace -ASTConsumer *clang::CreateASTPrinter(raw_ostream* out) { - return new ASTPrinter(out); +ASTConsumer *clang::CreateASTPrinter(raw_ostream *Out, + StringRef FilterString) { + return new ASTPrinter(Out, /*Dump=*/ false, FilterString); } -ASTConsumer *clang::CreateASTDumper() { - return new ASTPrinter(0, true); +ASTConsumer *clang::CreateASTDumper(StringRef FilterString) { + return new ASTPrinter(0, /*Dump=*/ true, FilterString); } //===----------------------------------------------------------------------===// diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 5fa01d3719..ea735ed122 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -558,6 +558,8 @@ static void FrontendOptsToArgs(const FrontendOptions &Opts, ToArgsList &Res) { for(unsigned i = 0, e = Opts.PluginArgs.size(); i != e; ++i) Res.push_back("-plugin-arg-" + Opts.ActionName, Opts.PluginArgs[i]); } + if (!Opts.ASTDumpFilter.empty()) + Res.push_back("-ast-dump-filter", Opts.ASTDumpFilter); for (unsigned i = 0, e = Opts.Plugins.size(); i != e; ++i) Res.push_back("-load", Opts.Plugins[i]); for (unsigned i = 0, e = Opts.AddPluginActions.size(); i != e; ++i) { @@ -1542,6 +1544,7 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args, Opts.FixOnlyWarnings = Args.hasArg(OPT_fix_only_warnings); Opts.FixAndRecompile = Args.hasArg(OPT_fixit_recompile); Opts.FixToTemporaries = Args.hasArg(OPT_fixit_to_temp); + Opts.ASTDumpFilter = Args.getLastArgValue(OPT_ast_dump_filter); Opts.CodeCompleteOpts.IncludeMacros = Args.hasArg(OPT_code_completion_macros); diff --git a/lib/Frontend/FrontendActions.cpp b/lib/Frontend/FrontendActions.cpp index b699ae8279..ffd3d90729 100644 --- a/lib/Frontend/FrontendActions.cpp +++ b/lib/Frontend/FrontendActions.cpp @@ -47,13 +47,13 @@ void InitOnlyAction::ExecuteAction() { ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile)) - return CreateASTPrinter(OS); + return CreateASTPrinter(OS, CI.getFrontendOpts().ASTDumpFilter); return 0; } ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { - return CreateASTDumper(); + return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter); } ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI, |