[前][次][番号順一覧][スレッド一覧]

ruby-changes:64297

From: Jeremy <ko1@a...>
Date: Sat, 19 Dec 2020 02:54:33 +0900 (JST)
Subject: [ruby-changes:64297] 52fb696ee7 (master): Switch rb_category_warn{, ing} to accept an rb_warning_category_t

https://git.ruby-lang.org/ruby.git/commit/?id=52fb696ee7

From 52fb696ee7d01b1d55a1d5c42c60c6a5ebfc4502 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Wed, 16 Dec 2020 08:15:13 -0800
Subject: Switch rb_category_warn{,ing} to accept an rb_warning_category_t

Since we decided to only allowing specific warning categories,
there is no reason to have an API that accepts a general string,
as it is more error-prone.  Switch to only allowing the specific
warning categories.

As rb_category_warn{,ing} are public API, this requires making
rb_warning_category_t public API as well.

diff --git a/error.c b/error.c
index e10485d..2b63742 100644
--- a/error.c
+++ b/error.c
@@ -77,6 +77,7 @@ static ID id_deprecated; https://github.com/ruby/ruby/blob/trunk/error.c#L77
 static ID id_experimental;
 static VALUE sym_category;
 static VALUE warning_categories;
+static VALUE warning_category_t_map;
 
 extern const char ruby_description[];
 
@@ -403,11 +404,11 @@ rb_warn(const char *fmt, ...) https://github.com/ruby/ruby/blob/trunk/error.c#L404
 }
 
 void
-rb_category_warn(const char *category, const char *fmt, ...)
+rb_category_warn(rb_warning_category_t category, const char *fmt, ...)
 {
     if (!NIL_P(ruby_verbose)) {
         with_warning_string(mesg, 0, fmt) {
-            rb_warn_category(mesg, ID2SYM(rb_intern(category)));
+            rb_warn_category(mesg, rb_hash_fetch(warning_category_t_map, INT2NUM(category)));
         }
     }
 }
@@ -435,11 +436,11 @@ rb_warning(const char *fmt, ...) https://github.com/ruby/ruby/blob/trunk/error.c#L436
 
 /* rb_category_warning() reports only in verbose mode */
 void
-rb_category_warning(const char *category, const char *fmt, ...)
+rb_category_warning(rb_warning_category_t category, const char *fmt, ...)
 {
     if (RTEST(ruby_verbose)) {
         with_warning_string(mesg, 0, fmt) {
-            rb_warn_category(mesg, ID2SYM(rb_intern(category)));
+            rb_warn_category(mesg, rb_hash_fetch(warning_category_t_map, INT2NUM(category)));
         }
     }
 }
@@ -2839,6 +2840,13 @@ Init_Exception(void) https://github.com/ruby/ruby/blob/trunk/error.c#L2840
     rb_gc_register_mark_object(warning_categories);
     rb_hash_aset(warning_categories, ID2SYM(rb_intern_const("deprecated")), Qtrue);
     rb_obj_freeze(warning_categories);
+
+    warning_category_t_map = rb_hash_new();
+    rb_gc_register_mark_object(warning_category_t_map);
+    rb_hash_aset(warning_category_t_map, INT2NUM(RB_WARN_CATEGORY_NONE), Qnil);
+    rb_hash_aset(warning_category_t_map, INT2NUM(RB_WARN_CATEGORY_DEPRECATED), ID2SYM(rb_intern_const("deprecated")));
+    rb_hash_aset(warning_category_t_map, INT2NUM(RB_WARN_CATEGORY_EXPERIMENTAL), ID2SYM(rb_intern_const("experimental")));
+    rb_obj_freeze(warning_category_t_map);
 }
 
 void
diff --git a/include/ruby/internal/error.h b/include/ruby/internal/error.h
index dc842cc..72ee622 100644
--- a/include/ruby/internal/error.h
+++ b/include/ruby/internal/error.h
@@ -29,6 +29,13 @@ RBIMPL_SYMBOL_EXPORT_BEGIN() https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/error.h#L29
 VALUE rb_errinfo(void);
 void rb_set_errinfo(VALUE);
 
+typedef enum {
+    RB_WARN_CATEGORY_NONE,
+    RB_WARN_CATEGORY_DEPRECATED,
+    RB_WARN_CATEGORY_EXPERIMENTAL,
+    RB_WARN_CATEGORY_ALL_BITS = 0x6 /* no RB_WARN_CATEGORY_NONE bit */
+} rb_warning_category_t;
+
 /* for rb_readwrite_sys_fail first argument */
 enum rb_io_wait_readwrite {RB_IO_WAIT_READABLE, RB_IO_WAIT_WRITABLE};
 #define RB_IO_WAIT_READABLE RB_IO_WAIT_READABLE
@@ -63,12 +70,12 @@ VALUE *rb_ruby_debug_ptr(void); https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/error.h#L70
 
 /* reports if `-W' specified */
 PRINTF_ARGS(void rb_warning(const char*, ...), 1, 2);
-PRINTF_ARGS(void rb_category_warning(const char*, const char*, ...), 2, 3);
+PRINTF_ARGS(void rb_category_warning(rb_warning_category_t category, const char*, ...), 2, 3);
 PRINTF_ARGS(void rb_compile_warning(const char *, int, const char*, ...), 3, 4);
 PRINTF_ARGS(void rb_sys_warning(const char*, ...), 1, 2);
 /* reports always */
 COLDFUNC PRINTF_ARGS(void rb_warn(const char*, ...), 1, 2);
-COLDFUNC PRINTF_ARGS(void rb_category_warn(const char *, const char*, ...), 2, 3);
+COLDFUNC PRINTF_ARGS(void rb_category_warn(rb_warning_category_t category, const char*, ...), 2, 3);
 PRINTF_ARGS(void rb_compile_warn(const char *, int, const char*, ...), 3, 4);
 
 RBIMPL_SYMBOL_EXPORT_END()
diff --git a/internal/error.h b/internal/error.h
index cf6495f..0dd629f 100644
--- a/internal/error.h
+++ b/internal/error.h
@@ -40,13 +40,6 @@ https://github.com/ruby/ruby/blob/trunk/internal/error.h#L40
 #endif
 
 /* error.c */
-typedef enum {
-    RB_WARN_CATEGORY_NONE,
-    RB_WARN_CATEGORY_DEPRECATED,
-    RB_WARN_CATEGORY_EXPERIMENTAL,
-    RB_WARN_CATEGORY_ALL_BITS = 0x6, /* no RB_WARN_CATEGORY_NONE bit */
-} rb_warning_category_t;
-
 extern long rb_backtrace_length_limit;
 extern VALUE rb_eEAGAIN;
 extern VALUE rb_eEWOULDBLOCK;
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]