[llvm] r219224 - Be consistent about using "const Twine &" for filenames.
Rafael Espindola
rafael.espindola at gmail.com
Tue Oct 7 11:58:55 PDT 2014
Author: rafael
Date: Tue Oct 7 13:58:55 2014
New Revision: 219224
URL: http://llvm.org/viewvc/llvm-project?rev=219224&view=rev
Log:
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.
Modified:
llvm/trunk/include/llvm/Support/MemoryBuffer.h
llvm/trunk/lib/Support/MemoryBuffer.cpp
Modified: llvm/trunk/include/llvm/Support/MemoryBuffer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MemoryBuffer.h?rev=219224&r1=219223&r2=219224&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MemoryBuffer.h (original)
+++ llvm/trunk/include/llvm/Support/MemoryBuffer.h Tue Oct 7 13:58:55 2014
@@ -72,7 +72,7 @@ public:
/// changing, e.g. when libclang tries to parse while the user is
/// editing/updating the file.
static ErrorOr<std::unique_ptr<MemoryBuffer>>
- 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<std::unique_ptr<MemoryBuffer>>
- 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<std::unique_ptr<MemoryBuffer>>
- 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<MemoryBuffer>
- 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<MemoryBuffer>
- 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<std::unique_ptr<MemoryBuffer>> getSTDIN();
@@ -128,7 +128,7 @@ public:
/// Open the specified file as a MemoryBuffer, or open stdin if the Filename
/// is "-".
static ErrorOr<std::unique_ptr<MemoryBuffer>>
- getFileOrSTDIN(StringRef Filename, int64_t FileSize = -1);
+ getFileOrSTDIN(const Twine &Filename, int64_t FileSize = -1);
//===--------------------------------------------------------------------===//
// Provided for performance analysis.
Modified: llvm/trunk/lib/Support/MemoryBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MemoryBuffer.cpp?rev=219224&r1=219223&r2=219224&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MemoryBuffer.cpp (original)
+++ llvm/trunk/lib/Support/MemoryBuffer.cpp Tue Oct 7 13:58:55 2014
@@ -64,14 +64,17 @@ static void CopyStringRef(char *Memory,
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(MemoryBufferR
}
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
}
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 Siz
}
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, StringR
}
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 *File
}
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);
More information about the llvm-commits
mailing list