Skip to content

Commit

Permalink
Add -cc1 flag -ast-dump-all to perform an AST dump including entities…
Browse files Browse the repository at this point in the history
… that haven't yet been deserialized.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@297412 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
zygoloid committed Mar 9, 2017
1 parent 21af181 commit 09a7b29
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 34 deletions.
5 changes: 3 additions & 2 deletions include/clang/AST/DeclBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ class LLVM_ALIGNAS(/*alignof(uint64_t)*/ 8) Decl {
void dump() const;
// Same as dump(), but forces color printing.
void dumpColor() const;
void dump(raw_ostream &Out) const;
void dump(raw_ostream &Out, bool Deserialize = false) const;

/// \brief Looks through the Decl's underlying type to extract a FunctionType
/// when possible. Will return null if the type underlying the Decl does not
Expand Down Expand Up @@ -1810,7 +1810,8 @@ class DeclContext {

void dumpDeclContext() const;
void dumpLookups() const;
void dumpLookups(llvm::raw_ostream &OS, bool DumpDecls = false) const;
void dumpLookups(llvm::raw_ostream &OS, bool DumpDecls = false,
bool Deserialize = false) const;

private:
void reconcileExternalVisibleStorage() const;
Expand Down
2 changes: 2 additions & 0 deletions include/clang/Driver/CC1Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ def ast_list : Flag<["-"], "ast-list">,
HelpText<"Build ASTs and print the list of declaration node qualified names">;
def ast_dump : Flag<["-"], "ast-dump">,
HelpText<"Build ASTs and then debug dump them">;
def ast_dump_all : Flag<["-"], "ast-dump-all">,
HelpText<"Build ASTs and then debug dump them, forcing deserialization">;
def ast_dump_lookups : Flag<["-"], "ast-dump-lookups">,
HelpText<"Build ASTs and then debug dump their name lookup tables">;
def ast_view : Flag<["-"], "ast-view">,
Expand Down
2 changes: 1 addition & 1 deletion include/clang/Frontend/ASTConsumers.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ std::unique_ptr<ASTConsumer> CreateASTPrinter(std::unique_ptr<raw_ostream> OS,
// AST dumper: dumps the raw AST in human-readable form to stderr; this is
// intended for debugging.
std::unique_ptr<ASTConsumer> CreateASTDumper(StringRef FilterString,
bool DumpDecls,
bool DumpDecls, bool Deserialize,
bool DumpLookups);

// AST Decl node lister: prints qualified names of all filterable AST Decl
Expand Down
2 changes: 2 additions & 0 deletions include/clang/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ class FrontendOptions {
///< global module index if needed.
unsigned ASTDumpDecls : 1; ///< Whether we include declaration
///< dumps in AST dumps.
unsigned ASTDumpAll : 1; ///< Whether we deserialize all decls
///< when forming AST dumps.
unsigned ASTDumpLookups : 1; ///< Whether we include lookup table
///< dumps in AST dumps.
unsigned BuildingImplicitModule : 1; ///< Whether we are performing an
Expand Down
45 changes: 27 additions & 18 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,26 @@ namespace {
/// Pending[i] is an action to dump an entity at level i.
llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending;

/// Indicates whether we should trigger deserialization of nodes that had
/// not already been loaded.
bool Deserialize = false;

/// Indicates whether we're at the top level.
bool TopLevel;
bool TopLevel = true;

/// Indicates if we're handling the first child after entering a new depth.
bool FirstChild;
bool FirstChild = true;

/// Prefix for currently-being-dumped entity.
std::string Prefix;

/// Keep track of the last location we print out so that we can
/// print out deltas from then on out.
const char *LastLocFilename;
unsigned LastLocLine;
const char *LastLocFilename = "";
unsigned LastLocLine = ~0U;

/// The \c FullComment parent of the comment being dumped.
const FullComment *FC;
const FullComment *FC = nullptr;

bool ShowColors;

Expand Down Expand Up @@ -203,15 +207,14 @@ namespace {
public:
ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
const SourceManager *SM)
: OS(OS), Traits(Traits), SM(SM), TopLevel(true), FirstChild(true),
LastLocFilename(""), LastLocLine(~0U), FC(nullptr),
: OS(OS), Traits(Traits), SM(SM),
ShowColors(SM && SM->getDiagnostics().getShowColors()) { }

ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
const SourceManager *SM, bool ShowColors)
: OS(OS), Traits(Traits), SM(SM), TopLevel(true), FirstChild(true),
LastLocFilename(""), LastLocLine(~0U),
ShowColors(ShowColors) { }
: OS(OS), Traits(Traits), SM(SM), ShowColors(ShowColors) {}

void setDeserialize(bool D) { Deserialize = D; }

void dumpDecl(const Decl *D);
void dumpStmt(const Stmt *S);
Expand Down Expand Up @@ -764,14 +767,15 @@ bool ASTDumper::hasNodes(const DeclContext *DC) {
return false;

return DC->hasExternalLexicalStorage() ||
DC->noload_decls_begin() != DC->noload_decls_end();
(Deserialize ? DC->decls_begin() != DC->decls_end()
: DC->noload_decls_begin() != DC->noload_decls_end());
}

void ASTDumper::dumpDeclContext(const DeclContext *DC) {
if (!DC)
return;

for (auto *D : DC->noload_decls())
for (auto *D : (Deserialize ? DC->decls() : DC->noload_decls()))
dumpDecl(D);

if (DC->hasExternalLexicalStorage()) {
Expand All @@ -795,11 +799,13 @@ void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {

bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();

DeclContext::all_lookups_iterator I = Primary->noload_lookups_begin(),
E = Primary->noload_lookups_end();
while (I != E) {
for (auto I = Deserialize ? Primary->lookups_begin()
: Primary->noload_lookups_begin(),
E = Deserialize ? Primary->lookups_end()
: Primary->noload_lookups_end();
I != E; ++I) {
DeclarationName Name = I.getLookupName();
DeclContextLookupResult R = *I++;
DeclContextLookupResult R = *I;

dumpChild([=] {
OS << "DeclarationName ";
Expand Down Expand Up @@ -2507,9 +2513,10 @@ LLVM_DUMP_METHOD void Type::dump(llvm::raw_ostream &OS) const {

LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }

LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS) const {
LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS, bool Deserialize) const {
ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
&getASTContext().getSourceManager());
P.setDeserialize(Deserialize);
P.dumpDecl(this);
}

Expand All @@ -2524,12 +2531,14 @@ LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
}

LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
bool DumpDecls) const {
bool DumpDecls,
bool Deserialize) const {
const DeclContext *DC = this;
while (!DC->isTranslationUnit())
DC = DC->getParent();
ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
P.setDeserialize(Deserialize);
P.dumpLookups(this, DumpDecls);
}

Expand Down
39 changes: 26 additions & 13 deletions lib/Frontend/ASTConsumers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ namespace {
typedef RecursiveASTVisitor<ASTPrinter> base;

public:
ASTPrinter(std::unique_ptr<raw_ostream> Out = nullptr, bool Dump = false,
StringRef FilterString = "", bool DumpLookups = false)
: Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)), Dump(Dump),
FilterString(FilterString), DumpLookups(DumpLookups) {}
enum Kind { DumpFull, Dump, Print, None };
ASTPrinter(std::unique_ptr<raw_ostream> Out, Kind K, StringRef FilterString,
bool DumpLookups = false)
: Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)),
OutputKind(K), FilterString(FilterString), DumpLookups(DumpLookups) {}

void HandleTranslationUnit(ASTContext &Context) override {
TranslationUnitDecl *D = Context.getTranslationUnitDecl();
Expand All @@ -55,7 +56,7 @@ namespace {
bool ShowColors = Out.has_colors();
if (ShowColors)
Out.changeColor(raw_ostream::BLUE);
Out << ((Dump || DumpLookups) ? "Dumping " : "Printing ") << getName(D)
Out << (OutputKind != Print ? "Dumping " : "Printing ") << getName(D)
<< ":\n";
if (ShowColors)
Out.resetColor();
Expand All @@ -80,22 +81,30 @@ namespace {
if (DumpLookups) {
if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
if (DC == DC->getPrimaryContext())
DC->dumpLookups(Out, Dump);
DC->dumpLookups(Out, OutputKind != None, OutputKind == DumpFull);
else
Out << "Lookup map is in primary DeclContext "
<< DC->getPrimaryContext() << "\n";
} else
Out << "Not a DeclContext\n";
} else if (Dump)
D->dump(Out);
else
} else if (OutputKind == Print)
D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
else if (OutputKind != None)
D->dump(Out, OutputKind == DumpFull);
}

raw_ostream &Out;
std::unique_ptr<raw_ostream> OwnedOut;
bool Dump;

/// How to output individual declarations.
Kind OutputKind;

/// Which declarations or DeclContexts to display.
std::string FilterString;

/// Whether the primary output is lookup results or declarations. Individual
/// results will be output with a format determined by OutputKind. This is
/// incompatible with OutputKind == Print.
bool DumpLookups;
};

Expand Down Expand Up @@ -125,16 +134,20 @@ namespace {
std::unique_ptr<ASTConsumer>
clang::CreateASTPrinter(std::unique_ptr<raw_ostream> Out,
StringRef FilterString) {
return llvm::make_unique<ASTPrinter>(std::move(Out), /*Dump=*/false,
return llvm::make_unique<ASTPrinter>(std::move(Out), ASTPrinter::Print,
FilterString);
}

std::unique_ptr<ASTConsumer> clang::CreateASTDumper(StringRef FilterString,
bool DumpDecls,
bool Deserialize,
bool DumpLookups) {
assert((DumpDecls || DumpLookups) && "nothing to dump");
return llvm::make_unique<ASTPrinter>(nullptr, DumpDecls, FilterString,
DumpLookups);
return llvm::make_unique<ASTPrinter>(nullptr,
Deserialize ? ASTPrinter::DumpFull :
DumpDecls ? ASTPrinter::Dump :
ASTPrinter::None,
FilterString, DumpLookups);
}

std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
Expand Down
2 changes: 2 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,7 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
case OPT_ast_list:
Opts.ProgramAction = frontend::ASTDeclList; break;
case OPT_ast_dump:
case OPT_ast_dump_all:
case OPT_ast_dump_lookups:
Opts.ProgramAction = frontend::ASTDump; break;
case OPT_ast_print:
Expand Down Expand Up @@ -1251,6 +1252,7 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
Opts.FixAndRecompile = Args.hasArg(OPT_fixit_recompile);
Opts.FixToTemporaries = Args.hasArg(OPT_fixit_to_temp);
Opts.ASTDumpDecls = Args.hasArg(OPT_ast_dump);
Opts.ASTDumpAll = Args.hasArg(OPT_ast_dump_all);
Opts.ASTDumpFilter = Args.getLastArgValue(OPT_ast_dump_filter);
Opts.ASTDumpLookups = Args.hasArg(OPT_ast_dump_lookups);
Opts.UseGlobalModuleIndex = !Args.hasArg(OPT_fno_modules_global_index);
Expand Down
1 change: 1 addition & 0 deletions lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ std::unique_ptr<ASTConsumer>
ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter,
CI.getFrontendOpts().ASTDumpDecls,
CI.getFrontendOpts().ASTDumpAll,
CI.getFrontendOpts().ASTDumpLookups);
}

Expand Down
1 change: 1 addition & 0 deletions tools/clang-check/ClangCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class ClangCheckActionFactory {
return clang::CreateASTDeclNodeLister();
if (ASTDump)
return clang::CreateASTDumper(ASTDumpFilter, /*DumpDecls=*/true,
/*Deserialize=*/false,
/*DumpLookups=*/false);
if (ASTPrint)
return clang::CreateASTPrinter(nullptr, ASTDumpFilter);
Expand Down

0 comments on commit 09a7b29

Please sign in to comment.