spirv_cross_error_handling.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2015-2021 Arm Limited
  3. * SPDX-License-Identifier: Apache-2.0 OR MIT
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. * At your option, you may choose to accept this material under either:
  19. * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or
  20. * 2. The MIT License, found at <http://opensource.org/licenses/MIT>.
  21. */
  22. #ifndef SPIRV_CROSS_ERROR_HANDLING
  23. #define SPIRV_CROSS_ERROR_HANDLING
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string>
  27. #ifndef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
  28. #include <stdexcept>
  29. #endif
  30. #ifdef SPIRV_CROSS_NAMESPACE_OVERRIDE
  31. #define SPIRV_CROSS_NAMESPACE SPIRV_CROSS_NAMESPACE_OVERRIDE
  32. #else
  33. #define SPIRV_CROSS_NAMESPACE spirv_cross
  34. #endif
  35. namespace SPIRV_CROSS_NAMESPACE
  36. {
  37. #ifdef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
  38. #if !defined(_MSC_VER) || defined(__clang__)
  39. [[noreturn]]
  40. #elif defined(_MSC_VER)
  41. __declspec(noreturn)
  42. #endif
  43. inline void
  44. report_and_abort(const std::string &msg)
  45. {
  46. #ifdef NDEBUG
  47. (void)msg;
  48. #else
  49. fprintf(stderr, "There was a compiler error: %s\n", msg.c_str());
  50. #endif
  51. fflush(stderr);
  52. abort();
  53. }
  54. #define SPIRV_CROSS_THROW(x) report_and_abort(x)
  55. #else
  56. class CompilerError : public std::runtime_error
  57. {
  58. public:
  59. explicit CompilerError(const std::string &str)
  60. : std::runtime_error(str)
  61. {
  62. }
  63. explicit CompilerError(const char *str)
  64. : std::runtime_error(str)
  65. {
  66. }
  67. };
  68. #define SPIRV_CROSS_THROW(x) throw CompilerError(x)
  69. #endif
  70. // MSVC 2013 does not have noexcept. We need this for Variant to get move constructor to work correctly
  71. // instead of copy constructor.
  72. // MSVC 2013 ignores that move constructors cannot throw in std::vector, so just don't define it.
  73. #if defined(_MSC_VER) && _MSC_VER < 1900
  74. #define SPIRV_CROSS_NOEXCEPT
  75. #else
  76. #define SPIRV_CROSS_NOEXCEPT noexcept
  77. #endif
  78. #if __cplusplus >= 201402l
  79. #define SPIRV_CROSS_DEPRECATED(reason) [[deprecated(reason)]]
  80. #elif defined(__GNUC__)
  81. #define SPIRV_CROSS_DEPRECATED(reason) __attribute__((deprecated))
  82. #elif defined(_MSC_VER)
  83. #define SPIRV_CROSS_DEPRECATED(reason) __declspec(deprecated(reason))
  84. #else
  85. #define SPIRV_CROSS_DEPRECATED(reason)
  86. #endif
  87. } // namespace SPIRV_CROSS_NAMESPACE
  88. #endif