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