1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MemoryBuffer interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
15 #define LLVM_SUPPORT_MEMORYBUFFER_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/CBindingWrapping.h"
20 #include "llvm/Support/ErrorOr.h"
21 #include "llvm-c/Types.h"
28 class MemoryBufferRef;
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'.
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.
41 const char *BufferStart; // Start of the buffer.
42 const char *BufferEnd; // End of the buffer.
46 MemoryBuffer() = default;
48 void init(const char *BufStart, const char *BufEnd,
49 bool RequiresNullTerminator);
51 MemoryBuffer(const MemoryBuffer &) = delete;
52 MemoryBuffer &operator=(const MemoryBuffer &) = delete;
53 virtual ~MemoryBuffer();
55 const char *getBufferStart() const { return BufferStart; }
56 const char *getBufferEnd() const { return BufferEnd; }
57 size_t getBufferSize() const { return BufferEnd-BufferStart; }
59 StringRef getBuffer() const {
60 return StringRef(BufferStart, getBufferSize());
63 /// Return an identifier for this buffer, typically the filename it was read
65 virtual StringRef getBufferIdentifier() const { return "Unknown buffer"; }
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
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);
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);
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);
92 /// Given an already-open file descriptor, read the file and return a
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);
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);
108 static std::unique_ptr<MemoryBuffer>
109 getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true);
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 = "");
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 = "");
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 = "");
128 /// Read all of stdin into a file buffer, and return it.
129 static ErrorOr<std::unique_ptr<MemoryBuffer>> getSTDIN();
131 /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
133 static ErrorOr<std::unique_ptr<MemoryBuffer>>
134 getFileOrSTDIN(const Twine &Filename, int64_t FileSize = -1,
135 bool RequiresNullTerminator = true);
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);
141 //===--------------------------------------------------------------------===//
142 // Provided for performance analysis.
143 //===--------------------------------------------------------------------===//
145 /// The kind of memory backing used to support the MemoryBuffer.
151 /// Return information on the memory mechanism used to support the
153 virtual BufferKind getBufferKind() const = 0;
155 MemoryBufferRef getMemBufferRef() const;
158 class MemoryBufferRef {
160 StringRef Identifier;
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) {}
169 StringRef getBuffer() const { return Buffer; }
171 StringRef getBufferIdentifier() const { return Identifier; }
173 const char *getBufferStart() const { return Buffer.begin(); }
174 const char *getBufferEnd() const { return Buffer.end(); }
175 size_t getBufferSize() const { return Buffer.size(); }
178 // Create wrappers for C Binding types (see CBindingWrapping.h).
179 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef)
181 } // end namespace llvm
183 #endif // LLVM_SUPPORT_MEMORYBUFFER_H