Skip to content

[ClangImporter][ModuleWrap] Turn off libc warnings. #76545

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 2 commits into from
Oct 1, 2024
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
9 changes: 7 additions & 2 deletions include/swift/ClangImporter/ClangImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ class ClangImporter final : public ClangModuleLoader {
static std::unique_ptr<ClangImporter>
create(ASTContext &ctx,
std::string swiftPCHHash = "", DependencyTracker *tracker = nullptr,
DWARFImporterDelegate *dwarfImporterDelegate = nullptr);
DWARFImporterDelegate *dwarfImporterDelegate = nullptr,
bool ignoreFileMapping = false);

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

} // end namespace swift

Expand Down
7 changes: 5 additions & 2 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,8 @@ std::unique_ptr<clang::CompilerInvocation> ClangImporter::createClangInvocation(
std::unique_ptr<ClangImporter>
ClangImporter::create(ASTContext &ctx,
std::string swiftPCHHash, DependencyTracker *tracker,
DWARFImporterDelegate *dwarfImporterDelegate) {
DWARFImporterDelegate *dwarfImporterDelegate,
bool ignoreFileMapping) {
std::unique_ptr<ClangImporter> importer{
new ClangImporter(ctx, tracker, dwarfImporterDelegate)};
auto &importerOpts = ctx.ClangImporterOpts;
Expand All @@ -1296,7 +1297,9 @@ ClangImporter::create(ASTContext &ctx,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
ctx.SourceMgr.getFileSystem();

auto fileMapping = getClangInvocationFileMapping(ctx);
ClangInvocationFileMapping fileMapping =
getClangInvocationFileMapping(ctx, nullptr, ignoreFileMapping);

// Avoid creating indirect file system when using include tree.
if (!ctx.ClangImporterOpts.HasClangIncludeTreeRoot) {
// Wrap Swift's FS to allow Clang to override the working directory
Expand Down
29 changes: 19 additions & 10 deletions lib/ClangImporter/ClangIncludePaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ ClangImporter::createClangArgs(const ClangImporterOptions &ClangImporterOpts,
static SmallVector<std::pair<std::string, std::string>, 2>
getLibcFileMapping(ASTContext &ctx, StringRef modulemapFileName,
std::optional<ArrayRef<StringRef>> maybeHeaderFileNames,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs,
bool suppressDiagnostic) {
const llvm::Triple &triple = ctx.LangOpts.Target;

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

Expand Down Expand Up @@ -251,7 +253,8 @@ getLibcFileMapping(ASTContext &ctx, StringRef modulemapFileName,

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

Expand Down Expand Up @@ -286,7 +289,8 @@ static void getLibStdCxxFileMapping(
{"cstdlib", "string", "vector"}, vfs)) {
cxxStdlibDir = dir.value();
} else {
ctx.Diags.diagnose(SourceLoc(), diag::libstdcxx_not_found, triple.str());
if (!suppressDiagnostic)
ctx.Diags.diagnose(SourceLoc(), diag::libstdcxx_not_found, triple.str());
return;
}

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

ClangInvocationFileMapping swift::getClangInvocationFileMapping(
ASTContext &ctx, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs) {
ASTContext &ctx, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs,
bool suppressDiagnostic) {
ClangInvocationFileMapping result;
if (!vfs)
vfs = llvm::vfs::getRealFileSystem();
Expand Down Expand Up @@ -571,18 +576,21 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
if (triple.isOSWASI()) {
// WASI Mappings
libcFileMapping =
getLibcFileMapping(ctx, "wasi-libc.modulemap", std::nullopt, vfs);
getLibcFileMapping(ctx, "wasi-libc.modulemap", std::nullopt, vfs,
suppressDiagnostic);

// WASI's module map needs fixing
result.requiresBuiltinHeadersInSystemModules = true;
} else if (triple.isMusl()) {
libcFileMapping =
getLibcFileMapping(ctx, "musl.modulemap", StringRef("SwiftMusl.h"), vfs);
getLibcFileMapping(ctx, "musl.modulemap", StringRef("SwiftMusl.h"), vfs,
suppressDiagnostic);
} else if (triple.isAndroid()) {
// Android uses the android-specific module map that overlays the NDK.
StringRef headerFiles[] = {"SwiftAndroidNDK.h", "SwiftBionic.h"};
libcFileMapping =
getLibcFileMapping(ctx, "android.modulemap", headerFiles, vfs);
getLibcFileMapping(ctx, "android.modulemap", headerFiles, vfs,
suppressDiagnostic);

if (!libcFileMapping.empty()) {
sysroot = libcFileMapping[0].first;
Expand All @@ -592,15 +600,16 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
triple.isOSFreeBSD()) {
// BSD/Linux Mappings
libcFileMapping = getLibcFileMapping(ctx, "glibc.modulemap",
StringRef("SwiftGlibc.h"), vfs);
StringRef("SwiftGlibc.h"), vfs,
suppressDiagnostic);

// glibc.modulemap needs fixing
result.requiresBuiltinHeadersInSystemModules = true;
}
result.redirectedFiles.append(libcFileMapping);

if (ctx.LangOpts.EnableCXXInterop)
getLibStdCxxFileMapping(result, ctx, vfs);
getLibStdCxxFileMapping(result, ctx, vfs, suppressDiagnostic);

result.redirectedFiles.append(GetWindowsFileMappings(
ctx, vfs, result.requiresBuiltinHeadersInSystemModules));
Expand Down
7 changes: 5 additions & 2 deletions lib/DriverTool/modulewrap_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,11 @@ int modulewrap_main(ArrayRef<const char *> Args, const char *Argv0,
llvm::makeIntrusiveRefCnt<llvm::vfs::OnDiskOutputBackend>());
registerParseRequestFunctions(ASTCtx.evaluator);
registerTypeCheckerRequestFunctions(ASTCtx.evaluator);

ASTCtx.addModuleLoader(ClangImporter::create(ASTCtx, ""), true);

ASTCtx.addModuleLoader(ClangImporter::create(ASTCtx, "",
nullptr, nullptr,
true),
true);
ModuleDecl *M = ModuleDecl::create(ASTCtx.getIdentifier("swiftmodule"), ASTCtx);
std::unique_ptr<Lowering::TypeConverter> TC(
new Lowering::TypeConverter(*M, ASTCtx.SILOpts.EnableSILOpaqueValues));
Expand Down