[clang] [clang-cl] Don't count linker inputs against /Fo and /Fa (PR #232611)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 15 23:25:45 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Haohai Wen (HaohaiWen)

<details>
<summary>Changes</summary>

The /Fo and /Fa checks reject a file name when there is more than one input. But the input list also holds object files, libraries, and linker options such as -Xlinker, which go straight to the linker and never get an output named by /Fo or /Fa. So

  clang-cl /c foo.c /Fofoo.obj -Xlinker -bar

failed with "cannot specify '/Fofoo.obj' when compiling multiple source files". MSVC's cl.exe also accepts .obj and .lib inputs next to /Fo<file>.

Count only the inputs that get compiled.

Fixes #<!-- -->216085.

---
Full diff: https://github.com/llvm/llvm-project/pull/232611.diff


2 Files Affected:

- (modified) clang/lib/Driver/Driver.cpp (+7-2) 
- (modified) clang/test/Driver/cl-outputs.c (+8) 


``````````diff
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index a9a21175af9c5..b6304dee0be61 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -3386,10 +3386,15 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
                              const InputList &Inputs,
                              ActionList &Actions) const {
 
+  // Linker inputs go straight to the linker, so they should not be counted
+  // toward the compiler outputs that /Fo and /Fa name.
+  unsigned NumCompiledInputs = llvm::count_if(
+      Inputs, [](const InputTy &I) { return I.first != types::TY_Object; });
+
   // Diagnose misuse of /Fo.
   if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fo)) {
     StringRef V = A->getValue();
-    if (Inputs.size() > 1 && !V.empty() &&
+    if (NumCompiledInputs > 1 && !V.empty() &&
         !llvm::sys::path::is_separator(V.back())) {
       // Check whether /Fo tries to name an output file for multiple inputs.
       Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources)
@@ -3401,7 +3406,7 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
   // Diagnose misuse of /Fa.
   if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fa)) {
     StringRef V = A->getValue();
-    if (Inputs.size() > 1 && !V.empty() &&
+    if (NumCompiledInputs > 1 && !V.empty() &&
         !llvm::sys::path::is_separator(V.back())) {
       // Check whether /Fa tries to name an asm file for multiple inputs.
       Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources)
diff --git a/clang/test/Driver/cl-outputs.c b/clang/test/Driver/cl-outputs.c
index c59adc8acb3b6..b480f62897214 100644
--- a/clang/test/Driver/cl-outputs.c
+++ b/clang/test/Driver/cl-outputs.c
@@ -37,6 +37,11 @@
 // CHECK-MULTIPLESOURCEOK2: "-o" "cl-outputs.obj"
 // CHECK-MULTIPLESOURCEOK2: "-o" "cl-outputs.obj"
 
+// RUN: %clang_cl /c /Foa.obj -Xlinker -foo -### -- %s foo.obj foo.lib 2>&1 | FileCheck -check-prefix=FoLINKERINPUT %s
+// RUN: %clang_cl /Foa.obj -Xlinker -foo -### -- %s foo.lib 2>&1 | FileCheck -check-prefix=FoLINKERINPUT %s
+// FoLINKERINPUT-NOT: error: cannot specify '/Foa.obj' when compiling multiple source files
+// FoLINKERINPUT: "-o" "a.obj"
+
 // RUN: %clang_cl /c /oa -### -- %s 2>&1 | FileCheck -check-prefix=oNAME1 %s
 // oNAME1:  "-o" "a.obj"
 
@@ -243,6 +248,9 @@
 // RUN: %clang_cl /Fa -### -- %s %s 2>&1 | FileCheck -check-prefix=FaMULTIPLESOURCEOK %s
 // FaMULTIPLESOURCEOK: "-o" "cl-outputs.asm"
 // FaMULTIPLESOURCEOK: "-o" "cl-outputs.asm"
+// RUN: %clang_cl /Faa.asm -Xlinker -foo -### -- %s foo.obj 2>&1 | FileCheck -check-prefix=FaLINKERINPUT %s
+// FaLINKERINPUT-NOT: error: cannot specify '/Faa.asm' when compiling multiple source files
+// FaLINKERINPUT: "-o" "a.asm"
 
 // Copy of the same tests above, but with /FAcsu
 // RUN: %clang_cl /FAcsu -### -- %s 2>&1 | FileCheck -check-prefix=FA_CSU %s

``````````

</details>


https://github.com/llvm/llvm-project/pull/232611


More information about the cfe-commits mailing list