]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Support/MemoryBuffer.h
MFV r338519:
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Support / MemoryBuffer.h
1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the MemoryBuffer interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
15 #define LLVM_SUPPORT_MEMORYBUFFER_H
16
17 #include "llvm-c/Types.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/CBindingWrapping.h"
22 #include "llvm/Support/ErrorOr.h"
23 #include <cstddef>
24 #include <cstdint>
25 #include <memory>
26
27 namespace llvm {
28
29 class MemoryBufferRef;
30
31 /// This interface provides simple read-only access to a block of memory, and
32 /// provides simple methods for reading files and standard input into a memory
33 /// buffer.  In addition to basic access to the characters in the file, this
34 /// interface guarantees you can read one character past the end of the file,
35 /// and that this character will read as '\0'.
36 ///
37 /// The '\0' guarantee is needed to support an optimization -- it's intended to
38 /// be more efficient for clients which are reading all the data to stop
39 /// reading when they encounter a '\0' than to continually check the file
40 /// position to see if it has reached the end of the file.
41 class MemoryBuffer {
42   const char *BufferStart; // Start of the buffer.
43   const char *BufferEnd;   // End of the buffer.
44
45
46 protected:
47   MemoryBuffer() = default;
48
49   void init(const char *BufStart, const char *BufEnd,
50             bool RequiresNullTerminator);
51
52   static constexpr bool Writable = false;
53
54 public:
55   MemoryBuffer(const MemoryBuffer &) = delete;
56   MemoryBuffer &operator=(const MemoryBuffer &) = delete;
57   virtual ~MemoryBuffer();
58
59   const char *getBufferStart() const { return BufferStart; }
60   const char *getBufferEnd() const   { return BufferEnd; }
61   size_t getBufferSize() const { return BufferEnd-BufferStart; }
62
63   StringRef getBuffer() const {
64     return StringRef(BufferStart, getBufferSize());
65   }
66
67   /// Return an identifier for this buffer, typically the filename it was read
68   /// from.
69   virtual StringRef getBufferIdentifier() const { return "Unknown buffer"; }
70
71   /// Open the specified file as a MemoryBuffer, returning a new MemoryBuffer
72   /// if successful, otherwise returning null. If FileSize is specified, this
73   /// means that the client knows that the file exists and that it has the
74   /// specified size.
75   ///
76   /// \param IsVolatile Set to true to indicate that the contents of the file
77   /// can change outside the user's control, e.g. when libclang tries to parse
78   /// while the user is editing/updating the file or if the file is on an NFS.
79   static ErrorOr<std::unique_ptr<MemoryBuffer>>
80   getFile(const Twine &Filename, int64_t FileSize = -1,
81           bool RequiresNullTerminator = true, bool IsVolatile = false);
82
83   /// Read all of the specified file into a MemoryBuffer as a stream
84   /// (i.e. until EOF reached). This is useful for special files that
85   /// look like a regular file but have 0 size (e.g. /proc/cpuinfo on Linux).
86   static ErrorOr<std::unique_ptr<MemoryBuffer>>
87   getFileAsStream(const Twine &Filename);
88
89   /// Given an already-open file descriptor, map some slice of it into a
90   /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize.
91   /// Since this is in the middle of a file, the buffer is not null terminated.
92   static ErrorOr<std::unique_ptr<MemoryBuffer>>
93   getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize,
94                    int64_t Offset, bool IsVolatile = false);
95
96   /// Given an already-open file descriptor, read the file and return a
97   /// MemoryBuffer.
98   ///
99   /// \param IsVolatile Set to true to indicate that the contents of the file
100   /// can change outside the user's control, e.g. when libclang tries to parse
101   /// while the user is editing/updating the file or if the file is on an NFS.
102   static ErrorOr<std::unique_ptr<MemoryBuffer>>
103   getOpenFile(int FD, const Twine &Filename, uint64_t FileSize,
104               bool RequiresNullTerminator = true, bool IsVolatile = false);
105
106   /// Open the specified memory range as a MemoryBuffer. Note that InputData
107   /// must be null terminated if RequiresNullTerminator is true.
108   static std::unique_ptr<MemoryBuffer>
109   getMemBuffer(StringRef InputData, StringRef BufferName = "",
110                bool RequiresNullTerminator = true);
111
112   static std::unique_ptr<MemoryBuffer>
113   getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true);
114
115   /// Open the specified memory range as a MemoryBuffer, copying the contents
116   /// and taking ownership of it. InputData does not have to be null terminated.
117   static std::unique_ptr<MemoryBuffer>
118   getMemBufferCopy(StringRef InputData, const Twine &BufferName = "");
119
120   /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
121   /// that the caller need not initialize the memory allocated by this method.
122   /// The memory is owned by the MemoryBuffer object.
123   static std::unique_ptr<MemoryBuffer>
124   getNewMemBuffer(size_t Size, StringRef BufferName = "");
125
126   /// Read all of stdin into a file buffer, and return it.
127   static ErrorOr<std::unique_ptr<MemoryBuffer>> getSTDIN();
128
129   /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
130   /// is "-".
131   static ErrorOr<std::unique_ptr<MemoryBuffer>>
132   getFileOrSTDIN(const Twine &Filename, int64_t FileSize = -1,
133                  bool RequiresNullTerminator = true);
134
135   /// Map a subrange of the specified file as a MemoryBuffer.
136   static ErrorOr<std::unique_ptr<MemoryBuffer>>
137   getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
138                bool IsVolatile = false);
139
140   //===--------------------------------------------------------------------===//
141   // Provided for performance analysis.
142   //===--------------------------------------------------------------------===//
143
144   /// The kind of memory backing used to support the MemoryBuffer.
145   enum BufferKind {
146     MemoryBuffer_Malloc,
147     MemoryBuffer_MMap
148   };
149
150   /// Return information on the memory mechanism used to support the
151   /// MemoryBuffer.
152   virtual BufferKind getBufferKind() const = 0;
153
154   MemoryBufferRef getMemBufferRef() const;
155 };
156
157 /// This class is an extension of MemoryBuffer, which allows writing to the
158 /// underlying contents.  It only supports creation methods that are guaranteed
159 /// to produce a writable buffer.  For example, mapping a file read-only is not
160 /// supported.
161 class WritableMemoryBuffer : public MemoryBuffer {
162 protected:
163   WritableMemoryBuffer() = default;
164
165   static constexpr bool Writable = true;
166
167 public:
168   using MemoryBuffer::getBuffer;
169   using MemoryBuffer::getBufferEnd;
170   using MemoryBuffer::getBufferStart;
171
172   // const_cast is well-defined here, because the underlying buffer is
173   // guaranteed to have been initialized with a mutable buffer.
174   char *getBufferStart() {
175     return const_cast<char *>(MemoryBuffer::getBufferStart());
176   }
177   char *getBufferEnd() {
178     return const_cast<char *>(MemoryBuffer::getBufferEnd());
179   }
180   MutableArrayRef<char> getBuffer() {
181     return {getBufferStart(), getBufferEnd()};
182   }
183
184   static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
185   getFile(const Twine &Filename, int64_t FileSize = -1,
186           bool IsVolatile = false);
187
188   /// Map a subrange of the specified file as a WritableMemoryBuffer.
189   static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
190   getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
191                bool IsVolatile = false);
192
193   /// Allocate a new MemoryBuffer of the specified size that is not initialized.
194   /// Note that the caller should initialize the memory allocated by this
195   /// method. The memory is owned by the MemoryBuffer object.
196   static std::unique_ptr<WritableMemoryBuffer>
197   getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "");
198
199 private:
200   // Hide these base class factory function so one can't write
201   //   WritableMemoryBuffer::getXXX()
202   // and be surprised that he got a read-only Buffer.
203   using MemoryBuffer::getFileAsStream;
204   using MemoryBuffer::getFileOrSTDIN;
205   using MemoryBuffer::getMemBuffer;
206   using MemoryBuffer::getMemBufferCopy;
207   using MemoryBuffer::getNewMemBuffer;
208   using MemoryBuffer::getOpenFile;
209   using MemoryBuffer::getOpenFileSlice;
210   using MemoryBuffer::getSTDIN;
211 };
212
213 class MemoryBufferRef {
214   StringRef Buffer;
215   StringRef Identifier;
216
217 public:
218   MemoryBufferRef() = default;
219   MemoryBufferRef(MemoryBuffer& Buffer)
220       : Buffer(Buffer.getBuffer()), Identifier(Buffer.getBufferIdentifier()) {}
221   MemoryBufferRef(StringRef Buffer, StringRef Identifier)
222       : Buffer(Buffer), Identifier(Identifier) {}
223
224   StringRef getBuffer() const { return Buffer; }
225
226   StringRef getBufferIdentifier() const { return Identifier; }
227
228   const char *getBufferStart() const { return Buffer.begin(); }
229   const char *getBufferEnd() const { return Buffer.end(); }
230   size_t getBufferSize() const { return Buffer.size(); }
231 };
232
233 // Create wrappers for C Binding types (see CBindingWrapping.h).
234 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef)
235
236 } // end namespace llvm
237
238 #endif // LLVM_SUPPORT_MEMORYBUFFER_H