~ubuntu-branches/ubuntu/intrepid/ruby1.9/intrepid-updates

« back to all changes in this revision

Viewing changes to error.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-09-04 16:01:17 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070904160117-i15zckg2nhxe9fyw
Tags: 1.9.0+20070830-2ubuntu1
* Sync from Debian; remaining changes:
  - Add -g to CFLAGS.
* Fixes build failure on ia64.
* Fixes build failure with gcc-4.2 on lpia.
* Robustify check for target_os, fixing build failure on lpia.
* Set Ubuntu maintainer address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
  error.c -
4
4
 
5
 
  $Author: nobu $
6
 
  $Date: 2007-05-02 06:45:48 +0900 (水, 02  5月 2007) $
 
5
  $Author: matz $
 
6
  $Date: 2007-08-25 12:29:39 +0900 (土, 25  8月 2007) $
7
7
  created at: Mon Aug  9 16:11:34 JST 1993
8
8
 
9
 
  Copyright (C) 1993-2003 Yukihiro Matsumoto
 
9
  Copyright (C) 1993-2007 Yukihiro Matsumoto
10
10
 
11
11
**********************************************************************/
12
12
 
13
 
#include "ruby.h"
14
 
#include "st.h"
15
 
#include "yarvcore.h"
 
13
#include "ruby/ruby.h"
 
14
#include "ruby/st.h"
 
15
#include "vm_core.h"
16
16
 
17
17
#include <stdio.h>
18
18
#include <stdarg.h>
24
24
#endif
25
25
 
26
26
extern const char ruby_version[], ruby_release_date[], ruby_platform[];
27
 
int ruby_nerrs;
28
 
 
29
 
const char *rb_sourcefile(void);
30
 
int rb_sourceline(void);
31
27
 
32
28
static int
33
 
err_position_0(char *buf, long len, const char *file, long line)
 
29
err_position_0(char *buf, long len, const char *file, int line)
34
30
{
35
31
    if (!file) {
36
32
        return 0;
49
45
    return err_position_0(buf, len, rb_sourcefile(), rb_sourceline());
50
46
}
51
47
 
52
 
static int
53
 
compile_position(char *buf, long len)
54
 
{
55
 
    ruby_set_current_source();
56
 
    return err_position_0(buf, len, ruby_sourcefile, ruby_sourceline);
57
 
}
58
 
 
59
48
static void
60
49
err_snprintf(char *buf, long len, const char *fmt, va_list args)
61
50
{
68
57
}
69
58
 
70
59
static void
71
 
compile_snprintf(char *buf, long len, const char *fmt, va_list args)
 
60
compile_snprintf(char *buf, long len, const char *file, int line, const char *fmt, va_list args)
72
61
{
73
62
    long n;
74
63
 
75
 
    n = compile_position(buf, len);
 
64
    n = err_position_0(buf, len, file, line);
76
65
    if (len > n) {
77
66
        vsnprintf((char*)buf+n, len-n, fmt, args);
78
67
    }
81
70
static void err_append(const char*);
82
71
 
83
72
void
84
 
rb_compile_error(const char *fmt, ...)
 
73
rb_compile_error(const char *file, int line, const char *fmt, ...)
85
74
{
86
75
    va_list args;
87
76
    char buf[BUFSIZ];
88
77
 
89
78
    va_start(args, fmt);
90
 
    compile_snprintf(buf, BUFSIZ, fmt, args);
 
79
    compile_snprintf(buf, BUFSIZ, file, line, fmt, args);
91
80
    va_end(args);
92
81
    err_append(buf);
93
 
    ruby_nerrs++;
94
82
}
95
83
 
96
84
void
106
94
}
107
95
 
108
96
static void
109
 
compile_warn_print(const char *fmt, va_list args)
 
97
compile_warn_print(const char *file, int line, const char *fmt, va_list args)
110
98
{
111
99
    char buf[BUFSIZ];
112
100
    int len;
113
101
 
114
 
    compile_snprintf(buf, BUFSIZ, fmt, args);
 
102
    compile_snprintf(buf, BUFSIZ, file, line, fmt, args);
115
103
    len = strlen(buf);
116
104
    buf[len++] = '\n';
117
105
    rb_write_error2(buf, len);
118
106
}
119
107
 
120
108
void
121
 
rb_compile_warn(const char *fmt, ...)
 
109
rb_compile_warn(const char *file, int line, const char *fmt, ...)
122
110
{
123
111
    char buf[BUFSIZ];
124
112
    va_list args;
128
116
    snprintf(buf, BUFSIZ, "warning: %s", fmt);
129
117
 
130
118
    va_start(args, fmt);
131
 
    compile_warn_print(buf, args);
 
119
    compile_warn_print(file, line, buf, args);
132
120
    va_end(args);
133
121
}
134
122
 
135
123
/* rb_compile_warning() reports only in verbose mode */
136
124
void
137
 
rb_compile_warning(const char *fmt, ...)
 
125
rb_compile_warning(const char *file, int line, const char *fmt, ...)
138
126
{
139
127
    char buf[BUFSIZ];
140
128
    va_list args;
144
132
    snprintf(buf, BUFSIZ, "warning: %s", fmt);
145
133
 
146
134
    va_start(args, fmt);
147
 
    compile_warn_print(buf, args);
 
135
    compile_warn_print(file, line, buf, args);
148
136
    va_end(args);
149
137
}
150
138
 
209
197
    return Qnil;
210
198
}
211
199
 
