From 0b5268afbcf11c299e11102c366e836ae55cc39f Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 10 Dec 2019 20:22:42 +0900 Subject: [PATCH] Moved Kernel#warn to warning.rb --- common.mk | 4 +++ error.c | 93 ++++++++++++++++-------------------------------------- inits.c | 1 + warning.rb | 44 ++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 66 deletions(-) create mode 100644 warning.rb diff --git a/common.mk b/common.mk index b8a33015bd..debc6d5efb 100644 --- a/common.mk +++ b/common.mk @@ -1001,6 +1001,7 @@ BUILTIN_RB_SRCS = \ $(srcdir)/io.rb \ $(srcdir)/pack.rb \ $(srcdir)/trace_point.rb \ + $(srcdir)/warning.rb \ $(srcdir)/prelude.rb \ $(srcdir)/gem_prelude.rb \ $(empty) @@ -1974,6 +1975,7 @@ error.$(OBJEXT): $(CCAN_DIR)/str/str.h error.$(OBJEXT): $(hdrdir)/ruby.h error.$(OBJEXT): $(hdrdir)/ruby/ruby.h error.$(OBJEXT): {$(VPATH)}assert.h +error.$(OBJEXT): {$(VPATH)}builtin.h error.$(OBJEXT): {$(VPATH)}config.h error.$(OBJEXT): {$(VPATH)}defines.h error.$(OBJEXT): {$(VPATH)}encoding.h @@ -1995,6 +1997,7 @@ error.$(OBJEXT): {$(VPATH)}thread_$(THREAD_MODEL).h error.$(OBJEXT): {$(VPATH)}thread_native.h error.$(OBJEXT): {$(VPATH)}vm_core.h error.$(OBJEXT): {$(VPATH)}vm_opts.h +error.$(OBJEXT): {$(VPATH)}warning.rbinc eval.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h eval.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h eval.$(OBJEXT): $(CCAN_DIR)/list/list.h @@ -2385,6 +2388,7 @@ miniinit.$(OBJEXT): {$(VPATH)}thread_native.h miniinit.$(OBJEXT): {$(VPATH)}trace_point.rb miniinit.$(OBJEXT): {$(VPATH)}vm_core.h miniinit.$(OBJEXT): {$(VPATH)}vm_opts.h +miniinit.$(OBJEXT): {$(VPATH)}warning.rb mjit.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h mjit.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h mjit.$(OBJEXT): $(CCAN_DIR)/list/list.h diff --git a/error.c b/error.c index 13d191007b..14114fc93c 100644 --- a/error.c +++ b/error.c @@ -14,6 +14,7 @@ #include "internal.h" #include "ruby_assert.h" #include "vm_core.h" +#include "builtin.h" #include #include @@ -291,73 +292,27 @@ warning_write(int argc, VALUE *argv, VALUE buf) return buf; } -/* - * call-seq: - * warn(*msgs, uplevel: nil) -> nil - * - * If warnings have been disabled (for example with the - * -W0 flag), does nothing. Otherwise, - * converts each of the messages to strings, appends a newline - * character to the string if the string does not end in a newline, - * and calls Warning.warn with the string. - * - * warn("warning 1", "warning 2") - * - * produces: - * - * warning 1 - * warning 2 - * - * If the uplevel keyword argument is given, the string will - * be prepended with information for the given caller frame in - * the same format used by the rb_warn C function. - * - * # In baz.rb - * def foo - * warn("invalid call to foo", uplevel: 1) - * end - * - * def bar - * foo - * end - * - * bar - * - * produces: - * - * baz.rb:6: warning: invalid call to foo - */ - static VALUE -rb_warn_m(int argc, VALUE *argv, VALUE exc) +rb_warn_m(rb_execution_context_t *ec, VALUE exc, VALUE msgs, VALUE uplevel) { - VALUE opts, location = Qnil; + VALUE location = Qnil; + int argc = RARRAY_LENINT(msgs); + const VALUE *argv = RARRAY_CONST_PTR(msgs); - if (!NIL_P(ruby_verbose) && argc > 0 && - (argc = rb_scan_args(argc, argv, "*:", NULL, &opts)) > 0) { - VALUE str = argv[0], uplevel = Qnil; - if (!NIL_P(opts)) { - static ID kwds[1]; - if (!kwds[0]) { - CONST_ID(kwds[0], "uplevel"); - } - rb_get_kwargs(opts, kwds, 0, 1, &uplevel); - if (uplevel == Qundef) { - uplevel = Qnil; - } - else if (!NIL_P(uplevel)) { - VALUE args[2]; - long lev = NUM2LONG(uplevel); - if (lev < 0) { - rb_raise(rb_eArgError, "negative level (%ld)", lev); - } - args[0] = LONG2NUM(lev + 1); - args[1] = INT2FIX(1); - location = rb_vm_thread_backtrace_locations(2, args, GET_THREAD()->self); - if (!NIL_P(location)) { - location = rb_ary_entry(location, 0); - } - } + if (!NIL_P(ruby_verbose) && argc > 0) { + VALUE str = argv[0]; + if (!NIL_P(uplevel)) { + VALUE args[2]; + long lev = NUM2LONG(uplevel); + if (lev < 0) { + rb_raise(rb_eArgError, "negative level (%ld)", lev); + } + args[0] = LONG2NUM(lev + 1); + args[1] = INT2FIX(1); + location = rb_vm_thread_backtrace_locations(2, args, GET_THREAD()->self); + if (!NIL_P(location)) { + location = rb_ary_entry(location, 0); + } } if (argc > 1 || !NIL_P(uplevel) || !end_with_asciichar(str, '\n')) { VALUE path; @@ -2555,8 +2510,6 @@ Init_Exception(void) rb_cWarningBuffer = rb_define_class_under(rb_mWarning, "buffer", rb_cString); rb_define_method(rb_cWarningBuffer, "write", warning_write, -1); - rb_define_global_function("warn", rb_warn_m, -1); - id_cause = rb_intern_const("cause"); id_message = rb_intern_const("message"); id_backtrace = rb_intern_const("backtrace"); @@ -2991,6 +2944,14 @@ Init_syserr(void) #undef undefined_error } +#include "warning.rbinc" + +void +Init_warning(void) +{ + load_warning(); +} + /*! * \} */ diff --git a/inits.c b/inits.c index 490506e3bf..79a6cf014e 100644 --- a/inits.c +++ b/inits.c @@ -76,6 +76,7 @@ rb_call_inits(void) CALL(ast); CALL(vm_trace); CALL(pack); + CALL(warning); load_prelude(); } #undef CALL diff --git a/warning.rb b/warning.rb new file mode 100644 index 0000000000..8361176491 --- /dev/null +++ b/warning.rb @@ -0,0 +1,44 @@ +# encoding: utf-8 +# fronzen-string-literal: true + +module Kernel + + # call-seq: + # warn(*msgs, uplevel: nil) -> nil + # + # If warnings have been disabled (for example with the + # -W0 flag), does nothing. Otherwise, + # converts each of the messages to strings, appends a newline + # character to the string if the string does not end in a newline, + # and calls Warning.warn with the string. + # + # warn("warning 1", "warning 2") + # + # produces: + # + # warning 1 + # warning 2 + # + # If the uplevel keyword argument is given, the string will + # be prepended with information for the given caller frame in + # the same format used by the rb_warn C function. + # + # # In baz.rb + # def foo + # warn("invalid call to foo", uplevel: 1) + # end + # + # def bar + # foo + # end + # + # bar + # + # produces: + # + # baz.rb:6: warning: invalid call to foo + # + def warn(*msgs, uplevel: nil) + __builtin_rb_warn_m(msgs, uplevel) + end +end