Ruby: internal/error.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

error.h
1#ifndef INTERNAL_ERROR_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_ERROR_H
11#include "ruby/internal/config.h"
12#include <stdarg.h> /* for va_list */
13#include "internal/string.h" /* for rb_fstring_cstr */
14#include "ruby/internal/stdbool.h" /* for bool */
15#include "ruby/encoding.h" /* for rb_encoding */
16#include "ruby/intern.h" /* for rb_exc_raise */
17#include "ruby/ruby.h" /* for enum ruby_value_type */
18
19#ifdef Check_Type
20# undef Check_Type /* in ruby/ruby.h */
21#endif
22
23#ifdef rb_raise_static
24# undef rb_raise_static
25# undef rb_sys_fail_path
26# undef rb_syserr_fail_path
27#endif
28
29#define rb_raise_static(e, m) \
30 rb_raise_cstr_i((e), rb_str_new_static((m), rb_strlen_lit(m)))
31#ifdef RUBY_FUNCTION_NAME_STRING
32# define rb_sys_fail_path(path) rb_sys_fail_path_in(RUBY_FUNCTION_NAME_STRING, path)
33# define rb_syserr_fail_path(err, path) rb_syserr_fail_path_in(RUBY_FUNCTION_NAME_STRING, (err), (path))
34# define rb_syserr_new_path(err, path) rb_syserr_new_path_in(RUBY_FUNCTION_NAME_STRING, (err), (path))
35#else
36# define rb_sys_fail_path(path) rb_sys_fail_str(path)
37# define rb_syserr_fail_path(err, path) rb_syserr_fail_str((err), (path))
38# define rb_syserr_new_path(err, path) rb_syserr_new_str((err), (path))
39#endif
40
41/* error.c */
42extern long rb_backtrace_length_limit;
43extern VALUE rb_eEAGAIN;
44extern VALUE rb_eEWOULDBLOCK;
45extern VALUE rb_eEINPROGRESS;
46 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 3, 0)
47void rb_report_bug_valist(VALUE file, int line, const char *fmt, va_list args);
48NORETURN(void rb_async_bug_errno(const char *,int));
49const char *rb_builtin_type_name(int t);
50const char *rb_builtin_class_name(VALUE x);
51PRINTF_ARGS(void rb_warn_deprecated(const char *fmt, const char *suggest, ...), 1, 3);
52PRINTF_ARGS(void rb_warn_deprecated_to_remove(const char *removal, const char *fmt, const char *suggest, ...), 2, 4);
53#if RUBY_DEBUG
54# include "ruby/version.h"
55# define RUBY_VERSION_SINCE(major, minor) (RUBY_API_VERSION_CODE >= (major * 10000) + (minor) * 100)
56# define RUBY_VERSION_BEFORE(major, minor) (RUBY_API_VERSION_CODE < (major * 10000) + (minor) * 100)
57# if defined(RBIMPL_WARNING_PRAGMA0)
58# define RBIMPL_TODO0(x) RBIMPL_WARNING_PRAGMA0(message(x))
59# elif RBIMPL_COMPILER_SINCE(MSVC, 12, 0, 0)
60# define RBIMPL_TODO0(x) __pragma(message(x))
61# endif
62
63# if RBIMPL_HAS_ATTRIBUTE(diagnose_if) || defined(__OPTIMIZE__)
64
65#define RUBY_VERSION_isdigit(c) ('0'<=(c)&&(c)<='9')
66// upto 99
67#define RUBY_VERSION__number_len(v, ofs) \
68 (!RUBY_VERSION_isdigit((v)[ofs]) ? \
69 0 : !RUBY_VERSION_isdigit((v)[(ofs) + 1]) ? 1 : 2)
70#define RUBY_VERSION__to_number(v, ofs) \
71 (!RUBY_VERSION_isdigit((v)[ofs]) ? \
72 0 : !RUBY_VERSION_isdigit((v)[(ofs) + 1]) ? \
73 ((v)[ofs]-'0') : \
74 (((v)[ofs]-'0')*10+(v)[(ofs)+1]-'0'))
75
76#define RUBY_VERSION_CODE_FROM_MAJOR_MINOR_STRING(v) \
77 (RUBY_VERSION__to_number(v, 0) * 10000 + \
78 ((v)[RUBY_VERSION__number_len(v, 0)] == '.' ? \
79 RUBY_VERSION__to_number(v, RUBY_VERSION__number_len(v, 0)+1) * 100 : 0))
80#define RUBY_VERSION_STRING_SINCE(v) (RUBY_API_VERSION_CODE >= RUBY_VERSION_CODE_FROM_MAJOR_MINOR_STRING(v))
81#define RUBY_VERSION_STRING_BEFORE(v) (RUBY_API_VERSION_CODE < RUBY_VERSION_CODE_FROM_MAJOR_MINOR_STRING(v))
82
83# if RBIMPL_HAS_ATTRIBUTE(diagnose_if)
84 RBIMPL_ATTR_FORCEINLINE()
85static void
86rb_deprecated_method_to_be_removed(const char *removal)
87 RBIMPL_ATTR_DIAGNOSE_IF(!RUBY_VERSION_isdigit(removal[0]), "malformed version number", "error")
88 RBIMPL_ATTR_DIAGNOSE_IF(RUBY_VERSION_STRING_SINCE(removal), "deprecated method to be removed", "error")
89{
90}
91# else
92 RBIMPL_ATTR_ERROR(("deprecated"))
93void rb_deprecated_method_to_be_removed(const char *);
94# define rb_deprecated_method_to_be_removed(removal) \
95 (sizeof(char[1-2*(!RUBY_VERSION_isdigit(removal[0]) || RUBY_VERSION_STRING_SINCE(removal))])!=1 ? \
96 rb_deprecated_method_to_be_removed(removal) : \
97 RBIMPL_ASSERT_NOTHING)
98# endif
99# define rb_warn_deprecated_to_remove_at(removal, ...) \
100 (rb_deprecated_method_to_be_removed(#removal), \
101 rb_warn_deprecated_to_remove(#removal, __VA_ARGS__))
102# endif
103#endif
104#ifndef rb_warn_deprecated_to_remove_at
105# define rb_warn_deprecated_to_remove_at(removal, ...) \
106 rb_warn_deprecated_to_remove(#removal, __VA_ARGS__)
107#endif
108#ifndef RUBY_VERSION_SINCE
109# define RUBY_VERSION_SINCE(major, minor) 0
110#endif
111#ifndef RUBY_VERSION_BEFORE
112# define RUBY_VERSION_BEFORE(major, minor) 0
113#endif
114#ifndef RBIMPL_TODO0
115# define RBIMPL_TODO0(x)
116#endif
117#define RBIMPL_TODO(message) RBIMPL_TODO0("TODO: " message)
118 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 6, 0)
119 VALUE rb_syntax_error_append(VALUE, VALUE, int, int, rb_encoding*, const char*, va_list);
120PRINTF_ARGS(void rb_enc_warn(rb_encoding *enc, const char *fmt, ...), 2, 3);
121PRINTF_ARGS(void rb_sys_enc_warning(rb_encoding *enc, const char *fmt, ...), 2, 3);
122PRINTF_ARGS(void rb_syserr_enc_warning(int err, rb_encoding *enc, const char *fmt, ...), 3, 4);
123 rb_warning_category_t rb_warning_category_from_name(VALUE category);
124bool rb_warning_category_enabled_p(rb_warning_category_t category);
125 VALUE rb_name_err_new(VALUE mesg, VALUE recv, VALUE method);
126 VALUE rb_nomethod_err_new(VALUE mesg, VALUE recv, VALUE method, VALUE args, int priv);
127 VALUE rb_key_err_new(VALUE mesg, VALUE recv, VALUE name);
128PRINTF_ARGS(VALUE rb_warning_string(const char *fmt, ...), 1, 2);
129 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 0)
130NORETURN(void rb_vraise(VALUE, const char *, va_list));
131NORETURN(static inline void rb_raise_cstr(VALUE etype, const char *mesg));
132NORETURN(static inline void rb_raise_cstr_i(VALUE etype, VALUE mesg));
133NORETURN(static inline void rb_name_err_raise_str(VALUE mesg, VALUE recv, VALUE name));
134NORETURN(static inline void rb_name_err_raise(const char *mesg, VALUE recv, VALUE name));
135NORETURN(static inline void rb_key_err_raise(VALUE mesg, VALUE recv, VALUE name));
136static inline void Check_Type(VALUE v, enum ruby_value_type t);
137static inline bool rb_typeddata_is_instance_of_inline(VALUE obj, const rb_data_type_t *data_type);
138#define rb_typeddata_is_instance_of rb_typeddata_is_instance_of_inline
139
140RUBY_SYMBOL_EXPORT_BEGIN
141/* error.c (export) */
142int rb_bug_reporter_add(void (*func)(FILE *, void *), void *data);
143#ifdef RUBY_FUNCTION_NAME_STRING
144NORETURN(void rb_sys_fail_path_in(const char *func_name, VALUE path));
145NORETURN(void rb_syserr_fail_path_in(const char *func_name, int err, VALUE path));
146 VALUE rb_syserr_new_path_in(const char *func_name, int n, VALUE path);
147#endif
148RUBY_SYMBOL_EXPORT_END
149
150static inline void
151rb_raise_cstr_i(VALUE etype, VALUE mesg)
152{
153 VALUE exc = rb_exc_new_str(etype, mesg);
154 rb_exc_raise(exc);
155}
156
157static inline void
158rb_raise_cstr(VALUE etype, const char *mesg)
159{
160 VALUE str = rb_str_new_cstr(mesg);
161 rb_raise_cstr_i(etype, str);
162}
163
164static inline void
165rb_name_err_raise_str(VALUE mesg, VALUE recv, VALUE name)
166{
167 VALUE exc = rb_name_err_new(mesg, recv, name);
168 rb_exc_raise(exc);
169}
170
171static inline void
172rb_name_err_raise(const char *mesg, VALUE recv, VALUE name)
173{
174 VALUE str = rb_fstring_cstr(mesg);
175 rb_name_err_raise_str(str, recv, name);
176}
177
178static inline void
179rb_key_err_raise(VALUE mesg, VALUE recv, VALUE name)
180{
181 VALUE exc = rb_key_err_new(mesg, recv, name);
182 rb_exc_raise(exc);
183}
184
185static inline bool
186rb_typeddata_is_instance_of_inline(VALUE obj, const rb_data_type_t *data_type)
187{
188 return RB_TYPE_P(obj, T_DATA) && RTYPEDDATA_P(obj) && (RTYPEDDATA_TYPE(obj) == data_type);
189}
190
191#endif /* INTERNAL_ERROR_H */
RBIMPL_ATTR_DIAGNOSE_IF
#define RBIMPL_ATTR_DIAGNOSE_IF(_, __, ___)
Wraps (or simulates) __attribute__((diagnose_if))
Definition diagnose_if.h:39
RBIMPL_ATTR_FORCEINLINE
#define RBIMPL_ATTR_FORCEINLINE()
Wraps (or simulates) __forceinline.
Definition forceinline.h:37
RBIMPL_ATTR_FORMAT
#define RBIMPL_ATTR_FORMAT(x, y, z)
Wraps (or simulates) __attribute__((format))
Definition format.h:29
T_DATA
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
rb_warning_category_t
rb_warning_category_t
Warning categories.
Definition error.h:43
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:688
rb_exc_new_str
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
Definition error.c:1142
encoding.h
Encoding relates APIs.
RBIMPL_ATTR_ERROR
#define RBIMPL_ATTR_ERROR(msg)
Wraps (or simulates) __attribute__((error))
Definition error.h:29
rb_str_new_cstr
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1514
version.h
intern.h
RTYPEDDATA_P
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition rtypeddata.h:540
RTYPEDDATA_TYPE
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition rtypeddata.h:563
ruby.h
stdarg.h
Defines old _.
stdbool.h
C99 shim for <stdbool.h>
__sFILE
Definition vsnprintf.c:160
OnigEncodingTypeST
Definition onigmo.h:160
rb_data_type_struct
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:190
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
RB_TYPE_P
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:375
ruby_value_type
ruby_value_type
C-level type of an object.
Definition value_type.h:112