LLVM  3.7.0
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/Support.h"
18 #include "llvm/ADT/Twine.h"
20 #include "llvm/Support/DataTypes.h"
21 #include "llvm/Support/ErrorOr.h"
22 #include <memory>
23 
24 namespace llvm {
25 class MemoryBufferRef;
26 
27 /// This interface provides simple read-only access to a block of memory, and
28 /// provides simple methods for reading files and standard input into a memory
29 /// buffer. In addition to basic access to the characters in the file, this
30 /// interface guarantees you can read one character past the end of the file,
31 /// and that this character will read as '\0'.
32 ///
33 /// The '\0' guarantee is needed to support an optimization -- it's intended to
34 /// be more efficient for clients which are reading all the data to stop
35 /// reading when they encounter a '\0' than to continually check the file
36 /// position to see if it has reached the end of the file.
37 class MemoryBuffer {
38  const char *BufferStart; // Start of the buffer.
39  const char *BufferEnd; // End of the buffer.
40 
41  MemoryBuffer(const MemoryBuffer &) = delete;
42  MemoryBuffer &operator=(const MemoryBuffer &) = delete;
43 protected:
45  void init(const char *BufStart, const char *BufEnd,
46  bool RequiresNullTerminator);
47 public:
48  virtual ~MemoryBuffer();
49 
50  const char *getBufferStart() const { return BufferStart; }
51  const char *getBufferEnd() const { return BufferEnd; }
52  size_t getBufferSize() const { return BufferEnd-BufferStart; }
53 
54  StringRef getBuffer() const {
55  return StringRef(BufferStart, getBufferSize());
56  }
57 
58  /// Return an identifier for this buffer, typically the filename it was read
59  /// from.
60  virtual const char *getBufferIdentifier() const {
61  return "Unknown buffer";
62  }
63 
64  /// Open the specified file as a MemoryBuffer, returning a new MemoryBuffer
65  /// if successful, otherwise returning null. If FileSize is specified, this
66  /// means that the client knows that the file exists and that it has the
67  /// specified size.
68  ///
69  /// \param IsVolatileSize Set to true to indicate that the file size may be
70  /// changing, e.g. when libclang tries to parse while the user is
71  /// editing/updating the file.
73  getFile(const Twine &Filename, int64_t FileSize = -1,
74  bool RequiresNullTerminator = true, bool IsVolatileSize = false);
75 
76  /// Given an already-open file descriptor, map some slice of it into a
77  /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize.
78  /// Since this is in the middle of a file, the buffer is not null terminated.
80  getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize,
81  int64_t Offset);
82 
83  /// Given an already-open file descriptor, read the file and return a
84  /// MemoryBuffer.
85  ///
86  /// \param IsVolatileSize Set to true to indicate that the file size may be
87  /// changing, e.g. when libclang tries to parse while the user is
88  /// editing/updating the file.
90  getOpenFile(int FD, const Twine &Filename, uint64_t FileSize,
91  bool RequiresNullTerminator = true, bool IsVolatileSize = false);
92 
93  /// Open the specified memory range as a MemoryBuffer. Note that InputData
94  /// must be null terminated if RequiresNullTerminator is true.
95  static std::unique_ptr<MemoryBuffer>
96  getMemBuffer(StringRef InputData, StringRef BufferName = "",
97  bool RequiresNullTerminator = true);
98 
99  static std::unique_ptr<MemoryBuffer>
100  getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true);
101 
102  /// Open the specified memory range as a MemoryBuffer, copying the contents
103  /// and taking ownership of it. InputData does not have to be null terminated.
104  static std::unique_ptr<MemoryBuffer>
105  getMemBufferCopy(StringRef InputData, const Twine &BufferName = "");
106 
107  /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
108  /// that the caller need not initialize the memory allocated by this method.
109  /// The memory is owned by the MemoryBuffer object.
110  static std::unique_ptr<MemoryBuffer>
111  getNewMemBuffer(size_t Size, StringRef BufferName = "");
112 
113  /// Allocate a new MemoryBuffer of the specified size that is not initialized.
114  /// Note that the caller should initialize the memory allocated by this
115  /// method. The memory is owned by the MemoryBuffer object.
116  static std::unique_ptr<MemoryBuffer>
117  getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "");
118 
119  /// Read all of stdin into a file buffer, and return it.
121 
122  /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
123  /// is "-".
125  getFileOrSTDIN(const Twine &Filename, int64_t FileSize = -1);
126 
127  /// Map a subrange of the specified file as a MemoryBuffer.
129  getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset);
130 
131  //===--------------------------------------------------------------------===//
132  // Provided for performance analysis.
133  //===--------------------------------------------------------------------===//
134 
135  /// The kind of memory backing used to support the MemoryBuffer.
136  enum BufferKind {
139  };
140 
141  /// Return information on the memory mechanism used to support the
142  /// MemoryBuffer.
143  virtual BufferKind getBufferKind() const = 0;
144 
146 };
147 
149  StringRef Buffer;
150  StringRef Identifier;
151 
152 public:
154  MemoryBufferRef(StringRef Buffer, StringRef Identifier)
155  : Buffer(Buffer), Identifier(Identifier) {}
156 
157  StringRef getBuffer() const { return Buffer; }
158 
159  StringRef getBufferIdentifier() const { return Identifier; }
160 
161  const char *getBufferStart() const { return Buffer.begin(); }
162  const char *getBufferEnd() const { return Buffer.end(); }
163  size_t getBufferSize() const { return Buffer.size(); }
164 };
165 
166 // Create wrappers for C Binding types (see CBindingWrapping.h).
168 
169 } // end namespace llvm
170 
171 #endif
MemoryBufferRef getMemBufferRef() const
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.
Represents either an error or a value T.
Definition: ErrorOr.h:82
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
const char * getBufferStart() const
Definition: MemoryBuffer.h:50
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
Used to pass regions of memory through LLVM interfaces.
Definition: Support.h:36
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
StringRef getBuffer() const
Definition: MemoryBuffer.h:54
const char * getBufferStart() const
Definition: MemoryBuffer.h:161
static std::unique_ptr< MemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new MemoryBuffer of the specified size that is not initialized.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(int FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatileSize=false)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > getNewMemBuffer(size_t Size, StringRef BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset)
Map a subrange of the specified file as a MemoryBuffer.
virtual const char * getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:60
virtual BufferKind getBufferKind() const =0
Return information on the memory mechanism used to support the MemoryBuffer.
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
iterator begin() const
Definition: StringRef.h:90
static ErrorOr< std::unique_ptr< MemoryBuffer > > getSTDIN()
Read all of stdin into a file buffer, and return it.
const char * getBufferEnd() const
Definition: MemoryBuffer.h:162
size_t getBufferSize() const
Definition: MemoryBuffer.h:163
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
StringRef getBuffer() const
Definition: MemoryBuffer.h:157
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:37
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...
size_t getBufferSize() const
Definition: MemoryBuffer.h:52
virtual ~MemoryBuffer()
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatileSize=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
BufferKind
The kind of memory backing used to support the MemoryBuffer.
Definition: MemoryBuffer.h:136
const char * getBufferEnd() const
Definition: MemoryBuffer.h:51
Provides ErrorOr<T> smart pointer.
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:159
MemoryBufferRef(StringRef Buffer, StringRef Identifier)
Definition: MemoryBuffer.h:154
iterator end() const
Definition: StringRef.h:92
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize, int64_t Offset)
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.