LLVM  6.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/StringRef.h"
19 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/ErrorOr.h"
22 #include <cstddef>
23 #include <cstdint>
24 #include <memory>
25 
26 namespace llvm {
27 
28 class MemoryBufferRef;
29 
30 /// This interface provides simple read-only access to a block of memory, and
31 /// provides simple methods for reading files and standard input into a memory
32 /// buffer. In addition to basic access to the characters in the file, this
33 /// interface guarantees you can read one character past the end of the file,
34 /// and that this character will read as '\0'.
35 ///
36 /// The '\0' guarantee is needed to support an optimization -- it's intended to
37 /// be more efficient for clients which are reading all the data to stop
38 /// reading when they encounter a '\0' than to continually check the file
39 /// position to see if it has reached the end of the file.
40 class MemoryBuffer {
41  const char *BufferStart; // Start of the buffer.
42  const char *BufferEnd; // End of the buffer.
43 
44 
45 protected:
46  MemoryBuffer() = default;
47 
48  void init(const char *BufStart, const char *BufEnd,
49  bool RequiresNullTerminator);
50 public:
51  MemoryBuffer(const MemoryBuffer &) = delete;
52  MemoryBuffer &operator=(const MemoryBuffer &) = delete;
53  virtual ~MemoryBuffer();
54 
55  const char *getBufferStart() const { return BufferStart; }
56  const char *getBufferEnd() const { return BufferEnd; }
57  size_t getBufferSize() const { return BufferEnd-BufferStart; }
58 
59  StringRef getBuffer() const {
60  return StringRef(BufferStart, getBufferSize());
61  }
62 
63  /// Return an identifier for this buffer, typically the filename it was read
64  /// from.
65  virtual StringRef getBufferIdentifier() const { return "Unknown buffer"; }
66 
67  /// Open the specified file as a MemoryBuffer, returning a new MemoryBuffer
68  /// if successful, otherwise returning null. If FileSize is specified, this
69  /// means that the client knows that the file exists and that it has the
70  /// specified size.
71  ///
72  /// \param IsVolatile Set to true to indicate that the contents of the file
73  /// can change outside the user's control, e.g. when libclang tries to parse
74  /// while the user is editing/updating the file or if the file is on an NFS.
76  getFile(const Twine &Filename, int64_t FileSize = -1,
77  bool RequiresNullTerminator = true, bool IsVolatile = false);
78 
79  /// Read all of the specified file into a MemoryBuffer as a stream
80  /// (i.e. until EOF reached). This is useful for special files that
81  /// look like a regular file but have 0 size (e.g. /proc/cpuinfo on Linux).
83  getFileAsStream(const Twine &Filename);
84 
85  /// Given an already-open file descriptor, map some slice of it into a
86  /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize.
87  /// Since this is in the middle of a file, the buffer is not null terminated.
89  getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize,
90  int64_t Offset, bool IsVolatile = false);
91 
92  /// Given an already-open file descriptor, read the file and return a
93  /// MemoryBuffer.
94  ///
95  /// \param IsVolatile Set to true to indicate that the contents of the file
96  /// can change outside the user's control, e.g. when libclang tries to parse
97  /// while the user is editing/updating the file or if the file is on an NFS.
99  getOpenFile(int FD, const Twine &Filename, uint64_t FileSize,
100  bool RequiresNullTerminator = true, bool IsVolatile = false);
101 
102  /// Open the specified memory range as a MemoryBuffer. Note that InputData
103  /// must be null terminated if RequiresNullTerminator is true.
104  static std::unique_ptr<MemoryBuffer>
105  getMemBuffer(StringRef InputData, StringRef BufferName = "",
106  bool RequiresNullTerminator = true);
107 
108  static std::unique_ptr<MemoryBuffer>
109  getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true);
110 
111  /// Open the specified memory range as a MemoryBuffer, copying the contents
112  /// and taking ownership of it. InputData does not have to be null terminated.
113  static std::unique_ptr<MemoryBuffer>
114  getMemBufferCopy(StringRef InputData, const Twine &BufferName = "");
115 
116  /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
117  /// that the caller need not initialize the memory allocated by this method.
118  /// The memory is owned by the MemoryBuffer object.
119  static std::unique_ptr<MemoryBuffer>
120  getNewMemBuffer(size_t Size, StringRef BufferName = "");
121 
122  /// Allocate a new MemoryBuffer of the specified size that is not initialized.
123  /// Note that the caller should initialize the memory allocated by this
124  /// method. The memory is owned by the MemoryBuffer object.
125  static std::unique_ptr<MemoryBuffer>
126  getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "");
127 
128  /// Read all of stdin into a file buffer, and return it.
130 
131  /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
132  /// is "-".
134  getFileOrSTDIN(const Twine &Filename, int64_t FileSize = -1,
135  bool RequiresNullTerminator = true);
136 
137  /// Map a subrange of the specified file as a MemoryBuffer.
139  getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
140  bool IsVolatile = false);
141 
142  //===--------------------------------------------------------------------===//
143  // Provided for performance analysis.
144  //===--------------------------------------------------------------------===//
145 
146  /// The kind of memory backing used to support the MemoryBuffer.
147  enum BufferKind {
150  };
151 
152  /// Return information on the memory mechanism used to support the
153  /// MemoryBuffer.
154  virtual BufferKind getBufferKind() const = 0;
155 
157 };
158 
160  StringRef Buffer;
162 
163 public:
164  MemoryBufferRef() = default;
166  : Buffer(Buffer.getBuffer()), Identifier(Buffer.getBufferIdentifier()) {}
167  MemoryBufferRef(StringRef Buffer, StringRef Identifier)
168  : Buffer(Buffer), Identifier(Identifier) {}
169 
170  StringRef getBuffer() const { return Buffer; }
171 
173 
174  const char *getBufferStart() const { return Buffer.begin(); }
175  const char *getBufferEnd() const { return Buffer.end(); }
176  size_t getBufferSize() const { return Buffer.size(); }
177 };
178 
179 // Create wrappers for C Binding types (see CBindingWrapping.h).
181 
182 } // end namespace llvm
183 
184 #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:65
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:59
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
const char * getBufferEnd() const
Definition: MemoryBuffer.h:175
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.
static std::unique_ptr< MemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new MemoryBuffer of the specified size that is not initialized.
size_t getBufferSize() const
Definition: MemoryBuffer.h:57
static std::unique_ptr< MemoryBuffer > getNewMemBuffer(size_t Size, StringRef BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
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:170
static ErrorOr< std::unique_ptr< MemoryBuffer > > getSTDIN()
Read all of stdin into a file buffer, and return it.
size_t getBufferSize() const
Definition: MemoryBuffer.h:176
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:40
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:172
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:56
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.
BufferKind
The kind of memory backing used to support the MemoryBuffer.
Definition: MemoryBuffer.h:147
const char * getBufferStart() const
Definition: MemoryBuffer.h:55
MemoryBufferRef(MemoryBuffer &Buffer)
Definition: MemoryBuffer.h:165
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:174
MemoryBufferRef(StringRef Buffer, StringRef Identifier)
Definition: MemoryBuffer.h:167
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
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