Skip to content

Commit cf3e40f

Browse files
authored
Merge pull request swiftlang#21750 from nkcsgexi/parser-delayed-control
[Parser] Expose a flag to allow users explicitly disable delayed parsing. NFC
2 parents 3eb24d4 + 6057a60 commit cf3e40f

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

include/swift/Parse/Parser.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ class Parser {
206206
/// Always empty if !SF.shouldBuildSyntaxTree().
207207
syntax::Trivia TrailingTrivia;
208208

209+
/// Whether we should disable delayed parsing.
210+
bool DisableDelayedParsing;
211+
209212
/// The receiver to collect all consumed tokens.
210213
ConsumeTokenReceiver *TokReceiver;
211214

@@ -352,14 +355,17 @@ class Parser {
352355
Parser(unsigned BufferID, SourceFile &SF, DiagnosticEngine* LexerDiags,
353356
SILParserTUStateBase *SIL,
354357
PersistentParserState *PersistentState,
355-
std::shared_ptr<SyntaxParseActions> SPActions = nullptr);
358+
std::shared_ptr<SyntaxParseActions> SPActions = nullptr,
359+
bool DisableDelayedParsing = false);
356360
Parser(unsigned BufferID, SourceFile &SF, SILParserTUStateBase *SIL,
357361
PersistentParserState *PersistentState = nullptr,
358-
std::shared_ptr<SyntaxParseActions> SPActions = nullptr);
362+
std::shared_ptr<SyntaxParseActions> SPActions = nullptr,
363+
bool DisableDelayedParsing = false);
359364
Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
360365
SILParserTUStateBase *SIL = nullptr,
361366
PersistentParserState *PersistentState = nullptr,
362-
std::shared_ptr<SyntaxParseActions> SPActions = nullptr);
367+
std::shared_ptr<SyntaxParseActions> SPActions = nullptr,
368+
bool DisableDelayedParsing = false);
363369
~Parser();
364370

365371
bool isInSILMode() const { return SIL != nullptr; }

include/swift/Subsystems.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,17 @@ namespace swift {
121121
bool parseIntoSourceFile(SourceFile &SF, unsigned BufferID, bool *Done,
122122
SILParserState *SIL = nullptr,
123123
PersistentParserState *PersistentState = nullptr,
124-
DelayedParsingCallbacks *DelayedParseCB = nullptr);
124+
DelayedParsingCallbacks *DelayedParseCB = nullptr,
125+
bool DisableDelayedParsing = false);
125126

126127
/// Parse a single buffer into the given source file, until the full source
127128
/// contents are parsed.
128129
///
129130
/// \return true if the parser found code with side effects.
130131
bool parseIntoSourceFileFull(SourceFile &SF, unsigned BufferID,
131132
PersistentParserState *PersistentState = nullptr,
132-
DelayedParsingCallbacks *DelayedParseCB = nullptr);
133+
DelayedParsingCallbacks *DelayedParseCB = nullptr,
134+
bool DisableDelayedParsing = false);
133135

134136
/// Finish the parsing by going over the nodes that were delayed
135137
/// during the first parsing pass.

lib/Parse/ParseDecl.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -3495,6 +3495,9 @@ bool Parser::parseDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
34953495
}
34963496

34973497
bool Parser::canDelayMemberDeclParsing() {
3498+
// If explicitly disabled, respect the flag.
3499+
if (DisableDelayedParsing)
3500+
return false;
34983501
// There's no fundamental reasons that SIL cannnot be lasily parsed. We need
34993502
// to keep SILParserTUStateBase persistent to make it happen.
35003503
if (isInSILMode())

lib/Parse/Parser.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,16 @@ swift::tokenizeWithTrivia(const LangOptions &LangOpts, const SourceManager &SM,
335335

336336
Parser::Parser(unsigned BufferID, SourceFile &SF, SILParserTUStateBase *SIL,
337337
PersistentParserState *PersistentState,
338-
std::shared_ptr<SyntaxParseActions> SPActions)
338+
std::shared_ptr<SyntaxParseActions> SPActions,
339+
bool DisableDelayedParsing)
339340
: Parser(BufferID, SF, &SF.getASTContext().Diags, SIL, PersistentState,
340-
std::move(SPActions)) {}
341+
std::move(SPActions), DisableDelayedParsing) {}
341342

