Index: clang/lib/Frontend/CompilerInstance.cpp =================================================================== --- clang/lib/Frontend/CompilerInstance.cpp +++ clang/lib/Frontend/CompilerInstance.cpp @@ -661,11 +661,7 @@ if (OF.TempFilename.empty()) continue; - // If '-working-directory' was passed, the output filename should be - // relative to that. - SmallString<128> NewOutFile(OF.Filename); - FileMgr->FixupRelativePath(NewOutFile); - std::error_code EC = llvm::sys::fs::rename(OF.TempFilename, NewOutFile); + std::error_code EC = llvm::sys::fs::rename(OF.TempFilename, OF.Filename); if (!EC) continue; getDiagnostics().Report(diag::err_unable_to_rename_temp) @@ -730,6 +726,15 @@ assert((!CreateMissingDirectories || UseTemporary) && "CreateMissingDirectories is only allowed when using temporary files"); + // If '-working-directory' was passed, the output filename should be + // relative to that. + Optional> AbsPath; + if (!llvm::sys::path::is_absolute(OutputPath)) { + AbsPath.emplace(OutputPath); + FileMgr->FixupRelativePath(*AbsPath); + OutputPath = *AbsPath; + } + std::unique_ptr OS; Optional OSFile; Index: clang/test/Frontend/output-paths.c =================================================================== --- clang/test/Frontend/output-paths.c +++ clang/test/Frontend/output-paths.c @@ -2,3 +2,9 @@ // RUN: FileCheck -check-prefix=OUTPUTFAIL -DMSG=%errc_ENOENT -input-file=%t %s // OUTPUTFAIL: error: unable to open output file '{{.*}}doesnotexist{{.}}somename': '[[MSG]]' + +// Check that -working-directory is respected when diagnosing output failures. +// +// RUN: rm -rf %t.d && mkdir -p %t.d/%basename_t-inner.d +// RUN: %clang_cc1 -emit-llvm -working-directory %t.d -E -o %basename_t-inner.d/somename %s -verify +// expected-no-diagnostics