Pass a StringRef to sys::identifyFileType.

llvm-svn: 183669
This commit is contained in:
Rafael Espindola 2013-06-10 15:27:39 +00:00
parent aecb66ec33
commit 1dc43065a7
9 changed files with 15 additions and 18 deletions

View File

@ -44,6 +44,7 @@ public:
const char *getBufferStart() const { return Buffer->getBufferStart(); } const char *getBufferStart() const { return Buffer->getBufferStart(); }
size_t getBufferSize() const { return Buffer->getBufferSize(); } size_t getBufferSize() const { return Buffer->getBufferSize(); }
StringRef getBuffer() const { return Buffer->getBuffer(); }
protected: protected:
// The memory contained in an ObjectBuffer // The memory contained in an ObjectBuffer

View File

@ -725,9 +725,9 @@ namespace sys {
/// This utility function allows any memory block to be examined in order /// This utility function allows any memory block to be examined in order
/// to determine its file type. /// to determine its file type.
LLVMFileType identifyFileType(const char *Magic, unsigned Length); LLVMFileType identifyFileType(StringRef Magic);
inline LLVMFileType IdentifyFileType(const char *Magic, unsigned Length) { inline LLVMFileType IdentifyFileType(const char *Magic, unsigned Length) {
return identifyFileType(Magic, Length); return identifyFileType(StringRef(Magic, Length));
} }
/// This function can be used to copy the file specified by Src to the /// This function can be used to copy the file specified by Src to the

View File

@ -129,7 +129,7 @@ bool ArchiveMember::replaceWith(const sys::Path& newFile, std::string* ErrMsg) {
} }
// Determine what kind of file it is. // Determine what kind of file it is.
switch (sys::IdentifyFileType(signature,4)) { switch (sys::identifyFileType(StringRef(signature, 4))) {
case sys::Bitcode_FileType: case sys::Bitcode_FileType:
flags |= BitcodeFlag; flags |= BitcodeFlag;
break; break;

View File

@ -205,7 +205,7 @@ Archive::parseMemberHeader(const char*& At, const char* End, std::string* error)
} }
// Determine if this is a bitcode file // Determine if this is a bitcode file
switch (sys::IdentifyFileType(At, 4)) { switch (sys::identifyFileType(StringRef(At, 4))) {
case sys::Bitcode_FileType: case sys::Bitcode_FileType:
flags |= ArchiveMember::BitcodeFlag; flags |= ArchiveMember::BitcodeFlag;
break; break;

View File

@ -501,9 +501,7 @@ RuntimeDyld::~RuntimeDyld() {
ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) { ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
if (!Dyld) { if (!Dyld) {
sys::LLVMFileType type = sys::IdentifyFileType( sys::LLVMFileType type = sys::identifyFileType(InputBuffer->getBuffer());
InputBuffer->getBufferStart(),
static_cast<unsigned>(InputBuffer->getBufferSize()));
switch (type) { switch (type) {
case sys::ELF_Relocatable_FileType: case sys::ELF_Relocatable_FileType:
case sys::ELF_Executable_FileType: case sys::ELF_Executable_FileType:

View File

@ -45,8 +45,7 @@ error_code object::createBinary(MemoryBuffer *Source,
OwningPtr<MemoryBuffer> scopedSource(Source); OwningPtr<MemoryBuffer> scopedSource(Source);
if (!Source) if (!Source)
return make_error_code(errc::invalid_argument); return make_error_code(errc::invalid_argument);
sys::LLVMFileType type = sys::IdentifyFileType(Source->getBufferStart(), sys::LLVMFileType type = sys::identifyFileType(Source->getBuffer());
static_cast<unsigned>(Source->getBufferSize()));
error_code ec; error_code ec;
switch (type) { switch (type) {
case sys::Archive_FileType: { case sys::Archive_FileType: {

View File

@ -40,8 +40,7 @@ section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) { ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) {
if (!Object || Object->getBufferSize() < 64) if (!Object || Object->getBufferSize() < 64)
return 0; return 0;
sys::LLVMFileType type = sys::IdentifyFileType(Object->getBufferStart(), sys::LLVMFileType type = sys::identifyFileType(Object->getBuffer());
static_cast<unsigned>(Object->getBufferSize()));
switch (type) { switch (type) {
case sys::Unknown_FileType: case sys::Unknown_FileType:
return 0; return 0;

View File

@ -38,8 +38,8 @@ bool Path::operator<(const Path& that) const {
} }
LLVMFileType LLVMFileType
sys::identifyFileType(const char *Magic, unsigned Length) { sys::identifyFileType(StringRef Magic) {
assert(Magic && "Invalid magic number string"); unsigned Length = Magic.size();
assert(Length >= 4 && "Invalid magic number length"); assert(Length >= 4 && "Invalid magic number length");
switch ((unsigned char)Magic[0]) { switch ((unsigned char)Magic[0]) {
case 0xDE: // 0x0B17C0DE = BC wraper case 0xDE: // 0x0B17C0DE = BC wraper
@ -53,7 +53,7 @@ sys::identifyFileType(const char *Magic, unsigned Length) {
break; break;
case '!': case '!':
if (Length >= 8) if (Length >= 8)
if (memcmp(Magic,"!<arch>\n",8) == 0) if (memcmp(Magic.data(),"!<arch>\n",8) == 0)
return Archive_FileType; return Archive_FileType;
break; break;
@ -136,9 +136,9 @@ sys::identifyFileType(const char *Magic, unsigned Length) {
case 0x4d: // Possible MS-DOS stub on Windows PE file case 0x4d: // Possible MS-DOS stub on Windows PE file
if (Magic[1] == 0x5a) { if (Magic[1] == 0x5a) {
uint32_t off = uint32_t off =
*reinterpret_cast<const ulittle32_t *>(Magic + 0x3c); *reinterpret_cast<const ulittle32_t *>(Magic.data() + 0x3c);
// PE/COFF file, either EXE or DLL. // PE/COFF file, either EXE or DLL.
if (off < Length && memcmp(Magic + off, "PE\0\0",4) == 0) if (off < Length && memcmp(Magic.data() + off, "PE\0\0",4) == 0)
return COFF_FileType; return COFF_FileType;
} }
break; break;

View File

@ -163,7 +163,7 @@ LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM /// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
/// bitcode. /// bitcode.
bool LTOModule::isBitcodeFile(const void *mem, size_t length) { bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
return llvm::sys::IdentifyFileType((const char*)mem, length) return llvm::sys::identifyFileType(StringRef((const char*)mem, length))
== llvm::sys::Bitcode_FileType; == llvm::sys::Bitcode_FileType;
} }