Extend MemoryBuffer::getFile() to take an optional "stat" structure

pointer. If given, the structure will be set with the stat information from
the file actually read.

llvm-svn: 98575
This commit is contained in:
Douglas Gregor 2010-03-15 20:32:14 +00:00
parent db8c479e0d
commit 84ef0ef3b9
2 changed files with 15 additions and 9 deletions

View File

@ -17,6 +17,7 @@
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/System/DataTypes.h" #include "llvm/System/DataTypes.h"
#include <string> #include <string>
#include <sys/stat.h>
namespace llvm { namespace llvm {
@ -59,7 +60,8 @@ public:
/// it has the specified size. /// it has the specified size.
static MemoryBuffer *getFile(StringRef Filename, static MemoryBuffer *getFile(StringRef Filename,
std::string *ErrStr = 0, std::string *ErrStr = 0,
int64_t FileSize = -1); int64_t FileSize = -1,
struct stat *FileInfo = 0);
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
/// that EndPtr[0] must be a null byte and be accessible! /// that EndPtr[0] must be a null byte and be accessible!
@ -95,7 +97,8 @@ public:
/// in *ErrStr with a reason. /// in *ErrStr with a reason.
static MemoryBuffer *getFileOrSTDIN(StringRef Filename, static MemoryBuffer *getFileOrSTDIN(StringRef Filename,
std::string *ErrStr = 0, std::string *ErrStr = 0,
int64_t FileSize = -1); int64_t FileSize = -1,
struct stat *FileInfo = 0);
}; };
} // end namespace llvm } // end namespace llvm

View File

@ -136,10 +136,11 @@ MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size,
/// returns an empty buffer. /// returns an empty buffer.
MemoryBuffer *MemoryBuffer::getFileOrSTDIN(StringRef Filename, MemoryBuffer *MemoryBuffer::getFileOrSTDIN(StringRef Filename,
std::string *ErrStr, std::string *ErrStr,
int64_t FileSize) { int64_t FileSize,
struct stat *FileInfo) {
if (Filename == "-") if (Filename == "-")
return getSTDIN(); return getSTDIN();
return getFile(Filename, ErrStr, FileSize); return getFile(Filename, ErrStr, FileSize, FileInfo);
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -169,7 +170,7 @@ public:
} }
MemoryBuffer *MemoryBuffer::getFile(StringRef Filename, std::string *ErrStr, MemoryBuffer *MemoryBuffer::getFile(StringRef Filename, std::string *ErrStr,
int64_t FileSize) { int64_t FileSize, struct stat *FileInfo) {
int OpenFlags = 0; int OpenFlags = 0;
#ifdef O_BINARY #ifdef O_BINARY
OpenFlags |= O_BINARY; // Open input file in binary mode on win32. OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
@ -183,15 +184,17 @@ MemoryBuffer *MemoryBuffer::getFile(StringRef Filename, std::string *ErrStr,
// If we don't know the file size, use fstat to find out. fstat on an open // If we don't know the file size, use fstat to find out. fstat on an open
// file descriptor is cheaper than stat on a random path. // file descriptor is cheaper than stat on a random path.
if (FileSize == -1) { if (FileSize == -1 || FileInfo) {
struct stat FileInfo; struct stat MyFileInfo;
struct stat *FileInfoPtr = FileInfo? FileInfo : &MyFileInfo;
// TODO: This should use fstat64 when available. // TODO: This should use fstat64 when available.
if (fstat(FD, &FileInfo) == -1) { if (fstat(FD, FileInfoPtr) == -1) {
if (ErrStr) *ErrStr = strerror(errno); if (ErrStr) *ErrStr = strerror(errno);
::close(FD); ::close(FD);
return 0; return 0;
} }
FileSize = FileInfo.st_size; FileSize = FileInfoPtr->st_size;
} }