MemoryBuffer.h revision 321369
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"
20#include "llvm/Support/CBindingWrapping.h"
21#include "llvm/Support/ErrorOr.h"
22#include <cstddef>
23#include <cstdint>
24#include <memory>
25
26namespace llvm {
27
28class 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.
40class MemoryBuffer {
41  const char *BufferStart; // Start of the buffer.
42  const char *BufferEnd;   // End of the buffer.
43
44
45protected:
46  MemoryBuffer() = default;
47
48  void init(const char *BufStart, const char *BufEnd,
49            bool RequiresNullTerminator);
50public:
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.
75  static ErrorOr<std::unique_ptr<MemoryBuffer>>
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).
82  static ErrorOr<std::unique_ptr<MemoryBuffer>>
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.
88  static ErrorOr<std::unique_ptr<MemoryBuffer>>
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.
98  static ErrorOr<std::unique_ptr<MemoryBuffer>>
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.
129  static ErrorOr<std::unique_ptr<MemoryBuffer>> getSTDIN();
130
131  /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
132  /// is "-".
133  static ErrorOr<std::unique_ptr<MemoryBuffer>>
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.
138  static ErrorOr<std::unique_ptr<MemoryBuffer>>
139  getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, 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 {
147    MemoryBuffer_Malloc,
148    MemoryBuffer_MMap
149  };
150
151  /// Return information on the memory mechanism used to support the
152  /// MemoryBuffer.
153  virtual BufferKind getBufferKind() const = 0;
154
155  MemoryBufferRef getMemBufferRef() const;
156};
157
158class MemoryBufferRef {
159  StringRef Buffer;
160  StringRef Identifier;
161
162public:
163  MemoryBufferRef() = default;
164  MemoryBufferRef(MemoryBuffer& Buffer)
165      : Buffer(Buffer.getBuffer()), Identifier(Buffer.getBufferIdentifier()) {}
166  MemoryBufferRef(StringRef Buffer, StringRef Identifier)
167      : Buffer(Buffer), Identifier(Identifier) {}
168
169  StringRef getBuffer() const { return Buffer; }
170
171  StringRef getBufferIdentifier() const { return Identifier; }
172
173  const char *getBufferStart() const { return Buffer.begin(); }
174  const char *getBufferEnd() const { return Buffer.end(); }
175  size_t getBufferSize() const { return Buffer.size(); }
176};
177
178// Create wrappers for C Binding types (see CBindingWrapping.h).
179DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef)
180
181} // end namespace llvm
182
183#endif // LLVM_SUPPORT_MEMORYBUFFER_H
184