Skip to content

Commit bace89f

Browse files
committed
[ClangImporter] Just turn off the warning, not the actual overlays.
We should still try adding the overlays, even if we're asked not to generate a diagnostic while doing so. That's slightly safer because it means that we're less likely to find ourselves in a situation where `swift-modulewrap` wants to use types from the C/C++ library and can't. rdar://115918181
1 parent 6e48b1d commit bace89f

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

include/swift/ClangImporter/ClangImporter.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,9 +727,13 @@ struct ClangInvocationFileMapping {
727727
/// On Linux, some platform libraries (glibc, libstdc++) are not modularized.
728728
/// We inject modulemaps for those libraries into their include directories
729729
/// to allow using them from Swift.
730+
///
731+
/// `suppressDiagnostic` prevents us from emitting warning messages when we
732+
/// are unable to find headers.
730733
ClangInvocationFileMapping getClangInvocationFileMapping(
731734
ASTContext &ctx,
732-
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs = nullptr);
735+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs = nullptr,
736+
bool suppressDiagnostic = false);
733737

734738
} // end namespace swift
735739

lib/ClangImporter/ClangImporter.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,10 +1297,8 @@ ClangImporter::create(ASTContext &ctx,
12971297
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
12981298
ctx.SourceMgr.getFileSystem();
12991299

1300-
ClangInvocationFileMapping fileMapping;
1301-
1302-
if (!ignoreFileMapping)
1303-
fileMapping = getClangInvocationFileMapping(ctx);
1300+
ClangInvocationFileMapping fileMapping =
1301+
getClangInvocationFileMapping(ctx, nullptr, ignoreFileMapping);
13041302

13051303
// Avoid creating indirect file system when using include tree.
13061304
if (!ctx.ClangImporterOpts.HasClangIncludeTreeRoot) {

lib/ClangImporter/ClangIncludePaths.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ ClangImporter::createClangArgs(const ClangImporterOptions &ClangImporterOpts,
189189
static SmallVector<std::pair<std::string, std::string>, 2>
190190
getLibcFileMapping(ASTContext &ctx, StringRef modulemapFileName,
191191
std::optional<ArrayRef<StringRef>> maybeHeaderFileNames,
192-
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
192+
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs,
193+
bool suppressDiagnostic) {
193194
const llvm::Triple &triple = ctx.LangOpts.Target;
194195

195196
// Extract the libc path from Clang driver.
@@ -213,7 +214,8 @@ getLibcFileMapping(ASTContext &ctx, StringRef modulemapFileName,
213214
parsedIncludeArgs, {"inttypes.h", "unistd.h", "stdint.h"}, vfs)) {
214215
libcDir = dir.value();
215216
} else {
216-
ctx.Diags.diagnose(SourceLoc(), diag::libc_not_found, triple.str());
217+
if (!suppressDiagnostic)
218+
ctx.Diags.diagnose(SourceLoc(), diag::libc_not_found, triple.str());
217219
return {};
218220
}
219221

@@ -251,7 +253,8 @@ getLibcFileMapping(ASTContext &ctx, StringRef modulemapFileName,
251253

252254
static void getLibStdCxxFileMapping(
253255
ClangInvocationFileMapping &fileMapping, ASTContext &ctx,
254-
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
256+
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs,
257+
bool suppressDiagnostic) {
255258
assert(ctx.LangOpts.EnableCXXInterop &&
256259
"libstdc++ is only injected if C++ interop is enabled");
257260

@@ -286,7 +289,8 @@ static void getLibStdCxxFileMapping(
286289
{"cstdlib", "string", "vector"}, vfs)) {
287290
cxxStdlibDir = dir.value();
288291
} else {
289-
ctx.Diags.diagnose(SourceLoc(), diag::libstdcxx_not_found, triple.str());
292+
if (!suppressDiagnostic)
293+
ctx.Diags.diagnose(SourceLoc(), diag::libstdcxx_not_found, triple.str());
290294
return;
291295
}
292296

@@ -541,7 +545,8 @@ SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
541545
} // namespace
542546

543547
ClangInvocationFileMapping swift::getClangInvocationFileMapping(
544-
ASTContext &ctx, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs) {
548+
ASTContext &ctx, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs,
549+
bool suppressDiagnostic) {
545550
ClangInvocationFileMapping result;
546551
if (!vfs)
547552
vfs = llvm::vfs::getRealFileSystem();
@@ -571,18 +576,21 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
571576
if (triple.isOSWASI()) {
572577
// WASI Mappings
573578
libcFileMapping =
574-
getLibcFileMapping(ctx, "wasi-libc.modulemap", std::nullopt, vfs);
579+
getLibcFileMapping(ctx, "wasi-libc.modulemap", std::nullopt, vfs,
580+
suppressDiagnostic);
575581

576582
// WASI's module map needs fixing
577583
result.requiresBuiltinHeadersInSystemModules = true;
578584
} else if (triple.isMusl()) {
579585
libcFileMapping =
580-
getLibcFileMapping(ctx, "musl.modulemap", StringRef("SwiftMusl.h"), vfs);
586+
getLibcFileMapping(ctx, "musl.modulemap", StringRef("SwiftMusl.h"), vfs,
587+
suppressDiagnostic);
581588
} else if (triple.isAndroid()) {
582589
// Android uses the android-specific module map that overlays the NDK.
583590
StringRef headerFiles[] = {"SwiftAndroidNDK.h", "SwiftBionic.h"};
584591
libcFileMapping =
585-
getLibcFileMapping(ctx, "android.modulemap", headerFiles, vfs);
592+
getLibcFileMapping(ctx, "android.modulemap", headerFiles, vfs,
593+
suppressDiagnostic);
586594

587595
if (!libcFileMapping.empty()) {
588596
sysroot = libcFileMapping[0].first;
@@ -592,15 +600,16 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
592600
triple.isOSFreeBSD()) {
593601
// BSD/Linux Mappings
594602
libcFileMapping = getLibcFileMapping(ctx, "glibc.modulemap",
595-
StringRef("SwiftGlibc.h"), vfs);
603+
StringRef("SwiftGlibc.h"), vfs,
604+
suppressDiagnostic);
596605

597606
// glibc.modulemap needs fixing
598607
result.requiresBuiltinHeadersInSystemModules = true;
599608
}
600609
result.redirectedFiles.append(libcFileMapping);
601610

602611
if (ctx.LangOpts.EnableCXXInterop)
603-
getLibStdCxxFileMapping(result, ctx, vfs);
612+
getLibStdCxxFileMapping(result, ctx, vfs, suppressDiagnostic);
604613

605614
result.redirectedFiles.append(GetWindowsFileMappings(
606615
ctx, vfs, result.requiresBuiltinHeadersInSystemModules));

0 commit comments

Comments
 (0)