Skip to content

Commit 983df92

Browse files
committed
[clang][deps] Stop creating FileManager
1 parent e86c7ef commit 983df92

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ class DependencyScanningAction {
387387
DisableFree(DisableFree), ModuleName(ModuleName) {}
388388

389389
bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
390-
FileManager *DriverFileMgr,
390+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
391391
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
392392
DiagnosticConsumer *DiagConsumer) {
393393
// Make a deep copy of the original Clang invocation.
@@ -418,8 +418,8 @@ class DependencyScanningAction {
418418
// Create the compiler's actual diagnostics engine.
419419
sanitizeDiagOpts(ScanInstance.getDiagnosticOpts());
420420
assert(!DiagConsumerFinished && "attempt to reuse finished consumer");
421-
ScanInstance.createDiagnostics(DriverFileMgr->getVirtualFileSystem(),
422-
DiagConsumer, /*ShouldOwnClient=*/false);
421+
ScanInstance.createDiagnostics(*FS, DiagConsumer,
422+
/*ShouldOwnClient=*/false);
423423
if (!ScanInstance.hasDiagnostics())
424424
return false;
425425

@@ -440,9 +440,9 @@ class DependencyScanningAction {
440440
any(Service.getOptimizeArgs() & ScanningOptimizations::VFS);
441441

442442
// Support for virtual file system overlays.
443-
auto FS = createVFSFromCompilerInvocation(
444-
ScanInstance.getInvocation(), ScanInstance.getDiagnostics(),
445-
DriverFileMgr->getVirtualFileSystemPtr());
443+
FS = createVFSFromCompilerInvocation(ScanInstance.getInvocation(),
444+
ScanInstance.getDiagnostics(),
445+
std::move(FS));
446446

447447
// Create a new FileManager to match the invocation's FileSystemOptions.
448448
auto *FileMgr = ScanInstance.createFileManager(FS);
@@ -553,9 +553,6 @@ class DependencyScanningAction {
553553
if (Result)
554554
setLastCC1Arguments(std::move(OriginalInvocation));
555555

556-
// Propagate the statistics to the parent FileManager.
557-
DriverFileMgr->AddStats(ScanInstance.getFileManager());
558-
559556
return Result;
560557
}
561558

@@ -668,15 +665,14 @@ llvm::Error DependencyScanningWorker::computeDependencies(
668665
}
669666

670667
static bool forEachDriverJob(
671-
ArrayRef<std::string> ArgStrs, DiagnosticsEngine &Diags, FileManager &FM,
668+
ArrayRef<std::string> ArgStrs, DiagnosticsEngine &Diags,
669+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
672670
llvm::function_ref<bool(const driver::Command &Cmd)> Callback) {
673671
SmallVector<const char *, 256> Argv;
674672
Argv.reserve(ArgStrs.size());
675673
for (const std::string &Arg : ArgStrs)
676674
Argv.push_back(Arg.c_str());
677675

678-
llvm::vfs::FileSystem *FS = &FM.getVirtualFileSystem();
679-
680676
std::unique_ptr<driver::Driver> Driver = std::make_unique<driver::Driver>(
681677
Argv[0], llvm::sys::getDefaultTargetTriple(), Diags,
682678
"clang LLVM compiler", FS);
@@ -686,7 +682,8 @@ static bool forEachDriverJob(
686682
bool CLMode = driver::IsClangCL(
687683
driver::getDriverMode(Argv[0], ArrayRef(Argv).slice(1)));
688684

689-
if (llvm::Error E = driver::expandResponseFiles(Argv, CLMode, Alloc, FS)) {
685+
if (llvm::Error E =
686+
driver::expandResponseFiles(Argv, CLMode, Alloc, FS.get())) {
690687
Diags.Report(diag::err_drv_expand_response_file)
691688
<< llvm::toString(std::move(E));
692689
return false;
@@ -709,7 +706,7 @@ static bool forEachDriverJob(
709706

710707
static bool createAndRunToolInvocation(
711708
std::vector<std::string> CommandLine, DependencyScanningAction &Action,
712-
FileManager &FM,
709+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
713710
std::shared_ptr<clang::PCHContainerOperations> &PCHContainerOps,
714711
DiagnosticsEngine &Diags, DependencyConsumer &Consumer) {
715712

@@ -726,8 +723,8 @@ static bool createAndRunToolInvocation(
726723
return false;
727724
}
728725

729-
if (!Action.runInvocation(std::move(Invocation), &FM, PCHContainerOps,
730-
Diags.getClient()))
726+
if (!Action.runInvocation(std::move(Invocation), std::move(FS),
727+
PCHContainerOps, Diags.getClient()))
731728
return false;
732729

733730
std::vector<std::string> Args = Action.takeLastCC1Arguments();
@@ -740,23 +737,14 @@ bool DependencyScanningWorker::scanDependencies(
740737
DependencyConsumer &Consumer, DependencyActionController &Controller,
741738
DiagnosticConsumer &DC, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
742739
std::optional<StringRef> ModuleName) {
743-
auto FileMgr =
744-
llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOptions{}, FS);
745-
746740
std::vector<const char *> CCommandLine(CommandLine.size(), nullptr);
747741
llvm::transform(CommandLine, CCommandLine.begin(),
748742
[](const std::string &Str) { return Str.c_str(); });
749743
auto DiagOpts = CreateAndPopulateDiagOpts(CCommandLine);
750744
sanitizeDiagOpts(*DiagOpts);
751-
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
752-
CompilerInstance::createDiagnostics(FileMgr->getVirtualFileSystem(),
753-
*DiagOpts, &DC,
754-
/*ShouldOwnClient=*/false);
755-
756-
// Although `Diagnostics` are used only for command-line parsing, the
757-
// custom `DiagConsumer` might expect a `SourceManager` to be present.
758-
SourceManager SrcMgr(*Diags, *FileMgr);
759-
Diags->setSourceManager(&SrcMgr);
745+
auto Diags = CompilerInstance::createDiagnostics(*FS, *DiagOpts, &DC,
746+
/*ShouldOwnClient=*/false);
747+
760748
// DisableFree is modified by Tooling for running
761749
// in-process; preserve the original value, which is
762750
// always true for a driver invocation.
@@ -766,11 +754,11 @@ bool DependencyScanningWorker::scanDependencies(
766754

767755
bool Success = false;
768756
if (CommandLine[1] == "-cc1") {
769-
Success = createAndRunToolInvocation(CommandLine, Action, *FileMgr,
757+
Success = createAndRunToolInvocation(CommandLine, Action, FS,
770758
PCHContainerOps, *Diags, Consumer);
771759
} else {
772760
Success = forEachDriverJob(
773-
CommandLine, *Diags, *FileMgr, [&](const driver::Command &Cmd) {
761+
CommandLine, *Diags, FS, [&](const driver::Command &Cmd) {
774762
if (StringRef(Cmd.getCreator().getName()) != "clang") {
775763
// Non-clang command. Just pass through to the dependency
776764
// consumer.
@@ -789,7 +777,7 @@ bool DependencyScanningWorker::scanDependencies(
789777
// system to ensure that any file system requests that
790778
// are made by the driver do not go through the
791779
// dependency scanning filesystem.
792-
return createAndRunToolInvocation(std::move(Argv), Action, *FileMgr,
780+
return createAndRunToolInvocation(std::move(Argv), Action, FS,
793781
PCHContainerOps, *Diags, Consumer);
794782
});
795783
}

0 commit comments

Comments
 (0)