Ruby: internal/error.h Source File

Ruby 3.3.5p100 (2024-09-03 revision ef084cc8f4958c1b6e4ead99136631bef6d8ddba)

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_syserr_fail_path(err, path) rb_syserr_fail_path_in(RUBY_FUNCTION_NAME_STRING, (err), (path))
33# define rb_syserr_new_path(err, path) rb_syserr_new_path_in(RUBY_FUNCTION_NAME_STRING, (err), (path))
34#else
35# define rb_syserr_fail_path(err, path) rb_syserr_fail_str((err), (path))
36# define rb_syserr_new_path(err, path) rb_syserr_new_str((err), (path))
37#endif
38
39#define rb_sys_fail(mesg) \
40do { \
41 int errno_to_fail = errno; \
42 rb_syserr_fail(errno_to_fail, (mesg)); \
43} while (0)
44
45#define rb_sys_fail_str(mesg) \
46do { \
47 int errno_to_fail = errno; \
48 rb_syserr_fail_str(errno_to_fail, (mesg)); \
49} while (0)
50
51#define rb_sys_fail_path(path) \
52do { \
53 int errno_to_fail = errno; \
54 rb_syserr_fail_path(errno_to_fail, (path)); \
55} while (0)
56
57#define rb_sys_fail_sprintf(...) \
58do { \
59 int errno_to_fail = errno; \
60 rb_syserr_fail_str(errno_to_fail, rb_sprintf("" __VA_ARGS__)); \
61} while (0)
62
63/* error.c */
64extern long rb_backtrace_length_limit;
65extern VALUE rb_eEAGAIN;
66extern VALUE rb_eEWOULDBLOCK;
67extern VALUE rb_eEINPROGRESS;
68 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 3, 0)
69void rb_report_bug_valist(VALUE file, int line, const char *fmt, va_list args);
70NORETURN(void rb_async_bug_errno(const char *,int));
71const char *rb_builtin_type_name(int t);
72const char *rb_builtin_class_name(VALUE x);
73PRINTF_ARGS(void rb_warn_deprecated(const char *fmt, const char *suggest, ...), 1, 3);
74PRINTF_ARGS(void rb_warn_deprecated_to_remove(const char *removal, const char *fmt, const char *suggest, ...), 2, 4);
75#if RUBY_DEBUG
76# include "ruby/version.h"
77# define RUBY_VERSION_SINCE(major, minor) (RUBY_API_VERSION_CODE >= (major * 10000) + (minor) * 100)
78# define RUBY_VERSION_BEFORE(major, minor) (RUBY_API_VERSION_CODE < (major * 10000) + (minor) * 100)
79# if defined(RBIMPL_WARNING_PRAGMA0)
80# define RBIMPL_TODO0(x) RBIMPL_WARNING_PRAGMA0(message(x))
81# elif RBIMPL_COMPILER_SINCE(MSVC, 12, 0, 0)
82# define RBIMPL_TODO0(x) __pragma(message(x))
83# endif
84
85# if RBIMPL_HAS_ATTRIBUTE(diagnose_if) || defined(__OPTIMIZE__)
86
87#define RUBY_VERSION_isdigit(c) ('0'<=(c)&&(c)<='9')
88// upto 99
89#define RUBY_VERSION__number_len(v, ofs) \
90 (!RUBY_VERSION_isdigit((v)[ofs]) ? \
91 0 : !RUBY_VERSION_isdigit((v)[(ofs) + 1]) ? 1 : 2)
92#define RUBY_VERSION__to_number(v, ofs) \
93 (!RUBY_VERSION_isdigit((v)[ofs]) ? \
94 0 : !RUBY_VERSION_isdigit((v)[(ofs) + 1]) ? \
95 ((v)[ofs]-'0') : \
96 (((v)[ofs]-'0')*10+(v)[(ofs)+1]-'0'))
97
98#define RUBY_VERSION_CODE_FROM_MAJOR_MINOR_STRING(v) \
99 (RUBY_VERSION__to_number(v, 0) * 10000 + \
100 ((v)[RUBY_VERSION__number_len(v, 0)] == '.' ? \
101 RUBY_VERSION__to_number(v, RUBY_VERSION__number_len(v, 0)+1) * 100 : 0))
102#define RUBY_VERSION_STRING_SINCE(v) (RUBY_API_VERSION_CODE >= RUBY_VERSION_CODE_FROM_MAJOR_MINOR_STRING(v))
103#define RUBY_VERSION_STRING_BEFORE(v) (RUBY_API_VERSION_CODE < RUBY_VERSION_CODE_FROM_MAJOR_MINOR_STRING(v))
104
105# if RBIMPL_HAS_ATTRIBUTE(diagnose_if)
106 RBIMPL_ATTR_FORCEINLINE()
107static void
108rb_deprecated_method_to_be_removed(const char *removal)
109 RBIMPL_ATTR_DIAGNOSE_IF(!RUBY_VERSION_isdigit(removal[0]), "malformed version number", "error")
110 RBIMPL_ATTR_DIAGNOSE_IF(RUBY_VERSION_STRING_SINCE(removal), "deprecated method to be removed", "error")
111{
112}
113# else
114 RBIMPL_ATTR_ERROR(("deprecated"))
115void rb_deprecated_method_to_be_removed(const char *);
116# define rb_deprecated_method_to_be_removed(removal) \
117 (sizeof(char[1-2*(!RUBY_VERSION_isdigit(removal[0]) || RUBY_VERSION_STRING_SINCE(removal))])!=1 ? \
118 rb_deprecated_method_to_be_removed(removal) : \
119 RBIMPL_ASSERT_NOTHING)
120# endif
121# define rb_warn_deprecated_to_remove_at(removal, ...) \
122 (rb_deprecated_method_to_be_removed(#removal), \
123 rb_warn_deprecated_to_remove(#removal, __VA_ARGS__))
124# endif
125#endif
126#ifndef rb_warn_deprecated_to_remove_at
127# define rb_warn_deprecated_to_remove_at(removal, ...) \
128 rb_warn_deprecated_to_remove(#removal, __VA_ARGS__)
129#endif
130#ifndef RUBY_VERSION_SINCE
131# define RUBY_VERSION_SINCE(major, minor) 0
132#endif
133#ifndef RUBY_VERSION_BEFORE
134# define RUBY_VERSION_BEFORE(major, minor) 0
135#endif
136#ifndef RBIMPL_TODO0
137# define RBIMPL_TODO0(x)
138#endif
139#define RBIMPL_TODO(message) RBIMPL_TODO0("TODO: " message)
140 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 6, 0)
141 VALUE rb_syntax_error_append(VALUE, VALUE, int, int, rb_encoding*, const char*, va_list);
142PRINTF_ARGS(void rb_enc_warn(rb_encoding *enc, const char *fmt, ...), 2, 3);
143PRINTF_ARGS(void rb_sys_enc_warning(rb_encoding *enc, const char *fmt, ...), 2, 3);
144PRINTF_ARGS(void rb_syserr_enc_warning(int err, rb_encoding *enc, const char *fmt, ...), 3, 4);
145 rb_warning_category_t rb_warning_category_from_name(VALUE category);
146bool rb_warning_category_enabled_p(rb_warning_category_t category);
147 VALUE rb_name_err_new(VALUE mesg, VALUE recv, VALUE method);
148 VALUE rb_nomethod_err_new(VALUE mesg, VALUE recv, VALUE method, VALUE args, int priv);
149 VALUE rb_key_err_new(VALUE mesg, VALUE recv, VALUE name);
150PRINTF_ARGS(VALUE rb_warning_string(const char *fmt, ...), 1, 2);
151 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 0)
152NORETURN(void rb_vraise(VALUE, const char *, va_list));
153NORETURN(static inline void rb_raise_cstr(VALUE etype, const char *mesg));
154NORETURN(static inline void rb_raise_cstr_i(VALUE etype, VALUE mesg));
155NORETURN(static inline void rb_name_err_raise_str(VALUE mesg, VALUE recv, VALUE name));
156NORETURN(static inline void rb_name_err_raise(const char *mesg, VALUE recv, VALUE name));
157NORETURN(static inline void rb_key_err_raise(VALUE mesg, VALUE recv, VALUE name));
158static inline void Check_Type(VALUE v, enum ruby_value_type t);
159static inline bool rb_typeddata_is_instance_of_inline(VALUE obj, const rb_data_type_t *data_type);
160#define rb_typeddata_is_instance_of rb_typeddata_is_instance_of_inline
161
162RUBY_SYMBOL_EXPORT_BEGIN
163/* error.c (export) */
164int rb_bug_reporter_add(void (*func)(FILE *, void *), void *data);
165#ifdef RUBY_FUNCTION_NAME_STRING
166NORETURN(void rb_sys_fail_path_in(const char *func_name, VALUE path));
167NORETURN(void rb_syserr_fail_path_in(const char *func_name, int err, VALUE path));
168 VALUE rb_syserr_new_path_in(const char *func_name, int n, VALUE path);
169#endif
170RUBY_SYMBOL_EXPORT_END
171
172/* vm.c */
173void rb_free_warning(void);
174
175static inline void
176rb_raise_cstr_i(VALUE etype, VALUE mesg)
177{
178 VALUE exc = rb_exc_new_str(etype, mesg);
179 rb_exc_raise(exc);
180}
181
182static inline void
183rb_raise_cstr(VALUE etype, const char *mesg)
184{
185 VALUE str = rb_str_new_cstr(mesg);
186 rb_raise_cstr_i(etype, str);
187}
188
189static inline void
190rb_name_err_raise_str(VALUE mesg, VALUE recv, VALUE name)
191{
192 VALUE exc = rb_name_err_new(mesg, recv, name);
193 rb_exc_raise(exc);
194}
195
196static inline void
197rb_name_err_raise(const char *mesg, VALUE recv, VALUE name)
198{
199 VALUE str = rb_fstring_cstr(mesg);
200 rb_name_err_raise_str(str, recv, name);
201}
202
203static inline void
204rb_key_err_raise(VALUE mesg, VALUE recv, VALUE name)
205{
206 VALUE exc = rb_key_err_new(mesg, recv, name);
207 rb_exc_raise(exc);
208}
209
210static inline bool
211rb_typeddata_is_instance_of_inline(VALUE obj, const rb_data_type_t *data_type)
212{
213 return RB_TYPE_P(obj, T_DATA) && RTYPEDDATA_P(obj) && (RTYPEDDATA_TYPE(obj) == data_type);
214}
215
216#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_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:1395
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
inline
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition defines.h:66
RTYPEDDATA_P
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition rtypeddata.h:579
RTYPEDDATA_TYPE
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition rtypeddata.h:602
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:200
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
ruby_value_type
ruby_value_type
C-level type of an object.
Definition value_type.h:112