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