Skip to content

Commit f4bac45

Browse files
authored
Merge pull request #64829 from artemcm/DisableASTGenParseInDepScan
[Dependency Scanning] Disable Swift Parser ASTGen during dependency scan
2 parents c1b08e5 + cc34ba0 commit f4bac45

File tree

4 files changed

+53
-32
lines changed

4 files changed

+53
-32
lines changed

include/swift/AST/SourceFile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ class SourceFile final : public FileUnit {
9090
/// Whether to suppress warnings when parsing. This is set for secondary
9191
/// files, as they get parsed multiple times.
9292
SuppressWarnings = 1 << 4,
93+
94+
/// Whether to disable the Swift Parser ASTGen
95+
/// e.g. in dependency scanning, where an AST is not needed.
96+
DisableSwiftParserASTGen = 1 << 5,
9397
};
9498
using ParsingOptions = OptionSet<ParsingFlags>;
9599

lib/Frontend/Frontend.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,12 @@ CompilerInstance::getSourceFileParsingOptions(bool forPrimary) const {
13691369
opts |= SourceFile::ParsingFlags::SuppressWarnings;
13701370
}
13711371

1372+
// Dependency scanning does not require an AST, so disable Swift Parser
1373+
// ASTGen parsing completely.
1374+
if (frontendOpts.RequestedAction ==
1375+
FrontendOptions::ActionType::ScanDependencies)
1376+
opts |= SourceFile::ParsingFlags::DisableSwiftParserASTGen;
1377+
13721378
// Enable interface hash computation for primaries or emit-module-separately,
13731379
// but not in WMO, as it's only currently needed for incremental mode.
13741380
if (forPrimary ||

lib/Parse/ParseDecl.cpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ extern "C" void swift_ASTGen_buildTopLevelASTNodes(void *sourceFile,
209209
void Parser::parseTopLevelItems(SmallVectorImpl<ASTNode> &items) {
210210
#if SWIFT_SWIFT_PARSER
211211
Optional<DiagnosticTransaction> existingParsingTransaction;
212-
parseSourceFileViaASTGen(items, existingParsingTransaction);
212+
if (!SF.getParsingOptions()
213+
.contains(SourceFile::ParsingFlags::DisableSwiftParserASTGen))
214+
parseSourceFileViaASTGen(items, existingParsingTransaction);
213215
#endif
214216

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

261263
#if SWIFT_SWIFT_PARSER
262-
if (existingParsingTransaction)
263-
existingParsingTransaction->abort();
264-
265-
// Perform round-trip and/or validation checking.
266-
if ((Context.LangOpts.hasFeature(Feature::ParserRoundTrip) ||
267-
Context.LangOpts.hasFeature(Feature::ParserValidation)) &&
268-
SF.exportedSourceFile) {
269-
if (Context.LangOpts.hasFeature(Feature::ParserRoundTrip) &&
270-
swift_ASTGen_roundTripCheck(SF.exportedSourceFile)) {
271-
SourceLoc loc;
272-
if (auto bufferID = SF.getBufferID()) {
273-
loc = Context.SourceMgr.getLocForBufferStart(*bufferID);
274-
}
275-
diagnose(loc, diag::parser_round_trip_error);
276-
} else if (Context.LangOpts.hasFeature(Feature::ParserValidation) &&
277-
!Context.Diags.hadAnyError() &&
278-
swift_ASTGen_emitParserDiagnostics(
279-
&Context.Diags, SF.exportedSourceFile,
280-
/*emitOnlyErrors=*/true,
281-
/*downgradePlaceholderErrorsToWarnings=*/
282-
Context.LangOpts.Playground ||
283-
Context.LangOpts.WarnOnEditorPlaceholder)) {
284-
// We might have emitted warnings in the C++ parser but no errors, in
285-
// which case we still have `hadAnyError() == false`. To avoid emitting
286-
// the same warnings from SwiftParser, only emit errors from SwiftParser
287-
SourceLoc loc;
288-
if (auto bufferID = SF.getBufferID()) {
289-
loc = Context.SourceMgr.getLocForBufferStart(*bufferID);
290-
}
291-
diagnose(loc, diag::parser_new_parser_errors);
264+
if (!SF.getParsingOptions().contains(
265+
SourceFile::ParsingFlags::DisableSwiftParserASTGen)) {
266+
if (existingParsingTransaction)
267+
existingParsingTransaction->abort();
268+
269+
// Perform round-trip and/or validation checking.
270+
if ((Context.LangOpts.hasFeature(Feature::ParserRoundTrip) ||
271+
Context.LangOpts.hasFeature(Feature::ParserValidation)) &&
272+
SF.exportedSourceFile) {
273+
if (Context.LangOpts.hasFeature(Feature::ParserRoundTrip) &&
274+
swift_ASTGen_roundTripCheck(SF.exportedSourceFile)) {
275+
SourceLoc loc;
276+
if (auto bufferID = SF.getBufferID()) {
277+
loc = Context.SourceMgr.getLocForBufferStart(*bufferID);
278+
}
279+
diagnose(loc, diag::parser_round_trip_error);
280+
} else if (Context.LangOpts.hasFeature(Feature::ParserValidation) &&
281+
!Context.Diags.hadAnyError() &&
282+
swift_ASTGen_emitParserDiagnostics(
283+
&Context.Diags, SF.exportedSourceFile,
284+
/*emitOnlyErrors=*/true,
285+
/*downgradePlaceholderErrorsToWarnings=*/
286+
Context.LangOpts.Playground ||
287+
Context.LangOpts.WarnOnEditorPlaceholder)) {
288+
// We might have emitted warnings in the C++ parser but no errors, in
289+
// which case we still have `hadAnyError() == false`. To avoid emitting
290+
// the same warnings from SwiftParser, only emit errors from SwiftParser
291+
SourceLoc loc;
292+
if (auto bufferID = SF.getBufferID()) {
293+
loc = Context.SourceMgr.getLocForBufferStart(*bufferID);
294+
}
295+
diagnose(loc, diag::parser_new_parser_errors);
296+
}
292297
}
293298
}
294299
#endif

lib/Serialization/ModuleDependencyScanner.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/ModuleDependencies.h"
1717
#include "swift/AST/SourceFile.h"
1818
#include "swift/AST/TypeCheckRequests.h"
19+
#include "swift/AST/SourceFile.h"
1920
#include "swift/Basic/FileTypes.h"
2021
#include "swift/Frontend/ModuleInterfaceLoader.h"
2122
#include "swift/Serialization/SerializedModuleLoader.h"
@@ -157,8 +158,13 @@ ErrorOr<ModuleDependencyInfo> ModuleDependencyScanner::scanInterfaceFile(
157158
// Create a source file.
158159
unsigned bufferID = Ctx.SourceMgr.addNewSourceBuffer(std::move(interfaceBuf.get()));
159160
auto moduleDecl = ModuleDecl::create(realModuleName, Ctx);
161+
162+
// Dependency scanning does not require an AST, so disable Swift Parser
163+
// ASTGen parsing completely.
164+
SourceFile::ParsingOptions parsingOpts;
165+
parsingOpts |= SourceFile::ParsingFlags::DisableSwiftParserASTGen;
160166
auto sourceFile = new (Ctx) SourceFile(
161-
*moduleDecl, SourceFileKind::Interface, bufferID);
167+
*moduleDecl, SourceFileKind::Interface, bufferID, parsingOpts);
162168
moduleDecl->addAuxiliaryFile(*sourceFile);
163169

164170
// Walk the source file to find the import declarations.

0 commit comments

Comments
 (0)