Home | History | Annotate | Download | only in Core
      1 //===- Error.h - system_error extensions for lld ----------------*- C++ -*-===//
      2 //
      3 //                             The LLVM Linker
      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 lld library.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLD_CORE_ERROR_H
     15 #define LLD_CORE_ERROR_H
     16 
     17 #include "lld/Core/LLVM.h"
     18 #include "llvm/ADT/Twine.h"
     19 #include "llvm/Support/Error.h"
     20 #include <system_error>
     21 
     22 namespace lld {
     23 
     24 const std::error_category &YamlReaderCategory();
     25 
     26 enum class YamlReaderError {
     27   unknown_keyword,
     28   illegal_value
     29 };
     30 
     31 inline std::error_code make_error_code(YamlReaderError e) {
     32   return std::error_code(static_cast<int>(e), YamlReaderCategory());
     33 }
     34 
     35 /// Creates an error_code object that has associated with it an arbitrary
     36 /// error messsage.  The value() of the error_code will always be non-zero
     37 /// but its value is meaningless. The messsage() will be (a copy of) the
     38 /// supplied error string.
     39 /// Note:  Once ErrorOr<> is updated to work with errors other than error_code,
     40 /// this can be updated to return some other kind of error.
     41 std::error_code make_dynamic_error_code(StringRef msg);
     42 
     43 /// Generic error.
     44 ///
     45 /// For errors that don't require their own specific sub-error (most errors)
     46 /// this class can be used to describe the error via a string message.
     47 class GenericError : public llvm::ErrorInfo<GenericError> {
     48 public:
     49   static char ID;
     50   GenericError(Twine Msg);
     51   const std::string &getMessage() const { return Msg; }
     52   void log(llvm::raw_ostream &OS) const override;
     53 
     54   std::error_code convertToErrorCode() const override {
     55     return make_dynamic_error_code(getMessage());
     56   }
     57 
     58 private:
     59   std::string Msg;
     60 };
     61 
     62 } // end namespace lld
     63 
     64 namespace std {
     65 template <> struct is_error_code_enum<lld::YamlReaderError> : std::true_type {};
     66 }
     67 
     68 #endif
     69