212
 
void yarv_bug(void);
 
200
void rb_vm_bugreport(void);
213
201
 
214
 
void
215
 
rb_bug(const char *fmt, ...)
 
202
static void
 
203
report_bug(const char *file, int line, const char *fmt, va_list args)
216
204
{
217
205
    char buf[BUFSIZ];
218
 
    va_list args;
219
206
    FILE *out = stderr;
220
 
    int len = err_position(buf, BUFSIZ);
 
207
    int len = err_position_0(buf, BUFSIZ, file, line);
221
208
 
222
209
    if (fwrite(buf, 1, len, out) == len ||
223
210
        fwrite(buf, 1, len, (out = stdout)) == len) {
224
 
        yarv_bug();
 
211
        rb_vm_bugreport();
225
212
        fputs("[BUG] ", out);
226
 
        va_start(args, fmt);
227
213
        vfprintf(out, fmt, args);
228
 
        va_end(args);
229
214
        fprintf(out, "\nruby %s (%s) [%s]\n\n",
230
215
                ruby_version, ruby_release_date, ruby_platform);
231
216
    }
 
217
}
 
218
 
 
219
void
 
220
rb_bug(const char *fmt, ...)
 
221
{
 
222
    va_list args;
 
223
 
 
224
    va_start(args, fmt);
 
225
    report_bug(rb_sourcefile(), rb_sourceline(), fmt, args);
 
226
    va_end(args);
 
227
 
 
228
    abort();
 
229
}
 
230
 
 
231
void
 
232
rb_compile_bug(const char *file, int line, const char *fmt, ...)
 
233
{
 
234
    va_list args;
 
235
 
 
236
    va_start(args, fmt);
 
237
    report_bug(file, line, fmt, args);
 
238
    va_end(args);
232
239
 
233
240
    abort();
234
241
}
488
495
static VALUE
489
496
exc_backtrace(VALUE exc)
490
497
{
491
 
    ID bt = rb_intern("bt");
 
498
    static ID bt;
492
499
 
493
 
    if (!rb_ivar_defined(exc, bt)) return Qnil;
494
 
    return rb_ivar_get(exc, bt);
 
500
    if (!bt) bt = rb_intern("bt");
 
501
    return rb_attr_get(exc, bt);
495
502
}
496
503
 
497
 
static VALUE
498
 
check_backtrace(VALUE bt)
 
504
VALUE
 
505
rb_check_backtrace(VALUE bt)
499
506
{
500
507
    long i;
501
508
    static const char *err = "backtrace must be Array of String";
529
536
static VALUE
530
537
exc_set_backtrace(VALUE exc, VALUE bt)
531
538
{
532
 
    return rb_iv_set(exc, "bt", check_backtrace(bt));
 
539
    return rb_iv_set(exc, "bt", rb_check_backtrace(bt));
533
540
}
534
541
 
535
542
/*
1535
1542
err_append(const char *s)
1536
1543
{
1537
1544
    rb_thread_t *th = GET_THREAD();
 
1545
    VALUE err = th->errinfo;
 
1546
 
1538
1547
    if (th->parse_in_eval) {
1539
 
        if (NIL_P(th->errinfo)) {
1540
 
            th->errinfo = rb_exc_new2(rb_eSyntaxError, s);
 
1548
        if (!RTEST(err)) {
 
1549
            err = rb_exc_new2(rb_eSyntaxError, s);
 
1550
            th->errinfo = err;
1541
1551
        }
1542
1552
        else {
1543
 
            VALUE str = rb_obj_as_string(GET_THREAD()->errinfo);
 
1553
            VALUE str = rb_obj_as_string(err);
1544
1554
 
1545
1555
            rb_str_cat2(str, "\n");
1546
1556
            rb_str_cat2(str, s);
1548
1558
        }
1549
1559
    }
1550
1560
    else {
 
1561
        if (!RTEST(err)) {
 
1562
            err = rb_exc_new2(rb_eSyntaxError, "compile error");
 
1563
            th->errinfo = err;
 
1564
        }
1551
1565
        rb_write_error(s);
1552
1566
        rb_write_error("\n");
1553
1567
    }