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