forked from OSchip/llvm-project
Added -ast-dump-filter option to clang -cc1.
llvm-svn: 160784
This commit is contained in:
parent
3c80d5aa15
commit
3db68ee109
|
@ -857,9 +857,8 @@ public:
|
||||||
static void printGroup(Decl** Begin, unsigned NumDecls,
|
static void printGroup(Decl** Begin, unsigned NumDecls,
|
||||||
raw_ostream &Out, const PrintingPolicy &Policy,
|
raw_ostream &Out, const PrintingPolicy &Policy,
|
||||||
unsigned Indentation = 0);
|
unsigned Indentation = 0);
|
||||||
LLVM_ATTRIBUTE_USED void dump() const;
|
LLVM_ATTRIBUTE_USED void dump(raw_ostream &Out = llvm::errs()) const;
|
||||||
LLVM_ATTRIBUTE_USED void dumpXML() const;
|
LLVM_ATTRIBUTE_USED void dumpXML(raw_ostream &OS = llvm::errs()) const;
|
||||||
void dumpXML(raw_ostream &OS) const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx);
|
void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx);
|
||||||
|
|
|
@ -286,6 +286,10 @@ def resource_dir : Separate<"-resource-dir">,
|
||||||
HelpText<"The directory which holds the compiler resource files">;
|
HelpText<"The directory which holds the compiler resource files">;
|
||||||
def version : Flag<"-version">,
|
def version : Flag<"-version">,
|
||||||
HelpText<"Print the compiler version">;
|
HelpText<"Print the compiler version">;
|
||||||
|
def ast_dump_filter : Separate<"-ast-dump-filter">,
|
||||||
|
MetaVarName<"<dump_filter>">,
|
||||||
|
HelpText<"Use with -ast-dump or -ast-print to dump/print only AST declaration"
|
||||||
|
" nodes having a certain substring in a qualified name.">;
|
||||||
|
|
||||||
let Group = Action_Group in {
|
let Group = Action_Group in {
|
||||||
|
|
||||||
|
|
|
@ -33,11 +33,11 @@ class TargetOptions;
|
||||||
// original C code. The output is intended to be in a format such that
|
// original C code. The output is intended to be in a format such that
|
||||||
// clang could re-parse the output back into the same AST, but the
|
// clang could re-parse the output back into the same AST, but the
|
||||||
// implementation is still incomplete.
|
// implementation is still incomplete.
|
||||||
ASTConsumer *CreateASTPrinter(raw_ostream *OS);
|
ASTConsumer *CreateASTPrinter(raw_ostream *OS, StringRef FilterString);
|
||||||
|
|
||||||
// AST dumper: dumps the raw AST in human-readable form to stderr; this is
|
// AST dumper: dumps the raw AST in human-readable form to stderr; this is
|
||||||
// intended for debugging.
|
// intended for debugging.
|
||||||
ASTConsumer *CreateASTDumper();
|
ASTConsumer *CreateASTDumper(StringRef FilterString);
|
||||||
|
|
||||||
// AST XML-dumper: dumps out the AST to stderr in a very detailed XML
|
// AST XML-dumper: dumps out the AST to stderr in a very detailed XML
|
||||||
// format; this is intended for particularly intense debugging.
|
// format; this is intended for particularly intense debugging.
|
||||||
|
|
|
@ -141,6 +141,9 @@ public:
|
||||||
/// If given, the new suffix for fix-it rewritten files.
|
/// If given, the new suffix for fix-it rewritten files.
|
||||||
std::string FixItSuffix;
|
std::string FixItSuffix;
|
||||||
|
|
||||||
|
/// If given, filter dumped AST Decl nodes by this substring.
|
||||||
|
std::string ASTDumpFilter;
|
||||||
|
|
||||||
/// If given, enable code completion at the provided location.
|
/// If given, enable code completion at the provided location.
|
||||||
ParsedSourceLocation CodeCompletionAt;
|
ParsedSourceLocation CodeCompletionAt;
|
||||||
|
|
||||||
|
|
|
@ -173,8 +173,10 @@ void DeclContext::dumpDeclContext() const {
|
||||||
Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
|
Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Decl::dump() const {
|
void Decl::dump(raw_ostream &Out) const {
|
||||||
print(llvm::errs());
|
PrintingPolicy Policy = getASTContext().getPrintingPolicy();
|
||||||
|
Policy.Dump = true;
|
||||||
|
print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ true);
|
||||||
}
|
}
|
||||||
|
|
||||||
raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
|
raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
|
||||||
|
|
|
@ -1022,17 +1022,12 @@ struct XMLDumper : public XMLDeclVisitor<XMLDumper>,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void Decl::dumpXML() const {
|
|
||||||
dumpXML(llvm::errs());
|
|
||||||
}
|
|
||||||
|
|
||||||
void Decl::dumpXML(raw_ostream &out) const {
|
void Decl::dumpXML(raw_ostream &out) const {
|
||||||
XMLDumper(out, getASTContext()).dispatch(const_cast<Decl*>(this));
|
XMLDumper(out, getASTContext()).dispatch(const_cast<Decl*>(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
#else /* ifndef NDEBUG */
|
#else /* ifndef NDEBUG */
|
||||||
|
|
||||||
void Decl::dumpXML() const {}
|
|
||||||
void Decl::dumpXML(raw_ostream &out) const {}
|
void Decl::dumpXML(raw_ostream &out) const {}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -12,47 +12,89 @@
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
#include "clang/Frontend/ASTConsumers.h"
|
#include "clang/Frontend/ASTConsumers.h"
|
||||||
|
#include "clang/Basic/FileManager.h"
|
||||||
#include "clang/Basic/Diagnostic.h"
|
#include "clang/Basic/Diagnostic.h"
|
||||||
#include "clang/Basic/SourceManager.h"
|
#include "clang/Basic/SourceManager.h"
|
||||||
#include "clang/Basic/FileManager.h"
|
|
||||||
#include "clang/AST/AST.h"
|
#include "clang/AST/AST.h"
|
||||||
#include "clang/AST/ASTConsumer.h"
|
#include "clang/AST/ASTConsumer.h"
|
||||||
#include "clang/AST/ASTContext.h"
|
#include "clang/AST/ASTContext.h"
|
||||||
#include "clang/AST/RecordLayout.h"
|
|
||||||
#include "clang/AST/PrettyPrinter.h"
|
#include "clang/AST/PrettyPrinter.h"
|
||||||
|
#include "clang/AST/RecordLayout.h"
|
||||||
|
#include "clang/AST/RecursiveASTVisitor.h"
|
||||||
#include "llvm/Module.h"
|
#include "llvm/Module.h"
|
||||||
#include "llvm/Support/Timer.h"
|
|
||||||
#include "llvm/Support/raw_ostream.h"
|
|
||||||
#include "llvm/Support/Path.h"
|
#include "llvm/Support/Path.h"
|
||||||
|
#include "llvm/Support/raw_ostream.h"
|
||||||
|
#include "llvm/Support/Timer.h"
|
||||||
using namespace clang;
|
using namespace clang;
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
/// ASTPrinter - Pretty-printer and dumper of ASTs
|
/// ASTPrinter - Pretty-printer and dumper of ASTs
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
class ASTPrinter : public ASTConsumer {
|
class ASTPrinter : public ASTConsumer,
|
||||||
raw_ostream &Out;
|
public RecursiveASTVisitor<ASTPrinter> {
|
||||||
bool Dump;
|
typedef RecursiveASTVisitor<ASTPrinter> base;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ASTPrinter(raw_ostream* o = NULL, bool Dump = false)
|
ASTPrinter(raw_ostream *Out = NULL, bool Dump = false,
|
||||||
: Out(o? *o : llvm::outs()), Dump(Dump) { }
|
StringRef FilterString = "")
|
||||||
|
: Out(Out ? *Out : llvm::outs()), Dump(Dump),
|
||||||
|
FilterString(FilterString) {}
|
||||||
|
|
||||||
virtual void HandleTranslationUnit(ASTContext &Context) {
|
virtual void HandleTranslationUnit(ASTContext &Context) {
|
||||||
PrintingPolicy Policy = Context.getPrintingPolicy();
|
TranslationUnitDecl *D = Context.getTranslationUnitDecl();
|
||||||
Policy.Dump = Dump;
|
|
||||||
Context.getTranslationUnitDecl()->print(Out, Policy, /*Indentation=*/0,
|
if (FilterString.empty()) {
|
||||||
/*PrintInstantiation=*/true);
|
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
|
} // end anonymous namespace
|
||||||
|
|
||||||
ASTConsumer *clang::CreateASTPrinter(raw_ostream* out) {
|
ASTConsumer *clang::CreateASTPrinter(raw_ostream *Out,
|
||||||
return new ASTPrinter(out);
|
StringRef FilterString) {
|
||||||
|
return new ASTPrinter(Out, /*Dump=*/ false, FilterString);
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTConsumer *clang::CreateASTDumper() {
|
ASTConsumer *clang::CreateASTDumper(StringRef FilterString) {
|
||||||
return new ASTPrinter(0, true);
|
return new ASTPrinter(0, /*Dump=*/ true, FilterString);
|
||||||
}
|
}
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
|
@ -558,6 +558,8 @@ static void FrontendOptsToArgs(const FrontendOptions &Opts, ToArgsList &Res) {
|
||||||
for(unsigned i = 0, e = Opts.PluginArgs.size(); i != e; ++i)
|
for(unsigned i = 0, e = Opts.PluginArgs.size(); i != e; ++i)
|
||||||
Res.push_back("-plugin-arg-" + Opts.ActionName, Opts.PluginArgs[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)
|
for (unsigned i = 0, e = Opts.Plugins.size(); i != e; ++i)
|
||||||
Res.push_back("-load", Opts.Plugins[i]);
|
Res.push_back("-load", Opts.Plugins[i]);
|
||||||
for (unsigned i = 0, e = Opts.AddPluginActions.size(); i != e; ++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.FixOnlyWarnings = Args.hasArg(OPT_fix_only_warnings);
|
||||||
Opts.FixAndRecompile = Args.hasArg(OPT_fixit_recompile);
|
Opts.FixAndRecompile = Args.hasArg(OPT_fixit_recompile);
|
||||||
Opts.FixToTemporaries = Args.hasArg(OPT_fixit_to_temp);
|
Opts.FixToTemporaries = Args.hasArg(OPT_fixit_to_temp);
|
||||||
|
Opts.ASTDumpFilter = Args.getLastArgValue(OPT_ast_dump_filter);
|
||||||
|
|
||||||
Opts.CodeCompleteOpts.IncludeMacros
|
Opts.CodeCompleteOpts.IncludeMacros
|
||||||
= Args.hasArg(OPT_code_completion_macros);
|
= Args.hasArg(OPT_code_completion_macros);
|
||||||
|
|
|
@ -47,13 +47,13 @@ void InitOnlyAction::ExecuteAction() {
|
||||||
ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
|
ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
|
||||||
StringRef InFile) {
|
StringRef InFile) {
|
||||||
if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
|
if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
|
||||||
return CreateASTPrinter(OS);
|
return CreateASTPrinter(OS, CI.getFrontendOpts().ASTDumpFilter);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
|
ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
|
||||||
StringRef InFile) {
|
StringRef InFile) {
|
||||||
return CreateASTDumper();
|
return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter);
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI,
|
ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI,
|
||||||
|
|
Loading…
Reference in New Issue