LLVM  7.0.0svn
MemoryBuffer.h
Go to the documentation of this file.
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"
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.
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).
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.
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.
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  /// Read all of stdin into a file buffer, and return it.
122 
123  /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
124  /// is "-".
126  getFileOrSTDIN(const Twine &Filename, int64_t FileSize = -1,
127  bool RequiresNullTerminator = true);
128 
129  /// Map a subrange of the specified file as a MemoryBuffer.
131  getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
132  bool IsVolatile = false);
133 
134  //===--------------------------------------------------------------------===//
135  // Provided for performance analysis.
136  //===--------------------------------------------------------------------===//
137 
138  /// The kind of memory backing used to support the MemoryBuffer.
139  enum BufferKind {
142  };
143 
144  /// Return information on the memory mechanism used to support the
145  /// MemoryBuffer.
146  virtual BufferKind getBufferKind() const = 0;
147 
149 };
150 
151 /// This class is an extension of MemoryBuffer, which allows writing to the
152 /// underlying contents. It only supports creation methods that are guaranteed
153 /// to produce a writable buffer. For example, mapping a file read-only is not
154 /// supported.
156 protected:
157  WritableMemoryBuffer() = default;
158 
159  static constexpr bool Writable = true;
160 
161 public:
165 
166  // const_cast is well-defined here, because the underlying buffer is
167  // guaranteed to have been initialized with a mutable buffer.
168  char *getBufferStart() {
169  return const_cast<char *>(MemoryBuffer::getBufferStart());
170  }
171  char *getBufferEnd() {
172  return const_cast<char *>(MemoryBuffer::getBufferEnd());
173  }
175  return {getBufferStart(), getBufferEnd()};
176  }
177 
179  getFile(const Twine &Filename, int64_t FileSize = -1,
180  bool IsVolatile = false);
181 
182  /// Map a subrange of the specified file as a WritableMemoryBuffer.
184  getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
185  bool IsVolatile = false);
186 
187  /// Allocate a new MemoryBuffer of the specified size that is not initialized.
188  /// Note that the caller should initialize the memory allocated by this
189  /// method. The memory is owned by the MemoryBuffer object.
190  static std::unique_ptr<WritableMemoryBuffer>
191  getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "");
192 
193  /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
194  /// that the caller need not initialize the memory allocated by this method.
195  /// The memory is owned by the MemoryBuffer object.
196  static std::unique_ptr<WritableMemoryBuffer>
197  getNewMemBuffer(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.
210 };
211 
213  StringRef Buffer;
215 
216 public:
217  MemoryBufferRef() = default;
219  : Buffer(Buffer.getBuffer()), Identifier(Buffer.getBufferIdentifier()) {}
220  MemoryBufferRef(StringRef Buffer, StringRef Identifier)
221  : Buffer(Buffer), Identifier(Identifier) {}
222 
223  StringRef getBuffer() const { return Buffer; }
224 
226 
227  const char *getBufferStart() const { return Buffer.begin(); }
228  const char *getBufferEnd() const { return Buffer.end(); }
229  size_t getBufferSize() const { return Buffer.size(); }
230 };
231 
232 // Create wrappers for C Binding types (see CBindingWrapping.h).
234 
235 } // end namespace llvm
236 
237 #endif // LLVM_SUPPORT_MEMORYBUFFER_H
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:69
void init(const char *BufStart, const char *BufEnd, bool RequiresNullTerminator)
init - Initialize this MemoryBuffer as a reference to externally allocated memory, memory that we know is already null terminated.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(int 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.
Represents either an error or a value T.
Definition: ErrorOr.h:69
StringRef getBuffer() const
Definition: MemoryBuffer.h:63
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
const char * getBufferEnd() const
Definition: MemoryBuffer.h:228
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
LLVM uses a polymorphic type hierarchy which C cannot represent, therefore parameters must be passed ...
Definition: Types.h:49
constexpr char IsVolatile[]
Key for Kernel::Arg::Metadata::mIsVolatile.
MemoryBuffer & operator=(const MemoryBuffer &)=delete
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
size_t getBufferSize() const
Definition: MemoryBuffer.h:61
virtual BufferKind getBufferKind() const =0
Return information on the memory mechanism used to support the MemoryBuffer.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
StringRef getBuffer() const
Definition: MemoryBuffer.h:223
static ErrorOr< std::unique_ptr< MemoryBuffer > > getSTDIN()
Read all of stdin into a file buffer, and return it.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291
size_t getBufferSize() const
Definition: MemoryBuffer.h:229
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::shared_ptr< Module >, LLVMSharedModuleRef) namespace detail
MemoryBuffer()=default
MemoryBufferRef getMemBufferRef() const
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:41
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...
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:225
iterator begin() const
Definition: StringRef.h:106
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.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
const char * getBufferEnd() const
Definition: MemoryBuffer.h:60
virtual ~MemoryBuffer()
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAsStream(const Twine &Filename)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
This class is an extension of MemoryBuffer, which allows writing to the underlying contents...
Definition: MemoryBuffer.h:155
BufferKind
The kind of memory backing used to support the MemoryBuffer.
Definition: MemoryBuffer.h:139
const char * getBufferStart() const
Definition: MemoryBuffer.h:59
MemoryBufferRef(MemoryBuffer &Buffer)
Definition: MemoryBuffer.h:218
Provides ErrorOr<T> smart pointer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
const char * getBufferStart() const
Definition: MemoryBuffer.h:227
MemoryBufferRef(StringRef Buffer, StringRef Identifier)
Definition: MemoryBuffer.h:220
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
The access may reference the value stored in memory.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(int 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.
iterator end() const
Definition: StringRef.h:108
static constexpr bool Writable
Definition: MemoryBuffer.h:52
MutableArrayRef< char > getBuffer()
Definition: MemoryBuffer.h:174