Skip to content

[5.4][ClangImporter] Load the stdlib even if there is a -fmodule-map-file argument pointing to a missing file #37481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsClangImporter.def
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,7 @@ WARNING(implicit_bridging_header_imported_from_module,none,
"is deprecated and will be removed in a later version of Swift",
(StringRef, Identifier))

ERROR(module_map_not_found, none, "module map file '%0' not found", (StringRef))

#define UNDEFINE_DIAGNOSTIC_MACROS
#include "DefineDiagnosticMacros.h"
34 changes: 32 additions & 2 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,38 @@ ClangImporter::createClangInvocation(ClangImporter *importer,
&tempDiagClient,
/*owned*/false);

return clang::createInvocationFromCommandLine(invocationArgs, tempClangDiags,
nullptr, false, CC1Args);
auto CI = clang::createInvocationFromCommandLine(
invocationArgs, tempClangDiags, nullptr, false, CC1Args);

if (!CI) {
return CI;
}

// FIXME: clang fails to generate a module if there is a `-fmodule-map-file`
// argument pointing to a missing file.
// Such missing module files occur frequently in SourceKit. If the files are
// missing, SourceKit fails to build SwiftShims (which wouldn't have required
// the missing module file), thus fails to load the stdlib and hence looses
// all semantic functionality.
// To work around this issue, drop all `-fmodule-map-file` arguments pointing
// to missing files and report the error that clang would throw manually.
// rdar://77516546 is tracking that the clang importer should be more
// resilient and provide a module even if there were building it.
auto VFS = clang::createVFSFromCompilerInvocation(
*CI, *tempClangDiags,
importer->Impl.SwiftContext.SourceMgr.getFileSystem());
std::vector<std::string> FilteredModuleMapFiles;
for (auto ModuleMapFile : CI->getFrontendOpts().ModuleMapFiles) {
if (VFS->exists(ModuleMapFile)) {
FilteredModuleMapFiles.push_back(ModuleMapFile);
} else {
importer->Impl.SwiftContext.Diags.diagnose(
SourceLoc(), diag::module_map_not_found, ModuleMapFile);
}
}
CI->getFrontendOpts().ModuleMapFiles = FilteredModuleMapFiles;

return CI;
}

std::unique_ptr<ClangImporter>
Expand Down
9 changes: 9 additions & 0 deletions test/SourceKit/CursorInfo/missing_module_map.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %empty-directory(%t.mcp)
// RUN: %sourcekitd-test -req=cursor -pos=6:9 %s -- -Xcc -fmodule-map-file=/some/missing/file -module-cache-path %t.mcp %s | %FileCheck %s

// We used to fail to load the stdlib if there is a `-fmodule-map-file` option pointing to a missing file

let x: String = "abc"
// CHECK: source.lang.swift.ref.struct ()
// CHECK: String
// CHECK: s:SS