diff options
author | Jan Svoboda <jan_svoboda@apple.com> | 2025-04-22 15:35:48 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-22 15:35:48 -0700 |
commit | 0797f708f52e5ab38845f18263de1b6d7c77c653 (patch) | |
tree | 45a4cd7ba3737c54e1fdaebba16fbbad655ba447 /clang/lib/Frontend/CompilerInstance.cpp | |
parent | a2be4543fc2c1cbd2f9dafade8c29475e7a37e51 (diff) | |
download | llvm-0797f708f52e5ab38845f18263de1b6d7c77c653.zip llvm-0797f708f52e5ab38845f18263de1b6d7c77c653.tar.gz llvm-0797f708f52e5ab38845f18263de1b6d7c77c653.tar.bz2 |
[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.
Diffstat (limited to 'clang/lib/Frontend/CompilerInstance.cpp')
-rw-r--r-- | clang/lib/Frontend/CompilerInstance.cpp | 26 |
1 files changed, 16 insertions, 10 deletions
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> CompilerInstance::cloneForModuleCompileImpl( SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input, StringRef OriginalModuleMapFile, StringRef ModuleFileName, - IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { + std::optional<ThreadSafeCloneConfig> ThreadSafeConfig) { // Construct a compiler invocation for creating this module. auto Invocation = std::make_shared<CompilerInvocation>(getInvocation()); @@ -1213,18 +1213,24 @@ std::unique_ptr<CompilerInstance> 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> CompilerInstance::cloneForModuleCompile( SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName, - IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { + std::optional<ThreadSafeCloneConfig> ThreadSafeConfig) { StringRef ModuleName = Module->getTopLevelModuleName(); InputKind IK(getLanguageFromOptions(getLangOpts()), InputKind::ModuleMap); @@ -1368,7 +1374,7 @@ std::unique_ptr<CompilerInstance> 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> CompilerInstance::cloneForModuleCompile( ImportLoc, ModuleName, FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem), ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName, - std::move(VFS)); + std::move(ThreadSafeConfig)); std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer = llvm::MemoryBuffer::getMemBufferCopy(InferredModuleMapContent); |