diff options
author | Jan Svoboda <jan_svoboda@apple.com> | 2025-04-21 11:09:23 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-21 11:09:23 -0700 |
commit | 09006611151c7f85862a9da8da34872c456c2c37 (patch) | |
tree | 6b6f69625e0532261f54d15495cf98efe4755a9b /clang/lib/Frontend/CompilerInstance.cpp | |
parent | 3d14596ccfa3aa6b0735a630e3efb575a2085a06 (diff) | |
download | llvm-09006611151c7f85862a9da8da34872c456c2c37.zip llvm-09006611151c7f85862a9da8da34872c456c2c37.tar.gz llvm-09006611151c7f85862a9da8da34872c456c2c37.tar.bz2 |
[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.
Diffstat (limited to 'clang/lib/Frontend/CompilerInstance.cpp')
-rw-r--r-- | clang/lib/Frontend/CompilerInstance.cpp | 28 |
1 files changed, 17 insertions, 11 deletions
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> CompilerInstance::cloneForModuleCompileImpl( SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input, - StringRef OriginalModuleMapFile, StringRef ModuleFileName) { + StringRef OriginalModuleMapFile, StringRef ModuleFileName, + IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { // Construct a compiler invocation for creating this module. auto Invocation = std::make_shared<CompilerInvocation>(getInvocation()); @@ -1212,19 +1213,21 @@ std::unique_ptr<CompilerInstance> 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> CompilerInstance::cloneForModuleCompile( - SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName) { + SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName, + IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { StringRef ModuleName = Module->getTopLevelModuleName(); InputKind IK(getLanguageFromOptions(getLangOpts()), InputKind::ModuleMap); @@ -1363,7 +1367,8 @@ std::unique_ptr<CompilerInstance> 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> 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<llvm::MemoryBuffer> ModuleMapBuffer = llvm::MemoryBuffer::getMemBufferCopy(InferredModuleMapContent); |