Skip to content

Commit c974ac6

Browse files
[VirtualOutputBackend] Proper error reporting for FileSystem errors
Return proper llvm::Error from virtual output backend when it discovers file system error when keep/discard. Instead of triggering the fatal error in raw_fd_ostream, report the error_code as llvm::Error to caller. rdar://107855318
1 parent 73bb615 commit c974ac6

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

llvm/lib/Support/VirtualOutputBackends.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ class OnDiskOutputFile final : public OutputFileImpl {
213213

214214
Error initializeFD(Optional<int> &FD);
215215
Error initializeStream();
216+
Error reset();
216217

217218
OnDiskOutputFile(StringRef OutputPath, Optional<OutputConfig> Config,
218219
const OnDiskOutputBackend::OutputSettings &Settings)
@@ -420,10 +421,23 @@ areFilesDifferent(const llvm::Twine &Source, const llvm::Twine &Destination) {
420421
return FileDifference::SameContents;
421422
}
422423

423-
Error OnDiskOutputFile::keep() {
424+
Error OnDiskOutputFile::reset() {
424425
// Destroy the streams to flush them.
425426
BufferOS.reset();
427+
if (!FileOS)
428+
return Error::success();
429+
430+
// Remember the error in raw_fd_ostream to be reported later.
431+
std::error_code EC = FileOS->error();
432+
// Clear the error to avoid fatal error when reset.
433+
FileOS->clear_error();
426434
FileOS.reset();
435+
return errorCodeToError(EC);
436+
}
437+
438+
Error OnDiskOutputFile::keep() {
439+
if (auto E = reset())
440+
return E;
427441

428442
// Close the file descriptor and remove crash cleanup before exit.
429443
auto RemoveDiscardOnSignal = make_scope_exit([&]() {
@@ -483,8 +497,8 @@ Error OnDiskOutputFile::keep() {
483497

484498
Error OnDiskOutputFile::discard() {
485499
// Destroy the streams to flush them.
486-
BufferOS.reset();
487-
FileOS.reset();
500+
if (auto E = reset())
501+
return E;
488502

489503
// Nothing on the filesystem to remove for stdout.
490504
if (OutputPath == "-")

0 commit comments

Comments
 (0)