Ruby  3.4.0dev (2024-11-05 revision 348a53415339076afc4a02fcd09f3ae36e9c4c61)
Macros
assert.h File Reference

(348a53415339076afc4a02fcd09f3ae36e9c4c61)

#include "ruby/internal/assume.h"
#include "ruby/internal/attr/cold.h"
#include "ruby/internal/attr/format.h"
#include "ruby/internal/attr/noreturn.h"
#include "ruby/internal/cast.h"
#include "ruby/internal/dllexport.h"
#include "ruby/backward/2/assume.h"
Include dependency graph for assert.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define RUBY_DEBUG   0
 Define this macro when you want assertions. More...
 
#define NDEBUG
 Define this macro when you don't want assertions. More...
 
#define RUBY_NDEBUG   1
 This macro is basically the same as NDEBUG. More...
 
#define RBIMPL_VA_OPT_ARGS(...)   __VA_OPT__(,) __VA_ARGS__
 Prints the given message, and terminates the entire process abnormally. More...
 
#define RUBY_ASSERT_FAIL(mesg, ...)
 
#define RUBY_ASSERT_MESG(expr, ...)    (RB_LIKELY(expr) ? RBIMPL_ASSERT_NOTHING : RUBY_ASSERT_FAIL(__VA_ARGS__))
 Asserts that the expression is truthy. More...
 
#define RUBY_ASSERT_ALWAYS(expr, ...)    RUBY_ASSERT_MESG(expr, #expr RBIMPL_VA_OPT_ARGS(__VA_ARGS__))
 A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG. More...
 
#define RUBY_ASSERT(...)   RBIMPL_ASSERT_NOTHING
 Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy. More...
 
#define RUBY_ASSERT_NDEBUG(...)   RBIMPL_ASSERT_NOTHING
 A variant of RUBY_ASSERT that interfaces with NDEBUG instead of RUBY_DEBUG. More...
 
#define RUBY_ASSERT_MESG_WHEN(cond, expr, ...)    ((cond) ? RUBY_ASSERT_MESG((expr), __VA_ARGS__) : RBIMPL_ASSERT_NOTHING)
 A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or cond parameter is truthy. More...
 
#define RUBY_ASSERT_WHEN(cond, expr, ...)    RUBY_ASSERT_MESG_WHEN(cond, expr, #expr RBIMPL_VA_OPT_ARGS(__VA_ARGS__))
 A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or cond parameter is truthy. More...
 
#define RUBY_ASSERT_BUILTIN_TYPE(obj, type)
 A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or built-in type of obj is type. More...
 
#define RBIMPL_ASSERT_OR_ASSUME(...)   RBIMPL_ASSERT_NOTHING
 This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG. More...
 

Detailed Description

Author
Ruby developers ruby-.nosp@m.core.nosp@m.@ruby.nosp@m.-lan.nosp@m.g.org
Date
Wed May 18 00:21:44 JST 1994
Warning
Symbols prefixed with either RBIMPL or rbimpl are 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.

Definition in file assert.h.

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Define this macro when you don't want assertions.

Definition at line 90 of file assert.h.

◆ RBIMPL_ASSERT_OR_ASSUME

#define RBIMPL_ASSERT_OR_ASSUME (   ...)    RBIMPL_ASSERT_NOTHING

This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.

Parameters
exprWhat supposedly evaluates to true.

Definition at line 311 of file assert.h.

◆ RBIMPL_VA_OPT_ARGS

#define RBIMPL_VA_OPT_ARGS (   ...)    __VA_OPT__(,) __VA_ARGS__

Prints the given message, and terminates the entire process abnormally.

Parameters
mesgThe message to display.

Definition at line 162 of file assert.h.

◆ RUBY_ASSERT

#define RUBY_ASSERT (   ...)    RBIMPL_ASSERT_NOTHING

Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.

Parameters
exprWhat supposedly evaluates to true.

Definition at line 219 of file assert.h.

◆ RUBY_ASSERT_ALWAYS

#define RUBY_ASSERT_ALWAYS (   expr,
  ... 
)     RUBY_ASSERT_MESG(expr, #expr RBIMPL_VA_OPT_ARGS(__VA_ARGS__))

A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.

Parameters
exprWhat supposedly evaluates to true.

Definition at line 199 of file assert.h.

◆ RUBY_ASSERT_BUILTIN_TYPE

#define RUBY_ASSERT_BUILTIN_TYPE (   obj,
  type 
)
Value:
"Actual type is %s", rb_builtin_type_name(BUILTIN_TYPE(obj)))
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition: assert.h:219
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition: value_type.h:85
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition: value_type.h:376

A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or built-in type of obj is type.

Parameters
objObject to check its built-in typue.
typeBuilt-in type constant, T_ARRAY, T_STRING, etc.

Definition at line 291 of file assert.h.

◆ RUBY_ASSERT_FAIL

#define RUBY_ASSERT_FAIL (   mesg,
  ... 
)
Value:
rb_assert_failure##__VA_OPT__(_detail)( \
__FILE__, __LINE__, RBIMPL_ASSERT_FUNC, mesg RBIMPL_VA_OPT_ARGS(__VA_ARGS__))
#define RBIMPL_VA_OPT_ARGS(...)
Prints the given message, and terminates the entire process abnormally.
Definition: assert.h:162

Definition at line 164 of file assert.h.

◆ RUBY_ASSERT_MESG

#define RUBY_ASSERT_MESG (   expr,
  ... 
)     (RB_LIKELY(expr) ? RBIMPL_ASSERT_NOTHING : RUBY_ASSERT_FAIL(__VA_ARGS__))

Asserts that the expression is truthy.

If not aborts with the message.

Parameters
exprWhat supposedly evaluates to true.
mesgThe message to display on failure.

Definition at line 186 of file assert.h.

◆ RUBY_ASSERT_MESG_WHEN

#define RUBY_ASSERT_MESG_WHEN (   cond,
  expr,
  ... 
)     ((cond) ? RUBY_ASSERT_MESG((expr), __VA_ARGS__) : RBIMPL_ASSERT_NOTHING)

A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or cond parameter is truthy.

Parameters
condExtra condition that shall hold for assertion to take effect.
exprWhat supposedly evaluates to true.
mesgThe message to display on failure.

Definition at line 262 of file assert.h.

◆ RUBY_ASSERT_NDEBUG

#define RUBY_ASSERT_NDEBUG (   ...)    RBIMPL_ASSERT_NOTHING

A variant of RUBY_ASSERT that interfaces with NDEBUG instead of RUBY_DEBUG.

This almost resembles assert C standard macro, except minor implementation details.

Parameters
exprWhat supposedly evaluates to true.

Definition at line 236 of file assert.h.

◆ RUBY_ASSERT_WHEN

#define RUBY_ASSERT_WHEN (   cond,
  expr,
  ... 
)     RUBY_ASSERT_MESG_WHEN(cond, expr, #expr RBIMPL_VA_OPT_ARGS(__VA_ARGS__))

A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or cond parameter is truthy.

Parameters
condExtra condition that shall hold for assertion to take effect.
exprWhat supposedly evaluates to true.

Definition at line 278 of file assert.h.

◆ RUBY_DEBUG

#define RUBY_DEBUG   0

Define this macro when you want assertions.

Definition at line 88 of file assert.h.

◆ RUBY_NDEBUG

#define RUBY_NDEBUG   1

This macro is basically the same as NDEBUG.

Definition at line 92 of file assert.h.