Cleanup system_error extensions.

llvm-svn: 149432
This commit is contained in:
Michael J. Spencer 2012-01-31 21:47:13 +00:00
parent aa37613305
commit 7aba8957c1
5 changed files with 193 additions and 105 deletions

View File

@ -0,0 +1,76 @@
//===- Error.h - system_error extensions for lld ----------------*- C++ -*-===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This declares a new error_category for the lld library.
//
//===----------------------------------------------------------------------===//
#ifndef LLD_CORE_ERROR_H
#define LLD_CORE_ERROR_H
#include "llvm/Support/system_error.h"
namespace lld {
const llvm::error_category &native_reader_category();
struct native_reader_error {
enum _ {
success = 0,
unknown_file_format,
file_too_short,
file_malformed,
unknown_chunk_type,
memory_error,
};
_ v_;
native_reader_error(_ v) : v_(v) {}
explicit native_reader_error(int v) : v_(_(v)) {}
operator int() const {return v_;}
};
inline llvm::error_code make_error_code(native_reader_error e) {
return llvm::error_code(static_cast<int>(e), native_reader_category());
}
const llvm::error_category &yaml_reader_category();
struct yaml_reader_error {
enum _ {
success = 0,
unknown_keyword,
illegal_value
};
_ v_;
yaml_reader_error(_ v) : v_(v) {}
explicit yaml_reader_error(int v) : v_(_(v)) {}
operator int() const {return v_;}
};
inline llvm::error_code make_error_code(yaml_reader_error e) {
return llvm::error_code(static_cast<int>(e), yaml_reader_category());
}
} // end namespace lld
namespace llvm {
template <> struct is_error_code_enum<lld::native_reader_error> : true_type { };
template <>
struct is_error_code_enum<lld::native_reader_error::_> : true_type { };
template <> struct is_error_code_enum<lld::yaml_reader_error> : true_type { };
template <>
struct is_error_code_enum<lld::yaml_reader_error::_> : true_type { };
} // end namespace llvm
#endif

View File

