diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 90d8e823d1d02..072b6415d7cb9 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -6204,10 +6204,28 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA, } llvm::PrettyStackTraceString CrashInfo("Computing output path"); + // Output to a user requested destination? if (AtTopLevel && !isa(JA) && !isa(JA)) { - if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) + bool IsCLNonPCH = + IsCLMode() && !C.getArgs().hasArg(options::OPT__SLASH_Yc) && + (isa(JA) || isa(JA)); + bool HasAnyOutputArg = + C.getArgs().hasArg(options::OPT_o, options::OPT__SLASH_Fo); + + Arg *FinalOutput = nullptr; + if (IsCLNonPCH && HasAnyOutputArg) { + FinalOutput = + C.getArgs().getLastArg(options::OPT_o, options::OPT__SLASH_Fo); + } else if (IsCLNonPCH) { + FinalOutput = C.getArgs().getLastArg(options::OPT__SLASH_Fo); + } else { + FinalOutput = C.getArgs().getLastArg(options::OPT_o); + } + + if (FinalOutput) { return C.addResultFile(FinalOutput->getValue(), &JA); + } } // For /P, preprocess to file named after BaseInput. diff --git a/clang/test/Driver/cl-cxx20-modules.cppm b/clang/test/Driver/cl-cxx20-modules.cppm index 43dbf517485a0..dc5e312159b3f 100644 --- a/clang/test/Driver/cl-cxx20-modules.cppm +++ b/clang/test/Driver/cl-cxx20-modules.cppm @@ -14,3 +14,11 @@ //--- test.pcm // CPP20WARNING-NOT: clang-cl: warning: argument unused during compilation: '/std:c++20' [-Wunused-command-line-argument] + +// test whether the following outputs %Hello.bmi +// RUN: %clang_cl /std:c++20 --precompile -x c++-module -Fo:"%t/Hello.bmi" -c -- %t/Hello.cppm -### 2>&1 | FileCheck %s +// CHECK: "-emit-module-interface" +// CHECK: "-o" "{{.*}}Hello.bmi" + +//--- Hello.cppm +export module Hello;