From 6477842ceebcab08b7ca07b7ea92a679161fd32f Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 7 Oct 2014 18:58:55 +0000 Subject: [PATCH] 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 --- include/llvm/Support/MemoryBuffer.h | 12 +++---- lib/Support/MemoryBuffer.cpp | 49 ++++++++++++++++------------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/include/llvm/Support/MemoryBuffer.h b/include/llvm/Support/MemoryBuffer.h index bba32a102e4..a8dc9d7613e 100644 --- a/include/llvm/Support/MemoryBuffer.h +++ b/include/llvm/Support/MemoryBuffer.h @@ -72,7 +72,7 @@ public: /// changing, e.g. when libclang tries to parse while the user is /// editing/updating the file. static ErrorOr> - getFile(Twine Filename, int64_t FileSize = -1, + getFile(const Twine &Filename, int64_t FileSize = -1, bool RequiresNullTerminator = true, bool IsVolatileSize = false); /// Given an already-open file descriptor, map some slice of it into a @@ -83,7 +83,7 @@ public: /// changing, e.g. when libclang tries to parse while the user is /// editing/updating the file. static ErrorOr> - getOpenFileSlice(int FD, const char *Filename, uint64_t MapSize, + getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize, int64_t Offset, bool IsVolatileSize = false); /// Given an already-open file descriptor, read the file and return a @@ -93,7 +93,7 @@ public: /// changing, e.g. when libclang tries to parse while the user is /// editing/updating the file. static ErrorOr> - getOpenFile(int FD, const char *Filename, uint64_t FileSize, + getOpenFile(int FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator = true, bool IsVolatileSize = false); /// Open the specified memory range as a MemoryBuffer. Note that InputData @@ -108,7 +108,7 @@ public: /// Open the specified memory range as a MemoryBuffer, copying the contents /// and taking ownership of it. InputData does not have to be null terminated. static std::unique_ptr - getMemBufferCopy(StringRef InputData, StringRef BufferName = ""); + getMemBufferCopy(StringRef InputData, const Twine &BufferName = ""); /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note /// that the caller need not initialize the memory allocated by this method. @@ -120,7 +120,7 @@ public: /// Note that the caller should initialize the memory allocated by this /// method. The memory is owned by the MemoryBuffer object. static std::unique_ptr - getNewUninitMemBuffer(size_t Size, StringRef BufferName = ""); + getNewUninitMemBuffer(size_t Size, const Twine &BufferName = ""); /// Read all of stdin into a file buffer, and return it. static ErrorOr> getSTDIN(); @@ -128,7 +128,7 @@ public: /// Open the specified file as a MemoryBuffer, or open stdin if the Filename /// is "-". static ErrorOr> - getFileOrSTDIN(StringRef Filename, int64_t FileSize = -1); + getFileOrSTDIN(const Twine &Filename, int64_t FileSize = -1); //===--------------------------------------------------------------------===// // Provided for performance analysis. diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp index fe4759219c9..9ccbcbf587d 100644 --- a/lib/Support/MemoryBuffer.cpp +++ b/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(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(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::getMemBufferCopy(StringRef InputData, StringRef BufferName) { +MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) { std::unique_ptr Buf = getNewUninitMemBuffer(InputData.size(), BufferName); if (!Buf) @@ -120,20 +123,22 @@ MemoryBuffer::getMemBufferCopy(StringRef InputData, StringRef BufferName) { } std::unique_ptr -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(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> -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> -getMemoryBufferForStream(int FD, StringRef BufferName) { +getMemoryBufferForStream(int FD, const Twine &BufferName) { const ssize_t ChunkSize = 4096*4; SmallString Buffer; ssize_t ReadBytes; @@ -225,26 +233,23 @@ getMemoryBufferForStream(int FD, StringRef BufferName) { } static ErrorOr> -getFileAux(const char *Filename, int64_t FileSize, bool RequiresNullTerminator, +getFileAux(const Twine &Filename, int64_t FileSize, bool RequiresNullTerminator, bool IsVolatileSize); ErrorOr> -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> -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> -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> -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> -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> -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); -- 2.34.1