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