xref: /freebsd/contrib/llvm-project/libcxx/include/__assert (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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 <__verbose_abort>
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// TODO: In LLVM 16, make it an error to define _LIBCPP_DEBUG
23#if defined(_LIBCPP_DEBUG)
24# ifndef _LIBCPP_ENABLE_ASSERTIONS
25#   define _LIBCPP_ENABLE_ASSERTIONS 1
26# endif
27#endif
28
29// Automatically enable assertions when the debug mode is enabled.
30#if defined(_LIBCPP_ENABLE_DEBUG_MODE)
31# ifndef _LIBCPP_ENABLE_ASSERTIONS
32#   define _LIBCPP_ENABLE_ASSERTIONS 1
33# endif
34#endif
35
36#ifndef _LIBCPP_ENABLE_ASSERTIONS
37# define _LIBCPP_ENABLE_ASSERTIONS _LIBCPP_ENABLE_ASSERTIONS_DEFAULT
38#endif
39
40#if _LIBCPP_ENABLE_ASSERTIONS != 0 && _LIBCPP_ENABLE_ASSERTIONS != 1
41# error "_LIBCPP_ENABLE_ASSERTIONS must be set to 0 or 1"
42#endif
43
44#if _LIBCPP_ENABLE_ASSERTIONS
45# define _LIBCPP_ASSERT(expression, message)                                        \
46    (__builtin_expect(static_cast<bool>(expression), 1) ?                           \
47      (void)0 :                                                                     \
48      ::std::__libcpp_verbose_abort("%s:%d: assertion %s failed: %s", __FILE__, __LINE__, #expression, message))
49#elif !defined(_LIBCPP_ASSERTIONS_DISABLE_ASSUME) && __has_builtin(__builtin_assume)
50# define _LIBCPP_ASSERT(expression, message)                                        \
51    (_LIBCPP_DIAGNOSTIC_PUSH                                                        \
52    _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wassume")                                    \
53    __builtin_assume(static_cast<bool>(expression))                                 \
54    _LIBCPP_DIAGNOSTIC_POP)
55#else
56# define _LIBCPP_ASSERT(expression, message) ((void)0)
57#endif
58
59#endif // _LIBCPP___ASSERT
60