From d3ec0a233785fb21c1b65c7918e55988c99d0f89 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Wed, 15 Oct 2025 10:30:25 -0700 Subject: [PATCH 1/2] WIP --- lib/ClangImporter/ClangImporter.cpp | 54 ++++++++++++----------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index 995932bdb0118..2ca9c30a1140d 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -1015,7 +1015,9 @@ bool ClangImporter::canReadPCH(StringRef PCHFilename) { // Note: Reusing the file manager is safe; this is a component that's already // reused when building PCM files for the module cache. - CI.createSourceManager(Impl.Instance->getFileManager()); + CI.setVirtualFileSystem(Impl.Instance->getVirtualFileSystem()); + CI.setFileManager(Impl.Instance->getFileManager()); + CI.createSourceManager(); auto &clangSrcMgr = CI.getSourceManager(); auto FID = clangSrcMgr.createFileID( std::make_unique(1, "
")); @@ -1394,21 +1396,15 @@ std::unique_ptr ClangImporter::create( if (tracker) instance.addDependencyCollector(tracker->getClangCollector()); - { - // Now set up the real client for Clang diagnostics---configured with proper - // options---as opposed to the temporary one we made above. - auto actualDiagClient = std::make_unique( - importer->Impl, instance.getDiagnosticOpts(), - importerOpts.DumpClangDiagnostics); - instance.createDiagnostics(*VFS, actualDiagClient.release()); - } - - // Set up the file manager. - { - VFS = clang::createVFSFromCompilerInvocation( - instance.getInvocation(), instance.getDiagnostics(), std::move(VFS)); - instance.createFileManager(VFS); - } + // Now set up the real client for Clang diagnostics---configured with proper + // options---as opposed to the temporary one we made above. + auto actualDiagClient = std::make_unique( + importer->Impl, instance.getDiagnosticOpts(), + importerOpts.DumpClangDiagnostics); + + instance.createVirtualFileSystem(std::move(VFS), actualDiagClient.get()); + instance.createFileManager(); + instance.createDiagnostics(actualDiagClient.release()); // Don't stop emitting messages if we ever can't load a module. // FIXME: This is actually a general problem: any "fatal" error could mess up @@ -1427,11 +1423,11 @@ std::unique_ptr ClangImporter::create( if (ctx.LangOpts.ClangTarget.has_value()) { // If '-clang-target' is set, create a mock invocation with the Swift triple // to configure CodeGen and Target options for Swift compilation. - auto swiftTargetClangArgs = importer->getClangCC1Arguments(ctx, VFS, true); + auto swiftTargetClangArgs = importer->getClangCC1Arguments(ctx, instance.getVirtualFileSystemPtr(), true); if (!swiftTargetClangArgs) return nullptr; auto swiftTargetClangInvocation = createClangInvocation( - importer.get(), importerOpts, VFS, *swiftTargetClangArgs); + importer.get(), importerOpts, instance.getVirtualFileSystemPtr(), *swiftTargetClangArgs); if (!swiftTargetClangInvocation) return nullptr; @@ -1916,15 +1912,13 @@ std::string ClangImporter::getBridgingHeaderContents( invocation->getPreprocessorOpts().resetNonModularOptions(); - clang::FileManager &fileManager = Impl.Instance->getFileManager(); - clang::CompilerInstance rewriteInstance( std::move(invocation), Impl.Instance->getPCHContainerOperations(), &Impl.Instance->getModuleCache()); - rewriteInstance.createDiagnostics(fileManager.getVirtualFileSystem(), - new clang::IgnoringDiagConsumer); - rewriteInstance.setFileManager(&fileManager); - rewriteInstance.createSourceManager(fileManager); + rewriteInstance.setVirtualFileSystem(Impl.Instance->getVirtualFileSystem()); + rewriteInstance.setFileManager(Impl.Instance->getFileSystem()); + rewriteInstance.createDiagnostics(new clang::IgnoringDiagConsumer); + rewriteInstance.createSourceManager(); rewriteInstance.setTarget(&Impl.Instance->getTarget()); std::string result; @@ -1972,7 +1966,7 @@ std::string ClangImporter::getBridgingHeaderContents( return ""; } - if (auto fileInfo = fileManager.getOptionalFileRef(headerPath)) { + if (auto fileInfo = rewriteInstance.getFileManager().getOptionalFileRef(headerPath)) { fileSize = fileInfo->getSize(); fileModTime = fileInfo->getModificationTime(); } @@ -2021,16 +2015,14 @@ ClangImporter::cloneCompilerInstanceForPrecompiling() { // Share the CASOption and the underlying CAS. invocation->setCASOption(Impl.Invocation->getCASOptsPtr()); - clang::FileManager &fileManager = Impl.Instance->getFileManager(); - auto clonedInstance = std::make_unique( std::move(invocation), Impl.Instance->getPCHContainerOperations(), &Impl.Instance->getModuleCache()); - clonedInstance->createDiagnostics(fileManager.getVirtualFileSystem(), - &Impl.Instance->getDiagnosticClient(), + clonedInstance->setVirtualFileSystem(Impl.Instance->getVirtualFileSystem()); + clonedInstance->setFileManager(Impl.Instance->getFileSystem()); + clonedInstance->createDiagnostics(&Impl.Instance->getDiagnosticClient(), /*ShouldOwnClient=*/false); - clonedInstance->setFileManager(&fileManager); - clonedInstance->createSourceManager(fileManager); + clonedInstance->createSourceManager(); clonedInstance->setTarget(&Impl.Instance->getTarget()); clonedInstance->setOutputBackend(Impl.SwiftContext.OutputBackend); From 26683477f075d5ecc586b30c1c9a292fa58e8a38 Mon Sep 17 00:00:00 2001 From: Qiongsi Wu Date: Wed, 15 Oct 2025 12:28:37 -0700 Subject: [PATCH 2/2] Fix build failure due to clang changes. --- lib/ClangImporter/ClangImporter.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index 2ca9c30a1140d..7d6866d1aac45 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -1015,8 +1015,8 @@ bool ClangImporter::canReadPCH(StringRef PCHFilename) { // Note: Reusing the file manager is safe; this is a component that's already // reused when building PCM files for the module cache. - CI.setVirtualFileSystem(Impl.Instance->getVirtualFileSystem()); - CI.setFileManager(Impl.Instance->getFileManager()); + CI.setVirtualFileSystem(Impl.Instance->getVirtualFileSystemPtr()); + CI.setFileManager(Impl.Instance->getFileManagerPtr()); CI.createSourceManager(); auto &clangSrcMgr = CI.getSourceManager(); auto FID = clangSrcMgr.createFileID( @@ -1915,8 +1915,8 @@ std::string ClangImporter::getBridgingHeaderContents( clang::CompilerInstance rewriteInstance( std::move(invocation), Impl.Instance->getPCHContainerOperations(), &Impl.Instance->getModuleCache()); - rewriteInstance.setVirtualFileSystem(Impl.Instance->getVirtualFileSystem()); - rewriteInstance.setFileManager(Impl.Instance->getFileSystem()); + rewriteInstance.setVirtualFileSystem(Impl.Instance->getVirtualFileSystemPtr()); + rewriteInstance.setFileManager(Impl.Instance->getFileManagerPtr()); rewriteInstance.createDiagnostics(new clang::IgnoringDiagConsumer); rewriteInstance.createSourceManager(); rewriteInstance.setTarget(&Impl.Instance->getTarget()); @@ -2018,8 +2018,8 @@ ClangImporter::cloneCompilerInstanceForPrecompiling() { auto clonedInstance = std::make_unique( std::move(invocation), Impl.Instance->getPCHContainerOperations(), &Impl.Instance->getModuleCache()); - clonedInstance->setVirtualFileSystem(Impl.Instance->getVirtualFileSystem()); - clonedInstance->setFileManager(Impl.Instance->getFileSystem()); + clonedInstance->setVirtualFileSystem(Impl.Instance->getVirtualFileSystemPtr()); + clonedInstance->setFileManager(Impl.Instance->getFileManagerPtr()); clonedInstance->createDiagnostics(&Impl.Instance->getDiagnosticClient(), /*ShouldOwnClient=*/false); clonedInstance->createSourceManager();