__assert 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 <iosfwd> // for std::string
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. #if _LIBCPP_DEBUG_LEVEL >= 1
  17. # define _LIBCPP_ASSERT_IMPL(x, m) ((x) ? (void)0 : ::std::__libcpp_debug_function(::std::__libcpp_debug_info(__FILE__, __LINE__, #x, m)))
  18. #else
  19. # define _LIBCPP_ASSERT_IMPL(x, m) ((void)0)
  20. #endif
  21. // We do this dance because some of our tests re-define _LIBCPP_ASSERT to something else.
  22. // In the future, we should find other ways to test our assertions and disallow re-defining
  23. // _LIBCPP_ASSERT.
  24. #if !defined(_LIBCPP_ASSERT)
  25. # define _LIBCPP_ASSERT(x, m) _LIBCPP_ASSERT_IMPL(x, m)
  26. #endif
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. struct _LIBCPP_TEMPLATE_VIS __libcpp_debug_info {
  29. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  30. __libcpp_debug_info()
  31. : __file_(nullptr), __line_(-1), __pred_(nullptr), __msg_(nullptr) {}
  32. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  33. __libcpp_debug_info(const char* __f, int __l, const char* __p, const char* __m)
  34. : __file_(__f), __line_(__l), __pred_(__p), __msg_(__m) {}
  35. _LIBCPP_FUNC_VIS string what() const;
  36. const char* __file_;
  37. int __line_;
  38. const char* __pred_;
  39. const char* __msg_;
  40. };
  41. /// __libcpp_debug_function_type - The type of the assertion failure handler.
  42. typedef void(*__libcpp_debug_function_type)(__libcpp_debug_info const&);
  43. /// __libcpp_debug_function - The handler function called when a _LIBCPP_ASSERT
  44. /// fails.
  45. extern _LIBCPP_EXPORTED_FROM_ABI __libcpp_debug_function_type __libcpp_debug_function;
  46. /// __libcpp_abort_debug_function - A debug handler that aborts when called.
  47. _LIBCPP_NORETURN _LIBCPP_FUNC_VIS
  48. void __libcpp_abort_debug_function(__libcpp_debug_info const&);
  49. /// __libcpp_set_debug_function - Set the debug handler to the specified
  50. /// function.
  51. _LIBCPP_FUNC_VIS
  52. bool __libcpp_set_debug_function(__libcpp_debug_function_type __func);
  53. _LIBCPP_END_NAMESPACE_STD
  54. #endif // _LIBCPP___ASSERT