Change the MemoryBuffer::getFile* methods to take just a pointer to the

start of a filename, not a filename+length.  All clients can produce a
null terminated name, and the system api's require null terminated 
strings anyway.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49041 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2008-04-01 18:04:03 +00:00
parent 8eedc9ee2c
commit 038112a4e0
14 changed files with 35 additions and 53 deletions

View File

@ -52,7 +52,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 MemoryBuffer *getFile(const char *FilenameStart, unsigned FnSize,
static MemoryBuffer *getFile(const char *Filename,
std::string *ErrStr = 0,
int64_t FileSize = -1);
@ -90,7 +90,7 @@ public:
/// 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 *FilenameStart,unsigned FnSize,
static MemoryBuffer *getFileOrSTDIN(const char *Filename,
std::string *ErrStr = 0,
int64_t FileSize = -1);
@ -100,7 +100,7 @@ public:
static MemoryBuffer *getFileOrSTDIN(const std::string &FN,
std::string *ErrStr = 0,
int64_t FileSize = -1) {
return getFileOrSTDIN(&FN[0], FN.size(), ErrStr, FileSize);
return getFileOrSTDIN(FN.c_str(), ErrStr, FileSize);
}
};

View File

@ -145,7 +145,7 @@ Archive::Archive(const sys::Path& filename)
bool
Archive::mapToMemory(std::string* ErrMsg) {
mapfile = MemoryBuffer::getFile(archPath.c_str(), archPath.size(), ErrMsg);
mapfile = MemoryBuffer::getFile(archPath.c_str(), ErrMsg);
if (mapfile == 0)
return true;
base = mapfile->getBufferStart();
@ -211,8 +211,7 @@ bool llvm::GetBitcodeSymbols(const sys::Path& fName,
std::vector<std::string>& symbols,
std::string* ErrMsg) {
std::auto_ptr<MemoryBuffer> Buffer(
MemoryBuffer::getFileOrSTDIN(&fName.toString()[0],
fName.toString().size()));
MemoryBuffer::getFileOrSTDIN(fName.c_str()));
if (!Buffer.get()) {
if (ErrMsg) *ErrMsg = "Could not open file '" + fName.toString() + "'";
return true;

View File

@ -212,8 +212,7 @@ Archive::writeMember(
const char *data = (const char*)member.getData();
MemoryBuffer *mFile = 0;
if (!data) {
mFile = MemoryBuffer::getFile(member.getPath().c_str(),
member.getPath().size(), ErrMsg);
mFile = MemoryBuffer::getFile(member.getPath().c_str(), ErrMsg);
if (mFile == 0)
return true;
data = mFile->getBufferStart();
@ -407,8 +406,7 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// Map in the archive we just wrote.
{
OwningPtr<MemoryBuffer> arch(MemoryBuffer::getFile(TmpArchive.c_str(),
TmpArchive.size()));
OwningPtr<MemoryBuffer> arch(MemoryBuffer::getFile(TmpArchive.c_str()));
if (arch == 0) return true;
const char* base = arch->getBufferStart();

View File

@ -22,8 +22,7 @@ ParseError* TheParseError = 0; /// FIXME: Not threading friendly
Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError* Err) {
std::string ErrorStr;
MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(&Filename[0], Filename.size(),
&ErrorStr);
MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
if (F == 0) {
if (Err)
Err->setError(Filename, "Could not open input file '" + Filename + "'");

View File

@ -47,7 +47,7 @@ std::string Debugger::getProgramPath() const {
static Module *
getMaterializedModuleProvider(const std::string &Filename) {
std::auto_ptr<MemoryBuffer> Buffer;
Buffer.reset(MemoryBuffer::getFileOrSTDIN(&Filename[0], Filename.size()));
Buffer.reset(MemoryBuffer::getFileOrSTDIN(Filename.c_str()));
if (Buffer.get())
return ParseBitcodeFile(Buffer.get());
return 0;

View File

@ -19,7 +19,8 @@
#include "llvm/Support/Streams.h"
using namespace llvm;
Linker::Linker(const std::string& progname, const std::string& modname, unsigned flags)
Linker::Linker(const std::string& progname, const std::string& modname,
unsigned flags)
: Composite(0)
, LibPaths()
, Flags(flags)
@ -103,8 +104,7 @@ Linker::LoadObject(const sys::Path &FN) {
Module *Result = 0;
const std::string &FNS = FN.toString();
std::auto_ptr<MemoryBuffer> Buffer(
MemoryBuffer::getFileOrSTDIN(&FNS[0], FNS.size()));
std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FNS.c_str()));
if (Buffer.get())
Result = ParseBitcodeFile(Buffer.get(), &ParseErrorMessage);
else

View File

@ -189,10 +189,8 @@ int llvm::DiffFilesWithTolerance(const sys::PathWithStatus &FileA,
// Now its safe to mmap the files into memory becasue both files
// have a non-zero size.
OwningPtr<MemoryBuffer> F1(MemoryBuffer::getFile(FileA.c_str(), FileA.size(),
Error));
OwningPtr<MemoryBuffer> F2(MemoryBuffer::getFile(FileB.c_str(), FileB.size(),
Error));
OwningPtr<MemoryBuffer> F1(MemoryBuffer::getFile(FileA.c_str(), Error));
OwningPtr<MemoryBuffer> F2(MemoryBuffer::getFile(FileB.c_str(), Error));
if (F1 == 0 || F2 == 0)
return 2;

View File

@ -132,12 +132,11 @@ MemoryBuffer *MemoryBuffer::getNewMemBuffer(unsigned Size,
/// 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 *FilenameStart,
unsigned FnSize,
MemoryBuffer *MemoryBuffer::getFileOrSTDIN(const char *Filename,
std::string *ErrStr,
int64_t FileSize) {
if (FnSize != 1 || FilenameStart[0] != '-')
return getFile(FilenameStart, FnSize, ErrStr, FileSize);
if (Filename[0] != '-' || Filename[1] != 0)
return getFile(Filename, ErrStr, FileSize);
MemoryBuffer *M = getSTDIN();
if (M) return M;
@ -172,17 +171,13 @@ public:
};
}
MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize,
std::string *ErrStr, int64_t FileSize) {
// Null terminate the filename.
SmallString<1000> Filename(FilenameStart, FilenameStart+FnSize);
Filename.push_back(0);
MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr,
int64_t FileSize) {
int OpenFlags = 0;
#ifdef O_BINARY
Flags |= O_BINARY; // Open input file in binary mode on win32.
#endif
int FD = ::open(&Filename[0], O_RDONLY|OpenFlags);
int FD = ::open(Filename, O_RDONLY|OpenFlags);
if (FD == -1) {
if (ErrStr) *ErrStr = "could not open file";
return 0;
@ -211,12 +206,12 @@ MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize,
if (const char *Pages = sys::Path::MapInFilePages(FD, FileSize)) {
// Close the file descriptor, now that the whole file is in memory.
::close(FD);
return new MemoryBufferMMapFile(&Filename[0], Pages, FileSize);
return new MemoryBufferMMapFile(Filename, Pages, FileSize);
}
}
OwningPtr<MemoryBuffer> SB;
SB.reset(MemoryBuffer::getNewUninitMemBuffer(FileSize, &Filename[0]));
SB.reset(MemoryBuffer::getNewUninitMemBuffer(FileSize, Filename));
char *BufPtr = const_cast<char*>(SB->getBufferStart());
unsigned BytesLeft = FileSize;

View File

@ -1307,7 +1307,7 @@ int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
LLVMMemoryBufferRef *OutMemBuf,
char **OutMessage) {
std::string Error;
if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, strlen(Path), &Error)) {
if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
*OutMemBuf = wrap(MB);
return 0;
}

View File

@ -374,11 +374,7 @@ static void PrintSize(double Bits) {
/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
static int AnalyzeBitcode() {
// Read the input file.
MemoryBuffer *Buffer;
if (InputFilename == "-")
Buffer = MemoryBuffer::getSTDIN();
else
Buffer = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size());
MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str());
if (Buffer == 0)
return Error("Error reading '" + InputFilename + "'.");

View File

@ -186,8 +186,7 @@ const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
delete _nativeObjectFile;
// read .o file into memory buffer
_nativeObjectFile = MemoryBuffer::getFile(&uniqueObjStr[0],
uniqueObjStr.size(), &errMsg);
_nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(),&errMsg);
}
// remove temp files

View File

@ -43,11 +43,11 @@ bool LTOModule::isBitcodeFile(const char* path)
return llvm::sys::Path(path).isBitcodeFile();
}
bool LTOModule::isBitcodeFileForTarget(const void* mem,
size_t length, const char* triplePrefix)
bool LTOModule::isBitcodeFileForTarget(const void* mem, size_t length,
const char* triplePrefix)
{
MemoryBuffer* buffer = MemoryBuffer::getMemBuffer((char*)mem,
(char*)mem+length);
(char*)mem+length);
if ( buffer == NULL )
return false;
return isTargetMatch(buffer, triplePrefix);
@ -55,10 +55,10 @@ bool LTOModule::isBitcodeFileForTarget(const void* mem,
bool LTOModule::isBitcodeFileForTarget(const char* path,
const char* triplePrefix)
const char* triplePrefix)
{
MemoryBuffer* buffer = MemoryBuffer::getFile(path, strlen(path));
if ( buffer == NULL )
MemoryBuffer *buffer = MemoryBuffer::getFile(path);
if (buffer == NULL)
return false;
return isTargetMatch(buffer, triplePrefix);
}
@ -85,8 +85,7 @@ LTOModule::LTOModule(Module* m, TargetMachine* t)
LTOModule* LTOModule::makeLTOModule(const char* path, std::string& errMsg)
{
OwningPtr<MemoryBuffer> buffer(MemoryBuffer::getFile(
path, strlen(path), &errMsg));
OwningPtr<MemoryBuffer> buffer(MemoryBuffer::getFile(path, &errMsg));
if ( !buffer )
return NULL;
return makeLTOModule(buffer.get(), errMsg);

View File

@ -256,12 +256,12 @@ bool TGLexer::LexInclude() {
std::string Filename = CurStrVal;
// Try to find the file.
MemoryBuffer *NewBuf = MemoryBuffer::getFile(&Filename[0], Filename.size());
MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str());
// If the file didn't exist directly, see if it's in an include path.
for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
std::string IncFile = IncludeDirectories[i] + "/" + Filename;
NewBuf = MemoryBuffer::getFile(&IncFile[0], IncFile.size());
NewBuf = MemoryBuffer::getFile(IncFile.c_str());
}
if (NewBuf == 0) {

View File

@ -107,8 +107,7 @@ RecordKeeper llvm::Records;
static bool ParseFile(const std::string &Filename,
const std::vector<std::string> &IncludeDirs) {
std::string ErrorStr;
MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(&Filename[0], Filename.size(),
&ErrorStr);
MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
if (F == 0) {
cerr << "Could not open input file '" + Filename + "': " << ErrorStr <<"\n";
return true;