Skip to content

Commit ac750a6

Browse files
authored
Merge pull request #76545 from al45tair/eng/PR-115918181
[ClangImporter][ModuleWrap] Turn off libc warnings.
2 parents 106ab41 + bace89f commit ac750a6

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

include/swift/ClangImporter/ClangImporter.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ class ClangImporter final : public ClangModuleLoader {
188188
static std::unique_ptr<ClangImporter>
189189
create(ASTContext &ctx,
190190
std::string swiftPCHHash = "", DependencyTracker *tracker = nullptr,
191-
DWARFImporterDelegate *dwarfImporterDelegate = nullptr);
191+
DWARFImporterDelegate *dwarfImporterDelegate = nullptr,
192+
bool ignoreFileMapping = false);
192193

193194
static std::vector<std::string>
194195
getClangDriverArguments(ASTContext &ctx, bool ignoreClangTarget = false);
@@ -725,9 +726,13 @@ struct ClangInvocationFileMapping {
725726
/// On Linux, some platform libraries (glibc, libstdc++) are not modularized.
726727
/// We inject modulemaps for those libraries into their include directories
727728
/// to allow using them from Swift.
729+
///
730+
/// `suppressDiagnostic` prevents us from emitting warning messages when we
731+
/// are unable to find headers.
728732
ClangInvocationFileMapping getClangInvocationFileMapping(
729733
ASTContext &ctx,
730-
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs = nullptr);
734+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs = nullptr,
735+
bool suppressDiagnostic = false);
731736

732737
} // end namespace swift
733738

