Merge remote-tracking branch 'origin/main' into rebranch

This commit is contained in:
swift-ci
2025-05-29 18:36:55 -07:00
7 changed files with 80 additions and 55 deletions

View File

@@ -200,7 +200,7 @@ ClangImporter::createClangArgs(const ClangImporterOptions &ClangImporterOpts,
}
static SmallVector<std::pair<std::string, std::string>, 2>
getLibcFileMapping(ASTContext &ctx, StringRef modulemapFileName,
getLibcFileMapping(const ASTContext &ctx, StringRef modulemapFileName,
std::optional<ArrayRef<StringRef>> maybeHeaderFileNames,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs,
bool suppressDiagnostic) {
@@ -266,7 +266,7 @@ getLibcFileMapping(ASTContext &ctx, StringRef modulemapFileName,
}
static void getLibStdCxxFileMapping(
ClangInvocationFileMapping &fileMapping, ASTContext &ctx,
ClangInvocationFileMapping &fileMapping, const ASTContext &ctx,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs,
bool suppressDiagnostic) {
assert(ctx.LangOpts.EnableCXXInterop &&
@@ -457,7 +457,7 @@ GetPlatformAuxiliaryFile(StringRef Platform, StringRef File,
}
void GetWindowsFileMappings(
ClangInvocationFileMapping &fileMapping, ASTContext &Context,
ClangInvocationFileMapping &fileMapping, const ASTContext &Context,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &driverVFS,
bool &requiresBuiltinHeadersInSystemModules) {
const llvm::Triple &Triple = Context.LangOpts.Target;
@@ -588,7 +588,7 @@ void GetWindowsFileMappings(
} // namespace
ClangInvocationFileMapping swift::getClangInvocationFileMapping(
ASTContext &ctx, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs,
const ASTContext &ctx, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs,
bool suppressDiagnostic) {
ClangInvocationFileMapping result;
if (!vfs)
@@ -658,3 +658,52 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
result.requiresBuiltinHeadersInSystemModules);
return result;
}
ClangInvocationFileMapping swift::applyClangInvocationMapping(const ASTContext &ctx,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> baseVFS,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &fileSystem,
bool suppressDiagnostics) {
if (ctx.CASOpts.HasImmutableFileSystem)
return ClangInvocationFileMapping();
ClangInvocationFileMapping fileMapping =
getClangInvocationFileMapping(ctx, baseVFS, suppressDiagnostics);
auto importerOpts = ctx.ClangImporterOpts;
// Wrap Swift's FS to allow Clang to override the working directory
fileSystem = llvm::vfs::RedirectingFileSystem::create(
fileMapping.redirectedFiles, true, *fileSystem);
if (importerOpts.DumpClangDiagnostics) {
llvm::errs() << "clang importer redirected file mappings:\n";
for (const auto &mapping : fileMapping.redirectedFiles) {
llvm::errs() << " mapping real file '" << mapping.second
<< "' to virtual file '" << mapping.first << "'\n";
}
llvm::errs() << "\n";
}
if (!fileMapping.overridenFiles.empty()) {
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> overridenVFS =
new llvm::vfs::InMemoryFileSystem();
for (const auto &file : fileMapping.overridenFiles) {
if (importerOpts.DumpClangDiagnostics) {
llvm::errs() << "clang importer overriding file '" << file.first
<< "' with the following contents:\n";
llvm::errs() << file.second << "\n";
}
auto contents = ctx.Allocate<char>(file.second.size() + 1);
std::copy(file.second.begin(), file.second.end(), contents.begin());
// null terminate the buffer.
contents[contents.size() - 1] = '\0';
overridenVFS->addFile(file.first, 0,
llvm::MemoryBuffer::getMemBuffer(StringRef(
contents.begin(), contents.size() - 1)));
}
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> overlayVFS =
new llvm::vfs::OverlayFileSystem(fileSystem);
fileSystem = overlayVFS;
overlayVFS->pushOverlay(overridenVFS);
}
return fileMapping;
}