Skip to content

Commit 0d5a5e1

Browse files
committed
Move #if evaluation flag out of PersistentParserState
Move this flag onto the parser instead. Now the only client of PersistentParserState is code completion.
1 parent d494cc8 commit 0d5a5e1

File tree

10 files changed

+29
-25
lines changed

10 files changed

+29
-25
lines changed

include/swift/Parse/Parser.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ class Parser {
223223
/// lazily parsed and type checked.
224224
bool DelayBodyParsing;
225225

226+
/// Whether to evaluate the conditions of #if decls, meaning that the bodies
227+
/// of any active clauses are hoisted such that they become sibling nodes with
228+
/// the #if decl.
229+
// FIXME: When condition evaluation moves to a later phase, remove this bit
230+
// and adjust the client call 'performParseOnly'.
231+
bool EvaluateConditionals;
232+
226233
/// The receiver to collect all consumed tokens.
227234
ConsumeTokenReceiver *TokReceiver;
228235

@@ -401,16 +408,16 @@ class Parser {
401408
SILParserTUStateBase *SIL,
402409
PersistentParserState *PersistentState,
403410
std::shared_ptr<SyntaxParseActions> SPActions = nullptr,
404-
bool DelayBodyParsing = true);
411+
bool DelayBodyParsing = true, bool EvaluateConditionals = true);
405412
Parser(unsigned BufferID, SourceFile &SF, SILParserTUStateBase *SIL,
406413
PersistentParserState *PersistentState = nullptr,
407414
std::shared_ptr<SyntaxParseActions> SPActions = nullptr,
408-
bool DelayBodyParsing = true);
415+
bool DelayBodyParsing = true, bool EvaluateConditionals = true);
409416
Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
410417
SILParserTUStateBase *SIL = nullptr,
411418
PersistentParserState *PersistentState = nullptr,
412419
std::shared_ptr<SyntaxParseActions> SPActions = nullptr,
413-
bool DelayBodyParsing = true);
420+
bool DelayBodyParsing = true, bool EvaluateConditionals = true);
414421
~Parser();
415422

416423
/// Returns true if the buffer being parsed is allowed to contain SIL.

include/swift/Parse/PersistentParserState.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include "swift/Basic/SourceLoc.h"
2121
#include "swift/Parse/LocalContext.h"
22-
#include "swift/Parse/ParserPosition.h"
2322
#include "swift/Parse/Scope.h"
2423
#include "llvm/ADT/DenseMap.h"
2524

@@ -58,11 +57,6 @@ class CodeCompletionDelayedDeclState {
5857

5958
/// Parser state persistent across multiple parses.
6059
class PersistentParserState {
61-
public:
62-
// FIXME: When condition evaluation moves to a later phase, remove this bit
63-
// and adjust the client call 'performParseOnly'.
64-
bool PerformConditionEvaluation = true;
65-
private:
6660
swift::ScopeInfo ScopeInfo;
6761

6862
std::unique_ptr<CodeCompletionDelayedDeclState> CodeCompletionDelayedDeclStat;

include/swift/Subsystems.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ namespace swift {
117117
/// delayed.
118118
void parseIntoSourceFile(SourceFile &SF, unsigned BufferID,
119119
PersistentParserState *PersistentState = nullptr,
120-
bool DelayBodyParsing = true);
120+
bool DelayBodyParsing = true,
121+
bool EvaluateConditionals = true);
121122

122123
/// Parse a source file's SIL declarations into a given SIL module.
123124
void parseSourceFileSIL(SourceFile &SF, SILParserState *sil);

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,6 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
11221122
}
11231123

11241124
PersistentState = std::make_unique<PersistentParserState>();
1125-
PersistentState->PerformConditionEvaluation = EvaluateConditionals;
11261125

11271126
auto shouldDelayBodies = [&](unsigned bufferID) -> bool {
11281127
if (!CanDelayBodies)
@@ -1142,7 +1141,7 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
11421141
BufferID);
11431142

11441143
parseIntoSourceFile(*NextInput, BufferID, PersistentState.get(),
1145-
shouldDelayBodies(BufferID));
1144+
shouldDelayBodies(BufferID), EvaluateConditionals);
11461145
}
11471146

11481147
// Now parse the main file.
@@ -1153,7 +1152,7 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
11531152
assert(MainBufferID == MainFile.getBufferID());
11541153

11551154
parseIntoSourceFile(MainFile, MainBufferID, PersistentState.get(),
1156-
shouldDelayBodies(MainBufferID));
1155+
shouldDelayBodies(MainBufferID), EvaluateConditionals);
11571156
}
11581157

11591158
assert(Context->LoadedModules.size() == 1 &&

lib/Parse/ParseIfConfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
609609

610610
bool shouldEvaluate =
611611
// Don't evaluate if it's in '-parse' mode, etc.
612-
State->PerformConditionEvaluation &&
612+
EvaluateConditionals &&
613613
// If it's in inactive #if ... #endif block, there's no point to do it.
614614
!getScopeInfo().isInactiveConfigBlock();
615615

lib/Parse/Parser.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,15 @@ static LexerMode sourceFileKindToLexerMode(SourceFileKind kind) {
366366
Parser::Parser(unsigned BufferID, SourceFile &SF, SILParserTUStateBase *SIL,
367367
PersistentParserState *PersistentState,
368368
std::shared_ptr<SyntaxParseActions> SPActions,
369-
bool DelayBodyParsing)
369+
bool DelayBodyParsing, bool EvaluateConditionals)
370370
: Parser(BufferID, SF, &SF.getASTContext().Diags, SIL, PersistentState,
371-
std::move(SPActions), DelayBodyParsing) {}
371+
std::move(SPActions), DelayBodyParsing, EvaluateConditionals) {}
372372

