[clang][deps] Stop using `ClangTool` for virtual files

This patch changes how the dependency scanner creates the fake input file when scanning dependencies of a single module (introduced in D109485). The scanner now has its own `InMemoryFilesystem` which sits under the minimizing FS (when that's requested). This makes it possible to drop the duplicate work in `DependencyScanningActions::runInvocation` that sets up the main file ID. Besides that, this patch makes it possible to land D108979, where we drop `ClangTool` entirely.

Depends on D109485.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D109498
This commit is contained in:
Diana Picus 2021-09-10 10:07:24 +02:00 committed by Jan Svoboda
parent 4f9217c519
commit b2528fc490
2 changed files with 15 additions and 28 deletions

View File

@ -92,7 +92,10 @@ private:
std::shared_ptr<PCHContainerOperations> PCHContainerOps;
std::unique_ptr<ExcludedPreprocessorDirectiveSkipMapping> PPSkipMappings;
/// The physical filesystem overlaid by `InMemoryFS`.
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> RealFS;
/// The in-memory filesystem laid on top the physical filesystem in `RealFS`.
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFS;
/// The file system that is used by each worker when scanning for
/// dependencies. This filesystem persists accross multiple compiler
/// invocations.

View File

@ -141,11 +141,10 @@ public:
StringRef WorkingDirectory, DependencyConsumer &Consumer,
llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS,
ExcludedPreprocessorDirectiveSkipMapping *PPSkipMappings,
ScanningOutputFormat Format, llvm::Optional<StringRef> ModuleName = None,
llvm::Optional<llvm::MemoryBufferRef> FakeMemBuffer = None)
ScanningOutputFormat Format, llvm::Optional<StringRef> ModuleName = None)
: WorkingDirectory(WorkingDirectory), Consumer(Consumer),
DepFS(std::move(DepFS)), PPSkipMappings(PPSkipMappings), Format(Format),
ModuleName(ModuleName), FakeMemBuffer(FakeMemBuffer) {}
ModuleName(ModuleName) {}
bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
FileManager *FileMgr,
@ -215,16 +214,6 @@ public:
.ExcludedConditionalDirectiveSkipMappings = PPSkipMappings;
}
if (ModuleName.hasValue()) {
SmallString<128> FullPath(*ModuleName);
llvm::sys::fs::make_absolute(WorkingDirectory, FullPath);
SourceManager &SrcMgr = Compiler.getSourceManager();
FileMgr->getVirtualFile(FullPath.c_str(), FakeMemBuffer->getBufferSize(),
0);
FileID MainFileID = SrcMgr.createFileID(*FakeMemBuffer);
SrcMgr.setMainFileID(MainFileID);
}
// Create the dependency collector that will collect the produced
// dependencies.
//
@ -280,7 +269,6 @@ private:
ExcludedPreprocessorDirectiveSkipMapping *PPSkipMappings;
ScanningOutputFormat Format;
llvm::Optional<StringRef> ModuleName;
llvm::Optional<llvm::MemoryBufferRef> FakeMemBuffer;
};
} // end anonymous namespace
@ -298,7 +286,12 @@ DependencyScanningWorker::DependencyScanningWorker(
PCHContainerOps->registerWriter(
std::make_unique<ObjectFilePCHContainerWriter>());
RealFS = llvm::vfs::createPhysicalFileSystem();
auto OverlayFS = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
llvm::vfs::createPhysicalFileSystem());
InMemoryFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
OverlayFS->pushOverlay(InMemoryFS);
RealFS = OverlayFS;
if (Service.canSkipExcludedPPRanges())
PPSkipMappings =
std::make_unique<ExcludedPreprocessorDirectiveSkipMapping>();
@ -329,13 +322,10 @@ llvm::Error DependencyScanningWorker::computeDependencies(
const CompilationDatabase &CDB, DependencyConsumer &Consumer,
llvm::Optional<StringRef> ModuleName) {
RealFS->setCurrentWorkingDirectory(WorkingDirectory);
std::unique_ptr<llvm::MemoryBuffer> FakeMemBuffer =
ModuleName.hasValue() ? llvm::MemoryBuffer::getMemBuffer(" ") : nullptr;
return runWithDiags(DiagOpts.get(), [&](DiagnosticConsumer &DC) {
/// Create the tool that uses the underlying file system to ensure that any
/// file system requests that are made by the driver do not go through the
/// dependency scanning filesystem.
SmallString<128> FullPath;
tooling::ClangTool Tool(CDB,
ModuleName.hasValue() ? ModuleName->str() : Input,
PCHContainerOps, RealFS, Files);
@ -343,21 +333,15 @@ llvm::Error DependencyScanningWorker::computeDependencies(
Tool.setRestoreWorkingDir(false);
Tool.setPrintErrorMessage(false);
Tool.setDiagnosticConsumer(&DC);
DependencyScanningAction Action(
WorkingDirectory, Consumer, DepFS, PPSkipMappings.get(), Format,
ModuleName,
FakeMemBuffer
? llvm::Optional<llvm::MemoryBufferRef>(*FakeMemBuffer.get())
: None);
DependencyScanningAction Action(WorkingDirectory, Consumer, DepFS,
PPSkipMappings.get(), Format, ModuleName);
if (ModuleName.hasValue()) {
Tool.mapVirtualFile(*ModuleName, FakeMemBuffer->getBuffer());
FullPath = *ModuleName;
llvm::sys::fs::make_absolute(WorkingDirectory, FullPath);
InMemoryFS->addFile(*ModuleName, 0, llvm::MemoryBuffer::getMemBuffer(""));
Tool.appendArgumentsAdjuster(
[&](const tooling::CommandLineArguments &Args, StringRef FileName) {
tooling::CommandLineArguments AdjustedArgs(Args);
AdjustedArgs.push_back(std::string(FullPath));
AdjustedArgs.emplace_back(*ModuleName);
return AdjustedArgs;
});
}