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