https://github.com/dzbarsky created 
https://github.com/llvm/llvm-project/pull/182202

I ran into a failure when invoking clang on a .c file to produce a binary 
directly, in the path that re-execs with -cc1. In my setup, clang was a symlink 
to the LLVM multicall binary, and the re-exec argument order became wrong 
(effectively inserting an extra tool-name argument before -cc1), which broke 
the integrated cc1 handling.

This patch changes when setPrependArg() is used. Instead of prepending whenever 
-canonical-prefixes is enabled, it now prepends only when 
ToolContext.NeedsPrependArg is true, or when canonicalization resolves the 
executable to llvm. The goal is to keep prepend behavior for multicall 
dispatch, but avoid shifting -cc1 out of the expected position when the 
resolved executable is already clang-like.

This feels a bit suboptimal because it relies on stem(Path) == "llvm". That may 
miss valid multicall setups with different executable names (for example 
llvm-driver or versioned names). Is there a better way to detect “this 
invocation still needs prepend for multicall dispatch”?

>From c004e99f118e9458466aa543d341fb98efecac53 Mon Sep 17 00:00:00 2001
From: David Zbarsky <[email protected]>
Date: Mon, 9 Feb 2026 15:20:41 -0500
Subject: [PATCH] Fix clang-scan-deps invocation order

---
 clang/tools/driver/driver.cpp | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/clang/tools/driver/driver.cpp b/clang/tools/driver/driver.cpp
index e6cabcc7eb530..5d9973d62e894 100644
--- a/clang/tools/driver/driver.cpp
+++ b/clang/tools/driver/driver.cpp
@@ -361,12 +361,17 @@ int clang_main(int Argc, char **Argv, const 
llvm::ToolContext &ToolContext) {
                    /*Title=*/"clang LLVM compiler", VFS);
   auto TargetAndMode = ToolChain::getTargetAndModeFromProgramName(ProgName);
   TheDriver.setTargetAndMode(TargetAndMode);
-  // If -canonical-prefixes is set, GetExecutablePath will have resolved Path
-  // to the llvm driver binary, not clang. In this case, we need to use
-  // PrependArg which should be clang-*. Checking just CanonicalPrefixes is
-  // safe even in the normal case because PrependArg will be null so
-  // setPrependArg will be a no-op.
-  if (ToolContext.NeedsPrependArg || CanonicalPrefixes)
+  // If -canonical-prefixes is set, GetExecutablePath may resolve Path to the
+  // llvm multicall driver binary, not clang. In that case we need to prepend
+  // clang-* so llvm-driver dispatches into the clang entrypoint before cc1
+  // handling. Do not prepend when the resolved executable is already clang,
+  // since that would shift -cc1 out of argv[1].
+  const bool ResolvedExecutableIsLLVMDriver =
+      llvm::sys::path::stem(Path).equals_insensitive("llvm");
+  const bool ShouldSetPrependArg =
+      ToolContext.NeedsPrependArg ||
+      (CanonicalPrefixes && ResolvedExecutableIsLLVMDriver);
+  if (ShouldSetPrependArg)
     TheDriver.setPrependArg(ToolContext.PrependArg);
 
   insertTargetAndModeArgs(TargetAndMode, Args, SavedStrings);

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

Reply via email to