Skip to content

Commit 5d72c46

Browse files
committed
[Frontend] Remove parsing option params from performParseOnly
Lift the `DisablePoundIfEvaluation` parsing option into `LangOptions` to subsume the need for the `EvaluateConditionals` parameter, and sink the computation of `CanDelayBodies` down into `createSourceFileForMainModule`.
1 parent 81483cc commit 5d72c46

File tree

12 files changed

+47
-33
lines changed

12 files changed

+47
-33
lines changed

include/swift/AST/SourceFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class SourceFile final : public FileUnit {
105105
/// decl.
106106
///
107107
/// FIXME: When condition evaluation moves to a later phase, remove this
108-
/// and adjust the client call 'performParseOnly'.
108+
/// and the associated language option.
109109
DisablePoundIfEvaluation = 1 << 1,
110110

111111
/// Whether to build a syntax tree.

include/swift/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ namespace swift {
335335
/// Whether to verify the parsed syntax tree and emit related diagnostics.
336336
bool VerifySyntaxTree = false;
337337

338+
/// Whether to disable the evaluation of '#if' decls, such that the bodies
339+
/// of active clauses aren't hoisted into the enclosing scope.
340+
bool DisablePoundIfEvaluation = false;
341+
338342
/// Instead of hashing tokens inside of NominalType and ExtensionBodies into
339343
/// the interface hash, hash them into per-iterable-decl-context
340344
/// fingerprints. Fine-grained dependency types won't dirty every provides

include/swift/Frontend/Frontend.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,7 @@ class CompilerInstance {
616616
void performSema();
617617

618618
/// Parses the input file but does no type-checking or module imports.
619-
void performParseOnly(bool EvaluateConditionals = false,
620-
bool CanDelayBodies = true);
619+
void performParseOnly();
621620

622621
/// Parses and performs import resolution on all input files.
623622
///
@@ -633,8 +632,7 @@ class CompilerInstance {
633632
private:
634633
SourceFile *
635634
createSourceFileForMainModule(SourceFileKind FileKind,
636-
Optional<unsigned> BufferID,
637-
SourceFile::ParsingOptions options = {});
635+
Optional<unsigned> BufferID);
638636

639637
public:
640638
void freeASTContext();
@@ -647,8 +645,7 @@ class CompilerInstance {
647645
/// Retrieve a description of which modules should be implicitly imported.
648646
ImplicitImportInfo getImplicitImportInfo() const;
649647

650-
void performSemaUpTo(SourceFile::ASTStage_t LimitStage,
651-
SourceFile::ParsingOptions POpts = {});
648+
void performSemaUpTo(SourceFile::ASTStage_t LimitStage);
652649

653650
/// Return true if had load error
654651
bool loadPartialModulesAndImplicitImports();

lib/AST/Module.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,6 +2233,8 @@ SourceFile::SourceFile(ModuleDecl &M, SourceFileKind K,
22332233
SourceFile::ParsingOptions
22342234
SourceFile::getDefaultParsingOptions(const LangOptions &langOpts) {
22352235
ParsingOptions opts;
2236+
if (langOpts.DisablePoundIfEvaluation)
2237+
opts |= ParsingFlags::DisablePoundIfEvaluation;
22362238
if (langOpts.BuildSyntaxTree)
22372239
opts |= ParsingFlags::BuildSyntaxTree;
22382240
if (langOpts.CollectParsedToken)

lib/Frontend/Frontend.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -729,20 +729,14 @@ void CompilerInstance::setMainModule(ModuleDecl *newMod) {
729729
Context->LoadedModules[newMod->getName()] = newMod;
730730
}
731731

732-
void CompilerInstance::performParseOnly(bool EvaluateConditionals,
733-
bool CanDelayBodies) {
732+
void CompilerInstance::performParseOnly() {
734733
const InputFileKind Kind = Invocation.getInputKind();
735734
assert((Kind == InputFileKind::Swift || Kind == InputFileKind::SwiftLibrary ||
736735
Kind == InputFileKind::SwiftModuleInterface) &&
737736
"only supports parsing .swift files");
738737
(void)Kind;
739738

740-
SourceFile::ParsingOptions parsingOpts;
741-
if (!EvaluateConditionals)
742-
parsingOpts |= SourceFile::ParsingFlags::DisablePoundIfEvaluation;
743-
if (!CanDelayBodies)
744-
parsingOpts |= SourceFile::ParsingFlags::DisableDelayedBodies;
745-
performSemaUpTo(SourceFile::Unprocessed, parsingOpts);
739+
performSemaUpTo(SourceFile::Unprocessed);
746740
assert(Context->LoadedModules.size() == 1 &&
747741
"Loaded a module during parse-only");
748742
}
@@ -755,8 +749,7 @@ void CompilerInstance::performSema() {
755749
performSemaUpTo(SourceFile::TypeChecked);
756750
}
757751

758-
void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage,
759-
SourceFile::ParsingOptions POpts) {
752+
void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage) {
760753
FrontendStatsTracer tracer(getStatsReporter(), "perform-sema");
761754

762755
ModuleDecl *mainModule = getMainModule();
@@ -765,7 +758,7 @@ void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage,
765758
// Make sure the main file is the first file in the module, so do this now.
766759
if (MainBufferID != NO_SUCH_BUFFER) {
767760
auto *mainFile = createSourceFileForMainModule(
768-
Invocation.getSourceFileKind(), MainBufferID, POpts);
761+
Invocation.getSourceFileKind(), MainBufferID);
769762
mainFile->SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache();
770763
}
771764

@@ -789,8 +782,7 @@ void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage,
789782
SF = &getMainModule()->getMainSourceFile(Invocation.getSourceFileKind());
790783
} else {
791784
// Otherwise create a library file.
792-
SF = createSourceFileForMainModule(SourceFileKind::Library,
793-
BufferID, POpts);
785+
SF = createSourceFileForMainModule(SourceFileKind::Library, BufferID);
794786
}
795787
// Trigger parsing of the file.
796788
if (LimitStage == SourceFile::Unprocessed) {
@@ -898,10 +890,28 @@ void CompilerInstance::finishTypeChecking() {
898890
}
899891

900892
SourceFile *CompilerInstance::createSourceFileForMainModule(
901-
SourceFileKind fileKind, Optional<unsigned> bufferID,
902-
SourceFile::ParsingOptions opts) {
893+
SourceFileKind fileKind, Optional<unsigned> bufferID) {
903894
ModuleDecl *mainModule = getMainModule();
904895

896+
const auto &frontendOpts = Invocation.getFrontendOptions();
897+
const auto action = frontendOpts.RequestedAction;
898+
899+
auto opts = SourceFile::getDefaultParsingOptions(getASTContext().LangOpts);
900+
if (FrontendOptions::shouldActionOnlyParse(action)) {
901+
// Generally in a parse-only invocation, we want to disable #if evaluation.
902+
// However, there are a couple of modes where we need to know which clauses
903+
// are active.
904+
if (action != FrontendOptions::ActionType::EmitImportedModules &&
905+
action != FrontendOptions::ActionType::ScanDependencies) {
906+
opts |= SourceFile::ParsingFlags::DisablePoundIfEvaluation;
907+
}
908+
909+
// If we need to dump the parse tree, disable delayed bodies as we want to
910+
// show everything.
911+
if (action == FrontendOptions::ActionType::DumpParse)
912+
opts |= SourceFile::ParsingFlags::DisableDelayedBodies;
913+
}
914+
905915
auto isPrimary = bufferID && isPrimaryInput(*bufferID);
906916
if (isPrimary || isWholeModuleCompilation()) {
907917
// Disable delayed body parsing for primaries.
@@ -917,7 +927,6 @@ SourceFile *CompilerInstance::createSourceFileForMainModule(
917927
if (isPrimary) {
918928
opts |= SourceFile::ParsingFlags::EnableInterfaceHash;
919929
}
920-
opts |= SourceFile::getDefaultParsingOptions(getASTContext().LangOpts);
921930

922931
auto *inputFile = new (*Context)
923932
SourceFile(*mainModule, fileKind, bufferID, opts, isPrimary);

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,14 +1274,7 @@ static bool performCompile(CompilerInstance &Instance,
12741274
}
12751275

12761276
if (FrontendOptions::shouldActionOnlyParse(Action)) {
1277-
// Disable delayed parsing of type and function bodies when we've been
1278-
// asked to dump the resulting AST.
1279-
bool CanDelayBodies = Action != FrontendOptions::ActionType::DumpParse;
1280-
bool EvaluateConditionals =
1281-
Action == FrontendOptions::ActionType::EmitImportedModules
1282-
|| Action == FrontendOptions::ActionType::ScanDependencies;
1283-
Instance.performParseOnly(EvaluateConditionals,
1284-
CanDelayBodies);
1277+
Instance.performParseOnly();
12851278
} else if (Action == FrontendOptions::ActionType::ResolveImports) {
12861279
Instance.performParseAndResolveImportsOnly();
12871280
} else {

lib/IDE/Refactoring.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,7 @@ getNotableRegions(StringRef SourceText, unsigned NameOffset, StringRef Name,
11741174
Invocation.getFrontendOptions().InputsAndOutputs.addInput(
11751175
InputFile("<extract>", true, InputBuffer.get()));
11761176
Invocation.getFrontendOptions().ModuleName = "extract";
1177+
Invocation.getLangOptions().DisablePoundIfEvaluation = true;
11771178

11781179
auto Instance = std::make_unique<swift::CompilerInstance>();
11791180
if (Instance->setup(Invocation))

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ static bool makeParserAST(CompilerInstance &CI, StringRef Text,
808808
Invocation.getFrontendOptions().InputsAndOutputs.clearInputs();
809809
Invocation.setModuleName("main");
810810
Invocation.setInputKind(InputFileKind::Swift);
811+
Invocation.getLangOptions().DisablePoundIfEvaluation = true;
811812

812813
std::unique_ptr<llvm::MemoryBuffer> Buf;
813814
Buf = llvm::MemoryBuffer::getMemBuffer(Text, "<module-interface>");
@@ -1406,7 +1407,7 @@ SourceFile *SwiftLangSupport::getSyntacticSourceFile(
14061407
Error = "Compiler invocation set up failed";
14071408
return nullptr;
14081409
}
1409-
ParseCI.performParseOnly(/*EvaluateConditionals*/true);
1410+
ParseCI.performParseOnly();
14101411

14111412
SourceFile *SF = nullptr;
14121413
unsigned BufferID = ParseCI.getInputBufferIDs().back();

tools/SourceKit/lib/SwiftLang/SwiftEditorInterfaceGen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ static bool makeParserAST(CompilerInstance &CI, StringRef Text,
234234
Invocation.getFrontendOptions().InputsAndOutputs.clearInputs();
235235
Invocation.setModuleName("main");
236236
Invocation.setInputKind(InputFileKind::Swift);
237+
Invocation.getLangOptions().DisablePoundIfEvaluation = true;
237238

238239
std::unique_ptr<llvm::MemoryBuffer> Buf;
239240
Buf = llvm::MemoryBuffer::getMemBuffer(Text, "<module-interface>");

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,6 +1707,9 @@ static int doPrintAST(const CompilerInvocation &InitInvok,
17071707
CompilerInvocation Invocation(InitInvok);
17081708
Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(SourceFilename);
17091709

1710+
if (!RunTypeChecker)
1711+
Invocation.getLangOptions().DisablePoundIfEvaluation = true;
1712+
17101713
CompilerInstance CI;
17111714

17121715
// Display diagnostics to stderr.

0 commit comments

Comments
 (0)