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
This commit is contained in:
Rafael Espindola 2014-10-07 18:58:55 +00:00
parent 5c98f14b78
commit 6477842cee
2 changed files with 33 additions and 28 deletions

View File

@ -72,7 +72,7 @@ public:
/// changing, e.g. when libclang tries to parse while the user is /// changing, e.g. when libclang tries to parse while the user is
/// editing/updating the file. /// editing/updating the file.
static ErrorOr<std::unique_ptr<MemoryBuffer>> 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); bool RequiresNullTerminator = true, bool IsVolatileSize = false);
/// Given an already-open file descriptor, map some slice of it into a /// 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 /// changing, e.g. when libclang tries to parse while the user is
/// editing/updating the file. /// editing/updating the file.
static ErrorOr<std::unique_ptr<MemoryBuffer>> 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); int64_t Offset, bool IsVolatileSize = false);
/// Given an already-open file descriptor, read the file and return a /// 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 /// changing, e.g. when libclang tries to parse while the user is
/// editing/updating the file. /// editing/updating the file.
static ErrorOr<std::unique_ptr<MemoryBuffer>> 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); bool RequiresNullTerminator = true, bool IsVolatileSize = false);
/// Open the specified memory range as a MemoryBuffer. Note that InputData /// 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 /// Open the specified memory range as a MemoryBuffer, copying the contents
/// and taking ownership of it. InputData does not have to be null terminated. /// and taking ownership of it. InputData does not have to be null terminated.
static std::unique_ptr<MemoryBuffer> 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 /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
/// that the caller need not initialize the memory allocated by this method. /// 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 /// Note that the caller should initialize the memory allocated by this
/// method. The memory is owned by the MemoryBuffer object. /// method. The memory is owned by the MemoryBuffer object.
static std::unique_ptr<MemoryBuffer> 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. /// Read all of stdin into a file buffer, and return it.
static ErrorOr<std::unique_ptr<MemoryBuffer>> getSTDIN(); 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 /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
/// is "-". /// is "-".
static ErrorOr<std::unique_ptr<MemoryBuffer>> 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. // Provided for performance analysis.

View File

