@@ -578,21 +578,6 @@ SourceFile *CompilerInstance::getCodeCompletionFile() const {
578
578
return evaluateOrDefault (eval, CodeCompletionFileRequest{mod}, nullptr );
579
579
}
580
580
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
-
596
581
bool CompilerInstance::setUpInputs () {
597
582
// Adds to InputSourceCodeBufferIDs, so may need to happen before the
598
583
// per-input setup.
@@ -619,15 +604,6 @@ bool CompilerInstance::setUpInputs() {
619
604
recordPrimaryInputBuffer (*codeCompletionBufferID);
620
605
}
621
606
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
-
631
607
return false ;
632
608
}
633
609
@@ -772,6 +748,62 @@ ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
772
748
return imports;
773
749
}
774
750
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
+
775
807
bool CompilerInstance::createFilesForMainModule (
776
808
ModuleDecl *mod, SmallVectorImpl<FileUnit *> &files) const {
777
809
// Try to pull out the main source file, if any. This ensures that it
@@ -994,14 +1026,14 @@ CompilerInstance::getSourceFileParsingOptions(bool forPrimary) const {
994
1026
995
1027
SourceFile *CompilerInstance::createSourceFileForMainModule (
996
1028
ModuleDecl *mod, SourceFileKind fileKind,
997
- Optional<unsigned > bufferID) const {
1029
+ Optional<unsigned > bufferID, bool isMainBuffer ) const {
998
1030
auto isPrimary = bufferID && isPrimaryInput (*bufferID);
999
1031
auto opts = getSourceFileParsingOptions (isPrimary);
1000
1032
1001
1033
auto *inputFile = new (*Context)
1002
1034
SourceFile (*mod, fileKind, bufferID, opts, isPrimary);
1003
1035
1004
- if (bufferID == MainBufferID )
1036
+ if (isMainBuffer )
1005
1037
inputFile->SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache ();
1006
1038
1007
1039
return inputFile;
0 commit comments