Skip to content

Commit a5b85b1

Browse files
committed
Extract VFS and DiagnosticConsumer into ThreadSafeCloneConfig
1 parent 1c8f898 commit a5b85b1

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,23 @@ class CompilerInstance : public ModuleLoader {
825825
bool loadModuleFile(StringRef FileName,
826826
serialization::ModuleFile *&LoadedModuleFile);
827827

828+
/// Configuration object for making the result of \c cloneForModuleCompile()
829+
/// thread-safe.
830+
class ThreadSafeCloneConfig {
831+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS;
832+
DiagnosticConsumer &DiagConsumer;
833+
834+
public:
835+
ThreadSafeCloneConfig(IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
836+
DiagnosticConsumer &DiagConsumer)
837+
: VFS(std::move(VFS)), DiagConsumer(DiagConsumer) {
838+
assert(this->VFS && "Clone config requires non-null VFS");
839+
}
840+
841+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> getVFS() const { return VFS; }
842+
DiagnosticConsumer &getDiagConsumer() const { return DiagConsumer; }
843+
};
844+
828845
private:
829846
/// Find a module, potentially compiling it, before reading its AST. This is
830847
/// the guts of loadModule.
@@ -845,32 +862,22 @@ class CompilerInstance : public ModuleLoader {
845862
/// Creates a \c CompilerInstance for compiling a module.
846863
///
847864
/// This expects a properly initialized \c FrontendInputFile.
848-
///
849-
/// Explicitly-specified \c VFS takes precedence over the VFS of this instance
850-
/// when creating the clone and also prevents \c FileManager sharing.
851-
/// Explicitly-specified \c DiagConsumer takes precedence over forwarding to
852-
/// this instance.
853865
std::unique_ptr<CompilerInstance> cloneForModuleCompileImpl(
854866
SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input,
855867
StringRef OriginalModuleMapFile, StringRef ModuleFileName,
856-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr,
857-
DiagnosticConsumer *DiagConsumer = nullptr);
868+
std::optional<ThreadSafeCloneConfig> ThreadSafeConfig = std::nullopt);
858869

859870
public:
860871
/// Creates a new \c CompilerInstance for compiling a module.
861872
///
862873
/// This takes care of creating appropriate \c FrontendInputFile for
863874
/// public/private frameworks, inferred modules and such.
864875
///
865-
/// Explicitly-specified \c VFS takes precedence over the VFS of this instance
866-
/// when creating the clone and also prevents \c FileManager sharing.
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);
876+
/// The \c ThreadSafeConfig takes precedence over the \c DiagnosticConsumer
877+
/// and \c FileSystem of this instance (and disables \c FileManager sharing).
878+
std::unique_ptr<CompilerInstance> cloneForModuleCompile(
879+
SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName,
880+
std::optional<ThreadSafeCloneConfig> ThreadSafeConfig = std::nullopt);
874881

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

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,8 +1153,7 @@ 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,
1157-
DiagnosticConsumer *DiagConsumer) {
1156+
std::optional<ThreadSafeCloneConfig> ThreadSafeConfig) {
11581157
// Construct a compiler invocation for creating this module.
11591158
auto Invocation = std::make_shared<CompilerInvocation>(getInvocation());
11601159

@@ -1214,16 +1213,17 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
12141213
auto &Inv = *Invocation;
12151214
Instance.setInvocation(std::move(Invocation));
12161215

1217-
if (VFS) {
1218-
Instance.createFileManager(std::move(VFS));
1216+
if (ThreadSafeConfig) {
1217+
Instance.createFileManager(ThreadSafeConfig->getVFS());
12191218
} else if (FrontendOpts.ModulesShareFileManager) {
12201219
Instance.setFileManager(&getFileManager());
12211220
} else {
12221221
Instance.createFileManager(&getVirtualFileSystem());
12231222
}
12241223

1225-
if (DiagConsumer) {
1226-
Instance.createDiagnostics(Instance.getVirtualFileSystem(), DiagConsumer,
1224+
if (ThreadSafeConfig) {
1225+
Instance.createDiagnostics(Instance.getVirtualFileSystem(),
1226+
&ThreadSafeConfig->getDiagConsumer(),
12271227
/*ShouldOwnClient=*/false);
12281228
} else {
12291229
Instance.createDiagnostics(
@@ -1328,8 +1328,7 @@ static OptionalFileEntryRef getPublicModuleMap(FileEntryRef File,
13281328

13291329
std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
13301330
SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName,
1331-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
1332-
DiagnosticConsumer *DiagConsumer) {
1331+
std::optional<ThreadSafeCloneConfig> ThreadSafeConfig) {
13331332
StringRef ModuleName = Module->getTopLevelModuleName();
13341333

13351334
InputKind IK(getLanguageFromOptions(getLangOpts()), InputKind::ModuleMap);
@@ -1375,7 +1374,7 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
13751374
ImportLoc, ModuleName,
13761375
FrontendInputFile(ModuleMapFilePath, IK, IsSystem),
13771376
ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName,
1378-
std::move(VFS), DiagConsumer);
1377+
std::move(ThreadSafeConfig));
13791378
}
13801379

13811380
// FIXME: We only need to fake up an input file here as a way of
@@ -1393,7 +1392,7 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
13931392
ImportLoc, ModuleName,
13941393
FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem),
13951394
ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName,
1396-
std::move(VFS), DiagConsumer);
1395+
std::move(ThreadSafeConfig));
13971396

13981397
std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer =
13991398
llvm::MemoryBuffer::getMemBufferCopy(InferredModuleMapContent);

0 commit comments

Comments
 (0)