master
 1// -*- C++ -*-
 2//===----------------------------------------------------------------------===//
 3//
 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 5// See https://llvm.org/LICENSE.txt for license information.
 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 7//
 8//===----------------------------------------------------------------------===//
 9
10#ifndef _LIBCPP___SYSTEM_ERROR_SYSTEM_ERROR_H
11#define _LIBCPP___SYSTEM_ERROR_SYSTEM_ERROR_H
12
13#include <__config>
14#include <__system_error/error_category.h>
15#include <__system_error/error_code.h>
16#include <__verbose_abort>
17#include <stdexcept>
18#include <string>
19
20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21#  pragma GCC system_header
22#endif
23
24_LIBCPP_BEGIN_NAMESPACE_STD
25
26class _LIBCPP_EXPORTED_FROM_ABI system_error : public runtime_error {
27  error_code __ec_;
28
29public:
30  system_error(error_code __ec, const string& __what_arg);
31  system_error(error_code __ec, const char* __what_arg);
32  system_error(error_code __ec);
33  system_error(int __ev, const error_category& __ecat, const string& __what_arg);
34  system_error(int __ev, const error_category& __ecat, const char* __what_arg);
35  system_error(int __ev, const error_category& __ecat);
36  _LIBCPP_HIDE_FROM_ABI system_error(const system_error&) _NOEXCEPT = default;
37  ~system_error() _NOEXCEPT override;
38
39  _LIBCPP_HIDE_FROM_ABI const error_code& code() const _NOEXCEPT { return __ec_; }
40};
41
42// __ev is expected to be an error in the generic_category domain (e.g. from
43// errno, or std::errc::*), not system_category (e.g. from windows syscalls).
44[[__noreturn__]] _LIBCPP_EXPORTED_FROM_ABI void __throw_system_error(int __ev, const char* __what_arg);
45
46[[__noreturn__]] _LIBCPP_HIDE_FROM_ABI inline void __throw_system_error(error_code __ec, const char* __what_arg) {
47#if _LIBCPP_HAS_EXCEPTIONS
48  throw system_error(__ec, __what_arg);
49#else
50  _LIBCPP_VERBOSE_ABORT(
51      "system_error was thrown in -fno-exceptions mode with error %i and message \"%s\"", __ec.value(), __what_arg);
52#endif
53}
54
55_LIBCPP_END_NAMESPACE_STD
56
57#endif // _LIBCPP___SYSTEM_ERROR_SYSTEM_ERROR_H