Skip to content

Commit 3287c4b

Browse files
committed
[NFC] Refactor Main File Computation
1 parent c5a6cd6 commit 3287c4b

File tree

3 files changed

+68
-35
lines changed

3 files changed

+68
-35
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,6 @@ class CompilerInstance {
430430
/// \c mutable as it is consumed by \c loadPartialModulesAndImplicitImports.
431431
mutable std::vector<ModuleBuffers> PartialModules;
432432

433-
enum : unsigned { NO_SUCH_BUFFER = ~0U };
434-
unsigned MainBufferID = NO_SUCH_BUFFER;
435-
436433
/// Identifies the set of input buffers in the SourceManager that are
437434
/// considered primaries.
438435
llvm::SetVector<unsigned> PrimaryBufferIDs;
@@ -602,12 +599,14 @@ class CompilerInstance {
602599
/// Creates a new source file for the main module.
603600
SourceFile *createSourceFileForMainModule(ModuleDecl *mod,
604601
SourceFileKind FileKind,
605-
Optional<unsigned> BufferID) const;
602+
Optional<unsigned> BufferID,
603+
bool isMainBuffer = false) const;
606604

607605
/// Creates all the files to be added to the main module, appending them to
608606
/// \p files. If a loading error occurs, returns \c true.
609607
bool createFilesForMainModule(ModuleDecl *mod,
610608
SmallVectorImpl<FileUnit *> &files) const;
609+
SourceFile *computeMainSourceFileForModule(ModuleDecl *mod) const;
611610

612611
public:
613612
void freeASTContext();

lib/Frontend/Frontend.cpp

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -578,21 +578,6 @@ SourceFile *CompilerInstance::getCodeCompletionFile() const {
578578
return evaluateOrDefault(eval, CodeCompletionFileRequest{mod}, nullptr);
579579
}
580580

581-
static bool shouldTreatSingleInputAsMain(InputFileKind inputKind) {
582-
switch (inputKind) {
583-
case InputFileKind::Swift:
584-
case InputFileKind::SwiftModuleInterface:
585-
case InputFileKind::SIL:
586-
return true;
587-
case InputFileKind::SwiftLibrary:
588-
case InputFileKind::LLVM:
589-
case InputFileKind::ObjCHeader:
590-
case InputFileKind::None:
591-
return false;
592-
}
593-
llvm_unreachable("unhandled input kind");
594-
}
595-
596581
bool CompilerInstance::setUpInputs() {
597582
// Adds to InputSourceCodeBufferIDs, so may need to happen before the
598583
// per-input setup.
@@ -619,15 +604,6 @@ bool CompilerInstance::setUpInputs() {
619604
recordPrimaryInputBuffer(*codeCompletionBufferID);
620605
}
621606

622-
if (MainBufferID == NO_SUCH_BUFFER &&
623-
InputSourceCodeBufferIDs.size() == 1 &&
624-
shouldTreatSingleInputAsMain(Invocation.getInputKind())) {
625-
MainBufferID = InputSourceCodeBufferIDs.front();
626-
}
627-
628-
return false;
629-
}
630-
631607
return false;
632608
}
633609

@@ -772,6 +748,62 @@ ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
772748
return imports;
773749
}
774750

