Skip to content

Commit f56b061

Browse files
committed
[Parse] Check the SourceFile for #if evaluation
Remove the `EvaluateConditionals` flags from the parser, and instead query the source file. This commit also changes ParserUnit such that it doesn't evaluate #if conditions by default, as none of its clients appear to require it. The only client that wasn't explicitly disabling #if evaluation and is processing the resulting AST is swift-indent, so this commit also adds a test to ensure it continues to work correctly with #if decls.
1 parent 2724cf6 commit f56b061

File tree

15 files changed

+74
-43
lines changed

15 files changed

+74
-43
lines changed

include/swift/Parse/Parser.h

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ class Parser {
177177
/// bodies.
178178
bool isDelayedParsingEnabled() const;
179179

180+
/// Whether to evaluate the conditions of #if decls, meaning that the bodies
181+
/// of any active clauses are hoisted such that they become sibling nodes with
182+
/// the #if decl.
183+
bool shouldEvaluatePoundIfDecls() const;
184+
180185
void setCodeCompletionCallbacks(CodeCompletionCallbacks *Callbacks) {
181186
CodeCompletion = Callbacks;
182187
}
@@ -213,13 +218,6 @@ class Parser {
213218
/// Always empty if !SF.shouldBuildSyntaxTree().
214219
ParsedTrivia TrailingTrivia;
215220

216-
/// Whether to evaluate the conditions of #if decls, meaning that the bodies
217-
/// of any active clauses are hoisted such that they become sibling nodes with
218-
/// the #if decl.
219-
// FIXME: When condition evaluation moves to a later phase, remove this bit
220-
// and adjust the client call 'performParseOnly'.
221-
bool EvaluateConditionals;
222-
223221
/// The receiver to collect all consumed tokens.
224222
ConsumeTokenReceiver *TokReceiver;
225223

@@ -397,17 +395,14 @@ class Parser {
397395
Parser(unsigned BufferID, SourceFile &SF, DiagnosticEngine* LexerDiags,
398396
SILParserTUStateBase *SIL,
399397
PersistentParserState *PersistentState,
400-
std::shared_ptr<SyntaxParseActions> SPActions = nullptr,
401-
bool EvaluateConditionals = true);
398+
std::shared_ptr<SyntaxParseActions> SPActions = nullptr);
402399
Parser(unsigned BufferID, SourceFile &SF, SILParserTUStateBase *SIL,
403400
PersistentParserState *PersistentState = nullptr,
404-
std::shared_ptr<SyntaxParseActions> SPActions = nullptr,
405-
bool EvaluateConditionals = true);
401+
std::shared_ptr<SyntaxParseActions> SPActions = nullptr);
406402
Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
407403
SILParserTUStateBase *SIL = nullptr,
408404
PersistentParserState *PersistentState = nullptr,
409-
std::shared_ptr<SyntaxParseActions> SPActions = nullptr,
410-
bool EvaluateConditionals = true);
405+
std::shared_ptr<SyntaxParseActions> SPActions = nullptr);
411406
~Parser();
412407

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

include/swift/Subsystems.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ namespace swift {
108108
/// \param SF The file within the module being parsed.
109109
///
110110
/// \param BufferID The buffer to parse from.
111-
void parseIntoSourceFile(SourceFile &SF, unsigned BufferID,
112-
bool EvaluateConditionals = true);
111+
void parseIntoSourceFile(SourceFile &SF, unsigned BufferID);
113112

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

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
11471147
SourceFileKind::Library, SourceFile::ImplicitModuleImportKind::None,
11481148
BufferID, parsingOpts);
11491149

1150-
parseIntoSourceFile(*NextInput, BufferID, EvaluateConditionals);
1150+
parseIntoSourceFile(*NextInput, BufferID);
11511151
}
11521152

11531153
// Now parse the main file.
@@ -1157,7 +1157,7 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
11571157
MainFile.SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache();
11581158
assert(MainBufferID == MainFile.getBufferID());
11591159

1160-
parseIntoSourceFile(MainFile, MainBufferID, EvaluateConditionals);
1160+
parseIntoSourceFile(MainFile, MainBufferID);
11611161
}
11621162

11631163
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-
EvaluateConditionals &&
612+
shouldEvaluatePoundIfDecls() &&
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: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -362,16 +362,14 @@ static LexerMode sourceFileKindToLexerMode(SourceFileKind kind) {
362362

363363
Parser::Parser(unsigned BufferID, SourceFile &SF, SILParserTUStateBase *SIL,
364364
PersistentParserState *PersistentState,
365-
std::shared_ptr<SyntaxParseActions> SPActions,
366-
bool EvaluateConditionals)
365+
std::shared_ptr<SyntaxParseActions> SPActions)
367366
: Parser(BufferID, SF, &SF.getASTContext().Diags, SIL, PersistentState,
368-
std::move(SPActions), EvaluateConditionals) {}
367+
std::move(SPActions)) {}
369368

