diff options
-rw-r--r-- | include/llvm/Support/MemoryBuffer.h | 3 | ||||
-rw-r--r-- | lib/Linker/LinkItems.cpp | 21 | ||||
-rw-r--r-- | lib/Support/MemoryBuffer.cpp | 9 | ||||
-rw-r--r-- | lib/VMCore/Core.cpp | 13 |
4 files changed, 26 insertions, 20 deletions
diff --git a/include/llvm/Support/MemoryBuffer.h b/include/llvm/Support/MemoryBuffer.h index ef7af69..38f6193 100644 --- a/include/llvm/Support/MemoryBuffer.h +++ b/include/llvm/Support/MemoryBuffer.h @@ -89,7 +89,8 @@ public: StringRef BufferName = ""); /// getSTDIN - Read all of stdin into a file buffer, and return it. - static MemoryBuffer *getSTDIN(); + /// If an error occurs, this returns null and fills in *ErrStr with a reason. + static MemoryBuffer *getSTDIN(std::string *ErrStr = 0); /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin diff --git a/lib/Linker/LinkItems.cpp b/lib/Linker/LinkItems.cpp index f0c7b68..1be2bec 100644 --- a/lib/Linker/LinkItems.cpp +++ b/lib/Linker/LinkItems.cpp @@ -160,16 +160,17 @@ bool Linker::LinkInFile(const sys::Path &File, bool &is_native) { // Check for a file of name "-", which means "read standard input" if (File.str() == "-") { std::auto_ptr<Module> M; - 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; + if (MemoryBuffer *Buffer = MemoryBuffer::getSTDIN(&Error)) { + 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; + } } return error("Cannot link stdin: " + Error); } diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp index 2b95089..ac79518 100644 --- a/lib/Support/MemoryBuffer.cpp +++ b/lib/Support/MemoryBuffer.cpp @@ -137,7 +137,7 @@ MemoryBuffer *MemoryBuffer::getFileOrSTDIN(StringRef Filename, int64_t FileSize, struct stat *FileInfo) { if (Filename == "-") - return getSTDIN(); + return getSTDIN(ErrStr); return getFile(Filename, ErrStr, FileSize, FileInfo); } @@ -263,7 +263,7 @@ public: }; } -MemoryBuffer *MemoryBuffer::getSTDIN() { +MemoryBuffer *MemoryBuffer::getSTDIN(std::string *ErrStr) { char Buffer[4096*4]; std::vector<char> FileData; @@ -279,6 +279,11 @@ MemoryBuffer *MemoryBuffer::getSTDIN() { FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes); } while (ReadBytes == sizeof(Buffer)); + if (!feof(stdin)) { + if (ErrStr) *ErrStr = "error reading from stdin"; + return 0; + } + FileData.push_back(0); // &FileData[Size] is invalid. So is &*FileData.end(). size_t Size = FileData.size(); MemoryBuffer *B = new STDINBufferFile(); diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp index bbf1375..295d148 100644 --- a/lib/VMCore/Core.cpp +++ b/lib/VMCore/Core.cpp @@ -2205,15 +2205,14 @@ LLVMBool LLVMCreateMemoryBufferWithContentsOfFile( LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf, char **OutMessage) { - MemoryBuffer *MB = MemoryBuffer::getSTDIN(); - if (!MB->getBufferSize()) { - delete MB; - *OutMessage = strdup("stdin is empty."); - return 1; + std::string Error; + if (MemoryBuffer *MB = MemoryBuffer::getSTDIN(&Error)) { + *OutMemBuf = wrap(MB); + return 0; } - *OutMemBuf = wrap(MB); - return 0; + *OutMessage = strdup(Error.c_str()); + return 1; } void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) { |