1 //===- Error.h - system_error extensions for lld ----------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This declares a new error_category for the lld library.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLD_CORE_ERROR_H
15 #define LLD_CORE_ERROR_H
17 #include "lld/Core/LLVM.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/Error.h"
20 #include <system_error>
24 const std::error_category &YamlReaderCategory();
26 enum class YamlReaderError {
31 inline std::error_code make_error_code(YamlReaderError e) {
32 return std::error_code(static_cast<int>(e), YamlReaderCategory());
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);
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> {
50 GenericError(Twine Msg);
51 const std::string &getMessage() const { return Msg; }
52 void log(llvm::raw_ostream &OS) const override;
54 std::error_code convertToErrorCode() const override {
55 return make_dynamic_error_code(getMessage());
62 } // end namespace lld
65 template <> struct is_error_code_enum<lld::YamlReaderError> : std::true_type {};