@ -64,14 +64,17 @@ static void CopyStringRef(char *Memory, StringRef Data) {
namespace { namespace {
struct NamedBufferAlloc { struct NamedBufferAlloc {
StringRef Name; const Twine &Name;
NamedBufferAlloc(StringRef Name) : Name(Name) {} NamedBufferAlloc(const Twine &Name) : Name(Name) {}
}; };
} }
void *operator new(size_t N, const NamedBufferAlloc &Alloc) { void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
char *Mem = static_cast<char *>(operator new(N + Alloc.Name.size() + 1)); SmallString<256> NameBuf;
CopyStringRef(Mem + N, Alloc.Name); StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
char *Mem = static_cast<char *>(operator new(N + NameRef.size() + 1));
CopyStringRef(Mem + N, NameRef);
return Mem; return Mem;
} }
@ -109,7 +112,7 @@ MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
} }
std::unique_ptr<MemoryBuffer> std::unique_ptr<MemoryBuffer>
MemoryBuffer::getMemBufferCopy(StringRef InputData, StringRef BufferName) { MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
std::unique_ptr<MemoryBuffer> Buf = std::unique_ptr<MemoryBuffer> Buf =
getNewUninitMemBuffer(InputData.size(), BufferName); getNewUninitMemBuffer(InputData.size(), BufferName);
if (!Buf) if (!Buf)
@ -120,20 +123,22 @@ MemoryBuffer::getMemBufferCopy(StringRef InputData, StringRef BufferName) {
} }
std::unique_ptr<MemoryBuffer> 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 // Allocate space for the MemoryBuffer, the data and the name. It is important
// that MemoryBuffer and data are aligned so PointerIntPair works with them. // that MemoryBuffer and data are aligned so PointerIntPair works with them.
// TODO: Is 16-byte alignment enough? We copy small object files with large // TODO: Is 16-byte alignment enough? We copy small object files with large
// alignment expectations into this buffer. // alignment expectations into this buffer.
SmallString<256> NameBuf;
StringRef NameRef = BufferName.toStringRef(NameBuf);
size_t AlignedStringLen = size_t AlignedStringLen =
RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1, 16); RoundUpToAlignment(sizeof(MemoryBufferMem) + NameRef.size() + 1, 16);
size_t RealLen = AlignedStringLen + Size + 1; size_t RealLen = AlignedStringLen + Size + 1;
char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow)); char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
if (!Mem) if (!Mem)
return nullptr; return nullptr;
// The name is stored after the class itself. // 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. // The buffer begins after the name and must be aligned.
char *Buf = Mem + AlignedStringLen; char *Buf = Mem + AlignedStringLen;
@ -153,8 +158,11 @@ MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
} }
ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOr<std::unique_ptr<MemoryBuffer>>
MemoryBuffer::getFileOrSTDIN(StringRef Filename, int64_t FileSize) { MemoryBuffer::getFileOrSTDIN(const Twine &Filename, int64_t FileSize) {
if (Filename == "-") SmallString<256> NameBuf;
StringRef NameRef = Filename.toStringRef(NameBuf);
if (NameRef == "-")
return getSTDIN(); return getSTDIN();
return getFile(Filename, FileSize); return getFile(Filename, FileSize);
} }
@ -206,7 +214,7 @@ public:
} }
static ErrorOr<std::unique_ptr<MemoryBuffer>> static ErrorOr<std::unique_ptr<MemoryBuffer>>
getMemoryBufferForStream(int FD, StringRef BufferName) { getMemoryBufferForStream(int FD, const Twine &BufferName) {
const ssize_t ChunkSize = 4096*4; const ssize_t ChunkSize = 4096*4;
SmallString<ChunkSize> Buffer; SmallString<ChunkSize> Buffer;
ssize_t ReadBytes; ssize_t ReadBytes;
@ -225,26 +233,23 @@ getMemoryBufferForStream(int FD, StringRef BufferName) {
} }
static ErrorOr<std::unique_ptr<MemoryBuffer>> 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); bool IsVolatileSize);
ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOr<std::unique_ptr<MemoryBuffer>>
MemoryBuffer::getFile(Twine Filename, int64_t FileSize, MemoryBuffer::getFile(const Twine &Filename, int64_t FileSize,
bool RequiresNullTerminator, bool IsVolatileSize) { bool RequiresNullTerminator, bool IsVolatileSize) {
// Ensure the path is null terminated. return getFileAux(Filename, FileSize, RequiresNullTerminator,
SmallString<256> PathBuf;
StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf);
return getFileAux(NullTerminatedName.data(), FileSize, RequiresNullTerminator,
IsVolatileSize); IsVolatileSize);
} }
static ErrorOr<std::unique_ptr<MemoryBuffer>> 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, uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
bool IsVolatileSize); bool IsVolatileSize);
static ErrorOr<std::unique_ptr<MemoryBuffer>> 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) { bool IsVolatileSize) {
int FD; int FD;
std::error_code EC = sys::fs::openFileForRead(Filename, 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>> 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, uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
bool IsVolatileSize) { bool IsVolatileSize) {
static int PageSize = sys::process::get_self()->page_size(); 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>> 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) { bool RequiresNullTerminator, bool IsVolatileSize) {
return getOpenFileImpl(FD, Filename, FileSize, FileSize, 0, return getOpenFileImpl(FD, Filename, FileSize, FileSize, 0,
RequiresNullTerminator, IsVolatileSize); RequiresNullTerminator, IsVolatileSize);
} }
ErrorOr<std::unique_ptr<MemoryBuffer>> 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) { int64_t Offset, bool IsVolatileSize) {
return getOpenFileImpl(FD, Filename, -1, MapSize, Offset, false, return getOpenFileImpl(FD, Filename, -1, MapSize, Offset, false,
IsVolatileSize); IsVolatileSize);