[clang] 146ec74 - [clang][deps] NFC: Stop going through ClangTool

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 10 02:02:45 PDT 2021


Author: Jan Svoboda
Date: 2021-09-10T11:02:41+02:00
New Revision: 146ec74a8382dc820809d0a2bf4b918d0b5e6603

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

LOG: [clang][deps] NFC: Stop going through ClangTool

The dependency scanner currently uses `ClangTool` to invoke the dependency scanning action.

However, `ClangTool` seems to be the wrong level of abstraction. It's intended to be run over a collection of compile commands, which we actively avoid via `SingleCommandCompilationDatabase`. It automatically injects `-fsyntax-only` and other flags, which we avoid by calling `clearArgumentsAdjusters()`. It deduces the resource directory based on the current executable path, which we'd like to change to deducing from `argv[0]`.

Internally, `ClangTool` uses `ToolInvocation` which seems to be more in line with what the dependency scanner tries to achieve. This patch switches to directly using `ToolInvocation` instead. NFC.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D108979

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
index 389bd1678fba..4ee01df4f807 100644
--- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
+++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
@@ -321,31 +321,38 @@ llvm::Error DependencyScanningWorker::computeDependencies(
     const std::string &Input, StringRef WorkingDirectory,
     const CompilationDatabase &CDB, DependencyConsumer &Consumer,
     llvm::Optional<StringRef> ModuleName) {
+  // Reset what might have been modified in the previous worker invocation.
   RealFS->setCurrentWorkingDirectory(WorkingDirectory);
+  if (Files)
+    Files->setVirtualFileSystem(RealFS);
+
+  llvm::IntrusiveRefCntPtr<FileManager> CurrentFiles =
+      Files ? Files : new FileManager(FileSystemOptions(), RealFS);
+
+  // FIXME: Avoid this copy.
+  std::vector<CompileCommand> CompileCommands = CDB.getCompileCommands(Input);
+  const std::vector<std::string> &CommandLine =
+      CompileCommands.front().CommandLine;
+
+  Optional<std::vector<std::string>> ModifiedCommandLine;
+  if (ModuleName.hasValue()) {
+    ModifiedCommandLine = CommandLine;
+    InMemoryFS->addFile(*ModuleName, 0, llvm::MemoryBuffer::getMemBuffer(""));
+    ModifiedCommandLine->emplace_back(*ModuleName);
+  }
+
+  const std::vector<std::string> &FinalCommandLine =
+      ModifiedCommandLine ? *ModifiedCommandLine : CommandLine;
+
   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.
-    tooling::ClangTool Tool(CDB,
-                            ModuleName.hasValue() ? ModuleName->str() : Input,
-                            PCHContainerOps, RealFS, Files);
-    Tool.clearArgumentsAdjusters();
-    Tool.setRestoreWorkingDir(false);
-    Tool.setPrintErrorMessage(false);
-    Tool.setDiagnosticConsumer(&DC);
     DependencyScanningAction Action(WorkingDirectory, Consumer, DepFS,
                                     PPSkipMappings.get(), Format, ModuleName);
-
-    if (ModuleName.hasValue()) {
-      InMemoryFS->addFile(*ModuleName, 0, llvm::MemoryBuffer::getMemBuffer(""));
-      Tool.appendArgumentsAdjuster(
-          [&](const tooling::CommandLineArguments &Args, StringRef FileName) {
-            tooling::CommandLineArguments AdjustedArgs(Args);
-            AdjustedArgs.emplace_back(*ModuleName);
-            return AdjustedArgs;
-          });
-    }
-
-    return !Tool.run(&Action);
+    // Create an invocation 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.
+    ToolInvocation Invocation(FinalCommandLine, &Action, CurrentFiles.get(),
+                              PCHContainerOps);
+    Invocation.setDiagnosticConsumer(&DC);
+    return Invocation.run();
   });
 }


        


More information about the cfe-commits mailing list