@@ -197,7 +197,7 @@ ClangImporter::createClangArgs(const ClangImporterOptions &ClangImporterOpts,
197197}
198198
199199static SmallVector<std::pair<std::string, std::string>, 2 >
200- getLibcFileMapping (ASTContext &ctx, StringRef modulemapFileName,
200+ getLibcFileMapping (const ASTContext &ctx, StringRef modulemapFileName,
201201 std::optional<ArrayRef<StringRef>> maybeHeaderFileNames,
202202 const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs,
203203 bool suppressDiagnostic) {
@@ -263,7 +263,7 @@ getLibcFileMapping(ASTContext &ctx, StringRef modulemapFileName,
263263}
264264
265265static void getLibStdCxxFileMapping (
266- ClangInvocationFileMapping &fileMapping, ASTContext &ctx,
266+ ClangInvocationFileMapping &fileMapping, const ASTContext &ctx,
267267 const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs,
268268 bool suppressDiagnostic) {
269269 assert (ctx.LangOpts .EnableCXXInterop &&
@@ -454,7 +454,7 @@ GetPlatformAuxiliaryFile(StringRef Platform, StringRef File,
454454}
455455
456456void GetWindowsFileMappings (
457- ClangInvocationFileMapping &fileMapping, ASTContext &Context,
457+ ClangInvocationFileMapping &fileMapping, const ASTContext &Context,
458458 const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &driverVFS,
459459 bool &requiresBuiltinHeadersInSystemModules) {
460460 const llvm::Triple &Triple = Context.LangOpts .Target ;
@@ -585,7 +585,7 @@ void GetWindowsFileMappings(
585585} // namespace
586586
587587ClangInvocationFileMapping swift::getClangInvocationFileMapping (
588- ASTContext &ctx, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs,
588+ const ASTContext &ctx, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs,
589589 bool suppressDiagnostic) {
590590 ClangInvocationFileMapping result;
591591 if (!vfs)
@@ -655,3 +655,52 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
655655 result.requiresBuiltinHeadersInSystemModules );
656656 return result;
657657}
658+
659+ ClangInvocationFileMapping swift::applyClangInvocationMapping (const ASTContext &ctx,
660+ llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> baseVFS,
661+ llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &fileSystem,
662+ bool suppressDiagnostics) {
663+ if (ctx.CASOpts .HasImmutableFileSystem )
664+ return ClangInvocationFileMapping ();
665+
666+ ClangInvocationFileMapping fileMapping =
667+ getClangInvocationFileMapping (ctx, baseVFS, suppressDiagnostics);
668+
669+ auto importerOpts = ctx.ClangImporterOpts ;
670+ // Wrap Swift's FS to allow Clang to override the working directory
671+ fileSystem = llvm::vfs::RedirectingFileSystem::create (
672+ fileMapping.redirectedFiles , true , *fileSystem);
673+ if (importerOpts.DumpClangDiagnostics ) {
674+ llvm::errs () << " clang importer redirected file mappings:\n " ;
675+ for (const auto &mapping : fileMapping.redirectedFiles ) {
676+ llvm::errs () << " mapping real file '" << mapping.second
677+ << " ' to virtual file '" << mapping.first << " '\n " ;
678+ }
679+ llvm::errs () << " \n " ;
680+ }
681+
682+ if (!fileMapping.overridenFiles .empty ()) {
683+ llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> overridenVFS =
684+ new llvm::vfs::InMemoryFileSystem ();
685+ for (const auto &file : fileMapping.overridenFiles ) {
686+ if (importerOpts.DumpClangDiagnostics ) {
687+ llvm::errs () << " clang importer overriding file '" << file.first
688+ << " ' with the following contents:\n " ;
689+ llvm::errs () << file.second << " \n " ;
690+ }
691+ auto contents = ctx.Allocate <char >(file.second .size () + 1 );
692+ std::copy (file.second .begin (), file.second .end (), contents.begin ());
693+ // null terminate the buffer.
694+ contents[contents.size () - 1 ] = ' \0 ' ;
695+ overridenVFS->addFile (file.first , 0 ,
696+ llvm::MemoryBuffer::getMemBuffer (StringRef (
697+ contents.begin (), contents.size () - 1 )));
698+ }
699+ llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> overlayVFS =
700+ new llvm::vfs::OverlayFileSystem (fileSystem);
701+ fileSystem = overlayVFS;
702+ overlayVFS->pushOverlay (overridenVFS);
703+ }
704+
705+ return fileMapping;
706+ }
0 commit comments