================
@@ -161,43 +164,107 @@ 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 &Jobs = Compilation->getJobs();
+  if (const auto It = llvm::find_if(Jobs, IsClangCmd); It != Jobs.end())
+    return buildCC1CommandLine(*It);
+  return std::nullopt;
+}
+
+static llvm::Error makeErrorFromDiagnosticsOS(
+    TextDiagnosticsPrinterWithOutput &DiagPrinterWithOS) {
+  return llvm::make_error<llvm::StringError>(
+      DiagPrinterWithOS.DiagnosticsOS.str(), llvm::inconvertibleErrorCode());
+}
+
+llvm::Error
+DependencyScanningTool::initializeCompilerInstanceWithContextOrError(
     StringRef CWD, ArrayRef<std::string> CommandLine) {
-  return Worker.initializeCompilerInstanceWithContextOrError(CWD, CommandLine);
+  DiagPrinterWithOS =
+      std::make_unique<TextDiagnosticsPrinterWithOutput>(CommandLine);
+
+  if (CommandLine.size() >= 2 && CommandLine[1] == "-cc1") {
+    if (Worker.initializeCompilerInstanceWithContext(
----------------
qiongsiwu wrote:

Nit: I think we can add some comments explaining that we are handling the 
`-cc1` jobs here, and right above line 233 add some comments to state that the 
code further below handles the case where we need to get the `cc1` command line 
from the driver. 

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