MemoryBuffer.h 6.6 KB

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