Skip to content

Commit d24cfed

Browse files
author
Brian Gaeke
committed
Throw better error messages, by calling strerror(errno) when we
get an error inside the bytecode reader. llvm-svn: 10415
1 parent 1f14737 commit d24cfed

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

llvm/lib/Bytecode/Reader/ReaderWrappers.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "Support/StringExtras.h"
2020
#include "Config/fcntl.h"
2121
#include <sys/stat.h>
22+
#include <cerrno>
2223
#include "Config/unistd.h"
2324
#include "Config/sys/mman.h"
2425
using namespace llvm;
@@ -57,22 +58,26 @@ namespace {
5758
};
5859
}
5960

61+
static std::string ErrnoMessage (int savedErrNum, std::string descr) {
62+
return ::strerror(savedErrNum) + std::string(", while trying to ") + descr;
63+
}
64+
6065
BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
6166
FDHandle FD = open(Filename.c_str(), O_RDONLY);
6267
if (FD == -1)
63-
throw std::string("Error opening file!");
68+
throw ErrnoMessage(errno, "open '" + Filename + "'");
6469

6570
// Stat the file to get its length...
6671
struct stat StatBuf;
6772
if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
68-
throw std::string("Error stat'ing file!");
73+
throw ErrnoMessage(errno, "stat '" + Filename + "'");
6974

7075
// mmap in the file all at once...
7176
Length = StatBuf.st_size;
7277
Buffer = (unsigned char*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0);
7378

7479
if (Buffer == (unsigned char*)MAP_FAILED)
75-
throw std::string("Error mmapping file!");
80+
throw ErrnoMessage(errno, "map '" + Filename + "' into memory");
7681

7782
try {
7883
// Parse the bytecode we mmapped in
@@ -167,7 +172,7 @@ BytecodeStdinReader::BytecodeStdinReader() {
167172
// Read in all of the data from stdin, we cannot mmap stdin...
168173
while ((BlockSize = ::read(0 /*stdin*/, Buffer, 4096*4))) {
169174
if (BlockSize == -1)
170-
throw std::string("Error reading from stdin!");
175+
throw ErrnoMessage(errno, "read from standard input");
171176

172177
FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
173178
}

0 commit comments

Comments
 (0)