Files
llvm-project/llvm/lib/Support/VirtualOutputError.cpp
Steven Wu 07d0225d41 Support: Add vfs::OutputBackend and OutputFile to virtualize compiler outputs (#113363)
Add OutputBackend and OutputFile to the `llvm::vfs` namespace for
virtualizing compiler outputs. This is intended for use in Clang,

The headers are:

- llvm/Support/VirtualOutputConfig.h
- llvm/Support/VirtualOutputError.h
- llvm/Support/VirtualOutputFile.h
- llvm/Support/VirtualOutputBackend.h

OutputFile is moveable and owns an OutputFileImpl, which is provided by
the derived OutputBackend.

- OutputFileImpl::keep() and OutputFileImpl::discard() should keep or
  discard the output.  OutputFile guarantees that exactly one of these
  will be called before destruction.
- OutputFile::keep() and OutputFile::discard() wrap OutputFileImpl
  and catch usage errors such as double-close.
- OutputFile::discardOnDestroy() installs an error handler for the
  destructor to use if the file is still open. The handler will be
  called if discard() fails.
- OutputFile::~OutputFile() calls report_fatal_error() if none of
  keep(), discard(), or discardOnDestroy() has been called. It still
  calls OutputFileImpl::discard().
- getOS() returns the wrapped raw_pwrite_stream. For convenience,
  OutputFile has an implicit conversion to `raw_ostream` and
  `raw_ostream &operator<<(OutputFile&, T&&)`.

OutputBackend can be stored in IntrusiveRefCntPtr.

- Most operations are thread-safe.
- clone() returns a backend that targets the same destination.
  All operations are thread-safe when done on different clones.
- createFile() takes a path and an OutputConfig (see below) and returns
  an OutputFile. Backends implement createFileImpl().

OutputConfig has flags to configure the output. Backends may ignore or
override flags that aren't relevant or implementable.

- The initial flags are:
    - AtomicWrite: whether the output should appear atomically (e.g., by
      using a temporary file and renaming it).
    - CrashCleanup: whether the output should be cleaned up if there's a
      crash (e.g., with RemoveFileOnSignal).
    - ImplyCreateDirectories: whether to implicitly create missing
      directories in the path to the file.
    - Text: matches sys::fs::OF_Text.
    - CRLF: matches sys::fs::OF_CRLF.
    - Append: matches sys::fs::OF_Append and can use with AtomicWrite
      for atomic append.
- OnlyIfDifferent: skip writting the output file if the existing file
      at the output path is identical to the content to be written.
- Each "Flag" has `setFlag(bool)` and `bool getFlag()` and shortcuts
  `setFlag()` and `setNoFlag()`. The setters are `constexpr` and return
  `OutputConfig&` to make it easy to declare a default value for a filed
  in a class or struct.
- Setters and getters for Binary and TextWithCRLF are derived from Text
  and CRLF. For convenience, sys::fs::OpenFlags can be passed
  directly to setOpenFlags().

This patch intentionally lacks a number of important features that have
been left for follow-ups:

- Set a (virtual) current working directory.
- Create a directory.
- Create a file or directory with a unique name (avoiding collisions
  with existing filenames).

Patch originally by dexonsmith
2025-09-05 10:31:02 -07:00

74 lines
2.0 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file implements the errors for output virtualization.
///
//===----------------------------------------------------------------------===//
#include "llvm/Support/VirtualOutputError.h"
using namespace llvm;
using namespace llvm::vfs;
void OutputError::anchor() {}
void OutputConfigError::anchor() {}
void TempFileOutputError::anchor() {}
char OutputError::ID = 0;
char OutputConfigError::ID = 0;
char TempFileOutputError::ID = 0;
void OutputError::log(raw_ostream &OS) const {
OS << getOutputPath() << ": ";
ECError::log(OS);
}
void OutputConfigError::log(raw_ostream &OS) const {
OutputError::log(OS);
OS << ": " << Config;
}
void TempFileOutputError::log(raw_ostream &OS) const {
OS << getTempPath() << " => ";
OutputError::log(OS);
}
namespace {
class OutputErrorCategory : public std::error_category {
public:
const char *name() const noexcept override;
std::string message(int EV) const override;
};
} // end namespace
const std::error_category &vfs::output_category() {
static OutputErrorCategory ErrorCategory;
return ErrorCategory;
}
const char *OutputErrorCategory::name() const noexcept {
return "llvm.vfs.output";
}
std::string OutputErrorCategory::message(int EV) const {
OutputErrorCode E = static_cast<OutputErrorCode>(EV);
switch (E) {
case OutputErrorCode::invalid_config:
return "invalid config";
case OutputErrorCode::not_closed:
return "output not closed";
case OutputErrorCode::already_closed:
return "output already closed";
case OutputErrorCode::has_open_proxy:
return "output has open proxy";
}
llvm_unreachable(
"An enumerator of OutputErrorCode does not have a message defined.");
}