373373
Parser::Parser(unsigned BufferID, SourceFile &SF, DiagnosticEngine* LexerDiags,
374374
SILParserTUStateBase *SIL,
375375
PersistentParserState *PersistentState,
376376
std::shared_ptr<SyntaxParseActions> SPActions,
377-
bool DelayBodyParsing)
377+
bool DelayBodyParsing, bool EvaluateConditionals)
378378
: Parser(
379379
std::unique_ptr<Lexer>(new Lexer(
380380
SF.getASTContext().LangOpts, SF.getASTContext().SourceMgr,
@@ -389,7 +389,8 @@ Parser::Parser(unsigned BufferID, SourceFile &SF, DiagnosticEngine* LexerDiags,
389389
SF.shouldBuildSyntaxTree()
390390
? TriviaRetentionMode::WithTrivia
391391
: TriviaRetentionMode::WithoutTrivia)),
392-
SF, SIL, PersistentState, std::move(SPActions), DelayBodyParsing) {}
392+
SF, SIL, PersistentState, std::move(SPActions), DelayBodyParsing,
393+
EvaluateConditionals) {}
393394

394395
namespace {
395396

@@ -510,7 +511,7 @@ Parser::Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
510511
SILParserTUStateBase *SIL,
511512
PersistentParserState *PersistentState,
512513
std::shared_ptr<SyntaxParseActions> SPActions,
513-
bool DelayBodyParsing)
514+
bool DelayBodyParsing, bool EvaluateConditionals)
514515
: SourceMgr(SF.getASTContext().SourceMgr),
515516
Diags(SF.getASTContext().Diags),
516517
SF(SF),
@@ -520,6 +521,7 @@ Parser::Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
520521
Context(SF.getASTContext()),
521522
CurrentTokenHash(SF.getInterfaceHashPtr()),
522523
DelayBodyParsing(DelayBodyParsing),
524+
EvaluateConditionals(EvaluateConditionals),
523525
TokReceiver(SF.shouldCollectToken() ?
524526
new TokenRecorder(SF, L->getBufferID()) :
525527
new ConsumeTokenReceiver()),

lib/ParseSIL/ParseSIL.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ void PrettyStackTraceParser::print(llvm::raw_ostream &out) const {
115115

116116
void swift::parseIntoSourceFile(SourceFile &SF, unsigned int BufferID,
117117
PersistentParserState *PersistentState,
118-
bool DelayBodyParsing) {
118+
bool DelayBodyParsing,
119+
bool EvaluateConditionals) {
119120
std::shared_ptr<SyntaxTreeCreator> STreeCreator;
120121
if (SF.shouldBuildSyntaxTree()) {
121122
STreeCreator = std::make_shared<SyntaxTreeCreator>(
@@ -136,7 +137,7 @@ void swift::parseIntoSourceFile(SourceFile &SF, unsigned int BufferID,
136137
FrontendStatsTracer tracer(SF.getASTContext().Stats,
137138
"Parsing");
138139
Parser P(BufferID, SF, /*SIL*/ nullptr, PersistentState, STreeCreator,
139-
DelayBodyParsing);
140+
DelayBodyParsing, EvaluateConditionals);
140141
PrettyStackTraceParser StackTrace(P);
141142

142143
llvm::SaveAndRestore<NullablePtr<llvm::MD5>> S(P.CurrentTokenHash,

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ class SwiftDocumentSyntaxInfo {
715715
Parser->getDiagnosticEngine().addConsumer(DiagConsumer);
716716

717717
// Collecting syntactic information shouldn't evaluate # conditions.
718-
Parser->getParser().State->PerformConditionEvaluation = false;
718+
Parser->getParser().EvaluateConditionals = false;
719719

720720
// If there is a syntax parsing cache, incremental syntax parsing is
721721
// performed and thus the generated AST may not be up-to-date.

tools/libSwiftSyntaxParser/libSwiftSyntaxParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ swiftparse_client_node_t SynParser::parse(const char *source) {
290290
"syntax_parse_module", std::move(parseActions),
291291
/*SyntaxCache=*/nullptr);
292292
// Evaluating pound conditions may lead to unknown syntax.
293-
PU.getParser().State->PerformConditionEvaluation = false;
293+
PU.getParser().EvaluateConditionals = false;
294294
std::unique_ptr<SynParserDiagConsumer> pConsumer;
295295
if (DiagHandler) {
296296
pConsumer = std::make_unique<SynParserDiagConsumer>(*this, bufID);

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ static int doSyntaxColoring(const CompilerInvocation &InitInvok,
11241124
registerTypeCheckerRequestFunctions(Parser.getParser().Context.evaluator);
11251125

11261126
// Collecting syntactic information shouldn't evaluate # conditions.
1127-
Parser.getParser().State->PerformConditionEvaluation = false;
1127+
Parser.getParser().EvaluateConditionals = false;
11281128
Parser.getDiagnosticEngine().addConsumer(PrintDiags);
11291129

11301130
(void)Parser.parse();
@@ -1361,7 +1361,7 @@ static int doStructureAnnotation(const CompilerInvocation &InitInvok,
13611361
Parser.getParser().Context.evaluator);
13621362

13631363
// Collecting syntactic information shouldn't evaluate # conditions.
1364-
Parser.getParser().State->PerformConditionEvaluation = false;
1364+
Parser.getParser().EvaluateConditionals = false;
13651365

13661366
// Display diagnostics to stderr.
13671367
PrintingDiagnosticConsumer PrintDiags;

0 commit comments

Comments
 (0)