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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
/*
* Error and warning reporting and related functions.
*
* Copyright (C) 2001-2007 Peter Johnson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "util.h"
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include "coretype.h"
#include "linemap.h"
#include "errwarn.h"
#define MSG_MAXSIZE 1024
#if !defined(HAVE_TOASCII) || defined(lint)
# define toascii(c) ((c) & 0x7F)
#endif
/* Default handlers for replacable functions */
static /*@exits@*/ void def_internal_error_
(const char *file, unsigned int line, const char *message);
static /*@exits@*/ void def_fatal(const char *message, va_list va);
static const char *def_gettext_hook(const char *msgid);
/* Storage for errwarn's "extern" functions */
/*@exits@*/ void (*yasm_internal_error_)
(const char *file, unsigned int line, const char *message)
= def_internal_error_;
/*@exits@*/ void (*yasm_fatal) (const char *message, va_list va) = def_fatal;
const char * (*yasm_gettext_hook) (const char *msgid) = def_gettext_hook;
/* Error indicator */
/* yasm_eclass is not static so that yasm_error_occurred macro can access it */
yasm_error_class yasm_eclass;
static /*@only@*/ /*@null@*/ char *yasm_estr;
static unsigned long yasm_exrefline;
static /*@only@*/ /*@null@*/ char *yasm_exrefstr;
/* Warning indicator */
typedef struct warn {
/*@reldef@*/ STAILQ_ENTRY(warn) link;
yasm_warn_class wclass;
/*@owned@*/ /*@null@*/ char *wstr;
} warn;
static STAILQ_HEAD(warn_head, warn) yasm_warns;
/* Enabled warnings. See errwarn.h for a list. */
static unsigned long warn_class_enabled;
typedef struct errwarn_data {
/*@reldef@*/ SLIST_ENTRY(errwarn_data) link;
enum { WE_UNKNOWN, WE_ERROR, WE_WARNING, WE_PARSERERROR } type;
unsigned long line;
unsigned long xrefline;
/*@owned@*/ char *msg;
/*@owned@*/ char *xrefmsg;
} errwarn_data;
struct yasm_errwarns {
/*@reldef@*/ SLIST_HEAD(errwarn_head, errwarn_data) errwarns;
/* Total error count */
unsigned int ecount;
/* Total warning count */
unsigned int wcount;
/* Last inserted error/warning. Used to speed up insertions. */
/*@null@*/ errwarn_data *previous_we;
};
/* Static buffer for use by conv_unprint(). */
static char unprint[5];
static const char *
def_gettext_hook(const char *msgid)
{
return msgid;
}
void
yasm_errwarn_initialize(void)
{
/* Default enabled warnings. See errwarn.h for a list. */
warn_class_enabled =
(1UL<<YASM_WARN_GENERAL) | (1UL<<YASM_WARN_UNREC_CHAR) |
(1UL<<YASM_WARN_PREPROC) | (0UL<<YASM_WARN_ORPHAN_LABEL) |
(1UL<<YASM_WARN_UNINIT_CONTENTS) | (0UL<<YASM_WARN_SIZE_OVERRIDE) |
(1UL<<YASM_WARN_IMPLICIT_SIZE_OVERRIDE);
yasm_eclass = YASM_ERROR_NONE;
yasm_estr = NULL;
yasm_exrefline = 0;
yasm_exrefstr = NULL;
STAILQ_INIT(&yasm_warns);
}
void
yasm_errwarn_cleanup(void)
{
yasm_error_clear();
yasm_warn_clear();
}
/* Convert a possibly unprintable character into a printable string, using
* standard cat(1) convention for unprintable characters.
*/
char *
yasm__conv_unprint(int ch)
{
int pos = 0;
if (((ch & ~0x7F) != 0) /*!isascii(ch)*/ && !isprint(ch)) {
unprint[pos++] = 'M';
unprint[pos++] = '-';
ch &= toascii(ch);
}
if (iscntrl(ch)) {
unprint[pos++] = '^';
unprint[pos++] = (ch == '\177') ? '?' : ch | 0100;
} else
unprint[pos++] = ch;
unprint[pos] = '\0';
return unprint;
}
/* Report an internal error. Essentially a fatal error with trace info.
* Exit immediately because it's essentially an assert() trap.
*/
static void
def_internal_error_(const char *file, unsigned int line, const char *message)
{
fprintf(stderr,
yasm_gettext_hook(N_("INTERNAL ERROR at %s, line %u: %s\n")),
file, line, yasm_gettext_hook(message));
#ifdef HAVE_ABORT
abort();
#else
exit(EXIT_FAILURE);
#endif
}
/* Report a fatal error. These are unrecoverable (such as running out of
* memory), so just exit immediately.
*/
static void
def_fatal(const char *fmt, va_list va)
{
fprintf(stderr, "%s: ", yasm_gettext_hook(N_("FATAL")));
vfprintf(stderr, yasm_gettext_hook(fmt), va);
fputc('\n', stderr);
exit(EXIT_FAILURE);
}
/* Create an errwarn structure in the correct linked list location.
* If replace_parser_error is nonzero, overwrites the last error if its
* type is WE_PARSERERROR.
*/
static errwarn_data *
errwarn_data_new(yasm_errwarns *errwarns, unsigned long line,
int replace_parser_error)
{
errwarn_data *first, *next, *ins_we, *we;
enum { INS_NONE, INS_HEAD, INS_AFTER } action = INS_NONE;
/* Find the entry with either line=line or the last one with line<line.
* Start with the last entry added to speed the search.
*/
ins_we = errwarns->previous_we;
first = SLIST_FIRST(&errwarns->errwarns);
if (!ins_we || !first)
action = INS_HEAD;
while (action == INS_NONE) {
next = SLIST_NEXT(ins_we, link);
if (line < ins_we->line) {
if (ins_we == first)
action = INS_HEAD;
else
ins_we = first;
} else if (!next)
action = INS_AFTER;
else if (line >= ins_we->line && line < next->line)
action = INS_AFTER;
else
ins_we = next;
}
if (replace_parser_error && ins_we && ins_we->type == WE_PARSERERROR) {
/* overwrite last error */
we = ins_we;
} else {
/* add a new error */
we = yasm_xmalloc(sizeof(errwarn_data));
we->type = WE_UNKNOWN;
we->line = line;
we->xrefline = 0;
we->msg = NULL;
we->xrefmsg = NULL;
if (action == INS_HEAD)
SLIST_INSERT_HEAD(&errwarns->errwarns, we, link);
else if (action == INS_AFTER) {
assert(ins_we != NULL);
SLIST_INSERT_AFTER(ins_we, we, link);
} else
yasm_internal_error(N_("Unexpected errwarn insert action"));
}
/* Remember previous err/warn */
errwarns->previous_we = we;
return we;
}
void
yasm_error_clear(void)
{
if (yasm_estr)
yasm_xfree(yasm_estr);
if (yasm_exrefstr)
yasm_xfree(yasm_exrefstr);
yasm_eclass = YASM_ERROR_NONE;
yasm_estr = NULL;
yasm_exrefline = 0;
yasm_exrefstr = NULL;
}
int
yasm_error_matches(yasm_error_class eclass)
{
if (yasm_eclass == YASM_ERROR_NONE)
return eclass == YASM_ERROR_NONE;
if (yasm_eclass == YASM_ERROR_GENERAL)
return eclass == YASM_ERROR_GENERAL;
return (yasm_eclass & eclass) == eclass;
}
void
yasm_error_set_va(yasm_error_class eclass, const char *format, va_list va)
{
if (yasm_eclass != YASM_ERROR_NONE)
return;
yasm_eclass = eclass;
yasm_estr = yasm_xmalloc(MSG_MAXSIZE+1);
#ifdef HAVE_VSNPRINTF
vsnprintf(yasm_estr, MSG_MAXSIZE, yasm_gettext_hook(format), va);
#else
vsprintf(yasm_estr, yasm_gettext_hook(format), va);
#endif
}
void
yasm_error_set(yasm_error_class eclass, const char *format, ...)
{
va_list va;
va_start(va, format);
yasm_error_set_va(eclass, format, va);
va_end(va);
}
void
yasm_error_set_xref_va(unsigned long xrefline, const char *format, va_list va)
{
if (yasm_eclass != YASM_ERROR_NONE)
return;
yasm_exrefline = xrefline;
yasm_exrefstr = yasm_xmalloc(MSG_MAXSIZE+1);
#ifdef HAVE_VSNPRINTF
vsnprintf(yasm_exrefstr, MSG_MAXSIZE, yasm_gettext_hook(format), va);
#else
vsprintf(yasm_exrefstr, yasm_gettext_hook(format), va);
#endif
}
void
yasm_error_set_xref(unsigned long xrefline, const char *format, ...)
{
va_list va;
va_start(va, format);
yasm_error_set_xref_va(xrefline, format, va);
va_end(va);
}
void
yasm_error_fetch(yasm_error_class *eclass, char **str, unsigned long *xrefline,
char **xrefstr)
{
*eclass = yasm_eclass;
*str = yasm_estr;
*xrefline = yasm_exrefline;
*xrefstr = yasm_exrefstr;
yasm_eclass = YASM_ERROR_NONE;
yasm_estr = NULL;
yasm_exrefline = 0;
yasm_exrefstr = NULL;
}
void yasm_warn_clear(void)
{
/* Delete all error/warnings */
while (!STAILQ_EMPTY(&yasm_warns)) {
warn *w = STAILQ_FIRST(&yasm_warns);
if (w->wstr)
yasm_xfree(w->wstr);
STAILQ_REMOVE_HEAD(&yasm_warns, link);
yasm_xfree(w);
}
}
yasm_warn_class
yasm_warn_occurred(void)
{
if (STAILQ_EMPTY(&yasm_warns))
return YASM_WARN_NONE;
return STAILQ_FIRST(&yasm_warns)->wclass;
}
void
yasm_warn_set_va(yasm_warn_class wclass, const char *format, va_list va)
{
warn *w;
if (!(warn_class_enabled & (1UL<<wclass)))
return; /* warning is part of disabled class */
w = yasm_xmalloc(sizeof(warn));
w->wclass = wclass;
w->wstr = yasm_xmalloc(MSG_MAXSIZE+1);
#ifdef HAVE_VSNPRINTF
vsnprintf(w->wstr, MSG_MAXSIZE, yasm_gettext_hook(format), va);
#else
vsprintf(w->wstr, yasm_gettext_hook(format), va);
#endif
STAILQ_INSERT_TAIL(&yasm_warns, w, link);
}
void
yasm_warn_set(yasm_warn_class wclass, const char *format, ...)
{
va_list va;
va_start(va, format);
yasm_warn_set_va(wclass, format, va);
va_end(va);
}
void
yasm_warn_fetch(yasm_warn_class *wclass, char **str)
{
warn *w = STAILQ_FIRST(&yasm_warns);
if (!w) {
*wclass = YASM_WARN_NONE;
*str = NULL;
return;
}
*wclass = w->wclass;
*str = w->wstr;
STAILQ_REMOVE_HEAD(&yasm_warns, link);
yasm_xfree(w);
}
void
yasm_warn_enable(yasm_warn_class num)
{
warn_class_enabled |= (1UL<<num);
}
void
yasm_warn_disable(yasm_warn_class num)
{
warn_class_enabled &= ~(1UL<<num);
}
void
yasm_warn_disable_all(void)
{
warn_class_enabled = 0;
}
yasm_errwarns *
yasm_errwarns_create(void)
{
yasm_errwarns *errwarns = yasm_xmalloc(sizeof(yasm_errwarns));
SLIST_INIT(&errwarns->errwarns);
errwarns->ecount = 0;
errwarns->wcount = 0;
errwarns->previous_we = NULL;
return errwarns;
}
void
yasm_errwarns_destroy(yasm_errwarns *errwarns)
{
errwarn_data *we;
/* Delete all error/warnings */
while (!SLIST_EMPTY(&errwarns->errwarns)) {
we = SLIST_FIRST(&errwarns->errwarns);
if (we->msg)
yasm_xfree(we->msg);
if (we->xrefmsg)
yasm_xfree(we->xrefmsg);
SLIST_REMOVE_HEAD(&errwarns->errwarns, link);
yasm_xfree(we);
}
yasm_xfree(errwarns);
}
void
yasm_errwarn_propagate(yasm_errwarns *errwarns, unsigned long line)
{
if (yasm_eclass != YASM_ERROR_NONE) {
errwarn_data *we = errwarn_data_new(errwarns, line, 1);
yasm_error_class eclass;
yasm_error_fetch(&eclass, &we->msg, &we->xrefline, &we->xrefmsg);
if (eclass != YASM_ERROR_GENERAL
&& (eclass & YASM_ERROR_PARSE) == YASM_ERROR_PARSE)
we->type = WE_PARSERERROR;
else
we->type = WE_ERROR;
errwarns->ecount++;
}
while (!STAILQ_EMPTY(&yasm_warns)) {
errwarn_data *we = errwarn_data_new(errwarns, line, 0);
yasm_warn_class wclass;
yasm_warn_fetch(&wclass, &we->msg);
we->type = WE_WARNING;
errwarns->wcount++;
}
}
unsigned int
yasm_errwarns_num_errors(yasm_errwarns *errwarns, int warning_as_error)
{
if (warning_as_error)
return errwarns->ecount+errwarns->wcount;
else
return errwarns->ecount;
}
void
yasm_errwarns_output_all(yasm_errwarns *errwarns, yasm_linemap *lm,
int warning_as_error,
yasm_print_error_func print_error,
yasm_print_warning_func print_warning)
{
errwarn_data *we;
const char *filename, *xref_filename;
unsigned long line, xref_line;
/* If we're treating warnings as errors, tell the user about it. */
if (warning_as_error && warning_as_error != 2) {
print_error("", 0,
yasm_gettext_hook(N_("warnings being treated as errors")),
NULL, 0, NULL);
warning_as_error = 2;
}
/* Output error/warnings. */
SLIST_FOREACH(we, &errwarns->errwarns, link) {
/* Output error/warning */
yasm_linemap_lookup(lm, we->line, &filename, &line);
if (we->xrefline)
yasm_linemap_lookup(lm, we->xrefline, &xref_filename, &xref_line);
else {
xref_filename = NULL;
xref_line = 0;
}
if (we->type == WE_ERROR || we->type == WE_PARSERERROR)
print_error(filename, line, we->msg, xref_filename, xref_line,
we->xrefmsg);
else
print_warning(filename, line, we->msg);
}
}
void
yasm__fatal(const char *message, ...)
{
va_list va;
va_start(va, message);
yasm_fatal(message, va);
/*@notreached@*/
va_end(va);
}
void
yasm__fatal_missing_input_file(const char *message, const char *filename)
{
fprintf(stderr, "%s: %s %s (%s)\n", yasm_gettext_hook(N_("FATAL")), yasm_gettext_hook(message), strerror(errno), yasm_gettext_hook(filename));
exit(EXIT_FAILURE);
}
|