Skip to content

[5.9][Dependency Scanning] Disable Swift Parser ASTGen during dependency scan #64830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/AST/SourceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ class SourceFile final : public FileUnit {
/// Whether to suppress warnings when parsing. This is set for secondary
/// files, as they get parsed multiple times.
SuppressWarnings = 1 << 4,

/// Whether to disable the Swift Parser ASTGen
/// e.g. in dependency scanning, where an AST is not needed.
DisableSwiftParserASTGen = 1 << 5,
};
using ParsingOptions = OptionSet<ParsingFlags>;

Expand Down
6 changes: 6 additions & 0 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,12 @@ CompilerInstance::getSourceFileParsingOptions(bool forPrimary) const {
opts |= SourceFile::ParsingFlags::SuppressWarnings;
}

// Dependency scanning does not require an AST, so disable Swift Parser
// ASTGen parsing completely.
if (frontendOpts.RequestedAction ==
FrontendOptions::ActionType::ScanDependencies)
opts |= SourceFile::ParsingFlags::DisableSwiftParserASTGen;

// Enable interface hash computation for primaries or emit-module-separately,
// but not in WMO, as it's only currently needed for incremental mode.
if (forPrimary ||
Expand Down
67 changes: 36 additions & 31 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ extern "C" void swift_ASTGen_buildTopLevelASTNodes(void *sourceFile,
void Parser::parseTopLevelItems(SmallVectorImpl<ASTNode> &items) {
#if SWIFT_SWIFT_PARSER
Optional<DiagnosticTransaction> existingParsingTransaction;
parseSourceFileViaASTGen(items, existingParsingTransaction);
if (!SF.getParsingOptions()
.contains(SourceFile::ParsingFlags::DisableSwiftParserASTGen))
parseSourceFileViaASTGen(items, existingParsingTransaction);
#endif

// Prime the lexer.
Expand Down Expand Up @@ -259,36 +261,39 @@ void Parser::parseTopLevelItems(SmallVectorImpl<ASTNode> &items) {
}

#if SWIFT_SWIFT_PARSER
if (existingParsingTransaction)
existingParsingTransaction->abort();

// Perform round-trip and/or validation checking.
if ((Context.LangOpts.hasFeature(Feature::ParserRoundTrip) ||
Context.LangOpts.hasFeature(Feature::ParserValidation)) &&
SF.exportedSourceFile) {
if (Context.LangOpts.hasFeature(Feature::ParserRoundTrip) &&
swift_ASTGen_roundTripCheck(SF.exportedSourceFile)) {
SourceLoc loc;
if (auto bufferID = SF.getBufferID()) {
loc = Context.SourceMgr.getLocForBufferStart(*bufferID);
}
diagnose(loc, diag::parser_round_trip_error);
} else if (Context.LangOpts.hasFeature(Feature::ParserValidation) &&
!Context.Diags.hadAnyError() &&
swift_ASTGen_emitParserDiagnostics(
&Context.Diags, SF.exportedSourceFile,
/*emitOnlyErrors=*/true,
/*downgradePlaceholderErrorsToWarnings=*/
Context.LangOpts.Playground ||
Context.LangOpts.WarnOnEditorPlaceholder)) {
// We might have emitted warnings in the C++ parser but no errors, in
// which case we still have `hadAnyError() == false`. To avoid emitting
// the same warnings from SwiftParser, only emit errors from SwiftParser
SourceLoc loc;
if (auto bufferID = SF.getBufferID()) {
loc = Context.SourceMgr.getLocForBufferStart(*bufferID);
}
diagnose(loc, diag::parser_new_parser_errors);
if (!SF.getParsingOptions().contains(
SourceFile::ParsingFlags::DisableSwiftParserASTGen)) {
if (existingParsingTransaction)
existingParsingTransaction->abort();

// Perform round-trip and/or validation checking.
if ((Context.LangOpts.hasFeature(Feature::ParserRoundTrip) ||
Context.LangOpts.hasFeature(Feature::ParserValidation)) &&
SF.exportedSourceFile) {
if (Context.LangOpts.hasFeature(Feature::ParserRoundTrip) &&
swift_ASTGen_roundTripCheck(SF.exportedSourceFile)) {
SourceLoc loc;
if (auto bufferID = SF.getBufferID()) {
loc = Context.SourceMgr.getLocForBufferStart(*bufferID);
}
diagnose(loc, diag::parser_round_trip_error);
} else if (Context.LangOpts.hasFeature(Feature::ParserValidation) &&
!Context.Diags.hadAnyError() &&
swift_ASTGen_emitParserDiagnostics(
&Context.Diags, SF.exportedSourceFile,
/*emitOnlyErrors=*/true,
/*downgradePlaceholderErrorsToWarnings=*/
Context.LangOpts.Playground ||
Context.LangOpts.WarnOnEditorPlaceholder)) {
// We might have emitted warnings in the C++ parser but no errors, in
// which case we still have `hadAnyError() == false`. To avoid emitting
// the same warnings from SwiftParser, only emit errors from SwiftParser
SourceLoc loc;
if (auto bufferID = SF.getBufferID()) {
loc = Context.SourceMgr.getLocForBufferStart(*bufferID);
}
diagnose(loc, diag::parser_new_parser_errors);
}
}
}
#endif
Expand Down
8 changes: 7 additions & 1 deletion lib/Serialization/ModuleDependencyScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "swift/AST/ModuleDependencies.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/SourceFile.h"
#include "swift/Basic/FileTypes.h"
#include "swift/Frontend/ModuleInterfaceLoader.h"
#include "swift/Serialization/SerializedModuleLoader.h"
Expand Down Expand Up @@ -157,8 +158,13 @@ ErrorOr<ModuleDependencyInfo> ModuleDependencyScanner::scanInterfaceFile(
// Create a source file.
unsigned bufferID = Ctx.SourceMgr.addNewSourceBuffer(std::move(interfaceBuf.get()));
auto moduleDecl = ModuleDecl::create(realModuleName, Ctx);

// Dependency scanning does not require an AST, so disable Swift Parser
// ASTGen parsing completely.
SourceFile::ParsingOptions parsingOpts;
parsingOpts |= SourceFile::ParsingFlags::DisableSwiftParserASTGen;
auto sourceFile = new (Ctx) SourceFile(
*moduleDecl, SourceFileKind::Interface, bufferID);
*moduleDecl, SourceFileKind::Interface, bufferID, parsingOpts);
moduleDecl->addAuxiliaryFile(*sourceFile);

// Walk the source file to find the import declarations.
Expand Down