[PATCH] D133504: Support: Add vfs::OutputBackend and OutputFile to virtualize compiler outputs

Steven Wu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 8 09:36:15 PDT 2022


steven_wu created this revision.
steven_wu added reviewers: sammccall, benlangmuir, raghavmedicherla, kzhuravl, dexonsmith.
Herald added subscribers: ributzka, hiraditya, mgorny.
Herald added a project: All.
steven_wu requested review of this revision.
Herald added a project: LLVM.

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.
- 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 by dexonsmith


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D133504

Files:
  llvm/include/llvm/Support/VirtualOutputBackend.h
  llvm/include/llvm/Support/VirtualOutputBackends.h
  llvm/include/llvm/Support/VirtualOutputConfig.def
  llvm/include/llvm/Support/VirtualOutputConfig.h
  llvm/include/llvm/Support/VirtualOutputError.h
  llvm/include/llvm/Support/VirtualOutputFile.h
  llvm/lib/Support/CMakeLists.txt
  llvm/lib/Support/VirtualOutputBackend.cpp
  llvm/lib/Support/VirtualOutputBackends.cpp
  llvm/lib/Support/VirtualOutputConfig.cpp
  llvm/lib/Support/VirtualOutputError.cpp
  llvm/lib/Support/VirtualOutputFile.cpp
  llvm/unittests/Support/CMakeLists.txt
  llvm/unittests/Support/VirtualOutputBackendTest.cpp
  llvm/unittests/Support/VirtualOutputBackendsTest.cpp
  llvm/unittests/Support/VirtualOutputConfigTest.cpp
  llvm/unittests/Support/VirtualOutputFileTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133504.458775.patch
Type: text/x-patch
Size: 94326 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220908/bd5e0bb0/attachment.bin>


More information about the llvm-commits mailing list