1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Moved Kernel#warn to warning.rb

This commit is contained in:
Nobuyoshi Nakada 2019-12-10 20:22:42 +09:00
parent 82cc2843a9
commit 0b5268afbc
Notes: git 2019-12-13 20:47:34 +09:00
4 changed files with 76 additions and 66 deletions

View file

@ -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

93
error.c
View file

@ -14,6 +14,7 @@
#include "internal.h"
#include "ruby_assert.h"
#include "vm_core.h"
#include "builtin.h"
#include <stdio.h>
#include <stdarg.h>
@ -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
* <code>-W0</code> 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")
*
* <em>produces:</em>
*
* warning 1
* warning 2
*
* If the <code>uplevel</code> keyword argument is given, the string will
* be prepended with information for the given caller frame in
* the same format used by the <code>rb_warn</code> C function.
*
* # In baz.rb
* def foo
* warn("invalid call to foo", uplevel: 1)
* end
*
* def bar
* foo
* end
*
* bar
*
* <em>produces:</em>
*
* 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();
}
/*!
* \}
*/

View file

@ -76,6 +76,7 @@ rb_call_inits(void)
CALL(ast);
CALL(vm_trace);
CALL(pack);
CALL(warning);
load_prelude();
}
#undef CALL

44
warning.rb Normal file
View file

@ -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
# <code>-W0</code> 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")
#
# <em>produces:</em>
#
# warning 1
# warning 2
#
# If the <code>uplevel</code> keyword argument is given, the string will
# be prepended with information for the given caller frame in
# the same format used by the <code>rb_warn</code> C function.
#
# # In baz.rb
# def foo
# warn("invalid call to foo", uplevel: 1)
# end
#
# def bar
# foo
# end
#
# bar
#
# <em>produces:</em>
#
# baz.rb:6: warning: invalid call to foo
#
def warn(*msgs, uplevel: nil)
__builtin_rb_warn_m(msgs, uplevel)
end
end