Skip to content

Commit c417464

Browse files
authored
Merge pull request swiftlang#37252 from ahoppen/pr/missing-module-map
[ClangImporter] Load the stdlib even if there is a `-fmodule-map-file` argument pointing to a missing file
2 parents 4c0e67c + aef9d51 commit c417464

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

include/swift/AST/DiagnosticsClangImporter.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,7 @@ WARNING(import_multiple_mainactor_attr,none,
8787
"this attribute for global actor '%0' is invalid; the declaration already has attribute for global actor '%1'",
8888
(StringRef, StringRef))
8989

90+
ERROR(module_map_not_found, none, "module map file '%0' not found", (StringRef))
91+
9092
#define UNDEFINE_DIAGNOSTIC_MACROS
9193
#include "DefineDiagnosticMacros.h"

lib/ClangImporter/ClangImporter.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,8 +995,38 @@ ClangImporter::createClangInvocation(ClangImporter *importer,
995995
&tempDiagClient,
996996
/*owned*/false);
997997

998-
return clang::createInvocationFromCommandLine(invocationArgs, tempClangDiags,
999-
nullptr, false, CC1Args);
998+
auto CI = clang::createInvocationFromCommandLine(
999+
invocationArgs, tempClangDiags, nullptr, false, CC1Args);
1000+
1001+
if (!CI) {
1002+
return CI;
1003+
}
1004+
1005+
// FIXME: clang fails to generate a module if there is a `-fmodule-map-file`
1006+
// argument pointing to a missing file.
1007+
// Such missing module files occur frequently in SourceKit. If the files are
1008+
// missing, SourceKit fails to build SwiftShims (which wouldn't have required
1009+
// the missing module file), thus fails to load the stdlib and hence looses
1010+
// all semantic functionality.
1011+
// To work around this issue, drop all `-fmodule-map-file` arguments pointing
1012+
// to missing files and report the error that clang would throw manually.
1013+
// rdar://77516546 is tracking that the clang importer should be more
1014+
// resilient and provide a module even if there were building it.
1015+
auto VFS = clang::createVFSFromCompilerInvocation(
1016+
*CI, *tempClangDiags,
1017+
importer->Impl.SwiftContext.SourceMgr.getFileSystem());
1018+
std::vector<std::string> FilteredModuleMapFiles;
1019+
for (auto ModuleMapFile : CI->getFrontendOpts().ModuleMapFiles) {
1020+
if (VFS->exists(ModuleMapFile)) {
1021+
FilteredModuleMapFiles.push_back(ModuleMapFile);
1022+
} else {
1023+
importer->Impl.diagnose(SourceLoc(), diag::module_map_not_found,
1024+
ModuleMapFile);
1025+
}
1026+
}
1027+
CI->getFrontendOpts().ModuleMapFiles = FilteredModuleMapFiles;
1028+
1029+
return CI;
10001030
}
10011031

10021032
std::unique_ptr<ClangImporter>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %empty-directory(%t.mcp)
2+
// RUN: %sourcekitd-test -req=cursor -pos=6:9 %s -- -Xcc -fmodule-map-file=/some/missing/file -module-cache-path %t.mcp %s | %FileCheck %s
3+
4+
// We used to fail to load the stdlib if there is a `-fmodule-map-file` option pointing to a missing file
5+
6+
let x: String = "abc"
7+
// CHECK: source.lang.swift.ref.struct ()
8+
// CHECK: String
9+
// CHECK: s:SS

0 commit comments

Comments
 (0)