[ASTDumper] Dump DeclContext

* Include `DeclContext` of the node where possible
* Add 'default-with-decl-contexts' dump style that dumps the dect context
  hierarchy in addition to the AST
* Support `-dump-parse` with `-dump-ast-format json`
This commit is contained in:
Rintaro Ishizaki
2025-02-11 20:38:28 -08:00
parent 04b2174c52
commit 71b24665fa
18 changed files with 340 additions and 99 deletions

View File

@@ -467,11 +467,15 @@ getPrimaryOrMainSourceFile(const CompilerInstance &Instance) {
static bool dumpAST(CompilerInstance &Instance,
ASTDumpMemberLoading memberLoading) {
const FrontendOptions &opts = Instance.getInvocation().getFrontendOptions();
auto dumpAST = [&](SourceFile *SF, raw_ostream &out) {
auto dumpAST = [&](SourceFile *SF, llvm::raw_ostream &out) {
switch (opts.DumpASTFormat) {
case FrontendOptions::ASTFormat::Default:
SF->dump(out, memberLoading);
break;
case FrontendOptions::ASTFormat::DefaultWithDeclContext:
swift::dumpDeclContextHierarchy(out, *SF);
SF->dump(out, memberLoading);
break;
case FrontendOptions::ASTFormat::JSON:
SF->dumpJSON(out, memberLoading);
break;
@@ -495,7 +499,7 @@ static bool dumpAST(CompilerInstance &Instance,
auto OutputFilename = PSPs.OutputFilename;
if (withOutputPath(Instance.getASTContext().Diags,
Instance.getOutputBackend(), OutputFilename,
[&](raw_ostream &out) -> bool {
[&](llvm::raw_ostream &out) -> bool {
dumpAST(sourceFile, out);
return false;
}))