-
Notifications
You must be signed in to change notification settings - Fork 20
/
error.c
343 lines (283 loc) · 7.42 KB
/
error.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* error.c - error output and modification routines */
/* (c) in 2002-2024 by Volker Barthelmann and Frank Wille */
#include <stdarg.h>
#include "vasm.h"
#include "error.h"
static struct err_out general_err_out[]={
#include "general_errors.h"
};
static const int general_errors=sizeof(general_err_out)/sizeof(general_err_out[0]);
static struct err_out syntax_err_out[]={
#include "syntax_errors.h"
};
static const int syntax_errors=sizeof(syntax_err_out)/sizeof(syntax_err_out[0]);
static struct err_out cpu_err_out[]={
#include "cpu_errors.h"
};
static const int cpu_errors=sizeof(cpu_err_out)/sizeof(cpu_err_out[0]);
static struct err_out output_err_out[]={
#include "output_errors.h"
};
static const int output_errors=sizeof(output_err_out)/sizeof(output_err_out[0]);
int errors,warnings; /* count */
/* options */
int max_errors=5;
int no_warn;
static void print_source_line(FILE *f,source *src,int l)
{
static char *buf = NULL;
static size_t bufsz = 0;
char c,*e,*p,*q;
/* allocate a sufficiently dimensioned line buffer */
if (src->bufsize > bufsz) {
bufsz = src->bufsize;
buf = myrealloc(buf,bufsz);
}
p = src->text;
q = buf;
e = buf + bufsz - 1;
do {
c = *p++;
if (c=='\n' || c=='\r') {
if (*p == ((c=='\n') ? '\r' : '\n'))
p++;
if (--l == 0) {
/* terminate error line in buffer and print it */
*q = '\0';
fprintf(f,"%s\n",buf);
return;
}
q = buf; /* next line, start to fill buffer from the beginning */
}
else if (q < e)
*q++ = c;
}
while (*p);
ierror(0); /* line doesn't exist */
}
static void print_source_file(FILE *f, source *src)
{
if (src->srcfile) {
if (src->srcfile->incpath != NULL)
fprintf(f,"\"%s%s%s\"",
src->srcfile->incpath->compdir_based
? compile_dir : emptystr,
src->srcfile->incpath->path,
src->srcfile->name);
else
fprintf(f,"\"%s\"",src->srcfile->name);
}
else
fprintf(f,"\"%s\"",src->name);
}
static void error(int n,va_list vl,struct err_out *errlist,int offset)
{
static source *last_err_source = NULL;
static int last_err_no;
static int last_err_line;
FILE *f;
int flags=errlist[n].flags;
if ((flags&DISABLED) || ((flags&WARNING) && no_warn))
return;
if ((flags&MESSAGE) && !(flags&(WARNING|ERROR|FATAL))) {
if (nostdout)
return;
f = stdout; /* print messages to stdout */
}
else {
f = stderr; /* otherwise stderr */
if (last_err_source) {
/* avoid printing the same error again and again, which might happen
when a line is evaluated in multiple passes */
if (cur_src!=NULL && cur_src==last_err_source &&
cur_src->line==last_err_line &&
n+offset==last_err_no)
return;
}
}
if (cur_src) {
last_err_source = cur_src;
last_err_line = cur_src->line;
last_err_no = n + offset;
}
fprintf(f,"\n");
if (cur_listing && (flags & ERROR))
cur_listing->error = n + offset;
if (flags & FATAL)
fprintf(f,"fatal ");
if (flags & ERROR) {
++errors;
fprintf(f,"error");
}
else if (flags & WARNING) {
++warnings;
fprintf(f,"warning");
}
else if (flags & MESSAGE)
fprintf(f,"message");
fprintf(f," %d",n+offset);
if (!(flags & NOLINE) && cur_src!=NULL) {
fprintf(f," in line %d of ",cur_src->line);
print_source_file(f,cur_src);
if (cur_src->defsrc) {
fprintf(f," (line %d of ",cur_src->defline+cur_src->line);
print_source_file(f,cur_src->defsrc);
fputc(')',f);
}
}
fprintf(f,": ");
vfprintf(f,errlist[n].text,vl);
fprintf(f,"\n");
if (!(flags & NOLINE) && cur_src!=NULL) {
if (cur_src->parent != NULL) {
source *parent,*child;
int recurs;
child = cur_src;
while (parent = child->parent) {
if (child->srcfile)
fprintf(f,"\tincluded"); /* included from */
else if (child->macro)
fprintf(f,"\tcalled"); /* macro called from */
else {
child = parent; /* skip parent for repetitions */
continue;
}
fprintf(f," from line %d of ",child->parent_line);
print_source_file(f,parent);
if (parent->defsrc) {
fprintf(f," (line %d of ",parent->defline+child->parent_line);
print_source_file(f,parent->defsrc);
fputc(')',f);
}
recurs = 1;
while (parent->parent!=NULL &&
child->parent_line==parent->parent_line &&
!strcmp(parent->name,parent->parent->name)) {
recurs++;
parent = parent->parent;
}
if (recurs > 1)
fprintf(f," %d times",recurs);
if (child==cur_src && child->macro!=NULL) {
fprintf(f,": ");
print_source_line(f,parent,child->parent_line);
}
else
fprintf(f,"\n");
child = parent;
}
}
fputc('>',f);
print_source_line(f,cur_src,cur_src->line);
}
if (flags & FATAL) {
fprintf(f,"aborting...\n");
leave();
}
if ((flags & ERROR) && max_errors!=0 && errors>=max_errors) {
fprintf(f,"***maximum number of errors reached!***\n");
leave();
}
}
void general_error(int n,...)
{
va_list vl;
va_start(vl,n);
error(n,vl,general_err_out,FIRST_GENERAL_ERROR);
va_end(vl);
}
void syntax_error(int n,...)
{
va_list vl;
va_start(vl,n);
error(n,vl,syntax_err_out,FIRST_SYNTAX_ERROR);
va_end(vl);
}
void cpu_error(int n,...)
{
va_list vl;
va_start(vl,n);
error(n,vl,cpu_err_out,FIRST_CPU_ERROR);
va_end(vl);
}
void output_error(int n,...)
{
va_list vl;
va_start(vl,n);
error(n,vl,output_err_out,FIRST_OUTPUT_ERROR);
va_end(vl);
}
void output_atom_error(int n,atom *a,...)
{
source *old = cur_src;
va_list vl;
va_start(vl,a);
/* temporarily set the source text and line from the given atom */
if (a != NULL) {
if ((cur_src = a->src) != NULL)
cur_src->line = a->line;
}
else
cur_src = NULL; /* no line information, when atom is missing */
error(n,vl,output_err_out,FIRST_OUTPUT_ERROR);
cur_src = old;
va_end(vl);
}
static void modify_errors(struct err_out *err,int flags,va_list vl)
{
int n;
while (n = va_arg(vl,int)) {
err[n].flags = flags;
}
}
void modify_gen_err(int flags,...)
{
va_list vl;
va_start(vl,flags);
modify_errors(general_err_out,flags,vl);
va_end(vl);
}
void modify_syntax_err(int flags,...)
{
va_list vl;
va_start(vl,flags);
modify_errors(syntax_err_out,flags,vl);
va_end(vl);
}
void modify_cpu_err(int flags,...)
{
va_list vl;
va_start(vl,flags);
modify_errors(cpu_err_out,flags,vl);
va_end(vl);
}
static void disable(int type,struct err_out *err,int errnum,int first,int max)
{
int n = errnum-first;
if (n>=0 && n<max) {
if (err[n].flags & type) {
err[n].flags |= DISABLED;
return;
}
}
general_error(33,errnum,type==WARNING?"warning":emptystr);
}
static void disable_type(int type,int n)
{
if (n >= FIRST_OUTPUT_ERROR)
disable(type,output_err_out,n,FIRST_OUTPUT_ERROR,output_errors);
else if (n >= FIRST_CPU_ERROR)
disable(type,cpu_err_out,n,FIRST_CPU_ERROR,cpu_errors);
else if (n >= FIRST_SYNTAX_ERROR)
disable(type,syntax_err_out,n,FIRST_SYNTAX_ERROR,syntax_errors);
else if (n >= FIRST_GENERAL_ERROR)
disable(type,general_err_out,n,FIRST_GENERAL_ERROR,general_errors);
}
void disable_message(int n)
{
disable_type(MESSAGE,n);
}
void disable_warning(int n)
{
disable_type(WARNING,n);
}