6363#include " llvm/ADT/StringExtras.h"
6464#include " llvm/Support/CrashRecoveryContext.h"
6565#include " llvm/Support/FileCollector.h"
66+ #include " llvm/Support/FileSystem.h"
6667#include " llvm/Support/Memory.h"
6768#include " llvm/Support/Path.h"
6869#include " llvm/Support/YAMLTraits.h"
@@ -971,11 +972,11 @@ ClangImporter::getClangArguments(ASTContext &ctx) {
971972 return invocationArgStrs;
972973}
973974
974- std::unique_ptr<clang::CompilerInvocation>
975- ClangImporter::createClangInvocation (ClangImporter *importer,
976- const ClangImporterOptions &importerOpts ,
977- ArrayRef<std::string> invocationArgStrs,
978- std::vector<std::string> *CC1Args) {
975+ std::unique_ptr<clang::CompilerInvocation> ClangImporter::createClangInvocation (
976+ ClangImporter *importer, const ClangImporterOptions &importerOpts ,
977+ llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS ,
978+ ArrayRef<std::string> invocationArgStrs,
979+ std::vector<std::string> *CC1Args) {
979980 std::vector<const char *> invocationArgs;
980981 invocationArgs.reserve (invocationArgStrs.size ());
981982 for (auto &argStr : invocationArgStrs)
@@ -1000,7 +1001,7 @@ ClangImporter::createClangInvocation(ClangImporter *importer,
10001001 /* owned*/ false );
10011002
10021003 auto CI = clang::createInvocationFromCommandLine (
1003- invocationArgs, tempClangDiags, nullptr , false , CC1Args);
1004+ invocationArgs, tempClangDiags, VFS , false , CC1Args);
10041005
10051006 if (!CI) {
10061007 return CI;
@@ -1016,12 +1017,12 @@ ClangImporter::createClangInvocation(ClangImporter *importer,
10161017 // to missing files and report the error that clang would throw manually.
10171018 // rdar://77516546 is tracking that the clang importer should be more
10181019 // resilient and provide a module even if there were building it.
1019- auto VFS = clang::createVFSFromCompilerInvocation (
1020+ auto TempVFS = clang::createVFSFromCompilerInvocation (
10201021 *CI, *tempClangDiags,
1021- importer->Impl .SwiftContext .SourceMgr .getFileSystem ());
1022+ VFS ? VFS : importer->Impl .SwiftContext .SourceMgr .getFileSystem ());
10221023 std::vector<std::string> FilteredModuleMapFiles;
10231024 for (auto ModuleMapFile : CI->getFrontendOpts ().ModuleMapFiles ) {
1024- if (VFS ->exists (ModuleMapFile)) {
1025+ if (TempVFS ->exists (ModuleMapFile)) {
10251026 FilteredModuleMapFiles.push_back (ModuleMapFile);
10261027 } else {
10271028 importer->Impl .diagnose (SourceLoc (), diag::module_map_not_found,
@@ -1066,11 +1067,15 @@ ClangImporter::create(ASTContext &ctx,
10661067 }
10671068 }
10681069
1070+ // Wrap Swift's FS to allow Clang to override the working directory
1071+ llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
1072+ llvm::vfs::RedirectingFileSystem::create ({}, true ,
1073+ *ctx.SourceMgr .getFileSystem ());
1074+
10691075 // Create a new Clang compiler invocation.
10701076 {
1071- importer->Impl .Invocation = createClangInvocation (importer.get (),
1072- importerOpts,
1073- invocationArgStrs);
1077+ importer->Impl .Invocation = createClangInvocation (
1078+ importer.get (), importerOpts, VFS, invocationArgStrs);
10741079 if (!importer->Impl .Invocation )
10751080 return nullptr ;
10761081 }
@@ -1122,11 +1127,9 @@ ClangImporter::create(ASTContext &ctx,
11221127
11231128 // Set up the file manager.
11241129 {
1125- llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
1126- clang::createVFSFromCompilerInvocation (instance.getInvocation (),
1127- instance.getDiagnostics (),
1128- ctx.SourceMgr .getFileSystem ());
1129- instance.createFileManager (std::move (VFS));
1130+ VFS = clang::createVFSFromCompilerInvocation (
1131+ instance.getInvocation (), instance.getDiagnostics (), std::move (VFS));
1132+ instance.createFileManager (VFS);
11301133 }
11311134
11321135 // Don't stop emitting messages if we ever can't load a module.
0 commit comments