From 0797f708f52e5ab38845f18263de1b6d7c77c653 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Tue, 22 Apr 2025 15:35:48 -0700 Subject: [clang] Enable making `CompilerInstance` diagnostics thread-safe (#136601) The `DiagnosticConsumer` interface is not thread-safe. To enable thread-safety of `CompilerInstance` objects cloned from the same parent, this PR allows passing an explicit `DiagnosticConsumer` to `cloneForModuleCompile()`. This will be used from the dependency scanner. --- clang/lib/Frontend/CompilerInstance.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'clang/lib/Frontend/CompilerInstance.cpp') diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index bc663ac..de633f0 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -1153,7 +1153,7 @@ static Language getLanguageFromOptions(const LangOptions &LangOpts) { std::unique_ptr CompilerInstance::cloneForModuleCompileImpl( SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input, StringRef OriginalModuleMapFile, StringRef ModuleFileName, - IntrusiveRefCntPtr VFS) { + std::optional ThreadSafeConfig) { // Construct a compiler invocation for creating this module. auto Invocation = std::make_shared(getInvocation()); @@ -1213,18 +1213,24 @@ std::unique_ptr CompilerInstance::cloneForModuleCompileImpl( auto &Inv = *Invocation; Instance.setInvocation(std::move(Invocation)); - if (VFS) { - Instance.createFileManager(std::move(VFS)); + if (ThreadSafeConfig) { + Instance.createFileManager(ThreadSafeConfig->getVFS()); } else if (FrontendOpts.ModulesShareFileManager) { Instance.setFileManager(&getFileManager()); } else { Instance.createFileManager(&getVirtualFileSystem()); } - Instance.createDiagnostics( - Instance.getVirtualFileSystem(), - new ForwardingDiagnosticConsumer(getDiagnosticClient()), - /*ShouldOwnClient=*/true); + if (ThreadSafeConfig) { + Instance.createDiagnostics(Instance.getVirtualFileSystem(), + &ThreadSafeConfig->getDiagConsumer(), + /*ShouldOwnClient=*/false); + } else { + Instance.createDiagnostics( + Instance.getVirtualFileSystem(), + new ForwardingDiagnosticConsumer(getDiagnosticClient()), + /*ShouldOwnClient=*/true); + } if (llvm::is_contained(DiagOpts.SystemHeaderWarningsModules, ModuleName)) Instance.getDiagnostics().setSuppressSystemWarnings(false); @@ -1322,7 +1328,7 @@ static OptionalFileEntryRef getPublicModuleMap(FileEntryRef File, std::unique_ptr CompilerInstance::cloneForModuleCompile( SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName, - IntrusiveRefCntPtr VFS) { + std::optional ThreadSafeConfig) { StringRef ModuleName = Module->getTopLevelModuleName(); InputKind IK(getLanguageFromOptions(getLangOpts()), InputKind::ModuleMap); @@ -1368,7 +1374,7 @@ std::unique_ptr CompilerInstance::cloneForModuleCompile( ImportLoc, ModuleName, FrontendInputFile(ModuleMapFilePath, IK, IsSystem), ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName, - std::move(VFS)); + std::move(ThreadSafeConfig)); } // FIXME: We only need to fake up an input file here as a way of @@ -1386,7 +1392,7 @@ std::unique_ptr CompilerInstance::cloneForModuleCompile( ImportLoc, ModuleName, FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem), ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName, - std::move(VFS)); + std::move(ThreadSafeConfig)); std::unique_ptr ModuleMapBuffer = llvm::MemoryBuffer::getMemBufferCopy(InferredModuleMapContent); -- cgit v1.1