751+
static Optional<SourceFileKind>
752+
tryMatchInputModeToSourceFileKind(FrontendOptions::ParseInputMode mode) {
753+
switch (mode) {
754+
case FrontendOptions::ParseInputMode::SwiftLibrary:
755+
// A Swift file in -parse-as-library mode is a library file.
756+
return SourceFileKind::Library;
757+
case FrontendOptions::ParseInputMode::SIL:
758+
// A Swift file in -parse-sil mode is a SIL file.
759+
return SourceFileKind::SIL;
760+
case FrontendOptions::ParseInputMode::SwiftModuleInterface:
761+
return SourceFileKind::Interface;
762+
case FrontendOptions::ParseInputMode::Swift:
763+
return SourceFileKind::Main;
764+
}
765+
llvm::outs() << (unsigned)mode;
766+
llvm_unreachable("Unhandled input parsing mode!");
767+
}
768+
769+
SourceFile *
770+
CompilerInstance::computeMainSourceFileForModule(ModuleDecl *mod) const {
771+
// Swift libraries cannot have a 'main'.
772+
const auto &FOpts = getInvocation().getFrontendOptions();
773+
const auto &Inputs = FOpts.InputsAndOutputs.getAllInputs();
774+
if (FOpts.InputMode == FrontendOptions::ParseInputMode::SwiftLibrary) {
775+
return nullptr;
776+
}
777+
778+
// Try to pull out a file called 'main.swift'.
779+
auto MainInputIter =
780+
std::find_if(Inputs.begin(), Inputs.end(), [](const InputFile &input) {
781+
return input.getType() == file_types::TY_Swift &&
782+
llvm::sys::path::filename(input.getFileName()) == "main.swift";
783+
});
784+
785+
Optional<unsigned> MainBufferID = None;
786+
if (MainInputIter != Inputs.end()) {
787+
MainBufferID =
788+
getSourceMgr().getIDForBufferIdentifier(MainInputIter->getFileName());
789+
} else if (InputSourceCodeBufferIDs.size() == 1) {
790+
// Barring that, just nominate a single Swift file as the main file.
791+
MainBufferID.emplace(InputSourceCodeBufferIDs.front());
792+
}
793+
794+
if (!MainBufferID.hasValue()) {
795+
return nullptr;
796+
}
797+
798+
auto SFK = tryMatchInputModeToSourceFileKind(FOpts.InputMode);
799+
if (!SFK.hasValue()) {
800+
return nullptr;
801+
}
802+
803+
return createSourceFileForMainModule(mod, *SFK,
804+
*MainBufferID, /*isMainBuffer*/true);
805+
}
806+
775807
bool CompilerInstance::createFilesForMainModule(
776808
ModuleDecl *mod, SmallVectorImpl<FileUnit *> &files) const {
777809
// Try to pull out the main source file, if any. This ensures that it
@@ -994,14 +1026,14 @@ CompilerInstance::getSourceFileParsingOptions(bool forPrimary) const {
9941026

9951027
SourceFile *CompilerInstance::createSourceFileForMainModule(
9961028
ModuleDecl *mod, SourceFileKind fileKind,
997-
Optional<unsigned> bufferID) const {
1029+
Optional<unsigned> bufferID, bool isMainBuffer) const {
9981030
auto isPrimary = bufferID && isPrimaryInput(*bufferID);
9991031
auto opts = getSourceFileParsingOptions(isPrimary);
10001032

10011033
auto *inputFile = new (*Context)
10021034
SourceFile(*mod, fileKind, bufferID, opts, isPrimary);
10031035

1004-
if (bufferID == MainBufferID)
1036+
if (isMainBuffer)
10051037
inputFile->SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache();
10061038

10071039
return inputFile;

lib/FrontendTool/FrontendTool.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,7 @@ static bool performCompile(CompilerInstance &Instance,
19781978
const FrontendOptions::ActionType Action = opts.RequestedAction;
19791979

19801980
// To compile LLVM IR, just pass it off unmodified.
1981-
if (Invocation.getInputKind() == InputFileKind::LLVM)
1981+
if (opts.InputsAndOutputs.shouldTreatAsLLVM())
19821982
return compileLLVMIR(Instance);
19831983

19841984
// If we aren't in a parse-only context and expect an implicit stdlib import,
@@ -1996,10 +1996,12 @@ static bool performCompile(CompilerInstance &Instance,
19961996
if (FrontendOptions::shouldActionOnlyParse(Action)) {
19971997
// Parsing gets triggered lazily, but let's make sure we have the right
19981998
// input kind.
1999-
auto kind = Invocation.getInputKind();
2000-
return kind == InputFileKind::Swift ||
2001-
kind == InputFileKind::SwiftLibrary ||
2002-
kind == InputFileKind::SwiftModuleInterface;
1999+
return llvm::all_of(
2000+
opts.InputsAndOutputs.getAllInputs(), [](const InputFile &IF) {
2001+
const auto kind = IF.getType();
2002+
return kind == file_types::TY_Swift ||
2003+
kind == file_types::TY_SwiftModuleInterfaceFile;
2004+
});
20032005
}
20042006
return true;
20052007
}() && "Only supports parsing .swift files");

0 commit comments

Comments
 (0)