diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2014-10-07 18:58:55 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2014-10-07 18:58:55 +0000 |
commit | 0cb3c9bb248780fe5eb32604bd8ea0c82d4e018f (patch) | |
tree | 2d20f157dc4b7568cc37ad9a0f3e2a116f43d556 /llvm/lib/Support/MemoryBuffer.cpp | |
parent | 963bc87dbde1874f658b5ae85a431faaca5df3b8 (diff) | |
download | bcm5719-llvm-0cb3c9bb248780fe5eb32604bd8ea0c82d4e018f.tar.gz bcm5719-llvm-0cb3c9bb248780fe5eb32604bd8ea0c82d4e018f.zip |
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.
llvm-svn: 219224
Diffstat (limited to 'llvm/lib/Support/MemoryBuffer.cpp')
-rw-r--r-- | llvm/lib/Support/MemoryBuffer.cpp | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp index fe4759219c9..9ccbcbf587d 100644 --- a/llvm/lib/Support/MemoryBuffer.cpp +++ b/llvm/lib/Support/MemoryBuffer.cpp @@ -64,14 +64,17 @@ static void CopyStringRef(char *Memory, StringRef Data) { namespace { struct NamedBufferAlloc { - StringRef Name; - NamedBufferAlloc(StringRef Name) : Name(Name) {} + const Twine &Name; + NamedBufferAlloc(const Twine &Name) : Name(Name) {} }; } void *operator new(size_t N, const NamedBufferAlloc &Alloc) { - char *Mem = static_cast<char *>(operator new(N + Alloc.Name.size() + 1)); - CopyStringRef(Mem + N, Alloc.Name); + SmallString<256> NameBuf; + StringRef NameRef = Alloc.Name.toStringRef(NameBuf); + + char *Mem = static_cast<char *>(operator new(N + NameRef.size() + 1)); + CopyStringRef(Mem + N, NameRef); return Mem; } @@ -109,7 +112,7 @@ MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) { } std::unique_ptr<MemoryBuffer> -MemoryBuffer::getMemBufferCopy(StringRef InputData, StringRef BufferName) { +MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) { std::unique_ptr<MemoryBuffer> Buf = getNewUninitMemBuffer(InputData.size(), BufferName); if (!Buf) @@ -120,20 +123,22 @@ MemoryBuffer::getMemBufferCopy(StringRef InputData, StringRef BufferName) { } std::unique_ptr<MemoryBuffer> -MemoryBuffer::getNewUninitMemBuffer(size_t Size, StringRef BufferName) { +MemoryBuffer::getNewUninitMemBuffer(size_t Size, const Twine &BufferName) { // Allocate space for the MemoryBuffer, the data and the name. It is important // that MemoryBuffer and data are aligned so PointerIntPair works with them. // TODO: Is 16-byte alignment enough? We copy small object files with large // alignment expectations into this buffer. + SmallString<256> NameBuf; + StringRef NameRef = BufferName.toStringRef(NameBuf); size_t AlignedStringLen = - RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1, 16); + RoundUpToAlignment(sizeof(MemoryBufferMem) + NameRef.size() + 1, 16); size_t RealLen = AlignedStringLen + Size + 1; char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow)); if (!Mem) return nullptr; // The name is stored after the class itself. - CopyStringRef(Mem + sizeof(MemoryBufferMem), BufferName); + CopyStringRef(Mem + sizeof(MemoryBufferMem), NameRef); // The buffer begins after the name and must be aligned. char *Buf = Mem + AlignedStringLen; @@ -153,8 +158,11 @@ MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) { } ErrorOr<std::unique_ptr<MemoryBuffer>> -MemoryBuffer::getFileOrSTDIN(StringRef Filename, int64_t FileSize) { - if (Filename == "-") +MemoryBuffer::getFileOrSTDIN(const Twine &Filename, int64_t FileSize) { + SmallString<256> NameBuf; + StringRef NameRef = Filename.toStringRef(NameBuf); + + if (NameRef == "-") return getSTDIN(); return getFile(Filename, FileSize); } @@ -206,7 +214,7 @@ public: } static ErrorOr<std::unique_ptr<MemoryBuffer>> -getMemoryBufferForStream(int FD, StringRef BufferName) { +getMemoryBufferForStream(int FD, const Twine &BufferName) { const ssize_t ChunkSize = 4096*4; SmallString<ChunkSize> Buffer; ssize_t ReadBytes; @@ -225,26 +233,23 @@ getMemoryBufferForStream(int FD, StringRef BufferName) { } static ErrorOr<std::unique_ptr<MemoryBuffer>> -getFileAux(const char *Filename, int64_t FileSize, bool RequiresNullTerminator, +getFileAux(const Twine &Filename, int64_t FileSize, bool RequiresNullTerminator, bool IsVolatileSize); ErrorOr<std::unique_ptr<MemoryBuffer>> -MemoryBuffer::getFile(Twine Filename, int64_t FileSize, +MemoryBuffer::getFile(const Twine &Filename, int64_t FileSize, bool RequiresNullTerminator, bool IsVolatileSize) { - // Ensure the path is null terminated. - SmallString<256> PathBuf; - StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf); - return getFileAux(NullTerminatedName.data(), FileSize, RequiresNullTerminator, + return getFileAux(Filename, FileSize, RequiresNullTerminator, IsVolatileSize); } static ErrorOr<std::unique_ptr<MemoryBuffer>> -getOpenFileImpl(int FD, const char *Filename, uint64_t FileSize, +getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize, uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator, bool IsVolatileSize); static ErrorOr<std::unique_ptr<MemoryBuffer>> -getFileAux(const char *Filename, int64_t FileSize, bool RequiresNullTerminator, +getFileAux(const Twine &Filename, int64_t FileSize, bool RequiresNullTerminator, bool IsVolatileSize) { int FD; std::error_code EC = sys::fs::openFileForRead(Filename, FD); @@ -315,7 +320,7 @@ static bool shouldUseMmap(int FD, } static ErrorOr<std::unique_ptr<MemoryBuffer>> -getOpenFileImpl(int FD, const char *Filename, uint64_t FileSize, +getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize, uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator, bool IsVolatileSize) { static int PageSize = sys::process::get_self()->page_size(); @@ -393,14 +398,14 @@ getOpenFileImpl(int FD, const char *Filename, uint64_t FileSize, } ErrorOr<std::unique_ptr<MemoryBuffer>> -MemoryBuffer::getOpenFile(int FD, const char *Filename, uint64_t FileSize, +MemoryBuffer::getOpenFile(int FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator, bool IsVolatileSize) { return getOpenFileImpl(FD, Filename, FileSize, FileSize, 0, RequiresNullTerminator, IsVolatileSize); } ErrorOr<std::unique_ptr<MemoryBuffer>> -MemoryBuffer::getOpenFileSlice(int FD, const char *Filename, uint64_t MapSize, +MemoryBuffer::getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize, int64_t Offset, bool IsVolatileSize) { return getOpenFileImpl(FD, Filename, -1, MapSize, Offset, false, IsVolatileSize); |