diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h --- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h +++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h @@ -92,7 +92,10 @@ std::shared_ptr PCHContainerOps; std::unique_ptr PPSkipMappings; + /// The physical filesystem overlaid by `InMemoryFS`. llvm::IntrusiveRefCntPtr RealFS; + /// The in-memory filesystem laid on top the physical filesystem in `RealFS`. + llvm::IntrusiveRefCntPtr InMemoryFS; /// The file system that is used by each worker when scanning for /// dependencies. This filesystem persists accross multiple compiler /// invocations. diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp @@ -141,11 +141,10 @@ StringRef WorkingDirectory, DependencyConsumer &Consumer, llvm::IntrusiveRefCntPtr DepFS, ExcludedPreprocessorDirectiveSkipMapping *PPSkipMappings, - ScanningOutputFormat Format, llvm::Optional ModuleName = None, - llvm::Optional FakeMemBuffer = None) + ScanningOutputFormat Format, llvm::Optional 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 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 ModuleName; - llvm::Optional FakeMemBuffer; }; } // end anonymous namespace @@ -298,7 +286,12 @@ PCHContainerOps->registerWriter( std::make_unique()); - RealFS = llvm::vfs::createPhysicalFileSystem(); + auto OverlayFS = llvm::makeIntrusiveRefCnt( + llvm::vfs::createPhysicalFileSystem()); + InMemoryFS = llvm::makeIntrusiveRefCnt(); + OverlayFS->pushOverlay(InMemoryFS); + RealFS = OverlayFS; + if (Service.canSkipExcludedPPRanges()) PPSkipMappings = std::make_unique(); @@ -329,13 +322,10 @@ const CompilationDatabase &CDB, DependencyConsumer &Consumer, llvm::Optional ModuleName) { RealFS->setCurrentWorkingDirectory(WorkingDirectory); - std::unique_ptr 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(*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; }); }