llvm.org GIT mirror llvm / 6477842
Be consistent about using "const Twine &" for filenames. On this file we had a mix of * Twine * const char * * StringRef The two that make sense are * const Twine & (caller convenience) * consc char * (that is what will eventually be passed to open. Given that sys::fs::openFileForRead takes a "const Twine &", I picked that. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@219224 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
2 changed file(s) with 33 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
7171 /// changing, e.g. when libclang tries to parse while the user is
7272 /// editing/updating the file.
7373 static ErrorOr>
74 getFile(Twine Filename, int64_t FileSize = -1,
74 getFile(const Twine &Filename, int64_t FileSize = -1,
7575 bool RequiresNullTerminator = true, bool IsVolatileSize = false);
7676
7777 /// Given an already-open file descriptor, map some slice of it into a
8282 /// changing, e.g. when libclang tries to parse while the user is
8383 /// editing/updating the file.
8484 static ErrorOr>
85 getOpenFileSlice(int FD, const char *Filename, uint64_t MapSize,
85 getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize,
8686 int64_t Offset, bool IsVolatileSize = false);
8787
8888 /// Given an already-open file descriptor, read the file and return a
9292 /// changing, e.g. when libclang tries to parse while the user is
9393 /// editing/updating the file.
9494 static ErrorOr>
95 getOpenFile(int FD, const char *Filename, uint64_t FileSize,
95 getOpenFile(int FD, const Twine &Filename, uint64_t FileSize,
9696 bool RequiresNullTerminator = true, bool IsVolatileSize = false);
9797
9898 /// Open the specified memory range as a MemoryBuffer. Note that InputData
107107 /// Open the specified memory range as a MemoryBuffer, copying the contents
108108 /// and taking ownership of it. InputData does not have to be null terminated.
109109 static std::unique_ptr
110 getMemBufferCopy(StringRef InputData, StringRef BufferName = "");
110 getMemBufferCopy(StringRef InputData, const Twine &BufferName = "");
111111
112112 /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
113113 /// that the caller need not initialize the memory allocated by this method.
119119 /// Note that the caller should initialize the memory allocated by this
120120 /// method. The memory is owned by the MemoryBuffer object.
121121 static std::unique_ptr
122 getNewUninitMemBuffer(size_t Size, StringRef BufferName = "");
122 getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "");
123123
124124 /// Read all of stdin into a file buffer, and return it.
125125 static ErrorOr> getSTDIN();
127127 /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
128128 /// is "-".
129129 static ErrorOr>
130 getFileOrSTDIN(StringRef Filename, int64_t FileSize = -1);
130 getFileOrSTDIN(const Twine &Filename, int64_t FileSize = -1);
131131
132132 //===--------------------------------------------------------------------===//
133133 // Provided for performance analysis.
6363
6464 namespace {
6565 struct NamedBufferAlloc {
66 StringRef Name;
67 NamedBufferAlloc(StringRef Name) : Name(Name) {}
66 const Twine &Name;
67 NamedBufferAlloc(const Twine &Name) : Name(Name) {}
6868 };
6969 }
7070
7171 void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
72 char *Mem = static_cast(operator new(N + Alloc.Name.size() + 1));
73 CopyStringRef(Mem + N, Alloc.Name);
72 SmallString<256> NameBuf;
73 StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
74
75 char *Mem = static_cast(operator new(N + NameRef.size() + 1));
76 CopyStringRef(Mem + N, NameRef);
7477 return Mem;
7578 }
7679
108111 }
109112
110113 std::unique_ptr
111 MemoryBuffer::getMemBufferCopy(StringRef InputData, StringRef BufferName) {
114 MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
112115 std::unique_ptr Buf =
113116 getNewUninitMemBuffer(InputData.size(), BufferName);
114117 if (!Buf)
119122 }
120123
121124 std::unique_ptr
122 MemoryBuffer::getNewUninitMemBuffer(size_t Size, StringRef BufferName) {
125 MemoryBuffer::getNewUninitMemBuffer(size_t Size, const Twine &BufferName) {
123126 // Allocate space for the MemoryBuffer, the data and the name. It is important
124127 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
125128 // TODO: Is 16-byte alignment enough? We copy small object files with large
126129 // alignment expectations into this buffer.
130 SmallString<256> NameBuf;
131 StringRef NameRef = BufferName.toStringRef(NameBuf);
127132 size_t AlignedStringLen =
128 RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1, 16);
133 RoundUpToAlignment(sizeof(MemoryBufferMem) + NameRef.size() + 1, 16);
129134 size_t RealLen = AlignedStringLen + Size + 1;
130135 char *Mem = static_cast(operator new(RealLen, std::nothrow));
131136 if (!Mem)
132137 return nullptr;
133138
134139 // The name is stored after the class itself.
135 CopyStringRef(Mem + sizeof(MemoryBufferMem), BufferName);
140 CopyStringRef(Mem + sizeof(MemoryBufferMem), NameRef);
136141
137142 // The buffer begins after the name and must be aligned.
138143 char *Buf = Mem + AlignedStringLen;
152157 }
153158
154159 ErrorOr>
155 MemoryBuffer::getFileOrSTDIN(StringRef Filename, int64_t FileSize) {
156 if (Filename == "-")
160 MemoryBuffer::getFileOrSTDIN(const Twine &Filename, int64_t FileSize) {
161 SmallString<256> NameBuf;
162 StringRef NameRef = Filename.toStringRef(NameBuf);
163
164 if (NameRef == "-")
157165 return getSTDIN();
158166 return getFile(Filename, FileSize);
159167 }
205213 }
206214
207215 static ErrorOr>
208 getMemoryBufferForStream(int FD, StringRef BufferName) {
216 getMemoryBufferForStream(int FD, const Twine &BufferName) {
209217 const ssize_t ChunkSize = 4096*4;
210218 SmallString Buffer;
211219 ssize_t ReadBytes;
224232 }
225233
226234 static ErrorOr>
227 getFileAux(const char *Filename, int64_t FileSize, bool RequiresNullTerminator,
235 getFileAux(const Twine &Filename, int64_t FileSize, bool RequiresNullTerminator,
228236 bool IsVolatileSize);
229237
230238 ErrorOr>
231 MemoryBuffer::getFile(Twine Filename, int64_t FileSize,
239 MemoryBuffer::getFile(const Twine &Filename, int64_t FileSize,
232240 bool RequiresNullTerminator, bool IsVolatileSize) {
233 // Ensure the path is null terminated.
234 SmallString<256> PathBuf;
235 StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf);
236 return getFileAux(NullTerminatedName.data(), FileSize, RequiresNullTerminator,
241 return getFileAux(Filename, FileSize, RequiresNullTerminator,
237242 IsVolatileSize);
238243 }
239244
240245 static ErrorOr>
241 getOpenFileImpl(int FD, const char *Filename, uint64_t FileSize,
246 getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
242247 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
243248 bool IsVolatileSize);
244249
245250 static ErrorOr>
246 getFileAux(const char *Filename, int64_t FileSize, bool RequiresNullTerminator,
251 getFileAux(const Twine &Filename, int64_t FileSize, bool RequiresNullTerminator,
247252 bool IsVolatileSize) {
248253 int FD;
249254 std::error_code EC = sys::fs::openFileForRead(Filename, FD);
314319 }
315320
316321 static ErrorOr>
317 getOpenFileImpl(int FD, const char *Filename, uint64_t FileSize,
322 getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
318323 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
319324 bool IsVolatileSize) {
320325 static int PageSize = sys::process::get_self()->page_size();
392397 }
393398
394399 ErrorOr>
395 MemoryBuffer::getOpenFile(int FD, const char *Filename, uint64_t FileSize,
400 MemoryBuffer::getOpenFile(int FD, const Twine &Filename, uint64_t FileSize,
396401 bool RequiresNullTerminator, bool IsVolatileSize) {
397402 return getOpenFileImpl(FD, Filename, FileSize, FileSize, 0,
398403 RequiresNullTerminator, IsVolatileSize);
399404 }
400405
401406 ErrorOr>
402 MemoryBuffer::getOpenFileSlice(int FD, const char *Filename, uint64_t MapSize,
407 MemoryBuffer::getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize,
403408 int64_t Offset, bool IsVolatileSize) {
404409 return getOpenFileImpl(FD, Filename, -1, MapSize, Offset, false,
405410 IsVolatileSize);