MemoryBuffer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
  13. #define LLVM_SUPPORT_MEMORYBUFFER_H
  14. #include "llvm-c/Types.h"
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/ADT/Twine.h"
  18. #include "llvm/Support/CBindingWrapping.h"
  19. #include "llvm/Support/ErrorOr.h"
  20. #include "llvm/Support/MemoryBufferRef.h"
  21. #include <cstddef>
  22. #include <cstdint>
  23. #include <memory>
  24. namespace llvm {
  25. namespace sys {
  26. namespace fs {
  27. // Duplicated from FileSystem.h to avoid a dependency.
  28. #if defined(_WIN32)
  29. // A Win32 HANDLE is a typedef of void*
  30. using file_t = void *;
  31. #else
  32. using file_t = int;
  33. #endif
  34. } // namespace fs
  35. } // namespace sys
  36. /// This interface provides simple read-only access to a block of memory, and
  37. /// provides simple methods for reading files and standard input into a memory
  38. /// buffer. In addition to basic access to the characters in the file, this
  39. /// interface guarantees you can read one character past the end of the file,
  40. /// and that this character will read as '\0'.
  41. ///
  42. /// The '\0' guarantee is needed to support an optimization -- it's intended to
  43. /// be more efficient for clients which are reading all the data to stop
  44. /// reading when they encounter a '\0' than to continually check the file
  45. /// position to see if it has reached the end of the file.
  46. class MemoryBuffer {
  47. const char *BufferStart; // Start of the buffer.
  48. const char *BufferEnd; // End of the buffer.
  49. protected:
  50. MemoryBuffer() = default;
  51. void init(const char *BufStart, const char *BufEnd,
  52. bool RequiresNullTerminator);
  53. public:
  54. MemoryBuffer(const MemoryBuffer &) = delete;
  55. MemoryBuffer &operator=(const MemoryBuffer &) = delete;
  56. virtual ~MemoryBuffer();
  57. const char *getBufferStart() const { return BufferStart; }
  58. const char *getBufferEnd() const { return BufferEnd; }
  59. size_t getBufferSize() const { return BufferEnd-BufferStart; }
  60. StringRef getBuffer() const {
  61. return StringRef(BufferStart, getBufferSize());
  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. /// Open the specified file as a MemoryBuffer, returning a new MemoryBuffer
  67. /// if successful, otherwise returning null.
  68. ///
  69. /// \param IsText Set to true to indicate that the file should be read in
  70. /// text mode.
  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, bool IsText = false,
  77. bool RequiresNullTerminator = true, bool IsVolatile = false);
  78. /// Read all of the specified file into a MemoryBuffer as a stream
  79. /// (i.e. until EOF reached). This is useful for special files that
  80. /// look like a regular file but have 0 size (e.g. /proc/cpuinfo on Linux).
  81. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  82. getFileAsStream(const Twine &Filename);
  83. /// Given an already-open file descriptor, map some slice of it into a
  84. /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize.
  85. /// Since this is in the middle of a file, the buffer is not null terminated.
  86. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  87. getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize,
  88. int64_t Offset, bool IsVolatile = false);
  89. /// Given an already-open file descriptor, read the file and return a
  90. /// MemoryBuffer.
  91. ///
  92. /// \param IsVolatile Set to true to indicate that the contents of the file
  93. /// can change outside the user's control, e.g. when libclang tries to parse
  94. /// while the user is editing/updating the file or if the file is on an NFS.
  95. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  96. getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
  97. bool RequiresNullTerminator = true, bool IsVolatile = false);
  98. /// Open the specified memory range as a MemoryBuffer. Note that InputData
  99. /// must be null terminated if RequiresNullTerminator is true.
  100. static std::unique_ptr<MemoryBuffer>
  101. getMemBuffer(StringRef InputData, StringRef BufferName = "",
  102. bool RequiresNullTerminator = true);
  103. static std::unique_ptr<MemoryBuffer>
  104. getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true);
  105. /// Open the specified memory range as a MemoryBuffer, copying the contents
  106. /// and taking ownership of it. InputData does not have to be null terminated.
  107. static std::unique_ptr<MemoryBuffer>
  108. getMemBufferCopy(StringRef InputData, const Twine &BufferName = "");
  109. /// Read all of stdin into a file buffer, and return it.
  110. static ErrorOr<std::unique_ptr<MemoryBuffer>> getSTDIN();
  111. /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
  112. /// is "-".
  113. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  114. getFileOrSTDIN(const Twine &Filename, bool IsText = false,
  115. bool RequiresNullTerminator = true);
  116. /// Map a subrange of the specified file as a MemoryBuffer.
  117. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  118. getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
  119. bool IsVolatile = false);
  120. //===--------------------------------------------------------------------===//
  121. // Provided for performance analysis.
  122. //===--------------------------------------------------------------------===//
  123. /// The kind of memory backing used to support the MemoryBuffer.
  124. enum BufferKind {
  125. MemoryBuffer_Malloc,
  126. MemoryBuffer_MMap
  127. };
  128. /// Return information on the memory mechanism used to support the
  129. /// MemoryBuffer.
  130. virtual BufferKind getBufferKind() const = 0;
  131. MemoryBufferRef getMemBufferRef() const;
  132. };
  133. /// This class is an extension of MemoryBuffer, which allows copy-on-write
  134. /// access to the underlying contents. It only supports creation methods that
  135. /// are guaranteed to produce a writable buffer. For example, mapping a file
  136. /// read-only is not supported.
  137. class WritableMemoryBuffer : public MemoryBuffer {
  138. protected:
  139. WritableMemoryBuffer() = default;
  140. public:
  141. using MemoryBuffer::getBuffer;
  142. using MemoryBuffer::getBufferEnd;
  143. using MemoryBuffer::getBufferStart;
  144. // const_cast is well-defined here, because the underlying buffer is
  145. // guaranteed to have been initialized with a mutable buffer.
  146. char *getBufferStart() {
  147. return const_cast<char *>(MemoryBuffer::getBufferStart());
  148. }
  149. char *getBufferEnd() {
  150. return const_cast<char *>(MemoryBuffer::getBufferEnd());
  151. }
  152. MutableArrayRef<char> getBuffer() {
  153. return {getBufferStart(), getBufferEnd()};
  154. }
  155. static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
  156. getFile(const Twine &Filename, bool IsVolatile = false);
  157. /// Map a subrange of the specified file as a WritableMemoryBuffer.
  158. static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
  159. getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
  160. bool IsVolatile = false);
  161. /// Allocate a new MemoryBuffer of the specified size that is not initialized.
  162. /// Note that the caller should initialize the memory allocated by this
  163. /// method. The memory is owned by the MemoryBuffer object.
  164. static std::unique_ptr<WritableMemoryBuffer>
  165. getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "");
  166. /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
  167. /// that the caller need not initialize the memory allocated by this method.
  168. /// The memory is owned by the MemoryBuffer object.
  169. static std::unique_ptr<WritableMemoryBuffer>
  170. getNewMemBuffer(size_t Size, const Twine &BufferName = "");
  171. private:
  172. // Hide these base class factory function so one can't write
  173. // WritableMemoryBuffer::getXXX()
  174. // and be surprised that he got a read-only Buffer.
  175. using MemoryBuffer::getFileAsStream;
  176. using MemoryBuffer::getFileOrSTDIN;
  177. using MemoryBuffer::getMemBuffer;
  178. using MemoryBuffer::getMemBufferCopy;
  179. using MemoryBuffer::getOpenFile;
  180. using MemoryBuffer::getOpenFileSlice;
  181. using MemoryBuffer::getSTDIN;
  182. };
  183. /// This class is an extension of MemoryBuffer, which allows write access to
  184. /// the underlying contents and committing those changes to the original source.
  185. /// It only supports creation methods that are guaranteed to produce a writable
  186. /// buffer. For example, mapping a file read-only is not supported.
  187. class WriteThroughMemoryBuffer : public MemoryBuffer {
  188. protected:
  189. WriteThroughMemoryBuffer() = default;
  190. public:
  191. using MemoryBuffer::getBuffer;
  192. using MemoryBuffer::getBufferEnd;
  193. using MemoryBuffer::getBufferStart;
  194. // const_cast is well-defined here, because the underlying buffer is
  195. // guaranteed to have been initialized with a mutable buffer.
  196. char *getBufferStart() {
  197. return const_cast<char *>(MemoryBuffer::getBufferStart());
  198. }
  199. char *getBufferEnd() {
  200. return const_cast<char *>(MemoryBuffer::getBufferEnd());
  201. }
  202. MutableArrayRef<char> getBuffer() {
  203. return {getBufferStart(), getBufferEnd()};
  204. }
  205. static ErrorOr<std::unique_ptr<WriteThroughMemoryBuffer>>
  206. getFile(const Twine &Filename, int64_t FileSize = -1);
  207. /// Map a subrange of the specified file as a ReadWriteMemoryBuffer.
  208. static ErrorOr<std::unique_ptr<WriteThroughMemoryBuffer>>
  209. getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset);
  210. private:
  211. // Hide these base class factory function so one can't write
  212. // WritableMemoryBuffer::getXXX()
  213. // and be surprised that he got a read-only Buffer.
  214. using MemoryBuffer::getFileAsStream;
  215. using MemoryBuffer::getFileOrSTDIN;
  216. using MemoryBuffer::getMemBuffer;
  217. using MemoryBuffer::getMemBufferCopy;
  218. using MemoryBuffer::getOpenFile;
  219. using MemoryBuffer::getOpenFileSlice;
  220. using MemoryBuffer::getSTDIN;
  221. };
  222. // Create wrappers for C Binding types (see CBindingWrapping.h).
  223. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef)
  224. } // end namespace llvm
  225. #endif // LLVM_SUPPORT_MEMORYBUFFER_H