exception.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===----------------------------------------------------------------------===//
  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. #ifndef _LIBCPP___EXCEPTION_EXCEPTION_H
  9. #define _LIBCPP___EXCEPTION_EXCEPTION_H
  10. #include <__config>
  11. // <vcruntime_exception.h> defines its own std::exception and std::bad_exception types,
  12. // which we use in order to be ABI-compatible with other STLs on Windows.
  13. #if defined(_LIBCPP_ABI_VCRUNTIME)
  14. # include <vcruntime_exception.h>
  15. #endif
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. namespace std { // purposefully not using versioning namespace
  20. #if defined(_LIBCPP_ABI_VCRUNTIME) && (!defined(_HAS_EXCEPTIONS) || _HAS_EXCEPTIONS != 0)
  21. // The std::exception class was already included above, but we're explicit about this condition here for clarity.
  22. #elif defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
  23. // However, <vcruntime_exception.h> does not define std::exception and std::bad_exception
  24. // when _HAS_EXCEPTIONS == 0.
  25. //
  26. // Since libc++ still wants to provide the std::exception hierarchy even when _HAS_EXCEPTIONS == 0
  27. // (after all those are simply types like any other), we define an ABI-compatible version
  28. // of the VCRuntime std::exception and std::bad_exception types in that mode.
  29. struct __std_exception_data {
  30. char const* _What;
  31. bool _DoFree;
  32. };
  33. class exception { // base of all library exceptions
  34. public:
  35. exception() _NOEXCEPT : __data_() {}
  36. explicit exception(char const* __message) _NOEXCEPT : __data_() {
  37. __data_._What = __message;
  38. __data_._DoFree = true;
  39. }
  40. exception(exception const&) _NOEXCEPT {}
  41. exception& operator=(exception const&) _NOEXCEPT { return *this; }
  42. virtual ~exception() _NOEXCEPT {}
  43. virtual char const* what() const _NOEXCEPT { return __data_._What ? __data_._What : "Unknown exception"; }
  44. private:
  45. __std_exception_data __data_;
  46. };
  47. class bad_exception : public exception {
  48. public:
  49. bad_exception() _NOEXCEPT : exception("bad exception") {}
  50. };
  51. #else // !defined(_LIBCPP_ABI_VCRUNTIME)
  52. // On all other platforms, we define our own std::exception and std::bad_exception types
  53. // regardless of whether exceptions are turned on as a language feature.
  54. class _LIBCPP_EXPORTED_FROM_ABI exception {
  55. public:
  56. _LIBCPP_HIDE_FROM_ABI exception() _NOEXCEPT {}
  57. _LIBCPP_HIDE_FROM_ABI exception(const exception&) _NOEXCEPT = default;
  58. _LIBCPP_HIDE_FROM_ABI exception& operator=(const exception&) _NOEXCEPT = default;
  59. virtual ~exception() _NOEXCEPT;
  60. virtual const char* what() const _NOEXCEPT;
  61. };
  62. class _LIBCPP_EXPORTED_FROM_ABI bad_exception : public exception {
  63. public:
  64. _LIBCPP_HIDE_FROM_ABI bad_exception() _NOEXCEPT {}
  65. _LIBCPP_HIDE_FROM_ABI bad_exception(const bad_exception&) _NOEXCEPT = default;
  66. _LIBCPP_HIDE_FROM_ABI bad_exception& operator=(const bad_exception&) _NOEXCEPT = default;
  67. ~bad_exception() _NOEXCEPT override;
  68. const char* what() const _NOEXCEPT override;
  69. };
  70. #endif // !_LIBCPP_ABI_VCRUNTIME
  71. } // namespace std
  72. #endif // _LIBCPP___EXCEPTION_EXCEPTION_H