@@ -604,35 +604,43 @@ std::optional<std::string>
604
604
SerializedModuleBaseName::getPackageInterfacePathIfInSamePackage (
605
605
llvm::vfs::FileSystem &fs, ASTContext &ctx) const {
606
606
if (!ctx.LangOpts .EnablePackageInterfaceLoad )
607
- return {} ;
607
+ return std::nullopt ;
608
608
609
609
std::string packagePath{
610
- getName (file_types::TY_PackageSwiftModuleInterfaceFile)};
610
+ getName (file_types::TY_PackageSwiftModuleInterfaceFile)};
611
611
612
612
if (fs.exists (packagePath)) {
613
613
// Read the interface file and extract its package-name argument value
614
- StringRef result;
615
- if (auto packageFile = llvm::MemoryBuffer::getFile (packagePath)) {
616
- llvm::BumpPtrAllocator alloc;
617
- llvm::StringSaver argSaver (alloc);
618
- SmallVector<const char *, 8 > args;
619
- (void )extractCompilerFlagsFromInterface (packagePath,
620
- (*packageFile)->getBuffer (), argSaver, args);
621
- for (unsigned I = 0 , N = args.size (); I + 1 < N; I++) {
622
- StringRef current (args[I]), next (args[I + 1 ]);
623
- if (current == " -package-name" ) {
624
- // Instead of `break` here, continue to get the last value in case of dupes,
625
- // to be consistent with the default parsing logic.
626
- result = next;
627
- }
614
+ if (auto packageName = getPackageNameFromInterfacePath (packagePath, fs)) {
615
+ // Return the .package.swiftinterface path if the package name applies to
616
+ // the importer module.
617
+ if (*packageName == ctx.LangOpts .PackageName )
618
+ return packagePath;
619
+ }
620
+ }
621
+ return std::nullopt;
622
+ }
623
+
624
+ std::optional<std::string>
625
+ SerializedModuleBaseName::getPackageNameFromInterfacePath (
626
+ StringRef interfacePath, llvm::vfs::FileSystem &fs) const {
627
+ std::optional<std::string> result;
628
+ if (auto interfaceFile = fs.getBufferForFile (interfacePath)) {
629
+ llvm::BumpPtrAllocator alloc;
630
+ llvm::StringSaver argSaver (alloc);
631
+ SmallVector<const char *, 8 > args;
632
+ (void )extractCompilerFlagsFromInterface (
633
+ interfacePath, (*interfaceFile)->getBuffer (), argSaver, args);
634
+ for (unsigned I = 0 , N = args.size (); I + 1 < N; I++) {
635
+ StringRef current (args[I]), next (args[I + 1 ]);
636
+ if (current == " -package-name" ) {
637
+ // Instead of `break` here, continue to get the last value in case of
638
+ // dupes, to be consistent with the default parsing logic.
639
+ result = next;
628
640
}
629
641
}
630
- // Return the .package.swiftinterface path if the package name applies to
631
- // the importer module.
632
- if (!result.empty () && result == ctx.LangOpts .PackageName )
633
- return packagePath;
634
642
}
635
- return {} ;
643
+ return result ;
636
644
}
637
645
638
646
std::optional<std::string>
@@ -642,16 +650,15 @@ SerializedModuleBaseName::findInterfacePath(llvm::vfs::FileSystem &fs,
642
650
// Ensure the public swiftinterface already exists, otherwise bail early
643
651
// as it's considered the module doesn't exist.
644
652
if (!fs.exists (interfacePath))
645
- return {} ;
653
+ return std::nullopt ;
646
654
647
- // Check if a package interface exists and if the package name applies to
648
- // the importer module.
649
- auto pkgPath = getPackageInterfacePathIfInSamePackage (fs, ctx). value_or ( " " );
650
- if (!pkgPath. empty () && fs. exists (pkgPath))
651
- return pkgPath;
655
+ // If in the same package, try return the package interface path.
656
+ if ( auto packageName = getPackageNameFromInterfacePath (interfacePath, fs)) {
657
+ if (*packageName == ctx. LangOpts . PackageName )
658
+ return getPackageInterfacePathIfInSamePackage (fs, ctx);
659
+ }
652
660
653
- // If above fails, use the existing logic as fallback.
654
- // If present, use the private interface instead of the public one.
661
+ // Otherwise, use the private interface instead of the public one.
655
662
std::string privatePath{
656
663
getName (file_types::TY_PrivateSwiftModuleInterfaceFile)};
657
664
if (fs.exists (privatePath))
@@ -704,35 +711,33 @@ bool SerializedModuleLoaderBase::findModule(
704
711
std::optional<SerializedModuleBaseName> firstAbsoluteBaseName;
705
712
706
713
for (const auto &targetSpecificBaseName : targetSpecificBaseNames) {
707
- SerializedModuleBaseName
708
- absoluteBaseName{currPath, targetSpecificBaseName};
714
+ SerializedModuleBaseName absoluteBaseName{currPath,
715
+ targetSpecificBaseName};
709
716
710
717
if (!firstAbsoluteBaseName.has_value ())
711
718
firstAbsoluteBaseName.emplace (absoluteBaseName);
712
719
713
720
auto result = findModuleFilesInDirectory (
714
721
moduleID, absoluteBaseName, moduleInterfacePath,
715
722
moduleInterfaceSourcePath, moduleBuffer, moduleDocBuffer,
716
- moduleSourceInfoBuffer, skipBuildingInterface,
717
- IsFramework, isTestableDependencyLookup);
718
- if (!result) {
723
+ moduleSourceInfoBuffer, skipBuildingInterface, IsFramework,
724
+ isTestableDependencyLookup);
725
+ if (!result)
719
726
return SearchResult::Found;
720
- } else if (result == std::errc::not_supported) {
727
+ if (result == std::errc::not_supported)
721
728
return SearchResult::Error;
722
- } else if (result != std::errc::no_such_file_or_directory) {
729
+ if (result != std::errc::no_such_file_or_directory)
723
730
return SearchResult::NotFound;
724
- }
725
731
}
726
732
727
733
// We can only get here if all targetFileNamePairs failed with
728
734
// 'std::errc::no_such_file_or_directory'.
729
- if (firstAbsoluteBaseName
730
- && maybeDiagnoseTargetMismatch (moduleID.Loc , moduleName,
731
- *firstAbsoluteBaseName)) {
735
+ if (firstAbsoluteBaseName &&
736
+ maybeDiagnoseTargetMismatch (moduleID.Loc , moduleName,
737
+ *firstAbsoluteBaseName))
732
738
return SearchResult::Error;
733
- } else {
734
- return SearchResult::NotFound;
735
- }
739
+
740
+ return SearchResult::NotFound;
736
741
};
737
742
738
743
SmallVector<std::string, 4 > InterestingFilenames = {
@@ -788,13 +793,11 @@ bool SerializedModuleLoaderBase::findModule(
788
793
moduleID, absoluteBaseName, moduleInterfacePath,
789
794
moduleInterfaceSourcePath, moduleBuffer, moduleDocBuffer,
790
795
moduleSourceInfoBuffer, skipBuildingInterface, isFramework);
791
- if (!result) {
796
+ if (!result)
792
797
return true ;
793
- } else if (result == std::errc::not_supported) {
798
+ if (result == std::errc::not_supported)
794
799
return false ;
795
- } else {
796
- continue ;
797
- }
800
+ continue ;
798
801
}
799
802
case ModuleSearchPathKind::Framework:
800
803
case ModuleSearchPathKind::DarwinImplicitFramework: {
0 commit comments