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