jansvoboda11 created this revision. jansvoboda11 added reviewers: Bigcheese, dexonsmith, ahatanak. jansvoboda11 requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
This patch changes how the dependency scanner creates the fake input file when scanning dependencies of a single module (introduced in D109485 <https://reviews.llvm.org/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 <https://reviews.llvm.org/D108979>, where we drop `ClangTool` entirely. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D109498 Files: clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
Index: clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp =================================================================== --- clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp +++ clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp @@ -141,11 +141,10 @@ 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 @@ .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 @@ ExcludedPreprocessorDirectiveSkipMapping *PPSkipMappings; ScanningOutputFormat Format; llvm::Optional<StringRef> ModuleName; - llvm::Optional<llvm::MemoryBufferRef> FakeMemBuffer; }; } // end anonymous namespace @@ -298,7 +286,12 @@ PCHContainerOps->registerWriter( std::make_unique<ObjectFilePCHContainerWriter>()); - RealFS = llvm::vfs::createPhysicalFileSystem(); + auto OverlayFS = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>( + llvm::vfs::createPhysicalFileSystem()); + MemoryFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>(); + OverlayFS->pushOverlay(MemoryFS); + RealFS = OverlayFS; + if (Service.canSkipExcludedPPRanges()) PPSkipMappings = std::make_unique<ExcludedPreprocessorDirectiveSkipMapping>(); @@ -329,13 +322,10 @@ 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 @@ 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); + MemoryFS->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; }); } Index: clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h =================================================================== --- clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h +++ clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h @@ -92,7 +92,10 @@ std::shared_ptr<PCHContainerOperations> PCHContainerOps; std::unique_ptr<ExcludedPreprocessorDirectiveSkipMapping> PPSkipMappings; + /// The physical filesystem overlaid by `MemoryFS`. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> RealFS; + /// The in-memory filesystem laid on top the physical filesystem in `RealFS`. + llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemoryFS; /// The file system that is used by each worker when scanning for /// dependencies. This filesystem persists accross multiple compiler /// invocations.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits