@@ -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-
596581bool 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+
775807bool 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
9951027SourceFile *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;
0 commit comments