diff --git a/llvm/include/llvm/Support/MemoryBuffer.h b/llvm/include/llvm/Support/MemoryBuffer.h index 4f28da4094c8..ff22fb67c84e 100644 --- a/llvm/include/llvm/Support/MemoryBuffer.h +++ b/llvm/include/llvm/Support/MemoryBuffer.h @@ -14,7 +14,7 @@ #ifndef LLVM_SUPPORT_MEMORYBUFFER_H #define LLVM_SUPPORT_MEMORYBUFFER_H -#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Twine.h" #include "llvm/Support/CBindingWrapping.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/DataTypes.h" @@ -66,11 +66,7 @@ public: /// MemoryBuffer if successful, otherwise returning null. If FileSize is /// specified, this means that the client knows that the file exists and that /// it has the specified size. - static error_code getFile(StringRef Filename, OwningPtr &result, - int64_t FileSize = -1, - bool RequiresNullTerminator = true); - static error_code getFile(const char *Filename, - OwningPtr &result, + static error_code getFile(Twine Filename, OwningPtr &result, int64_t FileSize = -1, bool RequiresNullTerminator = true); diff --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp index 53437c858b6f..dcd55299213b 100644 --- a/llvm/lib/Support/MemoryBuffer.cpp +++ b/llvm/lib/Support/MemoryBuffer.cpp @@ -238,14 +238,19 @@ static error_code getMemoryBufferForStream(int FD, return error_code::success(); } -error_code MemoryBuffer::getFile(StringRef Filename, +static error_code getFileAux(const char *Filename, + OwningPtr &result, int64_t FileSize, + bool RequiresNullTerminator); + +error_code MemoryBuffer::getFile(Twine Filename, OwningPtr &result, int64_t FileSize, bool RequiresNullTerminator) { // Ensure the path is null terminated. - SmallString<256> PathBuf(Filename.begin(), Filename.end()); - return MemoryBuffer::getFile(PathBuf.c_str(), result, FileSize, - RequiresNullTerminator); + SmallString<256> PathBuf; + StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf); + return getFileAux(NullTerminatedName.data(), result, FileSize, + RequiresNullTerminator); } static error_code getOpenFileImpl(int FD, const char *Filename, @@ -253,10 +258,9 @@ static error_code getOpenFileImpl(int FD, const char *Filename, uint64_t FileSize, uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator); -error_code MemoryBuffer::getFile(const char *Filename, - OwningPtr &result, - int64_t FileSize, - bool RequiresNullTerminator) { +static error_code getFileAux(const char *Filename, + OwningPtr &result, int64_t FileSize, + bool RequiresNullTerminator) { int FD; error_code EC = sys::fs::openFileForRead(Filename, FD); if (EC) diff --git a/llvm/unittests/Support/MemoryBufferTest.cpp b/llvm/unittests/Support/MemoryBufferTest.cpp index aa7ff6f3575c..2b8806c394f3 100644 --- a/llvm/unittests/Support/MemoryBufferTest.cpp +++ b/llvm/unittests/Support/MemoryBufferTest.cpp @@ -79,7 +79,7 @@ TEST_F(MemoryBufferTest, NullTerminator4K) { OF.close(); OwningPtr MB; - error_code EC = MemoryBuffer::getFile(TestPath, MB); + error_code EC = MemoryBuffer::getFile(TestPath.c_str(), MB); ASSERT_FALSE(EC); const char *BufData = MB->getBufferStart();