Skip to content

Commit 1c8f898

Browse files
committed
[clang] Enable making CompilerInstance diagnostics thread-safe
1 parent 4c0ea47 commit 1c8f898

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -848,10 +848,13 @@ class CompilerInstance : public ModuleLoader {
848848
///
849849
/// Explicitly-specified \c VFS takes precedence over the VFS of this instance
850850
/// when creating the clone and also prevents \c FileManager sharing.
851+
/// Explicitly-specified \c DiagConsumer takes precedence over forwarding to
852+
/// this instance.
851853
std::unique_ptr<CompilerInstance> cloneForModuleCompileImpl(
852854
SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input,
853855
StringRef OriginalModuleMapFile, StringRef ModuleFileName,
854-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr);
856+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr,
857+
DiagnosticConsumer *DiagConsumer = nullptr);
855858

856859
public:
857860
/// Creates a new \c CompilerInstance for compiling a module.
@@ -861,9 +864,13 @@ class CompilerInstance : public ModuleLoader {
861864
///
862865
/// Explicitly-specified \c VFS takes precedence over the VFS of this instance
863866
/// when creating the clone and also prevents \c FileManager sharing.
864-
std::unique_ptr<CompilerInstance> cloneForModuleCompile(
865-
SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName,
866-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr);
867+
/// Explicitly-specified \c DiagConsumer takes precedence over forwarding to
868+
/// this instance.
869+
std::unique_ptr<CompilerInstance>
870+
cloneForModuleCompile(SourceLocation ImportLoc, Module *Module,
871+
StringRef ModuleFileName,
872+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr,
873+
DiagnosticConsumer *DiagConsumer = nullptr);
867874

868875
/// Compile a module file for the given module, using the options
869876
/// provided by the importing compiler instance. Returns true if the module

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,8 @@ static Language getLanguageFromOptions(const LangOptions &LangOpts) {
11531153
std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
11541154
SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input,
11551155
StringRef OriginalModuleMapFile, StringRef ModuleFileName,
1156-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
1156+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
1157+
DiagnosticConsumer *DiagConsumer) {
11571158
// Construct a compiler invocation for creating this module.
11581159
auto Invocation = std::make_shared<CompilerInvocation>(getInvocation());
11591160

@@ -1221,10 +1222,15 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
12211222
Instance.createFileManager(&getVirtualFileSystem());
12221223
}
12231224

1224-
Instance.createDiagnostics(
1225-
Instance.getVirtualFileSystem(),
1226-
new ForwardingDiagnosticConsumer(getDiagnosticClient()),
1227-
/*ShouldOwnClient=*/true);
1225+
if (DiagConsumer) {
1226+
Instance.createDiagnostics(Instance.getVirtualFileSystem(), DiagConsumer,
1227+
/*ShouldOwnClient=*/false);
1228+
} else {
1229+
Instance.createDiagnostics(
1230+
Instance.getVirtualFileSystem(),
1231+
new ForwardingDiagnosticConsumer(getDiagnosticClient()),
1232+
/*ShouldOwnClient=*/true);
1233+
}
12281234
if (llvm::is_contained(DiagOpts.SystemHeaderWarningsModules, ModuleName))
12291235
Instance.getDiagnostics().setSuppressSystemWarnings(false);
12301236

@@ -1322,7 +1328,8 @@ static OptionalFileEntryRef getPublicModuleMap(FileEntryRef File,
13221328

13231329
std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
13241330
SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName,
1325-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
1331+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
1332+
DiagnosticConsumer *DiagConsumer) {
13261333
StringRef ModuleName = Module->getTopLevelModuleName();
13271334

13281335
InputKind IK(getLanguageFromOptions(getLangOpts()), InputKind::ModuleMap);
@@ -1368,7 +1375,7 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
13681375
ImportLoc, ModuleName,
13691376
FrontendInputFile(ModuleMapFilePath, IK, IsSystem),
13701377
ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName,
1371-
std::move(VFS));
1378+
std::move(VFS), DiagConsumer);
13721379
}
13731380

13741381
// FIXME: We only need to fake up an input file here as a way of
@@ -1386,7 +1393,7 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
13861393
ImportLoc, ModuleName,
13871394
FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem),
13881395
ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName,
1389-
std::move(VFS));
1396+
std::move(VFS), DiagConsumer);
13901397

13911398
std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer =
13921399
llvm::MemoryBuffer::getMemBufferCopy(InferredModuleMapContent);

0 commit comments

Comments
 (0)