diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index f817c9fc8558..35dcf206abb8 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -4261,10 +4261,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back(A->getValue()); } else { SmallString<128> F; - if (Output.isFilename() && (Args.hasArg(options::OPT_c) || - Args.hasArg(options::OPT_S))) { - F = Output.getFilename(); - } else { + + if (Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) { + if (Arg *FinalOutput = Args.getLastArg(options::OPT_o)) + F = FinalOutput->getValue(); + } + + if (F.empty()) { // Use the input filename. F = llvm::sys::path::stem(Input.getBaseInput()); diff --git a/clang/test/Driver/opt-record.c b/clang/test/Driver/opt-record.c index 6e684878d448..e261d16260eb 100644 --- a/clang/test/Driver/opt-record.c +++ b/clang/test/Driver/opt-record.c @@ -1,6 +1,13 @@ // RUN: %clang -### -S -o FOO -fsave-optimization-record %s 2>&1 | FileCheck %s // RUN: %clang -### -c -o FOO -fsave-optimization-record %s 2>&1 | FileCheck %s +// RUN: %clang -### -c -o FOO.o -fsave-optimization-record %s 2>&1 | FileCheck %s +// RUN: %clang -### -no-integrated-as -S -o FOO -fsave-optimization-record %s 2>&1 | FileCheck %s +// RUN: %clang -### -no-integrated-as -c -o FOO.o -fsave-optimization-record %s 2>&1 | FileCheck %s +// RUN: %clang -### -save-temps -S -o FOO -fsave-optimization-record %s 2>&1 | FileCheck %s +// RUN: %clang -### -save-temps -c -o FOO.o -fsave-optimization-record %s 2>&1 | FileCheck %s // RUN: %clang -### -c -fsave-optimization-record %s 2>&1 | FileCheck %s -check-prefix=CHECK-NO-O +// RUN: %clang -### -no-integrated-as -c -fsave-optimization-record %s 2>&1 | FileCheck %s -check-prefix=CHECK-NO-O +// RUN: %clang -### -save-temps -c -fsave-optimization-record %s 2>&1 | FileCheck %s -check-prefix=CHECK-NO-O // RUN: %clang -### -fsave-optimization-record %s 2>&1 | FileCheck %s -check-prefix=CHECK-NO-O // RUN: %clang -### -S -fsave-optimization-record -x cuda -nocudainc -nocudalib %s 2>&1 | FileCheck %s -check-prefix=CHECK-NO-O -check-prefix=CHECK-CUDA-DEV // RUN: %clang -### -fsave-optimization-record -x cuda -nocudainc -nocudalib %s 2>&1 | FileCheck %s -check-prefix=CHECK-NO-O -check-prefix=CHECK-CUDA-DEV