From 09006611151c7f85862a9da8da34872c456c2c37 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Mon, 21 Apr 2025 11:09:23 -0700 Subject: [clang] Enable making `CompilerInstance` VFS thread-safe (#135737) The `llvm::vfs::FileSystem` interface makes no promises around thread-safety. To enable making `CompilerInstance` thread-safe, this PR allows passing an explicit VFS to `cloneForModuleCompile()`. This will be used from the dependency scanner. --- clang/lib/Frontend/CompilerInstance.cpp | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'clang/lib/Frontend/CompilerInstance.cpp') diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index 533cef3..bc663ac 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -1152,7 +1152,8 @@ static Language getLanguageFromOptions(const LangOptions &LangOpts) { std::unique_ptr CompilerInstance::cloneForModuleCompileImpl( SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input, - StringRef OriginalModuleMapFile, StringRef ModuleFileName) { + StringRef OriginalModuleMapFile, StringRef ModuleFileName, + IntrusiveRefCntPtr VFS) { // Construct a compiler invocation for creating this module. auto Invocation = std::make_shared(getInvocation()); @@ -1212,19 +1213,21 @@ std::unique_ptr CompilerInstance::cloneForModuleCompileImpl( auto &Inv = *Invocation; Instance.setInvocation(std::move(Invocation)); + if (VFS) { + Instance.createFileManager(std::move(VFS)); + } else if (FrontendOpts.ModulesShareFileManager) { + Instance.setFileManager(&getFileManager()); + } else { + Instance.createFileManager(&getVirtualFileSystem()); + } + Instance.createDiagnostics( - getVirtualFileSystem(), + Instance.getVirtualFileSystem(), new ForwardingDiagnosticConsumer(getDiagnosticClient()), /*ShouldOwnClient=*/true); - if (llvm::is_contained(DiagOpts.SystemHeaderWarningsModules, ModuleName)) Instance.getDiagnostics().setSuppressSystemWarnings(false); - if (FrontendOpts.ModulesShareFileManager) { - Instance.setFileManager(&getFileManager()); - } else { - Instance.createFileManager(&getVirtualFileSystem()); - } Instance.createSourceManager(Instance.getFileManager()); SourceManager &SourceMgr = Instance.getSourceManager(); @@ -1318,7 +1321,8 @@ static OptionalFileEntryRef getPublicModuleMap(FileEntryRef File, } std::unique_ptr CompilerInstance::cloneForModuleCompile( - SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName) { + SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName, + IntrusiveRefCntPtr VFS) { StringRef ModuleName = Module->getTopLevelModuleName(); InputKind IK(getLanguageFromOptions(getLangOpts()), InputKind::ModuleMap); @@ -1363,7 +1367,8 @@ std::unique_ptr CompilerInstance::cloneForModuleCompile( return cloneForModuleCompileImpl( ImportLoc, ModuleName, FrontendInputFile(ModuleMapFilePath, IK, IsSystem), - ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName); + ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName, + std::move(VFS)); } // FIXME: We only need to fake up an input file here as a way of @@ -1380,7 +1385,8 @@ std::unique_ptr CompilerInstance::cloneForModuleCompile( auto Instance = cloneForModuleCompileImpl( ImportLoc, ModuleName, FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem), - ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName); + ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName, + std::move(VFS)); std::unique_ptr ModuleMapBuffer = llvm::MemoryBuffer::getMemBufferCopy(InferredModuleMapContent); -- cgit v1.1