__assert 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 <__config>
  12. #include <__verbose_abort>
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. // TODO: Remove in LLVM 17.
  17. #if defined(_LIBCPP_DEBUG)
  18. # error "Defining _LIBCPP_DEBUG is not supported anymore. Please use _LIBCPP_ENABLE_DEBUG_MODE instead."
  19. #endif
  20. // Automatically enable assertions when the debug mode is enabled.
  21. #if defined(_LIBCPP_ENABLE_DEBUG_MODE)
  22. # ifndef _LIBCPP_ENABLE_ASSERTIONS
  23. # define _LIBCPP_ENABLE_ASSERTIONS 1
  24. # endif
  25. #endif
  26. #ifndef _LIBCPP_ENABLE_ASSERTIONS
  27. # define _LIBCPP_ENABLE_ASSERTIONS _LIBCPP_ENABLE_ASSERTIONS_DEFAULT
  28. #endif
  29. #if _LIBCPP_ENABLE_ASSERTIONS != 0 && _LIBCPP_ENABLE_ASSERTIONS != 1
  30. # error "_LIBCPP_ENABLE_ASSERTIONS must be set to 0 or 1"
  31. #endif
  32. #if _LIBCPP_ENABLE_ASSERTIONS
  33. # define _LIBCPP_ASSERT(expression, message) \
  34. (__builtin_expect(static_cast<bool>(expression), 1) ? \
  35. (void)0 : \
  36. _LIBCPP_VERBOSE_ABORT("%s:%d: assertion %s failed: %s", __FILE__, __LINE__, #expression, message))
  37. // Disable Clang builtins which nvcc does not understand
  38. #elif !defined(_LIBCPP_ASSERTIONS_DISABLE_ASSUME) && __has_builtin(__builtin_assume) && !defined(__CUDACC__)
  39. # define _LIBCPP_ASSERT(expression, message) \
  40. (_LIBCPP_DIAGNOSTIC_PUSH \
  41. _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wassume") \
  42. __builtin_assume(static_cast<bool>(expression)) \
  43. _LIBCPP_DIAGNOSTIC_POP)
  44. #else
  45. # define _LIBCPP_ASSERT(expression, message) ((void)0)
  46. #endif
  47. #endif // _LIBCPP___ASSERT