Skip to content

[clang] Enable making CompilerInstance diagnostics thread-safe #136601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions clang/include/clang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,23 @@ class CompilerInstance : public ModuleLoader {
bool loadModuleFile(StringRef FileName,
serialization::ModuleFile *&LoadedModuleFile);

/// Configuration object for making the result of \c cloneForModuleCompile()
/// thread-safe.
class ThreadSafeCloneConfig {
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS;
DiagnosticConsumer &DiagConsumer;

public:
ThreadSafeCloneConfig(IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
DiagnosticConsumer &DiagConsumer)
: VFS(std::move(VFS)), DiagConsumer(DiagConsumer) {
assert(this->VFS && "Clone config requires non-null VFS");
}

IntrusiveRefCntPtr<llvm::vfs::FileSystem> getVFS() const { return VFS; }
DiagnosticConsumer &getDiagConsumer() const { return DiagConsumer; }
};

private:
/// Find a module, potentially compiling it, before reading its AST. This is
/// the guts of loadModule.
Expand All @@ -845,25 +862,22 @@ class CompilerInstance : public ModuleLoader {
/// Creates a \c CompilerInstance for compiling a module.
///
/// This expects a properly initialized \c FrontendInputFile.
///
/// Explicitly-specified \c VFS takes precedence over the VFS of this instance
/// when creating the clone and also prevents \c FileManager sharing.
std::unique_ptr<CompilerInstance> cloneForModuleCompileImpl(
SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input,
StringRef OriginalModuleMapFile, StringRef ModuleFileName,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr);
std::optional<ThreadSafeCloneConfig> ThreadSafeConfig = std::nullopt);

public:
/// Creates a new \c CompilerInstance for compiling a module.
///
/// This takes care of creating appropriate \c FrontendInputFile for
/// public/private frameworks, inferred modules and such.
///
/// Explicitly-specified \c VFS takes precedence over the VFS of this instance
/// when creating the clone and also prevents \c FileManager sharing.
/// The \c ThreadSafeConfig takes precedence over the \c DiagnosticConsumer
/// and \c FileSystem of this instance (and disables \c FileManager sharing).
std::unique_ptr<CompilerInstance> cloneForModuleCompile(
SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr);
std::optional<ThreadSafeCloneConfig> ThreadSafeConfig = std::nullopt);

/// Compile a module file for the given module, using the options
/// provided by the importing compiler instance. Returns true if the module
Expand Down
26 changes: 16 additions & 10 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
Loading