2020-04-10 01:11:40 -04:00
|
|
|
#ifndef RUBY_ASSERT_H /*-*-C++-*-vi:se ft=cpp:*/
|
|
|
|
#define RUBY_ASSERT_H
|
|
|
|
/**
|
2020-04-08 00:28:13 -04:00
|
|
|
* @file
|
|
|
|
* @author Ruby developers <ruby-core@ruby-lang.org>
|
|
|
|
* @date Wed May 18 00:21:44 JST 1994
|
|
|
|
* @copyright This file is a part of the programming language Ruby.
|
|
|
|
* Permission is hereby granted, to either redistribute and/or
|
|
|
|
* modify this file, provided that the conditions mentioned in the
|
|
|
|
* file COPYING are met. Consult the file for details.
|
2020-05-04 03:27:48 -04:00
|
|
|
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
|
2020-04-08 00:28:13 -04:00
|
|
|
* implementation details. Don't take them as canon. They could
|
|
|
|
* rapidly appear then vanish. The name (path) of this header file
|
|
|
|
* is also an implementation detail. Do not expect it to persist
|
|
|
|
* at the place it is now. Developers are free to move it anywhere
|
|
|
|
* anytime at will.
|
|
|
|
* @note To ruby-core: remember that this header can be possibly
|
|
|
|
* recursively included from extension libraries written in C++.
|
|
|
|
* Do not expect for instance `__VA_ARGS__` is always available.
|
|
|
|
* We assume C99 for ruby itself but we don't assume languages of
|
|
|
|
* extension libraries. They could be written in C++98.
|
|
|
|
*/
|
2020-05-04 02:35:26 -04:00
|
|
|
#include "ruby/impl/assume.h"
|
|
|
|
#include "ruby/impl/attr/cold.h"
|
|
|
|
#include "ruby/impl/attr/noreturn.h"
|
|
|
|
#include "ruby/impl/cast.h"
|
|
|
|
#include "ruby/impl/dllexport.h"
|
2020-04-08 00:28:13 -04:00
|
|
|
#include "ruby/backward/2/assume.h"
|
2019-07-14 00:05:16 -04:00
|
|
|
|
2020-04-08 00:28:13 -04:00
|
|
|
#ifndef RUBY_DEBUG
|
|
|
|
# define RUBY_DEBUG 0
|
2019-07-14 00:05:16 -04:00
|
|
|
#endif
|
2020-04-08 00:28:13 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Pro tip: `!!NDEBUG-1` expands to...
|
|
|
|
*
|
|
|
|
* - `!!(-1)` (== `!0` == `1`) when NDEBUG is defined to be empty,
|
|
|
|
* - `(!!0)-1` (== `0-1` == `-1`) when NDEBUG is defined as 0, and
|
|
|
|
* - `(!!n)-1` (== `1-1` == `0`) when NDEBUG is defined as something else.
|
|
|
|
*/
|
|
|
|
#if defined(RUBY_NDEBUG)
|
|
|
|
# /* Take that. */
|
|
|
|
#elif ! defined(NDEBUG)
|
|
|
|
# define RUBY_NDEBUG 0
|
|
|
|
#elif !!NDEBUG-1 < 0
|
|
|
|
# define RUBY_NDEBUG 0
|
|
|
|
#else
|
|
|
|
# define RUBY_NDEBUG 1
|
2019-07-14 00:05:16 -04:00
|
|
|
#endif
|
|
|
|
|
2020-05-04 02:52:56 -04:00
|
|
|
#define RBIMPL_ASSERT_NOTHING RBIMPL_CAST((void)0)
|
2020-04-08 00:28:13 -04:00
|
|
|
|
2020-05-04 02:52:56 -04:00
|
|
|
RBIMPL_SYMBOL_EXPORT_BEGIN()
|
|
|
|
RBIMPL_ATTR_NORETURN()
|
|
|
|
RBIMPL_ATTR_COLD()
|
2020-04-08 00:28:13 -04:00
|
|
|
void rb_assert_failure(const char *file, int line, const char *name, const char *expr);
|
2020-05-04 02:52:56 -04:00
|
|
|
RBIMPL_SYMBOL_EXPORT_END()
|
2020-04-08 00:28:13 -04:00
|
|
|
|
2019-07-14 00:05:16 -04:00
|
|
|
#ifdef RUBY_FUNCTION_NAME_STRING
|
2020-05-04 02:52:56 -04:00
|
|
|
# define RBIMPL_ASSERT_FUNC RUBY_FUNCTION_NAME_STRING
|
2019-07-14 00:05:16 -04:00
|
|
|
#else
|
2020-05-04 02:52:56 -04:00
|
|
|
# define RBIMPL_ASSERT_FUNC RBIMPL_CAST((const char *)0)
|
2019-07-14 00:05:16 -04:00
|
|
|
#endif
|
2020-04-08 00:28:13 -04:00
|
|
|
|
|
|
|
#define RUBY_ASSERT_FAIL(expr) \
|
2020-05-04 02:52:56 -04:00
|
|
|
rb_assert_failure(__FILE__, __LINE__, RBIMPL_ASSERT_FUNC, #expr)
|
2020-04-08 00:28:13 -04:00
|
|
|
|
2019-07-14 00:05:16 -04:00
|
|
|
#define RUBY_ASSERT_MESG(expr, mesg) \
|
2020-05-04 02:52:56 -04:00
|
|
|
(RB_LIKELY(expr) ? RBIMPL_ASSERT_NOTHING : RUBY_ASSERT_FAIL(mesg))
|
2020-04-08 00:28:13 -04:00
|
|
|
|
|
|
|
#if RUBY_DEBUG
|
|
|
|
# define RUBY_ASSERT_MESG_WHEN(cond, expr, mesg) RUBY_ASSERT_MESG((expr), mesg)
|
|
|
|
#elif ! defined(HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P)
|
|
|
|
# define RUBY_ASSERT_MESG_WHEN(cond, expr, mesg) RUBY_ASSERT_MESG(!(cond) || (expr), mesg)
|
|
|
|
#else
|
2019-07-14 00:05:16 -04:00
|
|
|
# define RUBY_ASSERT_MESG_WHEN(cond, expr, mesg) \
|
|
|
|
__builtin_choose_expr( \
|
2019-07-14 04:45:52 -04:00
|
|
|
__builtin_constant_p(cond), \
|
2020-04-08 00:28:13 -04:00
|
|
|
__builtin_choose_expr(cond, \
|
|
|
|
RUBY_ASSERT_MESG(expr, mesg), \
|
2020-05-04 02:52:56 -04:00
|
|
|
RBIMPL_ASSERT_NOTHING), \
|
2020-04-08 00:28:13 -04:00
|
|
|
RUBY_ASSERT_MESG(!(cond) || (expr), mesg))
|
|
|
|
#endif /* RUBY_DEBUG */
|
|
|
|
|
2019-07-14 22:00:16 -04:00
|
|
|
#define RUBY_ASSERT(expr) RUBY_ASSERT_MESG_WHEN((!RUBY_NDEBUG+0), expr, #expr)
|
2019-07-14 00:05:16 -04:00
|
|
|
#define RUBY_ASSERT_WHEN(cond, expr) RUBY_ASSERT_MESG_WHEN(cond, expr, #expr)
|
2019-07-14 21:19:25 -04:00
|
|
|
#define RUBY_ASSERT_ALWAYS(expr) RUBY_ASSERT_MESG_WHEN(TRUE, expr, #expr)
|
2019-07-14 00:05:16 -04:00
|
|
|
|
2020-04-08 00:28:13 -04:00
|
|
|
#if ! RUBY_NDEBUG
|
2020-05-04 02:52:56 -04:00
|
|
|
# define RBIMPL_ASSERT_OR_ASSUME(_) RUBY_ASSERT(_)
|
|
|
|
#elif defined(RBIMPL_HAVE___ASSUME)
|
|
|
|
# define RBIMPL_ASSERT_OR_ASSUME(_) RBIMPL_ASSUME(_)
|
|
|
|
#elif RBIMPL_HAS_BUILTIN(__builtin_assume)
|
|
|
|
# define RBIMPL_ASSERT_OR_ASSUME(_) RBIMPL_ASSUME(_)
|
2020-04-08 00:28:13 -04:00
|
|
|
#else
|
2020-05-04 02:52:56 -04:00
|
|
|
# define RBIMPL_ASSERT_OR_ASSUME(_) /* void */
|
2019-07-14 00:05:16 -04:00
|
|
|
#endif
|
|
|
|
|
2020-04-08 00:28:13 -04:00
|
|
|
#endif /* RUBY_ASSERT_H */
|