342343
Parser::Parser(unsigned BufferID, SourceFile &SF, DiagnosticEngine* LexerDiags,
343344
SILParserTUStateBase *SIL,
344345
PersistentParserState *PersistentState,
345-
std::shared_ptr<SyntaxParseActions> SPActions)
346+
std::shared_ptr<SyntaxParseActions> SPActions,
347+
bool DisableDelayedParsing)
346348
: Parser(
347349
std::unique_ptr<Lexer>(new Lexer(
348350
SF.getASTContext().LangOpts, SF.getASTContext().SourceMgr,
@@ -357,7 +359,7 @@ Parser::Parser(unsigned BufferID, SourceFile &SF, DiagnosticEngine* LexerDiags,
357359
SF.shouldBuildSyntaxTree()
358360
? TriviaRetentionMode::WithTrivia
359361
: TriviaRetentionMode::WithoutTrivia)),
360-
SF, SIL, PersistentState, std::move(SPActions)) {}
362+
SF, SIL, PersistentState, std::move(SPActions), DisableDelayedParsing) {}
361363

362364
namespace {
363365

@@ -477,14 +479,16 @@ class TokenRecorder: public ConsumeTokenReceiver {
477479
Parser::Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
478480
SILParserTUStateBase *SIL,
479481
PersistentParserState *PersistentState,
480-
std::shared_ptr<SyntaxParseActions> SPActions)
482+
std::shared_ptr<SyntaxParseActions> SPActions,
483+
bool DisableDelayedParsing)
481484
: SourceMgr(SF.getASTContext().SourceMgr),
482485
Diags(SF.getASTContext().Diags),
483486
SF(SF),
484487
L(Lex.release()),
485488
SIL(SIL),
486489
CurDeclContext(&SF),
487490
Context(SF.getASTContext()),
491+
DisableDelayedParsing(DisableDelayedParsing),
488492
TokReceiver(SF.shouldCollectToken() ?
489493
new TokenRecorder(SF) :
490494
new ConsumeTokenReceiver()),

lib/ParseSIL/ParseSIL.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ static bool parseIntoSourceFileImpl(SourceFile &SF,
115115
SILParserState *SIL,
116116
PersistentParserState *PersistentState,
117117
DelayedParsingCallbacks *DelayedParseCB,
118-
bool FullParse) {
118+
bool FullParse,
119+
bool DisableDelayedParsing) {
119120
assert((!FullParse || (SF.canBeParsedInFull() && !SIL)) &&
120121
"cannot parse in full with the given parameters!");
121122

@@ -127,7 +128,7 @@ static bool parseIntoSourceFileImpl(SourceFile &SF,
127128

128129
SharedTimer timer("Parsing");
129130
Parser P(BufferID, SF, SIL ? SIL->Impl.get() : nullptr,
130-
PersistentState, STreeCreator);
131+
PersistentState, STreeCreator, DisableDelayedParsing);
131132
PrettyStackTraceParser StackTrace(P);
132133

133134
llvm::SaveAndRestore<bool> S(P.IsParsingInterfaceTokens,
@@ -155,19 +156,22 @@ bool swift::parseIntoSourceFile(SourceFile &SF,
155156
bool *Done,
156157
SILParserState *SIL,
157158
PersistentParserState *PersistentState,
158-
DelayedParsingCallbacks *DelayedParseCB) {
159+
DelayedParsingCallbacks *DelayedParseCB,
160+
bool DisableDelayedParsing) {
159161
return parseIntoSourceFileImpl(SF, BufferID, Done, SIL,
160162
PersistentState, DelayedParseCB,
161-
/*FullParse=*/SF.shouldBuildSyntaxTree());
163+
/*FullParse=*/SF.shouldBuildSyntaxTree(),
164+
DisableDelayedParsing);
162165
}
163166

164167
bool swift::parseIntoSourceFileFull(SourceFile &SF, unsigned BufferID,
165168
PersistentParserState *PersistentState,
166-
DelayedParsingCallbacks *DelayedParseCB) {
169+
DelayedParsingCallbacks *DelayedParseCB,
170+
bool DisableDelayedParsing) {
167171
bool Done = false;
168172
return parseIntoSourceFileImpl(SF, BufferID, &Done, /*SIL=*/nullptr,
169173
PersistentState, DelayedParseCB,
170-
/*FullParse=*/true);
174+
/*FullParse=*/true, DisableDelayedParsing);
171175
}
172176

173177

0 commit comments

Comments
 (0)