LLVM 22.0.0git
VirtualOutputFile.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file contains the declarations of the llvm::vfs::OutputFile class,
11/// which is a virtualized output file from output backend. \c OutputFile can be
12/// use a \c raw_pwrite_stream for writing, and are required to be `keep()` or
13/// `discard()` in the end.
14///
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_SUPPORT_VIRTUALOUTPUTFILE_H
18#define LLVM_SUPPORT_VIRTUALOUTPUTFILE_H
19
21#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/Error.h"
26
27namespace llvm::vfs {
28
29class OutputFileImpl : public RTTIExtends<OutputFileImpl, RTTIRoot> {
30 void anchor() override;
31
32public:
33 static char ID;
34 virtual ~OutputFileImpl() = default;
35
36 virtual Error keep() = 0;
37 virtual Error discard() = 0;
38 virtual raw_pwrite_stream &getOS() = 0;
39};
40
42 : public RTTIExtends<NullOutputFileImpl, OutputFileImpl> {
43 void anchor() override;
44
45public:
46 static char ID;
47 Error keep() final { return Error::success(); }
48 Error discard() final { return Error::success(); }
49 raw_pwrite_stream &getOS() final { return OS; }
50
51private:
53};
54
55/// A virtualized output file that writes to a specific backend.
56///
57/// One of \a keep(), \a discard(), or \a discardOnDestroy() must be called
58/// before destruction.
60public:
61 StringRef getPath() const { return Path; }
62
63 /// Check if \a keep() or \a discard() has already been called.
64 bool isOpen() const { return bool(Impl); }
65
66 explicit operator bool() const { return isOpen(); }
67
69 assert(isOpen() && "Expected open output stream");
70 return Impl->getOS();
71 }
72 operator raw_pwrite_stream &() { return getOS(); }
73 template <class T> raw_ostream &operator<<(T &&V) {
74 return getOS() << std::forward<T>(V);
75 }
76
77 /// Keep an output. Errors if this fails.
78 ///
79 /// If it has already been closed, calls \a report_fatal_error().
80 ///
81 /// If there's an open proxy from \a createProxy(), calls \a discard() to
82 /// clean up temporaries followed by \a report_fatal_error().
83 Error keep();
84
85 /// Discard an output, cleaning up any temporary state. Errors if clean-up
86 /// fails.
87 ///
88 /// If it has already been closed, calls \a report_fatal_error().
89 Error discard();
90
91 /// Discard the output when destroying it if it's still open, sending the
92 /// result to \a Handler.
93 void discardOnDestroy(unique_function<void(Error E)> Handler) {
94 DiscardOnDestroyHandler = std::move(Handler);
95 }
96
97 /// Create a proxy stream for clients that need to pass an owned stream to a
98 /// producer. Errors if there's already a proxy. The proxy must be deleted
99 /// before calling \a keep(). The proxy will crash if it's written to after
100 /// calling \a discard().
102
103 bool hasOpenProxy() const { return OpenProxy; }
104
105 /// Take the implementation.
106 ///
107 /// \pre \a hasOpenProxy() is false.
108 /// \pre \a discardOnDestroy() has not been called.
109 std::unique_ptr<OutputFileImpl> takeImpl() {
110 assert(!hasOpenProxy() && "Unexpected open proxy");
111 assert(!DiscardOnDestroyHandler && "Unexpected discard handler");
112 return std::move(Impl);
113 }
114
115 /// Check whether this is a null output file.
116 bool isNull() const { return Impl && isa<NullOutputFileImpl>(*Impl); }
117
118 OutputFile() = default;
119
120 explicit OutputFile(const Twine &Path, std::unique_ptr<OutputFileImpl> Impl)
121 : Path(Path.str()), Impl(std::move(Impl)) {
122 assert(this->Impl && "Expected open output file");
123 }
124
125 ~OutputFile() { destroy(); }
126 OutputFile(OutputFile &&O) { moveFrom(O); }
128 destroy();
129 return moveFrom(O);
130 }
131
132private:
133 /// Destroy \a Impl. Reports fatal error if the file is open and there's no
134 /// handler from \a discardOnDestroy().
135 void destroy();
136 OutputFile &moveFrom(OutputFile &O) {
137 Path = std::move(O.Path);
138 Impl = std::move(O.Impl);
139 DiscardOnDestroyHandler = std::move(O.DiscardOnDestroyHandler);
140 OpenProxy = O.OpenProxy;
141 O.OpenProxy = nullptr;
142 return *this;
143 }
144
145 std::string Path;
146 std::unique_ptr<OutputFileImpl> Impl;
147 unique_function<void(Error E)> DiscardOnDestroyHandler;
148
149 class TrackedProxy;
150 TrackedProxy *OpenProxy = nullptr;
151};
152
153/// Update \p File to silently discard itself if it's still open when it's
154/// destroyed.
156 File.discardOnDestroy(consumeError);
157}
158
159/// Update \p File to silently discard itself if it's still open when it's
160/// destroyed.
166
167} // namespace llvm::vfs
168
169#endif // LLVM_SUPPORT_VIRTUALOUTPUTFILE_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
#define T
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Inheritance utility for extensible RTTI.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
A raw_ostream that discards all output.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
An abstract base class for streams implementations that also support a pwrite operation.
unique_function is a type-erasing functor similar to std::function.
Represents an open file.
raw_pwrite_stream & getOS() final
virtual raw_pwrite_stream & getOS()=0
virtual Error discard()=0
virtual Error keep()=0
virtual ~OutputFileImpl()=default
A virtualized output file that writes to a specific backend.
bool isOpen() const
Check if keep() or discard() has already been called.
void discardOnDestroy(unique_function< void(Error E)> Handler)
Discard the output when destroying it if it's still open, sending the result to Handler.
StringRef getPath() const
raw_ostream & operator<<(T &&V)
raw_pwrite_stream & getOS()
std::unique_ptr< OutputFileImpl > takeImpl()
Take the implementation.
bool isNull() const
Check whether this is a null output file.
OutputFile(const Twine &Path, std::unique_ptr< OutputFileImpl > Impl)
Expected< std::unique_ptr< raw_pwrite_stream > > createProxy()
Create a proxy stream for clients that need to pass an owned stream to a producer.
OutputFile & operator=(OutputFile &&O)
Error discard()
Discard an output, cleaning up any temporary state.
Error keep()
Keep an output.
void consumeDiscardOnDestroy(OutputFile &File)
Update File to silently discard itself if it's still open when it's destroyed.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1869
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1083
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:851