Mercurial > hg > CbC > CbC_llvm
comparison include/llvm/Support/MemoryBuffer.h @ 147:c2174574ed3a
LLVM 10
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 14 Aug 2019 16:55:33 +0900 |
parents | 3a76565eade5 |
children |
comparison
equal
deleted
inserted
replaced
134:3a76565eade5 | 147:c2174574ed3a |
---|---|
1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===// | 1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 // | 4 // See https://llvm.org/LICENSE.txt for license information. |
5 // This file is distributed under the University of Illinois Open Source | 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 // License. See LICENSE.TXT for details. | |
7 // | 6 // |
8 //===----------------------------------------------------------------------===// | 7 //===----------------------------------------------------------------------===// |
9 // | 8 // |
10 // This file defines the MemoryBuffer interface. | 9 // This file defines the MemoryBuffer interface. |
11 // | 10 // |
18 #include "llvm/ADT/ArrayRef.h" | 17 #include "llvm/ADT/ArrayRef.h" |
19 #include "llvm/ADT/StringRef.h" | 18 #include "llvm/ADT/StringRef.h" |
20 #include "llvm/ADT/Twine.h" | 19 #include "llvm/ADT/Twine.h" |
21 #include "llvm/Support/CBindingWrapping.h" | 20 #include "llvm/Support/CBindingWrapping.h" |
22 #include "llvm/Support/ErrorOr.h" | 21 #include "llvm/Support/ErrorOr.h" |
22 #include "llvm/Support/FileSystem.h" | |
23 #include <cstddef> | 23 #include <cstddef> |
24 #include <cstdint> | 24 #include <cstdint> |
25 #include <memory> | 25 #include <memory> |
26 | 26 |
27 namespace llvm { | 27 namespace llvm { |
40 /// position to see if it has reached the end of the file. | 40 /// position to see if it has reached the end of the file. |
41 class MemoryBuffer { | 41 class MemoryBuffer { |
42 const char *BufferStart; // Start of the buffer. | 42 const char *BufferStart; // Start of the buffer. |
43 const char *BufferEnd; // End of the buffer. | 43 const char *BufferEnd; // End of the buffer. |
44 | 44 |
45 | |
46 protected: | 45 protected: |
47 MemoryBuffer() = default; | 46 MemoryBuffer() = default; |
48 | 47 |
49 void init(const char *BufStart, const char *BufEnd, | 48 void init(const char *BufStart, const char *BufEnd, |
50 bool RequiresNullTerminator); | 49 bool RequiresNullTerminator); |
51 | 50 |
52 static constexpr bool Writable = false; | 51 static constexpr sys::fs::mapped_file_region::mapmode Mapmode = |
52 sys::fs::mapped_file_region::readonly; | |
53 | 53 |
54 public: | 54 public: |
55 MemoryBuffer(const MemoryBuffer &) = delete; | 55 MemoryBuffer(const MemoryBuffer &) = delete; |
56 MemoryBuffer &operator=(const MemoryBuffer &) = delete; | 56 MemoryBuffer &operator=(const MemoryBuffer &) = delete; |
57 virtual ~MemoryBuffer(); | 57 virtual ~MemoryBuffer(); |
88 | 88 |
89 /// Given an already-open file descriptor, map some slice of it into a | 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. | 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. | 91 /// Since this is in the middle of a file, the buffer is not null terminated. |
92 static ErrorOr<std::unique_ptr<MemoryBuffer>> | 92 static ErrorOr<std::unique_ptr<MemoryBuffer>> |
93 getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize, | 93 getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, |
94 int64_t Offset, bool IsVolatile = false); | 94 int64_t Offset, bool IsVolatile = false); |
95 | 95 |
96 /// Given an already-open file descriptor, read the file and return a | 96 /// Given an already-open file descriptor, read the file and return a |
97 /// MemoryBuffer. | 97 /// MemoryBuffer. |
98 /// | 98 /// |
99 /// \param IsVolatile Set to true to indicate that the contents of the file | 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 | 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. | 101 /// while the user is editing/updating the file or if the file is on an NFS. |
102 static ErrorOr<std::unique_ptr<MemoryBuffer>> | 102 static ErrorOr<std::unique_ptr<MemoryBuffer>> |
103 getOpenFile(int FD, const Twine &Filename, uint64_t FileSize, | 103 getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, |
104 bool RequiresNullTerminator = true, bool IsVolatile = false); | 104 bool RequiresNullTerminator = true, bool IsVolatile = false); |
105 | 105 |
106 /// Open the specified memory range as a MemoryBuffer. Note that InputData | 106 /// Open the specified memory range as a MemoryBuffer. Note that InputData |
107 /// must be null terminated if RequiresNullTerminator is true. | 107 /// must be null terminated if RequiresNullTerminator is true. |
108 static std::unique_ptr<MemoryBuffer> | 108 static std::unique_ptr<MemoryBuffer> |
146 virtual BufferKind getBufferKind() const = 0; | 146 virtual BufferKind getBufferKind() const = 0; |
147 | 147 |
148 MemoryBufferRef getMemBufferRef() const; | 148 MemoryBufferRef getMemBufferRef() const; |
149 }; | 149 }; |
150 | 150 |
151 /// This class is an extension of MemoryBuffer, which allows writing to the | 151 /// This class is an extension of MemoryBuffer, which allows copy-on-write |
152 /// underlying contents. It only supports creation methods that are guaranteed | 152 /// access to the underlying contents. It only supports creation methods that |
153 /// to produce a writable buffer. For example, mapping a file read-only is not | 153 /// are guaranteed to produce a writable buffer. For example, mapping a file |
154 /// supported. | 154 /// read-only is not supported. |
155 class WritableMemoryBuffer : public MemoryBuffer { | 155 class WritableMemoryBuffer : public MemoryBuffer { |
156 protected: | 156 protected: |
157 WritableMemoryBuffer() = default; | 157 WritableMemoryBuffer() = default; |
158 | 158 |
159 static constexpr bool Writable = true; | 159 static constexpr sys::fs::mapped_file_region::mapmode Mapmode = |
160 sys::fs::mapped_file_region::priv; | |
160 | 161 |
161 public: | 162 public: |
162 using MemoryBuffer::getBuffer; | 163 using MemoryBuffer::getBuffer; |
163 using MemoryBuffer::getBufferEnd; | 164 using MemoryBuffer::getBufferEnd; |
164 using MemoryBuffer::getBufferStart; | 165 using MemoryBuffer::getBufferStart; |
207 using MemoryBuffer::getOpenFile; | 208 using MemoryBuffer::getOpenFile; |
208 using MemoryBuffer::getOpenFileSlice; | 209 using MemoryBuffer::getOpenFileSlice; |
209 using MemoryBuffer::getSTDIN; | 210 using MemoryBuffer::getSTDIN; |
210 }; | 211 }; |
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. | |
217 class WriteThroughMemoryBuffer : public MemoryBuffer { | |
218 protected: | |
219 WriteThroughMemoryBuffer() = default; | |
220 | |
221 static constexpr sys::fs::mapped_file_region::mapmode Mapmode = | |
222 sys::fs::mapped_file_region::readwrite; | |
223 | |
224 public: | |
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 | |
248 private: | |
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 | |
212 class MemoryBufferRef { | 261 class MemoryBufferRef { |
213 StringRef Buffer; | 262 StringRef Buffer; |
214 StringRef Identifier; | 263 StringRef Identifier; |
215 | 264 |
216 public: | 265 public: |
217 MemoryBufferRef() = default; | 266 MemoryBufferRef() = default; |
218 MemoryBufferRef(MemoryBuffer& Buffer) | 267 MemoryBufferRef(const MemoryBuffer& Buffer) |
219 : Buffer(Buffer.getBuffer()), Identifier(Buffer.getBufferIdentifier()) {} | 268 : Buffer(Buffer.getBuffer()), Identifier(Buffer.getBufferIdentifier()) {} |
220 MemoryBufferRef(StringRef Buffer, StringRef Identifier) | 269 MemoryBufferRef(StringRef Buffer, StringRef Identifier) |
221 : Buffer(Buffer), Identifier(Identifier) {} | 270 : Buffer(Buffer), Identifier(Identifier) {} |
222 | 271 |
223 StringRef getBuffer() const { return Buffer; } | 272 StringRef getBuffer() const { return Buffer; } |