forked from libfirm/cparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagnostic.c
333 lines (300 loc) · 8.04 KB
/
diagnostic.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
/*
* This file is part of cparser.
* Copyright (C) 2012 Matthias Braun <[email protected]>
*/
#include "diagnostic.h"
#include <stdarg.h>
#include <stdio.h>
#include "adt/error.h"
#include "ast.h"
#include "entity_t.h"
#include "separator_t.h"
#include "symbol_t.h"
#include "type.h"
#include "unicode.h"
typedef struct colorscheme_t {
const char *fatal;
const char *error;
const char *warning;
const char *note;
const char *highlight;
const char *reset_highlight;
const char *reset_all;
} colorscheme_t;
static const colorscheme_t no_colors = { "", "", "", "", "", "", "" };
static const colorscheme_t colors_8 = {
.fatal = "\033[31m",
.error = "\033[31m",
.warning = "\033[33m",
.note = "\033[30m",
.highlight = "\033[1m",
.reset_highlight = "\033[22m",
.reset_all = "\033[0m",
};
static const colorscheme_t colors_256 = {
.fatal = "\033[38;5;124m",
.error = "\033[38;5;160m",
.warning = "\033[38;5;139m",
.note = "\033[38;5;008m",
.highlight = "\033[1m",
.reset_highlight = "\033[22m",
.reset_all = "\033[0m",
};
static colorscheme_t colors = { "", "", "", "", "", "", "" };
/** Number of occurred errors. */
unsigned error_count = 0;
/** Number of occurred warnings. */
unsigned warning_count = 0;
bool show_column = true;
bool diagnostics_show_option = true;
static void fpututf32(utf32 const c, FILE *const out)
{
if (c < 0x80U) {
fputc(c, out);
} else if (c < 0x800) {
fputc(0xC0 | (c >> 6), out);
fputc(0x80 | (c & 0x3F), out);
} else if (c < 0x10000) {
fputc(0xE0 | ( c >> 12), out);
fputc(0x80 | ((c >> 6) & 0x3F), out);
fputc(0x80 | ( c & 0x3F), out);
} else {
fputc(0xF0 | ( c >> 18), out);
fputc(0x80 | ((c >> 12) & 0x3F), out);
fputc(0x80 | ((c >> 6) & 0x3F), out);
fputc(0x80 | ( c & 0x3F), out);
}
}
/**
* Issue a diagnostic message.
*/
static void diagnosticvf(position_t const *const pos,
char const *const kind_color, char const *const kind,
char const *fmt, va_list ap)
{
FILE *const out = stderr;
if (pos) {
if (pos->colno != 0 && show_column) {
fprintf(out, "%s%s:%u:%u: ", colors.highlight, pos->input_name, pos->lineno,
pos->colno);
} else if (pos->lineno != 0) {
fprintf(out, "%s%s:%u: ", colors.highlight, pos->input_name, pos->lineno);
} else {
fprintf(out, "%s%s: ", colors.highlight, pos->input_name);
}
}
fprintf(out, "%s%s:%s ", kind_color, kind, colors.reset_all);
for (char const *f; (f = strchr(fmt, '%')); fmt = f) {
fwrite(fmt, sizeof(*fmt), f - fmt, out); // Print till '%'.
++f; // Skip '%'.
bool extended = false;
bool flag_zero = false;
bool flag_long = false;
bool flag_high = false;
for (;; ++f) {
switch (*f) {
case '#': extended = true; break;
case '0': flag_zero = true; break;
case 'l': flag_long = true; break;
case 'h': flag_high = true; break;
default: goto done_flags;
}
}
done_flags:;
int field_width = 0;
if (*f == '*') {
++f;
field_width = va_arg(ap, int);
}
if (flag_high)
fputs(colors.highlight, stderr);
switch (*f++) {
case '%':
fputc('%', out);
break;
case 'c': {
if (flag_long) {
const utf32 val = va_arg(ap, utf32);
fpututf32(val, out);
} else {
const unsigned char val = (unsigned char) va_arg(ap, int);
fputc(val, out);
}
break;
}
case 'd': {
const int val = va_arg(ap, int);
fprintf(out, "%d", val);
break;
}
case 's': {
const char* const str = va_arg(ap, const char*);
fputs(str, out);
break;
}
case 'S': {
const string_t *str = va_arg(ap, const string_t*);
fwrite(str->begin, 1, str->size, out);
break;
}
case 'u': {
const unsigned int val = va_arg(ap, unsigned int);
fprintf(out, "%u", val);
break;
}
case 'X': {
unsigned int const val = va_arg(ap, unsigned int);
char const *const xfmt = flag_zero ? "%0*X" : "%*X";
fprintf(out, xfmt, field_width, val);
break;
}
case 'Y': {
const symbol_t *const symbol = va_arg(ap, const symbol_t*);
fputs(colors.highlight, out);
fputs(symbol ? symbol->string : "(null)", out);
fputs(colors.reset_highlight, stderr);
break;
}
case 'E': {
const expression_t* const expr = va_arg(ap, const expression_t*);
fputs(colors.highlight, stderr);
print_expression(expr);
fputs(colors.reset_highlight, stderr);
break;
}
case 'Q': {
const unsigned qualifiers = va_arg(ap, unsigned);
fputs(colors.highlight, stderr);
print_type_qualifiers(qualifiers, QUAL_SEP_NONE);
fputs(colors.reset_highlight, stderr);
break;
}
case 'T': {
const type_t* const type = va_arg(ap, const type_t*);
fputs(colors.highlight, stderr);
print_type_ext(type, NULL, NULL);
fputs(colors.reset_highlight, stderr);
break;
}
case 'K': {
const token_t* const token = va_arg(ap, const token_t*);
fputs(colors.highlight, stderr);
print_token(out, token);
fputs(colors.reset_highlight, stderr);
break;
}
case 'k': {
fputs(colors.highlight, stderr);
if (extended) {
va_list* const toks = va_arg(ap, va_list*);
separator_t sep = { "", va_arg(ap, const char*) };
for (;;) {
const token_kind_t tok = (token_kind_t)va_arg(*toks, int);
if (tok == 0)
break;
fputs(sep_next(&sep), out);
fputc('\'', out);
print_token_kind(out, tok);
fputc('\'', out);
}
} else {
const token_kind_t token = (token_kind_t)va_arg(ap, int);
fputc('\'', out);
print_token_kind(out, token);
fputc('\'', out);
}
fputs(colors.reset_highlight, stderr);
break;
}
case 'N': {
entity_t const *const ent = va_arg(ap, entity_t const*);
fputs(colors.highlight, stderr);
if (extended && is_declaration(ent)) {
print_type_ext(ent->declaration.type, ent->base.symbol, NULL);
} else {
char const *const ent_kind = get_entity_kind_name(ent->kind);
symbol_t const *const sym = ent->base.symbol;
if (sym) {
fprintf(out, "%s%s %s%s", colors.highlight, ent_kind,
sym->string, colors.reset_highlight);
} else {
fprintf(out, "%sanonymous %s%s", colors.highlight, ent_kind,
colors.reset_highlight);
}
}
fputs(colors.reset_highlight, stderr);
break;
}
default:
panic("unknown format specifier");
}
if (flag_high)
fputs(colors.reset_highlight, stderr);
}
fputs(fmt, out); // Print rest.
}
static void errorvf(const position_t *pos,
const char *const fmt, va_list ap)
{
++error_count;
diagnosticvf(pos, colors.error, "error", fmt, ap);
fputc('\n', stderr);
if (is_warn_on(WARN_FATAL_ERRORS))
exit(EXIT_FAILURE);
}
void errorf(const position_t *pos, const char *const fmt, ...)
{
va_list ap;
va_start(ap, fmt);
errorvf(pos, fmt, ap);
va_end(ap);
}
void notef(position_t const *const pos, char const *const fmt, ...)
{
va_list ap;
va_start(ap, fmt);
diagnosticvf(pos, colors.note, "note", fmt, ap);
fputc('\n', stderr);
va_end(ap);
}
bool warningf(warning_t const warn, position_t const* pos, char const *const fmt, ...)
{
if (pos != NULL && pos->is_system_header && !is_warn_on(WARN_SYSTEM))
return false;
warning_switch_t const *const s = get_warn_switch(warn);
switch ((unsigned) s->state) {
char const* kind;
char const* kind_color;
case WARN_STATE_ON:
if (is_warn_on(WARN_ERROR)) {
case WARN_STATE_ON | WARN_STATE_ERROR:
++error_count;
kind = "error";
kind_color = colors.error;
} else {
case WARN_STATE_ON | WARN_STATE_NO_ERROR:
++warning_count;
kind = "warning";
kind_color = colors.warning;
}
va_list ap;
va_start(ap, fmt);
diagnosticvf(pos, kind_color, kind, fmt, ap);
va_end(ap);
if (diagnostics_show_option)
fprintf(stderr, " [-W%s]\n", s->name);
else
fputc('\n', stderr);
return true;
default:
return false;
}
}
void diagnostic_enable_color(int n_cols)
{
switch (n_cols) {
case 8: colors = colors_8; break;
case 256: colors = colors_256; break;
default: colors = no_colors; break;
}
}