lib/ClangImporter/ClangImporter.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,8 @@ std::unique_ptr<clang::CompilerInvocation> ClangImporter::createClangInvocation(
12771277
std::unique_ptr<ClangImporter>
12781278
ClangImporter::create(ASTContext &ctx,
12791279
std::string swiftPCHHash, DependencyTracker *tracker,
1280-
DWARFImporterDelegate *dwarfImporterDelegate) {
1280+
DWARFImporterDelegate *dwarfImporterDelegate,
1281+
bool ignoreFileMapping) {
12811282
std::unique_ptr<ClangImporter> importer{
12821283
new ClangImporter(ctx, tracker, dwarfImporterDelegate)};
12831284
auto &importerOpts = ctx.ClangImporterOpts;
@@ -1298,7 +1299,9 @@ ClangImporter::create(ASTContext &ctx,
12981299
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
12991300
ctx.SourceMgr.getFileSystem();
13001301

1301-
auto fileMapping = getClangInvocationFileMapping(ctx);
1302+
ClangInvocationFileMapping fileMapping =
1303+
getClangInvocationFileMapping(ctx, nullptr, ignoreFileMapping);
1304+
13021305
// Avoid creating indirect file system when using include tree.
13031306
if (!ctx.ClangImporterOpts.HasClangIncludeTreeRoot) {
13041307
// Wrap Swift's FS to allow Clang to override the working directory

lib/ClangImporter/ClangIncludePaths.cpp

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

199200
// Extract the libc path from Clang driver.
@@ -217,7 +218,8 @@ getLibcFileMapping(ASTContext &ctx, StringRef modulemapFileName,
217218
parsedIncludeArgs, {"inttypes.h", "unistd.h", "stdint.h"}, vfs)) {
218219
libcDir = dir.value();
219220
} else {
220-
ctx.Diags.diagnose(SourceLoc(), diag::libc_not_found, triple.str());
221+
if (!suppressDiagnostic)
222+
ctx.Diags.diagnose(SourceLoc(), diag::libc_not_found, triple.str());
221223
return {};
222224
}
223225

@@ -255,7 +257,8 @@ getLibcFileMapping(ASTContext &ctx, StringRef modulemapFileName,
255257

256258
static void getLibStdCxxFileMapping(
257259
ClangInvocationFileMapping &fileMapping, ASTContext &ctx,
258-
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
260+
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs,
261+
bool suppressDiagnostic) {
259262
assert(ctx.LangOpts.EnableCXXInterop &&
260263
"libstdc++ is only injected if C++ interop is enabled");
261264

@@ -290,7 +293,8 @@ static void getLibStdCxxFileMapping(
290293
{"cstdlib", "string", "vector"}, vfs)) {
291294
cxxStdlibDir = dir.value();
292295
} else {
293-
ctx.Diags.diagnose(SourceLoc(), diag::libstdcxx_not_found, triple.str());
296+
if (!suppressDiagnostic)
297+
ctx.Diags.diagnose(SourceLoc(), diag::libstdcxx_not_found, triple.str());
294298
return;
295299
}
296300

@@ -545,7 +549,8 @@ SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
545549
} // namespace
546550

547551
ClangInvocationFileMapping swift::getClangInvocationFileMapping(
548-
ASTContext &ctx, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs) {
552+
ASTContext &ctx, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs,
553+
bool suppressDiagnostic) {
549554
ClangInvocationFileMapping result;
550555
if (!vfs)
551556
vfs = llvm::vfs::getRealFileSystem();
@@ -575,18 +580,21 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
575580
if (triple.isOSWASI()) {
576581
// WASI Mappings
577582
libcFileMapping =
578-
getLibcFileMapping(ctx, "wasi-libc.modulemap", std::nullopt, vfs);
583+
getLibcFileMapping(ctx, "wasi-libc.modulemap", std::nullopt, vfs,
584+
suppressDiagnostic);
579585

580586
// WASI's module map needs fixing
581587
result.requiresBuiltinHeadersInSystemModules = true;
582588
} else if (triple.isMusl()) {
583589
libcFileMapping =
584-
getLibcFileMapping(ctx, "musl.modulemap", StringRef("SwiftMusl.h"), vfs);
590+
getLibcFileMapping(ctx, "musl.modulemap", StringRef("SwiftMusl.h"), vfs,
591+
suppressDiagnostic);
585592
} else if (triple.isAndroid()) {
586593
// Android uses the android-specific module map that overlays the NDK.
587594
StringRef headerFiles[] = {"SwiftAndroidNDK.h", "SwiftBionic.h"};
588595
libcFileMapping =
589-
getLibcFileMapping(ctx, "android.modulemap", headerFiles, vfs);
596+
getLibcFileMapping(ctx, "android.modulemap", headerFiles, vfs,
597+
suppressDiagnostic);
590598

591599
if (!libcFileMapping.empty()) {
592600
sysroot = libcFileMapping[0].first;
@@ -596,15 +604,16 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
596604
triple.isOSFreeBSD()) {
597605
// BSD/Linux Mappings
598606
libcFileMapping = getLibcFileMapping(ctx, "glibc.modulemap",
599-
StringRef("SwiftGlibc.h"), vfs);
607+
StringRef("SwiftGlibc.h"), vfs,
608+
suppressDiagnostic);
600609

601610
// glibc.modulemap needs fixing
602611
result.requiresBuiltinHeadersInSystemModules = true;
603612
}
604613
result.redirectedFiles.append(libcFileMapping);
605614

606615
if (ctx.LangOpts.EnableCXXInterop)
607-
getLibStdCxxFileMapping(result, ctx, vfs);
616+
getLibStdCxxFileMapping(result, ctx, vfs, suppressDiagnostic);
608617

609618
result.redirectedFiles.append(GetWindowsFileMappings(
610619
ctx, vfs, result.requiresBuiltinHeadersInSystemModules));

lib/DriverTool/modulewrap_main.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,11 @@ int modulewrap_main(ArrayRef<const char *> Args, const char *Argv0,
196196
llvm::makeIntrusiveRefCnt<llvm::vfs::OnDiskOutputBackend>());
197197
registerParseRequestFunctions(ASTCtx.evaluator);
198198
registerTypeCheckerRequestFunctions(ASTCtx.evaluator);
199-
200-
ASTCtx.addModuleLoader(ClangImporter::create(ASTCtx, ""), true);
199+
200+
ASTCtx.addModuleLoader(ClangImporter::create(ASTCtx, "",
201+
nullptr, nullptr,
202+
true),
203+
true);
201204
ModuleDecl *M = ModuleDecl::create(ASTCtx.getIdentifier("swiftmodule"), ASTCtx);
202205
std::unique_ptr<Lowering::TypeConverter> TC(
203206
new Lowering::TypeConverter(*M, ASTCtx.SILOpts.EnableSILOpaqueValues));

0 commit comments

Comments
 (0)