1 //===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This defines a new error_category for the Object library. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Object/Error.h" 15 #include "llvm/Support/ErrorHandling.h" 16 #include "llvm/Support/ManagedStatic.h" 17 18 using namespace llvm; 19 using namespace object; 20 21 namespace { 22 class _object_error_category : public std::error_category { 23 public: 24 const char* name() const LLVM_NOEXCEPT override; 25 std::string message(int ev) const override; 26 }; 27 } 28 29 const char *_object_error_category::name() const LLVM_NOEXCEPT { 30 return "llvm.object"; 31 } 32 33 std::string _object_error_category::message(int EV) const { 34 object_error E = static_cast<object_error>(EV); 35 switch (E) { 36 case object_error::arch_not_found: 37 return "No object file for requested architecture"; 38 case object_error::invalid_file_type: 39 return "The file was not recognized as a valid object file"; 40 case object_error::parse_failed: 41 return "Invalid data was encountered while parsing the file"; 42 case object_error::unexpected_eof: 43 return "The end of the file was unexpectedly encountered"; 44 case object_error::string_table_non_null_end: 45 return "String table must end with a null terminator"; 46 case object_error::invalid_section_index: 47 return "Invalid section index"; 48 case object_error::bitcode_section_not_found: 49 return "Bitcode section not found in object file"; 50 } 51 llvm_unreachable("An enumerator of object_error does not have a message " 52 "defined."); 53 } 54 55 char BinaryError::ID = 0; 56 char GenericBinaryError::ID = 0; 57 58 GenericBinaryError::GenericBinaryError(Twine Msg) : Msg(Msg.str()) {} 59 60 GenericBinaryError::GenericBinaryError(Twine Msg, object_error ECOverride) 61 : Msg(Msg.str()) { 62 setErrorCode(make_error_code(ECOverride)); 63 } 64 65 void GenericBinaryError::log(raw_ostream &OS) const { 66 OS << Msg; 67 } 68 69 static ManagedStatic<_object_error_category> error_category; 70 71 const std::error_category &object::object_category() { 72 return *error_category; 73 } 74 75 llvm::Error llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err) { 76 if (auto Err2 = 77 handleErrors(std::move(Err), 78 [](std::unique_ptr<ECError> M) { 79 // Try to handle 'M'. If successful, return a success value from 80 // the handler. 81 if (M->convertToErrorCode() == object_error::invalid_file_type) 82 return Error::success(); 83 84 // We failed to handle 'M' - return it from the handler. 85 // This value will be passed back from catchErrors and 86 // wind up in Err2, where it will be returned from this function. 87 return Error(std::move(M)); 88 })) 89 return Err2; 90 return Err; 91 } 92