[llvm-branch-commits] [clang] [clang][DependencyScanning] Extracting a Driver-free CompilerInstaneWithContext Initializer (PR #211405)

Qiongsi Wu via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jul 22 15:46:35 PDT 2026


https://github.com/qiongsiwu created https://github.com/llvm/llvm-project/pull/211405

This PR extracts out an initializer of `CompilerInstanceWithContext` that does not depend on any driver code in preperation of moving the `CompilerInstanceWithContext` into `DependencyScanningWorker.cpp` as an implementation detail that is not exposed by any APIs. 

---

<sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>

>From 13cd2be333733528e5cdfcc66d785b745fb60bb4 Mon Sep 17 00:00:00 2001
From: Qiongsi Wu <qiongsi_wu at apple.com>
Date: Tue, 21 Jul 2026 15:20:06 -0700
Subject: [PATCH] Extracting a driver-free CompilerInstaneWithContext
 initializer.

---
 .../clang/Tooling/DependencyScanningTool.h    | 24 ++++++++++++--
 clang/lib/Tooling/DependencyScanningTool.cpp  | 32 ++++++++++++-------
 2 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/clang/include/clang/Tooling/DependencyScanningTool.h b/clang/include/clang/Tooling/DependencyScanningTool.h
index 90216d8f1da82..908a70efee89b 100644
--- a/clang/include/clang/Tooling/DependencyScanningTool.h
+++ b/clang/include/clang/Tooling/DependencyScanningTool.h
@@ -172,9 +172,8 @@ class CompilerInstanceWithContext {
   int32_t SrcLocOffset = 0;
 
   CompilerInstanceWithContext(dependencies::DependencyScanningWorker &Worker,
-                              StringRef CWD,
-                              const std::vector<std::string> &CMD)
-      : Worker(Worker), CWD(CWD), CommandLine(CMD) {};
+                              StringRef CWD, ArrayRef<std::string> CMD)
+      : Worker(Worker), CWD(CWD), CommandLine(CMD.begin(), CMD.end()) {}
 
   bool initialize(dependencies::DependencyActionController &Controller,
                   std::unique_ptr<dependencies::DiagnosticsEngineWithDiagOpts>
@@ -198,6 +197,25 @@ class CompilerInstanceWithContext {
       dependencies::DependencyActionController &Controller,
       DiagnosticConsumer &DC);
 
+  /// @brief Initialize the compiler instance from an already-lowered cc1
+  ///        commandline (driver-free).
+  /// @param Worker The dependency scanning worker to initialize the compiler
+  ///        instance.
+  /// @param CWD The current working directory.
+  /// @param CC1CommandLine A cc1 command.
+  /// @param DiagEngineWithDiagOpts The diagnostic engine used during scan.
+  /// @param OverlayFS An overlay FS containing the input file, which may be
+  ///        from an in-memory buffer.
+  /// @param Controller A dependency action controller to gather some results.
+  static std::optional<CompilerInstanceWithContext>
+  initializeFromCC1Commandline(
+      dependencies::DependencyScanningWorker &Worker, StringRef CWD,
+      ArrayRef<std::string> CC1CommandLine,
+      std::unique_ptr<dependencies::DiagnosticsEngineWithDiagOpts>
+          DiagEngineWithDiagOpts,
+      IntrusiveRefCntPtr<llvm::vfs::FileSystem> OverlayFS,
+      dependencies::DependencyActionController &Controller);
+
   /// @brief Initializing the context and the compiler instance.
   ///        This method must be called before calling
   ///        computeDependenciesByNameWithContext.
diff --git a/clang/lib/Tooling/DependencyScanningTool.cpp b/clang/lib/Tooling/DependencyScanningTool.cpp
index b435e42af28b4..83e0c89dc6ff6 100644
--- a/clang/lib/Tooling/DependencyScanningTool.cpp
+++ b/clang/lib/Tooling/DependencyScanningTool.cpp
@@ -381,13 +381,9 @@ CompilerInstanceWithContext::initializeFromCommandline(
   if (ModifiedCommandLine.size() >= 2 && ModifiedCommandLine[1] == "-cc1") {
     // The input command line is already a -cc1 invocation; initialize the
     // compiler instance directly from it.
-    CompilerInstanceWithContext CIWithContext(Tool.Worker, CWD,
-                                              ModifiedCommandLine);
-    if (!CIWithContext.initialize(Controller,
-                                  std::move(DiagEngineWithCmdAndOpts),
-                                  std::move(OverlayFS)))
-      return std::nullopt;
-    return std::move(CIWithContext);
+    return initializeFromCC1Commandline(Tool.Worker, CWD, ModifiedCommandLine,
+                                        std::move(DiagEngineWithCmdAndOpts),
+                                        std::move(OverlayFS), Controller);
   }
 
   // The input command line is either a driver-style command line, or
@@ -400,12 +396,24 @@ CompilerInstanceWithContext::initializeFromCommandline(
 
   std::vector<std::string> CC1CommandLine(MaybeFirstCC1->begin(),
                                           MaybeFirstCC1->end());
-  CompilerInstanceWithContext CIWithContext(Tool.Worker, CWD,
-                                            std::move(CC1CommandLine));
-  if (!CIWithContext.initialize(Controller, std::move(DiagEngineWithCmdAndOpts),
-                                std::move(OverlayFS)))
+  return initializeFromCC1Commandline(Tool.Worker, CWD, CC1CommandLine,
+                                      std::move(DiagEngineWithCmdAndOpts),
+                                      std::move(OverlayFS), Controller);
+}
+
+std::optional<CompilerInstanceWithContext>
+CompilerInstanceWithContext::initializeFromCC1Commandline(
+    DependencyScanningWorker &Worker, StringRef CWD,
+    ArrayRef<std::string> CC1CommandLine,
+    std::unique_ptr<dependencies::DiagnosticsEngineWithDiagOpts>
+        DiagEngineWithDiagOpts,
+    IntrusiveRefCntPtr<llvm::vfs::FileSystem> OverlayFS,
+    DependencyActionController &Controller) {
+  CompilerInstanceWithContext CIWC(Worker, CWD, CC1CommandLine);
+  if (!CIWC.initialize(Controller, std::move(DiagEngineWithDiagOpts),
+                       std::move(OverlayFS)))
     return std::nullopt;
-  return std::move(CIWithContext);
+  return std::move(CIWC);
 }
 
 llvm::Expected<CompilerInstanceWithContext>



More information about the llvm-branch-commits mailing list