__assert 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #ifndef _LIBCPP___ASSERT
  10. #define _LIBCPP___ASSERT
  11. #include <__availability>
  12. #include <__config>
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. // This is for backwards compatibility with code that might have been enabling
  17. // assertions through the Debug mode previously.
  18. // TODO: In LLVM 16, make it an error to define _LIBCPP_DEBUG
  19. #if defined(_LIBCPP_DEBUG)
  20. # ifndef _LIBCPP_ENABLE_ASSERTIONS
  21. # define _LIBCPP_ENABLE_ASSERTIONS 1
  22. # endif
  23. #endif
  24. // Automatically enable assertions when the debug mode is enabled.
  25. #if defined(_LIBCPP_ENABLE_DEBUG_MODE)
  26. # ifndef _LIBCPP_ENABLE_ASSERTIONS
  27. # define _LIBCPP_ENABLE_ASSERTIONS 1
  28. # endif
  29. #endif
  30. #ifndef _LIBCPP_ENABLE_ASSERTIONS
  31. # define _LIBCPP_ENABLE_ASSERTIONS _LIBCPP_ENABLE_ASSERTIONS_DEFAULT
  32. #endif
  33. #if _LIBCPP_ENABLE_ASSERTIONS != 0 && _LIBCPP_ENABLE_ASSERTIONS != 1
  34. # error "_LIBCPP_ENABLE_ASSERTIONS must be set to 0 or 1"
  35. #endif
  36. #if _LIBCPP_ENABLE_ASSERTIONS
  37. # define _LIBCPP_ASSERT(expression, message) \
  38. (__builtin_expect(static_cast<bool>(expression), 1) ? \
  39. (void)0 : \
  40. ::std::__libcpp_assertion_handler(__FILE__, __LINE__, #expression, message))
  41. // Disable Clang builtins which nvcc does not understand
  42. #elif !defined(_LIBCPP_ASSERTIONS_DISABLE_ASSUME) && __has_builtin(__builtin_assume) && !defined(__CUDACC__)
  43. # define _LIBCPP_ASSERT(expression, message) \
  44. (_LIBCPP_DIAGNOSTIC_PUSH \
  45. _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wassume") \
  46. __builtin_assume(static_cast<bool>(expression)) \
  47. _LIBCPP_DIAGNOSTIC_POP)
  48. #else
  49. # define _LIBCPP_ASSERT(expression, message) ((void)0)
  50. #endif
  51. _LIBCPP_BEGIN_NAMESPACE_STD
  52. _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_ASSERTION_HANDLER
  53. void __libcpp_assertion_handler(char const* __file, int __line, char const* __expression, char const* __message);
  54. _LIBCPP_END_NAMESPACE_STD
  55. #endif // _LIBCPP___ASSERT