================
@@ -161,43 +164,109 @@ DependencyScanningTool::getModuleDependencies(
     StringRef ModuleName, ArrayRef<std::string> CommandLine, StringRef CWD,
     const llvm::DenseSet<ModuleID> &AlreadySeen,
     LookupModuleOutputCallback LookupModuleOutput) {
-  FullDependencyConsumer Consumer(AlreadySeen);
-  CallbackActionController Controller(LookupModuleOutput);
   if (auto Error =
-          Worker.initializeCompilerInstanceWithContextOrError(CWD, 
CommandLine))
-    return std::move(Error);
+          initializeCompilerInstanceWithContextOrError(CWD, CommandLine))
+    return Error;
 
-  auto Result = Worker.computeDependenciesByNameWithContextOrError(
-      ModuleName, Consumer, Controller);
+  auto Result = computeDependenciesByNameWithContextOrError(
+      ModuleName, AlreadySeen, LookupModuleOutput);
 
-  if (auto Error = Worker.finalizeCompilerInstanceWithContextOrError())
-    return std::move(Error);
+  if (auto Error = finalizeCompilerInstanceWithContextOrError())
+    return Error;
 
-  if (Result)
-    return std::move(Result);
+  return Result;
+}
 
-  return Consumer.takeTranslationUnitDeps();
+/// Constructs the full -cc1 command line, including executable, for the given
+/// driver \c Cmd.
+static std::vector<std::string>
+buildCC1CommandLine(const driver::Command &Cmd) {
+  const auto &Args = Cmd.getArguments();
+  std::vector<std::string> Out;
+  Out.reserve(Args.size() + 1);
+  Out.emplace_back(Cmd.getExecutable());
+  llvm::append_range(Out, Args);
+  return Out;
 }
 
-llvm::Error DependencyScanningTool::initializeCompilerInstanceWithContext(
+static std::optional<std::vector<std::string>> getFirstCC1CommandLine(
+    ArrayRef<std::string> CommandLine, DiagnosticsEngine &Diags,
+    llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> ScanFS) {
+  // Compilation holds a non-owning a reference to the Driver, hence we need to
+  // keep the Driver alive when we use Compilation. Arguments to commands may 
be
+  // owned by Alloc when expanded from response files.
+  llvm::BumpPtrAllocator Alloc;
+  const auto [Driver, Compilation] =
+      buildCompilation(CommandLine, Diags, ScanFS, Alloc);
+  if (!Compilation)
+    return std::nullopt;
+
+  const auto IsClangCmd = [](const driver::Command &Cmd) {
+    return StringRef(Cmd.getCreator().getName()) == "clang";
+  };
+  const auto CC1CommandLineRange = llvm::map_range(
+      llvm::make_filter_range(Compilation->getJobs(), IsClangCmd),
+      buildCC1CommandLine);
----------------
naveen-seth wrote:

I believe that both `map_range` and `make_filter_range` return lazy ranges.

In hindsight, I couldn’t justify why I wrote the code that way, so I’ve I've 
updated it to be simpler using `llvm::find`.


https://github.com/llvm/llvm-project/pull/171238
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to