[llvm-commits] [llvm] r86630 - in /llvm/trunk: include/llvm/Support/MemoryBuffer.h lib/Linker/LinkItems.cpp lib/Support/MemoryBuffer.cpp lib/VMCore/Core.cpp
Daniel Dunbar
daniel at zuster.org
Mon Nov 9 16:43:59 PST 2009
Author: ddunbar
Date: Mon Nov 9 18:43:58 2009
New Revision: 86630
URL: http://llvm.org/viewvc/llvm-project?rev=86630&view=rev
Log:
Fix MemoryBuffer::getSTDIN to *not* return null if stdin is empty, this is a lame API.
Also, Stringrefify some more MemoryBuffer functions, and add two performance FIXMEs.
Modified:
llvm/trunk/include/llvm/Support/MemoryBuffer.h
llvm/trunk/lib/Linker/LinkItems.cpp
llvm/trunk/lib/Support/MemoryBuffer.cpp
llvm/trunk/lib/VMCore/Core.cpp
Modified: llvm/trunk/include/llvm/Support/MemoryBuffer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MemoryBuffer.h?rev=86630&r1=86629&r2=86630&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MemoryBuffer.h (original)
+++ llvm/trunk/include/llvm/Support/MemoryBuffer.h Mon Nov 9 18:43:58 2009
@@ -57,7 +57,7 @@
/// 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 MemoryBuffer *getFile(const char *Filename,
+ static MemoryBuffer *getFile(StringRef Filename,
std::string *ErrStr = 0,
int64_t FileSize = -1);
@@ -84,29 +84,18 @@
/// memory allocated by this method. The memory is owned by the MemoryBuffer
/// object.
static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
- const char *BufferName = "");
+ StringRef BufferName = "");
- /// getSTDIN - Read all of stdin into a file buffer, and return it. This
- /// returns null if stdin is empty.
+ /// getSTDIN - Read all of stdin into a file buffer, and return it.
static MemoryBuffer *getSTDIN();
/// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
/// if the Filename is "-". If an error occurs, this returns null and fills
- /// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
- /// returns an empty buffer.
- static MemoryBuffer *getFileOrSTDIN(const char *Filename,
- std::string *ErrStr = 0,
- int64_t FileSize = -1);
-
- /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
- /// if the Filename is "-". If an error occurs, this returns null and fills
/// in *ErrStr with a reason.
- static MemoryBuffer *getFileOrSTDIN(const std::string &FN,
+ static MemoryBuffer *getFileOrSTDIN(StringRef Filename,
std::string *ErrStr = 0,
- int64_t FileSize = -1) {
- return getFileOrSTDIN(FN.c_str(), ErrStr, FileSize);
- }
+ int64_t FileSize = -1);
};
} // end namespace llvm
Modified: llvm/trunk/lib/Linker/LinkItems.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/LinkItems.cpp?rev=86630&r1=86629&r2=86630&view=diff
==============================================================================
--- llvm/trunk/lib/Linker/LinkItems.cpp (original)
+++ llvm/trunk/lib/Linker/LinkItems.cpp Mon Nov 9 18:43:58 2009
@@ -160,14 +160,17 @@
// Check for a file of name "-", which means "read standard input"
if (File.str() == "-") {
std::auto_ptr<Module> M;
- if (MemoryBuffer *Buffer = MemoryBuffer::getSTDIN()) {
+ MemoryBuffer *Buffer = MemoryBuffer::getSTDIN();
+ if (!Buffer->getBufferSize()) {
+ delete Buffer;
+ Error = "standard input is empty";
+ } else {
M.reset(ParseBitcodeFile(Buffer, Context, &Error));
delete Buffer;
if (M.get())
if (!LinkInModule(M.get(), &Error))
return false;
- } else
- Error = "standard input is empty";
+ }
return error("Cannot link stdin: " + Error);
}
Modified: llvm/trunk/lib/Support/MemoryBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MemoryBuffer.cpp?rev=86630&r1=86629&r2=86630&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MemoryBuffer.cpp (original)
+++ llvm/trunk/lib/Support/MemoryBuffer.cpp Mon Nov 9 18:43:58 2009
@@ -70,7 +70,7 @@
class MemoryBufferMem : public MemoryBuffer {
std::string FileID;
public:
- MemoryBufferMem(const char *Start, const char *End, const char *FID,
+ MemoryBufferMem(const char *Start, const char *End, StringRef FID,
bool Copy = false)
: FileID(FID) {
if (!Copy)
@@ -107,7 +107,7 @@
/// initialize the memory allocated by this method. The memory is owned by
/// the MemoryBuffer object.
MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
- const char *BufferName) {
+ StringRef BufferName) {
char *Buf = (char *)malloc((Size+1) * sizeof(char));
if (!Buf) return 0;
Buf[Size] = 0;
@@ -134,17 +134,12 @@
/// if the Filename is "-". If an error occurs, this returns null and fills
/// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
/// returns an empty buffer.
-MemoryBuffer *MemoryBuffer::getFileOrSTDIN(const char *Filename,
+MemoryBuffer *MemoryBuffer::getFileOrSTDIN(StringRef Filename,
std::string *ErrStr,
int64_t FileSize) {
- if (Filename[0] != '-' || Filename[1] != 0)
- return getFile(Filename, ErrStr, FileSize);
- MemoryBuffer *M = getSTDIN();
- if (M) return M;
-
- // If stdin was empty, M is null. Cons up an empty memory buffer now.
- const char *EmptyStr = "";
- return MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<stdin>");
+ if (Filename == "-")
+ return getSTDIN();
+ return getFile(Filename, ErrStr, FileSize);
}
//===----------------------------------------------------------------------===//
@@ -158,7 +153,7 @@
class MemoryBufferMMapFile : public MemoryBuffer {
std::string Filename;
public:
- MemoryBufferMMapFile(const char *filename, const char *Pages, uint64_t Size)
+ MemoryBufferMMapFile(StringRef filename, const char *Pages, uint64_t Size)
: Filename(filename) {
init(Pages, Pages+Size);
}
@@ -173,13 +168,13 @@
};
}
-MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr,
+MemoryBuffer *MemoryBuffer::getFile(StringRef Filename, std::string *ErrStr,
int64_t FileSize) {
int OpenFlags = 0;
#ifdef O_BINARY
OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
#endif
- int FD = ::open(Filename, O_RDONLY|OpenFlags);
+ int FD = ::open(Filename.str().c_str(), O_RDONLY|OpenFlags);
if (FD == -1) {
if (ErrStr) *ErrStr = "could not open file";
return 0;
@@ -203,6 +198,8 @@
// for small files, because this can severely fragment our address space. Also
// don't try to map files that are exactly a multiple of the system page size,
// as the file would not have the required null terminator.
+ //
+ // FIXME: Can we just mmap an extra page in the latter case?
if (FileSize >= 4096*4 &&
(FileSize & (sys::Process::GetPageSize()-1)) != 0) {
if (const char *Pages = sys::Path::MapInFilePages(FD, FileSize)) {
@@ -262,6 +259,9 @@
std::vector<char> FileData;
// Read in all of the data from stdin, we cannot mmap stdin.
+ //
+ // FIXME: That isn't necessarily true, we should try to mmap stdin and
+ // fallback if it fails.
sys::Program::ChangeStdinToBinary();
size_t ReadBytes;
do {
@@ -271,8 +271,6 @@
FileData.push_back(0); // &FileData[Size] is invalid. So is &*FileData.end().
size_t Size = FileData.size();
- if (Size <= 1)
- return 0;
MemoryBuffer *B = new STDINBufferFile();
B->initCopyOf(&FileData[0], &FileData[Size-1]);
return B;
Modified: llvm/trunk/lib/VMCore/Core.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Core.cpp?rev=86630&r1=86629&r2=86630&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Core.cpp (original)
+++ llvm/trunk/lib/VMCore/Core.cpp Mon Nov 9 18:43:58 2009
@@ -1987,13 +1987,15 @@
int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
char **OutMessage) {
- if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
- *OutMemBuf = wrap(MB);
- return 0;
+ MemoryBuffer *MB = MemoryBuffer::getSTDIN();
+ if (!MB->getBufferSize()) {
+ delete MB;
+ *OutMessage = strdup("stdin is empty.");
+ return 1;
}
-
- *OutMessage = strdup("stdin is empty.");
- return 1;
+
+ *OutMemBuf = wrap(MB);
+ return 0;
}
void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
More information about the llvm-commits
mailing list