Add option to dump AST to files

This adds two things:
- Calling `swiftc -dump-ast foo.swift [...] -o foo.ast` will dump the AST to the file `foo.ast`, instead of dumping it to `stderr` as usual.
- Calling `swiftc -dump-ast -output-file-map=outputFileMap.json *.swift [...]`, given an `outputFileMap.json` file that contains entries in the form `"ast-dump": "foo.ast"`, will dump the ASTs of the input files to their respective output files in the file map.

This should serve as a valid workaround to a bug mentioned in [the forums](https://forums.swift.org/t/error-when-dumping-the-ast-for-hundreds-of-files/17578) where the AST dump functionality crashes when called with too many input files. A few implementation details were also discussed in the same forum post.

As an aside, this also fixes a comment in `include/swift/Basic/PrimarySpecificPaths.h` that was incorrect.
This commit is contained in:
Vinicius Vendramini
2018-11-07 12:07:11 -02:00
parent 9d10068042
commit e123750c12
6 changed files with 30 additions and 4 deletions

View File

@@ -685,6 +685,19 @@ static SourceFile *getPrimaryOrMainSourceFile(CompilerInvocation &Invocation,
return SF;
}
static void dumpAST(CompilerInvocation &Invocation, CompilerInstance &Instance) {
SourceFile *sourceFile = getPrimaryOrMainSourceFile(Invocation, Instance);
const StringRef OutputFilename =
Instance.getPrimarySpecificPathsForSourceFile(*sourceFile).OutputFilename;
// PSP.OutputFilename is "-" whenever the output should be written to stdout.
if (OutputFilename == "-") {
sourceFile->dump();
} else {
auto OS = getFileOutputStream(OutputFilename, Instance.getMainModule()->getASTContext());
sourceFile->dump(*OS);
}
}
/// We may have been told to dump the AST (either after parsing or
/// type-checking, which is already differentiated in
/// CompilerInstance::performSema()), so dump or print the main source file and
@@ -728,7 +741,7 @@ static Optional<bool> dumpASTIfNeeded(CompilerInvocation &Invocation,
case FrontendOptions::ActionType::DumpParse:
case FrontendOptions::ActionType::DumpAST:
getPrimaryOrMainSourceFile(Invocation, Instance)->dump();
dumpAST(Invocation, Instance);
break;
case FrontendOptions::ActionType::EmitImportedModules: