1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MemoryBuffer interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
15 #define LLVM_SUPPORT_MEMORYBUFFER_H
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"
29 class MemoryBufferRef;
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'.
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.
42 const char *BufferStart; // Start of the buffer.
43 const char *BufferEnd; // End of the buffer.
47 MemoryBuffer() = default;
49 void init(const char *BufStart, const char *BufEnd,
50 bool RequiresNullTerminator);
52 static constexpr bool Writable = false;
55 MemoryBuffer(const MemoryBuffer &) = delete;
56 MemoryBuffer &operator=(const MemoryBuffer &) = delete;
57 virtual ~MemoryBuffer();
59 const char *getBufferStart() const { return BufferStart; }
60 const char *getBufferEnd() const { return BufferEnd; }
61 size_t getBufferSize() const { return BufferEnd-BufferStart; }
63 StringRef getBuffer() const {
64 return StringRef(BufferStart, getBufferSize());
67 /// Return an identifier for this buffer, typically the filename it was read
69 virtual StringRef getBufferIdentifier() const { return "Unknown buffer"; }
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
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);
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);
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);
96 /// Given an already-open file descriptor, read the file and return a
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);
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);
112 static std::unique_ptr<MemoryBuffer>
113 getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true);
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 = "");
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 = "");
126 /// Read all of stdin into a file buffer, and return it.
127 static ErrorOr<std::unique_ptr<MemoryBuffer>> getSTDIN();
129 /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
131 static ErrorOr<std::unique_ptr<MemoryBuffer>>
132 getFileOrSTDIN(const Twine &Filename, int64_t FileSize = -1,
133 bool RequiresNullTerminator = true);
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);
140 //===--------------------------------------------------------------------===//
141 // Provided for performance analysis.
142 //===--------------------------------------------------------------------===//
144 /// The kind of memory backing used to support the MemoryBuffer.
150 /// Return information on the memory mechanism used to support the
152 virtual BufferKind getBufferKind() const = 0;
154 MemoryBufferRef getMemBufferRef() const;
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
161 class WritableMemoryBuffer : public MemoryBuffer {
163 WritableMemoryBuffer() = default;
165 static constexpr bool Writable = true;
168 using MemoryBuffer::getBuffer;
169 using MemoryBuffer::getBufferEnd;
170 using MemoryBuffer::getBufferStart;
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());
177 char *getBufferEnd() {
178 return const_cast<char *>(MemoryBuffer::getBufferEnd());
180 MutableArrayRef<char> getBuffer() {
181 return {getBufferStart(), getBufferEnd()};
184 static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
185 getFile(const Twine &Filename, int64_t FileSize = -1,
186 bool IsVolatile = false);
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);
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 = "");
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;
213 class MemoryBufferRef {
215 StringRef Identifier;
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) {}
224 StringRef getBuffer() const { return Buffer; }
226 StringRef getBufferIdentifier() const { return Identifier; }
228 const char *getBufferStart() const { return Buffer.begin(); }
229 const char *getBufferEnd() const { return Buffer.end(); }
230 size_t getBufferSize() const { return Buffer.size(); }
233 // Create wrappers for C Binding types (see CBindingWrapping.h).
234 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef)
236 } // end namespace llvm
238 #endif // LLVM_SUPPORT_MEMORYBUFFER_H