@ -1,4 +1,5 @@
add_lld_library(lldCore
Error.cpp
File.cpp
NativeFileFormat.h
NativeReader.cpp

92
lld/lib/Core/Error.cpp Normal file
View File

@ -0,0 +1,92 @@
//===- Error.cpp - system_error extensions for lld --------------*- C++ -*-===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/Core/Error.h"
#include "llvm/Support/ErrorHandling.h"
using namespace lld;
class _native_reader_error_category : public llvm::_do_message {
public:
virtual const char* name() const {
return "lld.native.reader";
}
virtual std::string message(int ev) const {
switch (ev) {
case native_reader_error::success:
return "Success";
case native_reader_error::unknown_file_format:
return "Unknown file foramt";
case native_reader_error::file_too_short:
return "file truncated";
case native_reader_error::file_malformed:
return "file malformed";
case native_reader_error::memory_error:
return "out of memory";
case native_reader_error::unknown_chunk_type:
return "unknown chunk type";
default:
llvm_unreachable("An enumerator of native_reader_error does not have a "
"message defined.");
}
}
virtual llvm::error_condition default_error_condition(int ev) const {
if (ev == native_reader_error::success)
return llvm::errc::success;
return llvm::errc::invalid_argument;
}
};
const llvm::error_category &lld::native_reader_category() {
static _native_reader_error_category o;
return o;
}
inline llvm::error_code make_error_code(native_reader_error e) {
return llvm::error_code(static_cast<int>(e), native_reader_category());
}
class _yaml_reader_error_category : public llvm::_do_message {
public:
virtual const char* name() const {
return "lld.yaml.reader";
}
virtual std::string message(int ev) const {
switch (ev) {
case yaml_reader_error::success:
return "Success";
case yaml_reader_error::unknown_keyword:
return "Unknown keyword found in yaml file";
case yaml_reader_error::illegal_value:
return "Bad value found in yaml file";
default:
llvm_unreachable("An enumerator of yaml_reader_error does not have a "
"message defined.");
}
}
virtual llvm::error_condition default_error_condition(int ev) const {
if (ev == yaml_reader_error::success)
return llvm::errc::success;
return llvm::errc::invalid_argument;
}
};
const llvm::error_category &lld::yaml_reader_category() {
static _yaml_reader_error_category o;
return o;
}
inline llvm::error_code make_error_code(yaml_reader_error e) {
return llvm::error_code(static_cast<int>(e), yaml_reader_category());
}

View File

@ -16,8 +16,8 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/system_error.h"
#include "lld/Core/Error.h"
#include "lld/Core/File.h"
#include "lld/Core/Atom.h"
@ -28,53 +28,6 @@ namespace lld {
// forward reference
class NativeFile;
enum native_reader_errors {
success = 0,
unknown_file_format,
file_too_short,
file_malformed,
unknown_chunk_type,
memory_error,
};
class reader_error_category : public llvm::_do_message {
public:
virtual const char* name() const {
return "lld.native.reader";
}
virtual std::string message(int ev) const;
};
const reader_error_category reader_error_category_singleton;
std::string reader_error_category::message(int ev) const {
switch (ev) {
case success:
return "Success";
case unknown_file_format:
return "Unknown file foramt";
case file_too_short:
return "file truncated";
case file_malformed:
return "file malformed";
case memory_error:
return "out of memory";
case unknown_chunk_type:
return "unknown chunk type";
default:
llvm_unreachable("An enumerator of native_reader_errors does not have a "
"message defined.");
}
}
inline llvm::error_code make_error_code(native_reader_errors e) {
return llvm::error_code(static_cast<int>(e), reader_error_category_singleton);
}
//
// An object of this class is instantied for each NativeDefinedAtomIvarsV1
// struct in the NCS_DefinedAtomsV1 chunk.
@ -179,13 +132,13 @@ public:
reinterpret_cast<const NativeChunk*>(base + sizeof(NativeFileHeader));
// make sure magic matches
if ( memcmp(header->magic, NATIVE_FILE_HEADER_MAGIC, 16) != 0 )
return make_error_code(unknown_file_format);
return make_error_code(native_reader_error::unknown_file_format);
// make sure mapped file contains all needed data
const size_t fileSize = mb->getBufferSize();
if ( header->fileSize > fileSize )
return make_error_code(file_too_short);
return make_error_code(native_reader_error::file_too_short);
// instantiate NativeFile object and add values to it as found
NativeFile* file = new NativeFile(mb, path);
@ -194,10 +147,10 @@ public:
llvm::error_code ec;
const NativeChunk* chunk = &chunks[i];
// sanity check chunk is within file
if ( chunk->fileOffset > fileSize )
return make_error_code(file_malformed);
if ( (chunk->fileOffset + chunk->fileSize) > fileSize)
return make_error_code(file_malformed);
if ( chunk->fileOffset > fileSize )
return make_error_code(native_reader_error::file_malformed);
if ( (chunk->fileOffset + chunk->fileSize) > fileSize)
return make_error_code(native_reader_error::file_malformed);
// process chunk, based on signature
switch ( chunk->signature ) {
case NCS_DefinedAtomsV1:
@ -213,7 +166,7 @@ public:
ec = file->processStrings(base, chunk);
break;
default:
return make_error_code(unknown_chunk_type);
return make_error_code(native_reader_error::unknown_chunk_type);
}
if ( ec ) {
delete file;
@ -224,9 +177,9 @@ public:
result = file;
}
return make_error_code(success);
return make_error_code(native_reader_error::success);
}
virtual ~NativeFile() {
@ -266,11 +219,11 @@ private:
uint8_t* atomsStart = reinterpret_cast<uint8_t*>
(operator new(atomsArraySize, std::nothrow));
if (atomsStart == NULL )
return make_error_code(memory_error);
const size_t ivarElementSize = chunk->fileSize
return make_error_code(native_reader_error::memory_error);
const size_t ivarElementSize = chunk->fileSize
/ chunk->elementCount;
if ( ivarElementSize != sizeof(NativeDefinedAtomIvarsV1) )
return make_error_code(file_malformed);
return make_error_code(native_reader_error::file_malformed);
uint8_t* atomsEnd = atomsStart + atomsArraySize;
const NativeDefinedAtomIvarsV1* ivarData =
reinterpret_cast<const NativeDefinedAtomIvarsV1*>
@ -284,14 +237,14 @@ private:
this->_definedAtoms.arrayStart = atomsStart;
this->_definedAtoms.arrayEnd = atomsEnd;
this->_definedAtoms.elementSize = atomSize;
return make_error_code(success);
return make_error_code(native_reader_error::success);
}
// set up pointers to attributes array
llvm::error_code processAttributesV1(const uint8_t* base, const NativeChunk* chunk) {
this->_attributes = base + chunk->fileOffset;
this->_attributesMaxOffset = chunk->fileSize;
return make_error_code(success);
return make_error_code(native_reader_error::success);
}
// set up pointers to string pool in file
@ -299,7 +252,7 @@ private:
const NativeChunk* chunk) {
this->_strings = reinterpret_cast<const char*>(base + chunk->fileOffset);
this->_stringsMaxOffset = chunk->fileSize;
return make_error_code(success);
return make_error_code(native_reader_error::success);
}
// set up pointers to content area in file
@ -307,7 +260,7 @@ private:
const NativeChunk* chunk) {
this->_contentStart = base + chunk->fileOffset;
this->_contentEnd = base + chunk->fileOffset + chunk->fileSize;
return make_error_code(success);
return make_error_code(native_reader_error::success);
}
llvm::StringRef string(uint32_t offset) const {

View File

@ -11,6 +11,7 @@
#include "lld/Core/YamlReader.h"
#include "lld/Core/Atom.h"
#include "lld/Core/Error.h"
#include "lld/Core/File.h"
#include "lld/Core/Reference.h"
@ -29,41 +30,6 @@
namespace lld {
namespace yaml {
enum yaml_reader_errors {
success = 0,
unknown_keyword,
illegal_value
};
class reader_error_category : public llvm::_do_message {
public:
virtual const char* name() const {
return "lld.yaml.reader";
}
virtual std::string message(int ev) const;
};
const reader_error_category reader_error_category_singleton;
std::string reader_error_category::message(int ev) const {
switch (ev) {
case success:
return "Success";
case unknown_keyword:
return "Unknown keyword found in yaml file";
case illegal_value:
return "Bad value found in yaml file";
default:
llvm_unreachable("An enumerator of yaml_reader_errors does not have a "
"message defined.");
}
}
inline llvm::error_code make_error_code(yaml_reader_errors e) {
return llvm::error_code(static_cast<int>(e), reader_error_category_singleton);
}
class YAML {
public:
struct Entry {
@ -704,8 +670,8 @@ llvm::error_code parseObjectText( llvm::MemoryBuffer *mb
}
else if (strcmp(entry->key, KeyValues::sizeKeyword) == 0) {
llvm::StringRef val = entry->value;
if ( val.getAsInteger(0, atomState._size) )
return make_error_code(illegal_value);
if ( val.getAsInteger(0, atomState._size) )
return make_error_code(yaml_reader_error::illegal_value);
haveAtom = true;
}
else if (strcmp(entry->key, KeyValues::contentKeyword) == 0) {
@ -720,7 +686,7 @@ llvm::error_code parseObjectText( llvm::MemoryBuffer *mb
inFixups = true;
}
else {
return make_error_code(unknown_keyword);
return make_error_code(yaml_reader_error::unknown_keyword);
}
}
else if (depthForFixups == entry->depth) {
@ -749,7 +715,7 @@ llvm::error_code parseObjectText( llvm::MemoryBuffer *mb
}
result.push_back(file);
return make_error_code(success);
return make_error_code(yaml_reader_error::success);
}
//