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

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 10 01:19:32 PDT 2021


Author: Diana Picus
Date: 2021-09-10T10:19:27+02:00
New Revision: b2528fc49035588b69cec2b4bfba87a0d3eb725d

URL: https://github.com/llvm/llvm-project/commit/b2528fc49035588b69cec2b4bfba87a0d3eb725d
DIFF: https://github.com/llvm/llvm-project/commit/b2528fc49035588b69cec2b4bfba87a0d3eb725d.diff

LOG: [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

Added: 
    

Modified: 
    clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h
    clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h
index 3ae3bcda7239..8ede14bdc4f7 100644
--- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h
+++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h
@@ -92,7 +92,10 @@ class DependencyScanningWorker {
   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.

diff  --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
index 10385ab35669..389bd1678fba 100644
--- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
+++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
@@ -141,11 +141,10 @@ class DependencyScanningAction : public tooling::ToolAction {
       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 @@ class DependencyScanningAction : public tooling::ToolAction {
             .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 @@ class DependencyScanningAction : public tooling::ToolAction {
   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;
           });
     }


        


More information about the cfe-commits mailing list