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 <__availability>
14#include <__config>
15
16#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17#  pragma GCC system_header
18#endif
19
20// This is for backwards compatibility with code that might have been enabling
21// assertions through the Debug mode previously.
22#if _LIBCPP_DEBUG_LEVEL >= 1
23# ifndef _LIBCPP_ENABLE_ASSERTIONS
24#   define _LIBCPP_ENABLE_ASSERTIONS 1
25# endif
26#endif
27
28#ifndef _LIBCPP_ENABLE_ASSERTIONS
29# define _LIBCPP_ENABLE_ASSERTIONS _LIBCPP_ENABLE_ASSERTIONS_DEFAULT
30#endif
31
32#if _LIBCPP_ENABLE_ASSERTIONS != 0 && _LIBCPP_ENABLE_ASSERTIONS != 1
33# error "_LIBCPP_ENABLE_ASSERTIONS must be set to 0 or 1"
34#endif
35
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#else
42# if __has_builtin(__builtin_assume)
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#endif
52
53_LIBCPP_BEGIN_NAMESPACE_STD
54
55_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_ASSERTION_HANDLER
56void __libcpp_assertion_handler(char const* __file, int __line, char const* __expression, char const* __message);
57
58_LIBCPP_END_NAMESPACE_STD
59
60#endif // _LIBCPP___ASSERT
61