370369
Parser::Parser(unsigned BufferID, SourceFile &SF, DiagnosticEngine* LexerDiags,
371370
SILParserTUStateBase *SIL,
372371
PersistentParserState *PersistentState,
373-
std::shared_ptr<SyntaxParseActions> SPActions,
374-
bool EvaluateConditionals)
372+
std::shared_ptr<SyntaxParseActions> SPActions)
375373
: Parser(
376374
std::unique_ptr<Lexer>(new Lexer(
377375
SF.getASTContext().LangOpts, SF.getASTContext().SourceMgr,
@@ -386,8 +384,7 @@ Parser::Parser(unsigned BufferID, SourceFile &SF, DiagnosticEngine* LexerDiags,
386384
SF.shouldBuildSyntaxTree()
387385
? TriviaRetentionMode::WithTrivia
388386
: TriviaRetentionMode::WithoutTrivia)),
389-
SF, SIL, PersistentState, std::move(SPActions),
390-
EvaluateConditionals) {}
387+
SF, SIL, PersistentState, std::move(SPActions)) {}
391388

392389
namespace {
393390

@@ -507,8 +504,7 @@ class TokenRecorder: public ConsumeTokenReceiver {
507504
Parser::Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
508505
SILParserTUStateBase *SIL,
509506
PersistentParserState *PersistentState,
510-
std::shared_ptr<SyntaxParseActions> SPActions,
511-
bool EvaluateConditionals)
507+
std::shared_ptr<SyntaxParseActions> SPActions)
512508
: SourceMgr(SF.getASTContext().SourceMgr),
513509
Diags(SF.getASTContext().Diags),
514510
SF(SF),
@@ -517,7 +513,6 @@ Parser::Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
517513
CurDeclContext(&SF),
518514
Context(SF.getASTContext()),
519515
CurrentTokenHash(SF.getInterfaceHashPtr()),
520-
EvaluateConditionals(EvaluateConditionals),
521516
TokReceiver(SF.shouldCollectToken() ?
522517
new TokenRecorder(SF, L->getBufferID()) :
523518
new ConsumeTokenReceiver()),
@@ -551,6 +546,11 @@ bool Parser::isDelayedParsingEnabled() const {
551546
return SF.hasDelayedBodyParsing();
552547
}
553548

549+
bool Parser::shouldEvaluatePoundIfDecls() const {
550+
auto opts = SF.getParsingOptions();
551+
return !opts.contains(SourceFile::ParsingFlags::DisablePoundIfEvaluation);
552+
}
553+
554554
bool Parser::allowTopLevelCode() const {
555555
return SF.isScriptMode();
556556
}
@@ -1179,7 +1179,8 @@ struct ParserUnit::Implementation {
11791179
*ModuleDecl::create(Ctx.getIdentifier(ModuleName), Ctx), SFKind,
11801180
BufferID, SourceFile::ImplicitModuleImportKind::None,
11811181
Opts.CollectParsedToken, Opts.BuildSyntaxTree,
1182-
SourceFile::ParsingFlags::DisableDelayedBodies)) {}
1182+
SourceFile::ParsingFlags::DisableDelayedBodies |
1183+
SourceFile::ParsingFlags::DisablePoundIfEvaluation)) {}
11831184

11841185
~Implementation() {
11851186
// We need to delete the parser before the context so that it can finalize

lib/ParseSIL/ParseSIL.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ static void deletePersistentParserState(PersistentParserState *state) {
119119
delete state;
120120
}
121121

122-
void swift::parseIntoSourceFile(SourceFile &SF, unsigned int BufferID,
123-
bool EvaluateConditionals) {
122+
void swift::parseIntoSourceFile(SourceFile &SF, unsigned int BufferID) {
124123
auto &ctx = SF.getASTContext();
125124
std::shared_ptr<SyntaxTreeCreator> STreeCreator;
126125
if (SF.shouldBuildSyntaxTree()) {
@@ -139,8 +138,7 @@ void swift::parseIntoSourceFile(SourceFile &SF, unsigned int BufferID,
139138

140139
FrontendStatsTracer tracer(SF.getASTContext().Stats,
141140
"Parsing");
142-
Parser P(BufferID, SF, /*SIL*/ nullptr, state, STreeCreator,
143-
EvaluateConditionals);
141+
Parser P(BufferID, SF, /*SIL*/ nullptr, state, STreeCreator);
144142
PrettyStackTraceParser StackTrace(P);
145143

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

test/swift-indent/main.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,11 @@ break
3434
collatz(r)
3535
}
3636
}
37+
38+
#if true
39+
func foo() -> Int {
40+
0
41+
}
42+
#else
43+
1
44+
#endif

test/swift-indent/main.swift.indent2.response

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,11 @@ func collatz(n: Int, m: String?) {
3434
collatz(r)
3535
}
3636
}
37+
38+
#if true
39+
func foo() -> Int {
40+
0
41+
}
42+
#else
43+
1
44+
#endif

test/swift-indent/main.swift.indentswitch.response

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,11 @@ func collatz(n: Int, m: String?) {
3434
collatz(r)
3535
}
3636
}
37+
38+
#if true
39+
func foo() -> Int {
40+
0
41+
}
42+
#else
43+
1
44+
#endif

test/swift-indent/main.swift.lines.response

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,11 @@ break
3434
collatz(r)
3535
}
3636
}
37+
38+
#if true
39+
func foo() -> Int {
40+
0
41+
}
42+
#else
43+
1
44+
#endif

0 commit comments

Comments
 (0)