Frontend: Respect -working-directory when checking if output files can be written

Call `FixupRelativePath` when opening output files to ensure that
`-working-directory` is used when checking up front for write failures,
not just when finalizing the files at the end. This also moves the
temporary file into the same directory as the output file.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D95497
This commit is contained in:
Steven Wu 2022-09-09 08:56:53 -07:00
parent fa40fd40e0
commit 493766e068
2 changed files with 19 additions and 6 deletions

View File

@ -781,12 +781,7 @@ void CompilerInstance::clearOutputFiles(bool EraseFiles) {
continue;
}
// If '-working-directory' was passed, the output filename should be
// relative to that.
SmallString<128> NewOutFile(OF.Filename);
FileMgr->FixupRelativePath(NewOutFile);
llvm::Error E = OF.File->keep(NewOutFile);
llvm::Error E = OF.File->keep(OF.Filename);
if (!E)
continue;
@ -849,6 +844,15 @@ CompilerInstance::createOutputFileImpl(StringRef OutputPath, bool Binary,
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<SmallString<128>> AbsPath;
if (OutputPath != "-" && !llvm::sys::path::is_absolute(OutputPath)) {
AbsPath.emplace(OutputPath);
FileMgr->FixupRelativePath(*AbsPath);
OutputPath = *AbsPath;
}
std::unique_ptr<llvm::raw_fd_ostream> OS;
Optional<StringRef> OSFile;

View File

@ -2,3 +2,12 @@
// 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
// RUN: %clang_cc1 -working-directory %t.d -E %s -o - | FileCheck %s
// CHECK: # 1