Home | History | Annotate | Download | only in Object
      1 //===- Error.h - 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 declares a new error_category for the Object library.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_OBJECT_ERROR_H
     15 #define LLVM_OBJECT_ERROR_H
     16 
     17 #include "llvm/ADT/Twine.h"
     18 #include "llvm/Support/Error.h"
     19 #include <system_error>
     20 
     21 namespace llvm {
     22 namespace object {
     23 
     24 class Binary;
     25 
     26 const std::error_category &object_category();
     27 
     28 enum class object_error {
     29   // Error code 0 is absent. Use std::error_code() instead.
     30   arch_not_found = 1,
     31   invalid_file_type,
     32   parse_failed,
     33   unexpected_eof,
     34   string_table_non_null_end,
     35   invalid_section_index,
     36   bitcode_section_not_found,
     37 };
     38 
     39 inline std::error_code make_error_code(object_error e) {
     40   return std::error_code(static_cast<int>(e), object_category());
     41 }
     42 
     43 /// Base class for all errors indicating malformed binary files.
     44 ///
     45 /// Having a subclass for all malformed binary files allows archive-walking
     46 /// code to skip malformed files without having to understand every possible
     47 /// way that a binary file might be malformed.
     48 ///
     49 /// Currently inherits from ECError for easy interoperability with
     50 /// std::error_code, but this will be removed in the future.
     51 class BinaryError : public ErrorInfo<BinaryError, ECError> {
     52 public:
     53   static char ID;
     54   BinaryError() {
     55     // Default to parse_failed, can be overridden with setErrorCode.
     56     setErrorCode(make_error_code(object_error::parse_failed));
     57   }
     58 };
     59 
     60 /// Generic binary error.
     61 ///
     62 /// For errors that don't require their own specific sub-error (most errors)
     63 /// this class can be used to describe the error via a string message.
     64 class GenericBinaryError : public ErrorInfo<GenericBinaryError, BinaryError> {
     65 public:
     66   static char ID;
     67   GenericBinaryError(Twine Msg);
     68   GenericBinaryError(Twine Msg, object_error ECOverride);
     69   const std::string &getMessage() const { return Msg; }
     70   void log(raw_ostream &OS) const override;
     71 private:
     72   std::string Msg;
     73 };
     74 
     75 /// isNotObjectErrorInvalidFileType() is used when looping through the children
     76 /// of an archive after calling getAsBinary() on the child and it returns an
     77 /// llvm::Error.  In the cases we want to loop through the children and ignore the
     78 /// non-objects in the archive this is used to test the error to see if an
     79 /// error() function needs to called on the llvm::Error.
     80 Error isNotObjectErrorInvalidFileType(llvm::Error Err);
     81 
     82 } // end namespace object.
     83 
     84 } // end namespace llvm.
     85 
     86 namespace std {
     87 template <>
     88 struct is_error_code_enum<llvm::object::object_error> : std::true_type {};
     89 }
     90 
     91 #endif
     92