spirv_cross_error_handling.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. };
  64. #define SPIRV_CROSS_THROW(x) throw CompilerError(x)
  65. #endif
  66. // MSVC 2013 does not have noexcept. We need this for Variant to get move constructor to work correctly
  67. // instead of copy constructor.
  68. // MSVC 2013 ignores that move constructors cannot throw in std::vector, so just don't define it.
  69. #if defined(_MSC_VER) && _MSC_VER < 1900
  70. #define SPIRV_CROSS_NOEXCEPT
  71. #else
  72. #define SPIRV_CROSS_NOEXCEPT noexcept
  73. #endif
  74. #if __cplusplus >= 201402l
  75. #define SPIRV_CROSS_DEPRECATED(reason) [[deprecated(reason)]]
  76. #elif defined(__GNUC__)
  77. #define SPIRV_CROSS_DEPRECATED(reason) __attribute__((deprecated))
  78. #elif defined(_MSC_VER)
  79. #define SPIRV_CROSS_DEPRECATED(reason) __declspec(deprecated(reason))
  80. #else
  81. #define SPIRV_CROSS_DEPRECATED(reason)
  82. #endif
  83. } // namespace SPIRV_CROSS_NAMESPACE
  84. #endif