1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/eval_error.c
ko1 4552f74715 introduce EC_*_TAG() instead of TH_*_TAG()
* eval_intern.h:
  introduce EC_*_TAG() macros instead of TH_*_TAG() macros.
  * TH_PUSH_TAG() -> EC_PUSH_TAG()
  * TH_POP_TAG() -> EC_POP_TAG()
  * TH_TMPPOP_TAG() -> EC_TMPPOP_TAG()
  * TH_REPUSH_TAG() -> EC_REPUSH_TAG()
  * TH_EXEC_TAG() -> EC_EXEC_TAG()
  * TH_JUMP_TAG() -> EC_JUMP_TAG()

  rb_threadptr_tag_state() , rb_ec_tag_jump() also accept `ec` instead of `th`.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60450 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-26 11:02:13 +00:00

329 lines
7.7 KiB
C

/* -*-c-*- */
/*
* included by eval.c
*/
#ifdef HAVE_BUILTIN___BUILTIN_CONSTANT_P
#define warn_print(x) RB_GNUC_EXTENSION_BLOCK( \
(__builtin_constant_p(x)) ? \
rb_write_error2((x), (long)strlen(x)) : \
rb_write_error(x) \
)
#else
#define warn_print(x) rb_write_error(x)
#endif
#define warn_print2(x,l) rb_write_error2((x),(l))
#define warn_print_str(x) rb_write_error_str(x)
static VALUE error_pos_str(void);
static void
error_pos(void)
{
VALUE str = error_pos_str();
if (!NIL_P(str)) {
warn_print_str(str);
}
}
static VALUE
error_pos_str(void)
{
int sourceline;
VALUE sourcefile = rb_source_location(&sourceline);
if (sourcefile) {
ID caller_name;
if (sourceline == 0) {
return rb_sprintf("%"PRIsVALUE": ", sourcefile);
}
else if ((caller_name = rb_frame_callee()) != 0) {
return rb_sprintf("%"PRIsVALUE":%d:in `%"PRIsVALUE"': ",
sourcefile, sourceline,
rb_id2str(caller_name));
}
else {
return rb_sprintf("%"PRIsVALUE":%d: ", sourcefile, sourceline);
}
}
return Qnil;
}
static void
set_backtrace(VALUE info, VALUE bt)
{
ID set_backtrace = rb_intern("set_backtrace");
if (rb_backtrace_p(bt)) {
if (rb_method_basic_definition_p(CLASS_OF(info), set_backtrace)) {
rb_exc_set_backtrace(info, bt);
return;
}
else {
bt = rb_backtrace_to_str_ary(bt);
}
}
rb_check_funcall(info, set_backtrace, 1, &bt);
}
static void
error_print(rb_thread_t *th)
{
rb_threadptr_error_print(th, th->ec->errinfo);
}
static void
print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg)
{
const char *einfo = "";
long elen = 0;
VALUE mesg;
if (emesg != Qundef) {
if (NIL_P(errat) || RARRAY_LEN(errat) == 0 ||
NIL_P(mesg = RARRAY_AREF(errat, 0))) {
error_pos();
}
else {
warn_print_str(mesg);
warn_print(": ");
}
if (!NIL_P(emesg)) {
einfo = RSTRING_PTR(emesg);
elen = RSTRING_LEN(emesg);
}
}
if (eclass == rb_eRuntimeError && elen == 0) {
warn_print("unhandled exception\n");
}
else {
VALUE epath;
epath = rb_class_name(eclass);
if (elen == 0) {
warn_print_str(epath);
warn_print("\n");
}
else {
const char *tail = 0;
long len = elen;
if (RSTRING_PTR(epath)[0] == '#')
epath = 0;
if ((tail = memchr(einfo, '\n', elen)) != 0) {
len = tail - einfo;
tail++; /* skip newline */
}
warn_print_str(tail ? rb_str_subseq(emesg, 0, len) : emesg);
if (epath) {
warn_print(" (");
warn_print_str(epath);
warn_print(")\n");
}
if (tail) {
warn_print_str(rb_str_subseq(emesg, tail - einfo, elen - len - 1));
}
if (tail ? einfo[elen-1] != '\n' : !epath) warn_print2("\n", 1);
}
}
}
static void
print_backtrace(const VALUE eclass, const VALUE errat, int reverse)
{
if (!NIL_P(errat)) {
long i;
long len = RARRAY_LEN(errat);
int skip = eclass == rb_eSysStackError;
const int threshold = 1000000000;
int width = ((int)log10((double)(len > threshold ?
((len - 1) / threshold) :
len - 1)) +
(len < threshold ? 0 : 9) + 1);
#define TRACE_MAX (TRACE_HEAD+TRACE_TAIL+5)
#define TRACE_HEAD 8
#define TRACE_TAIL 5
for (i = 1; i < len; i++) {
VALUE line = RARRAY_AREF(errat, reverse ? len - i : i);
if (RB_TYPE_P(line, T_STRING)) {
VALUE str = rb_str_new_cstr("\t");
if (reverse) rb_str_catf(str, "%*ld: ", width, len - i);
warn_print_str(rb_str_catf(str, "from %"PRIsVALUE"\n", line));
}
if (skip && i == TRACE_HEAD && len > TRACE_MAX) {
warn_print_str(rb_sprintf("\t ... %ld levels...\n",
len - TRACE_HEAD - TRACE_TAIL));
i = len - TRACE_TAIL;
}
}
}
}
void
rb_threadptr_error_print(rb_thread_t *volatile th, volatile VALUE errinfo)
{
volatile VALUE errat = Qundef;
volatile int raised_flag = th->ec->raised_flag;
volatile VALUE eclass = Qundef, emesg = Qundef;
if (NIL_P(errinfo))
return;
rb_thread_raised_clear(th);
EC_PUSH_TAG(th->ec);
if (EC_EXEC_TAG() == TAG_NONE) {
errat = rb_get_backtrace(errinfo);
}
else if (errat == Qundef) {
errat = Qnil;
}
else if (eclass == Qundef || emesg != Qundef) {
goto error;
}
if ((eclass = CLASS_OF(errinfo)) != Qundef) {
VALUE e = rb_check_funcall(errinfo, rb_intern("message"), 0, 0);
if (e != Qundef) {
if (!RB_TYPE_P(e, T_STRING)) e = rb_check_string_type(e);
emesg = e;
}
}
if (rb_stderr_tty_p()) {
warn_print("Traceback (most recent call last):\n");
print_backtrace(eclass, errat, TRUE);
print_errinfo(eclass, errat, emesg);
}
else {
print_errinfo(eclass, errat, emesg);
print_backtrace(eclass, errat, FALSE);
}
error:
EC_POP_TAG();
th->ec->errinfo = errinfo;
rb_thread_raised_set(th, raised_flag);
}
#define undef_mesg_for(v, k) rb_fstring_cstr("undefined"v" method `%1$s' for "k" `%2$s'")
#define undef_mesg(v) ( \
is_mod ? \
undef_mesg_for(v, "module") : \
undef_mesg_for(v, "class"))
void
rb_print_undef(VALUE klass, ID id, rb_method_visibility_t visi)
{
const int is_mod = RB_TYPE_P(klass, T_MODULE);
VALUE mesg;
switch (visi & METHOD_VISI_MASK) {
case METHOD_VISI_UNDEF:
case METHOD_VISI_PUBLIC: mesg = undef_mesg(""); break;
case METHOD_VISI_PRIVATE: mesg = undef_mesg(" private"); break;
case METHOD_VISI_PROTECTED: mesg = undef_mesg(" protected"); break;
default: UNREACHABLE;
}
rb_name_err_raise_str(mesg, klass, ID2SYM(id));
}
void
rb_print_undef_str(VALUE klass, VALUE name)
{
const int is_mod = RB_TYPE_P(klass, T_MODULE);
rb_name_err_raise_str(undef_mesg(""), klass, name);
}
#define inaccessible_mesg_for(v, k) rb_fstring_cstr("method `%1$s' for "k" `%2$s' is "v)
#define inaccessible_mesg(v) ( \
is_mod ? \
inaccessible_mesg_for(v, "module") : \
inaccessible_mesg_for(v, "class"))
void
rb_print_inaccessible(VALUE klass, ID id, rb_method_visibility_t visi)
{
const int is_mod = RB_TYPE_P(klass, T_MODULE);
VALUE mesg;
switch (visi & METHOD_VISI_MASK) {
case METHOD_VISI_UNDEF:
case METHOD_VISI_PUBLIC: mesg = inaccessible_mesg(""); break;
case METHOD_VISI_PRIVATE: mesg = inaccessible_mesg(" private"); break;
case METHOD_VISI_PROTECTED: mesg = inaccessible_mesg(" protected"); break;
default: UNREACHABLE;
}
rb_name_err_raise_str(mesg, klass, ID2SYM(id));
}
static int
sysexit_status(VALUE err)
{
VALUE st = rb_ivar_get(err, id_status);
return NUM2INT(st);
}
#define unknown_longjmp_status(status) \
rb_bug("Unknown longjmp status %d", status)
static int
error_handle(int ex)
{
int status = EXIT_FAILURE;
rb_thread_t *th = GET_THREAD();
if (rb_threadptr_set_raised(th))
return EXIT_FAILURE;
switch (ex & TAG_MASK) {
case 0:
status = EXIT_SUCCESS;
break;
case TAG_RETURN:
error_pos();
warn_print("unexpected return\n");
break;
case TAG_NEXT:
error_pos();
warn_print("unexpected next\n");
break;
case TAG_BREAK:
error_pos();
warn_print("unexpected break\n");
break;
case TAG_REDO:
error_pos();
warn_print("unexpected redo\n");
break;
case TAG_RETRY:
error_pos();
warn_print("retry outside of rescue clause\n");
break;
case TAG_THROW:
/* TODO: fix me */
error_pos();
warn_print("unexpected throw\n");
break;
case TAG_RAISE: {
VALUE errinfo = th->ec->errinfo;
if (rb_obj_is_kind_of(errinfo, rb_eSystemExit)) {
status = sysexit_status(errinfo);
}
else if (rb_obj_is_instance_of(errinfo, rb_eSignal) &&
rb_ivar_get(errinfo, id_signo) != INT2FIX(SIGSEGV)) {
/* no message when exiting by signal */
}
else {
rb_threadptr_error_print(th, errinfo);
}
break;
}
case TAG_FATAL:
error_print(th);
break;
default:
unknown_longjmp_status(ex);
break;
}
rb_threadptr_reset_raised(th);
return status;
}