LLVM  14.0.0git
MemoryBuffer.h
Go to the documentation of this file.
1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
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 // This file defines the MemoryBuffer interface.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
14 #define LLVM_SUPPORT_MEMORYBUFFER_H
15 
16 #include "llvm-c/Types.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/ErrorOr.h"
23 #include <cstddef>
24 #include <cstdint>
25 #include <memory>
26 
27 namespace llvm {
28 namespace sys {
29 namespace fs {
30 // Duplicated from FileSystem.h to avoid a dependency.
31 #if defined(_WIN32)
32 // A Win32 HANDLE is a typedef of void*
33 using file_t = void *;
34 #else
35 using file_t = int;
36 #endif
37 } // namespace fs
38 } // namespace sys
39 
40 /// This interface provides simple read-only access to a block of memory, and
41 /// provides simple methods for reading files and standard input into a memory
42 /// buffer. In addition to basic access to the characters in the file, this
43 /// interface guarantees you can read one character past the end of the file,
44 /// and that this character will read as '\0'.
45 ///
46 /// The '\0' guarantee is needed to support an optimization -- it's intended to
47 /// be more efficient for clients which are reading all the data to stop
48 /// reading when they encounter a '\0' than to continually check the file
49 /// position to see if it has reached the end of the file.
50 class MemoryBuffer {
51  const char *BufferStart; // Start of the buffer.
52  const char *BufferEnd; // End of the buffer.
53 
54 protected:
55  MemoryBuffer() = default;
56 
57  void init(const char *BufStart, const char *BufEnd,
58  bool RequiresNullTerminator);
59 
60 public:
61  MemoryBuffer(const MemoryBuffer &) = delete;
62  MemoryBuffer &operator=(const MemoryBuffer &) = delete;
63  virtual ~MemoryBuffer();
64 
65  const char *getBufferStart() const { return BufferStart; }
66  const char *getBufferEnd() const { return BufferEnd; }
67  size_t getBufferSize() const { return BufferEnd-BufferStart; }
68 
69  StringRef getBuffer() const {
70  return StringRef(BufferStart, getBufferSize());
71  }
72 
73  /// Return an identifier for this buffer, typically the filename it was read
74  /// from.
75  virtual StringRef getBufferIdentifier() const { return "Unknown buffer"; }
76 
77  /// Open the specified file as a MemoryBuffer, returning a new MemoryBuffer
78  /// if successful, otherwise returning null.
79  ///
80  /// \param IsText Set to true to indicate that the file should be read in
81  /// text mode.
82  ///
83  /// \param IsVolatile Set to true to indicate that the contents of the file
84  /// can change outside the user's control, e.g. when libclang tries to parse
85  /// while the user is editing/updating the file or if the file is on an NFS.
87  getFile(const Twine &Filename, bool IsText = false,
88  bool RequiresNullTerminator = true, bool IsVolatile = false);
89 
90  /// Read all of the specified file into a MemoryBuffer as a stream
91  /// (i.e. until EOF reached). This is useful for special files that
92  /// look like a regular file but have 0 size (e.g. /proc/cpuinfo on Linux).
94  getFileAsStream(const Twine &Filename);
95 
96  /// Given an already-open file descriptor, map some slice of it into a
97  /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize.
98  /// Since this is in the middle of a file, the buffer is not null terminated.
100  getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize,
101  int64_t Offset, bool IsVolatile = false);
102 
103  /// Given an already-open file descriptor, read the file and return a
104  /// MemoryBuffer.
105  ///
106  /// \param IsVolatile Set to true to indicate that the contents of the file
107  /// can change outside the user's control, e.g. when libclang tries to parse
108  /// while the user is editing/updating the file or if the file is on an NFS.
110  getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
111  bool RequiresNullTerminator = true, bool IsVolatile = false);
112 
113  /// Open the specified memory range as a MemoryBuffer. Note that InputData
114  /// must be null terminated if RequiresNullTerminator is true.
115  static std::unique_ptr<MemoryBuffer>
116  getMemBuffer(StringRef InputData, StringRef BufferName = "",
117  bool RequiresNullTerminator = true);
118 
119  static std::unique_ptr<MemoryBuffer>
120  getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true);
121 
122  /// Open the specified memory range as a MemoryBuffer, copying the contents
123  /// and taking ownership of it. InputData does not have to be null terminated.
124  static std::unique_ptr<MemoryBuffer>
125  getMemBufferCopy(StringRef InputData, const Twine &BufferName = "");
126 
127  /// Read all of stdin into a file buffer, and return it.
129 
130  /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
131  /// is "-".
133  getFileOrSTDIN(const Twine &Filename, bool IsText = false,
134  bool RequiresNullTerminator = true);
135 
136  /// Map a subrange of the specified file as a MemoryBuffer.
138  getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
139  bool IsVolatile = false);
140 
141  //===--------------------------------------------------------------------===//
142  // Provided for performance analysis.
143  //===--------------------------------------------------------------------===//
144 
145  /// The kind of memory backing used to support the MemoryBuffer.
146  enum BufferKind {
149  };
150 
151  /// Return information on the memory mechanism used to support the
152  /// MemoryBuffer.
153  virtual BufferKind getBufferKind() const = 0;
154 
156 };
157 
158 /// This class is an extension of MemoryBuffer, which allows copy-on-write
159 /// access to the underlying contents. It only supports creation methods that
160 /// are guaranteed to produce a writable buffer. For example, mapping a file
161 /// read-only is not supported.
163 protected:
164  WritableMemoryBuffer() = default;
165 
166 public:
170 
171  // const_cast is well-defined here, because the underlying buffer is
172  // guaranteed to have been initialized with a mutable buffer.
173  char *getBufferStart() {
174  return const_cast<char *>(MemoryBuffer::getBufferStart());
175  }
176  char *getBufferEnd() {
177  return const_cast<char *>(MemoryBuffer::getBufferEnd());
178  }
180  return {getBufferStart(), getBufferEnd()};
181  }
182 
184  getFile(const Twine &Filename, bool IsVolatile = false);
185 
186  /// Map a subrange of the specified file as a WritableMemoryBuffer.
188  getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
189  bool IsVolatile = false);
190 
191  /// Allocate a new MemoryBuffer of the specified size that is not initialized.
192  /// Note that the caller should initialize the memory allocated by this
193  /// method. The memory is owned by the MemoryBuffer object.
194  static std::unique_ptr<WritableMemoryBuffer>
195  getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "");
196 
197  /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
198  /// that the caller need not initialize the memory allocated by this method.
199  /// The memory is owned by the MemoryBuffer object.
200  static std::unique_ptr<WritableMemoryBuffer>
201  getNewMemBuffer(size_t Size, const Twine &BufferName = "");
202 
203 private:
204  // Hide these base class factory function so one can't write
205  // WritableMemoryBuffer::getXXX()
206  // and be surprised that he got a read-only Buffer.
214 };
215 
216 /// This class is an extension of MemoryBuffer, which allows write access to
217 /// the underlying contents and committing those changes to the original source.
218 /// It only supports creation methods that are guaranteed to produce a writable
219 /// buffer. For example, mapping a file read-only is not supported.
221 protected:
222  WriteThroughMemoryBuffer() = default;
223 
224 public:
228 
229  // const_cast is well-defined here, because the underlying buffer is
230  // guaranteed to have been initialized with a mutable buffer.
231  char *getBufferStart() {
232  return const_cast<char *>(MemoryBuffer::getBufferStart());
233  }
234  char *getBufferEnd() {
235  return const_cast<char *>(MemoryBuffer::getBufferEnd());
236  }
238  return {getBufferStart(), getBufferEnd()};
239  }
240 
242  getFile(const Twine &Filename, int64_t FileSize = -1);
243 
244  /// Map a subrange of the specified file as a ReadWriteMemoryBuffer.
246  getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset);
247 
248 private:
249  // Hide these base class factory function so one can't write
250  // WritableMemoryBuffer::getXXX()
251  // and be surprised that he got a read-only Buffer.
259 };
260 
261 // Create wrappers for C Binding types (see CBindingWrapping.h).
263 
264 } // end namespace llvm
265 
266 #endif // LLVM_SUPPORT_MEMORYBUFFER_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::sys::fs::file_t
int file_t
Definition: FileSystem.h:60
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::MemoryBuffer::getOpenFile
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
Definition: MemoryBuffer.cpp:503
llvm::WritableMemoryBuffer::getBufferEnd
char * getBufferEnd()
Definition: MemoryBuffer.h:176
llvm::MemoryBuffer::~MemoryBuffer
virtual ~MemoryBuffer()
Definition: MemoryBuffer.cpp:42
llvm::WritableMemoryBuffer::getFile
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getFile(const Twine &Filename, bool IsVolatile=false)
Definition: MemoryBuffer.cpp:274
StringRef.h
llvm::MemoryBuffer::getOpenFileSlice
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset, bool IsVolatile=false)
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.
Definition: MemoryBuffer.cpp:510
Types.h
llvm::MemoryBuffer::getSTDIN
static ErrorOr< std::unique_ptr< MemoryBuffer > > getSTDIN()
Read all of stdin into a file buffer, and return it.
Definition: MemoryBuffer.cpp:517
MemoryBufferRef.h
llvm::WriteThroughMemoryBuffer
This class is an extension of MemoryBuffer, which allows write access to the underlying contents and ...
Definition: MemoryBuffer.h:220
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
CBindingWrapping.h
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
llvm::MemoryBuffer::MemoryBuffer
MemoryBuffer()=default
llvm::MemoryBuffer::getFileAsStream
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAsStream(const Twine &Filename)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
Definition: MemoryBuffer.cpp:528
DEFINE_SIMPLE_CONVERSION_FUNCTIONS
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
Definition: CBindingWrapping.h:19
llvm::MemoryBuffer::getFile
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Definition: MemoryBuffer.cpp:246
llvm::MemoryBuffer::getMemBuffer
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
Definition: MemoryBuffer.cpp:113
llvm::MemoryBuffer::init
void init(const char *BufStart, const char *BufEnd, bool RequiresNullTerminator)
init - Initialize this MemoryBuffer as a reference to externally allocated memory,...
Definition: MemoryBuffer.cpp:46
llvm::MutableArrayRef
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
llvm::MemoryBuffer::getBufferEnd
const char * getBufferEnd() const
Definition: MemoryBuffer.h:66
llvm::MemoryBuffer::getBufferSize
size_t getBufferSize() const
Definition: MemoryBuffer.h:67
Twine.h
int
Clang compiles this i1 i64 store i64 i64 store i64 i64 store i64 i64 store i64 align Which gets codegen d xmm0 movaps rbp movaps rbp movaps rbp movaps rbp rbp rbp rbp rbp It would be better to have movq s of instead of the movaps s LLVM produces ret int
Definition: README.txt:536
llvm::MemoryBuffer::MemoryBuffer_MMap
@ MemoryBuffer_MMap
Definition: MemoryBuffer.h:148
llvm::WritableMemoryBuffer::getNewMemBuffer
static std::unique_ptr< WritableMemoryBuffer > getNewMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
Definition: MemoryBuffer.cpp:315
llvm::MemoryBuffer::getBuffer
StringRef getBuffer() const
Definition: MemoryBuffer.h:69
llvm::MemoryBuffer::getBufferIdentifier
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:75
uint64_t
llvm::MemoryBuffer::operator=
MemoryBuffer & operator=(const MemoryBuffer &)=delete
llvm::MemoryBuffer::getFileSlice
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false)
Map a subrange of the specified file as a MemoryBuffer.
Definition: MemoryBuffer.cpp:156
llvm::MemoryBuffer::getMemBufferRef
MemoryBufferRef getMemBufferRef() const
Definition: MemoryBuffer.cpp:540
llvm::MemoryBuffer::BufferKind
BufferKind
The kind of memory backing used to support the MemoryBuffer.
Definition: MemoryBuffer.h:146
llvm::WriteThroughMemoryBuffer::WriteThroughMemoryBuffer
WriteThroughMemoryBuffer()=default
ErrorOr.h
llvm::MemoryBuffer::getFileOrSTDIN
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
Definition: MemoryBuffer.cpp:144
ArrayRef.h
llvm::WritableMemoryBuffer::getNewUninitMemBuffer
static std::unique_ptr< WritableMemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new MemoryBuffer of the specified size that is not initialized.
Definition: MemoryBuffer.cpp:289
llvm::WriteThroughMemoryBuffer::getBuffer
MutableArrayRef< char > getBuffer()
Definition: MemoryBuffer.h:237
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::AMDGPU::HSAMD::Kernel::Arg::Key::IsVolatile
constexpr char IsVolatile[]
Key for Kernel::Arg::Metadata::mIsVolatile.
Definition: AMDGPUMetadata.h:194
llvm::WritableMemoryBuffer::WritableMemoryBuffer
WritableMemoryBuffer()=default
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::MemoryBuffer::getBufferStart
const char * getBufferStart() const
Definition: MemoryBuffer.h:65
llvm::WritableMemoryBuffer::getBufferStart
char * getBufferStart()
Definition: MemoryBuffer.h:173
llvm::WriteThroughMemoryBuffer::getBufferStart
char * getBufferStart()
Definition: MemoryBuffer.h:231
llvm::WriteThroughMemoryBuffer::getFileSlice
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset)
Map a subrange of the specified file as a ReadWriteMemoryBuffer.
Definition: MemoryBuffer.cpp:426
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
llvm::WritableMemoryBuffer::getBuffer
MutableArrayRef< char > getBuffer()
Definition: MemoryBuffer.h:179
llvm::WritableMemoryBuffer::getFileSlice
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false)
Map a subrange of the specified file as a WritableMemoryBuffer.
Definition: MemoryBuffer.cpp:281
llvm::WriteThroughMemoryBuffer::getFile
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1)
Definition: MemoryBuffer.cpp:420
LLVMMemoryBufferRef
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
LLVM uses a polymorphic type hierarchy which C cannot represent, therefore parameters must be passed ...
Definition: Types.h:48
llvm::WriteThroughMemoryBuffer::getBufferEnd
char * getBufferEnd()
Definition: MemoryBuffer.h:234
llvm::WritableMemoryBuffer
This class is an extension of MemoryBuffer, which allows copy-on-write access to the underlying conte...
Definition: MemoryBuffer.h:162
llvm::MemoryBuffer::getMemBufferCopy
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
Definition: MemoryBuffer.cpp:136
llvm::MemoryBuffer::MemoryBuffer_Malloc
@ MemoryBuffer_Malloc
Definition: MemoryBuffer.h:147
llvm::MemoryBuffer::getBufferKind
virtual BufferKind getBufferKind() const =0
Return information on the memory mechanism used to support the MemoryBuffer.