33bf313ac1ce74a3254fed5f4cfac18e3dcf7d08
[cparser] / diagnostic.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include <stdarg.h>
21 #include <stdio.h>
22
23 #include "diagnostic.h"
24 #include "adt/error.h"
25 #include "entity_t.h"
26 #include "symbol_t.h"
27 #include "token_t.h"
28 #include "ast.h"
29 #include "type.h"
30 #include "warning.h"
31
32 /** Number of occurred errors. */
33 unsigned error_count             = 0;
34 /** Number of occurred warnings. */
35 unsigned warning_count           = 0;
36 bool     show_column             = true;
37 bool     diagnostics_show_option = true;
38
39 static const source_position_t *curr_pos = NULL;
40
41 /**
42  * prints an additional source position
43  */
44 static void print_source_position(FILE *out, const source_position_t *pos)
45 {
46         fprintf(out, "at line %u", pos->lineno);
47         if (show_column)
48                 fprintf(out, ":%u", (unsigned)pos->colno);
49         if (curr_pos == NULL || curr_pos->input_name != pos->input_name)
50                 fprintf(out, " of \"%s\"", pos->input_name);
51 }
52
53 static void fpututf32(utf32 const c, FILE *const out)
54 {
55         if (c < 0x80U) {
56                 fputc(c, out);
57         } else if (c < 0x800) {
58                 fputc(0xC0 | (c >> 6), out);
59                 fputc(0x80 | (c & 0x3F), out);
60         } else if (c < 0x10000) {
61                 fputc(0xE0 | ( c >> 12), out);
62                 fputc(0x80 | ((c >>  6) & 0x3F), out);
63                 fputc(0x80 | ( c        & 0x3F), out);
64         } else {
65                 fputc(0xF0 | ( c >> 18), out);
66                 fputc(0x80 | ((c >> 12) & 0x3F), out);
67                 fputc(0x80 | ((c >>  6) & 0x3F), out);
68                 fputc(0x80 | ( c        & 0x3F), out);
69         }
70 }
71
72 /**
73  * Issue a diagnostic message.
74  */
75 static void diagnosticvf(char const *fmt, va_list ap)
76 {
77         for (char const *f; (f = strchr(fmt, '%')); fmt = f) {
78                 fwrite(fmt, sizeof(*fmt), f - fmt, stderr); // Print till '%'.
79                 ++f; // Skip '%'.
80
81                 bool extended  = false;
82                 bool flag_zero = false;
83                 bool flag_long = false;
84                 for (;; ++f) {
85                         switch (*f) {
86                         case '#': extended  = true; break;
87                         case '0': flag_zero = true; break;
88                         case 'l': flag_long = true; break;
89                         default:  goto done_flags;
90                         }
91                 }
92 done_flags:;
93
94                 int field_width = 0;
95                 if (*f == '*') {
96                         ++f;
97                         field_width = va_arg(ap, int);
98                 }
99
100                 switch (*f++) {
101                 case '%':
102                         fputc('%', stderr);
103                         break;
104
105                 case 'c': {
106                         if (flag_long) {
107                                 const utf32 val = va_arg(ap, utf32);
108                                 fpututf32(val, stderr);
109                         } else {
110                                 const unsigned char val = (unsigned char) va_arg(ap, int);
111                                 fputc(val, stderr);
112                         }
113                         break;
114                 }
115
116                 case 'd': {
117                         const int val = va_arg(ap, int);
118                         fprintf(stderr, "%d", val);
119                         break;
120                 }
121
122                 case 's': {
123                         const char* const str = va_arg(ap, const char*);
124                         fputs(str, stderr);
125                         break;
126                 }
127
128                 case 'S': {
129                         const string_t *str = va_arg(ap, const string_t*);
130                         for (size_t i = 0; i < str->size; ++i) {
131                                 fputc(str->begin[i], stderr);
132                         }
133                         break;
134                 }
135
136                 case 'u': {
137                         const unsigned int val = va_arg(ap, unsigned int);
138                         fprintf(stderr, "%u", val);
139                         break;
140                 }
141
142                 case 'X': {
143                         unsigned int const val = va_arg(ap, unsigned int);
144                         char const  *const fmt = flag_zero ? "%0*X" : "%*X";
145                         fprintf(stderr, fmt, field_width, val);
146                         break;
147                 }
148
149                 case 'Y': {
150                         const symbol_t *const symbol = va_arg(ap, const symbol_t*);
151                         if (symbol == NULL)
152                                 fputs("(null)", stderr);
153                         else
154                                 fputs(symbol->string, stderr);
155                         break;
156                 }
157
158                 case 'E': {
159                         const expression_t* const expr = va_arg(ap, const expression_t*);
160                         print_expression(expr);
161                         break;
162                 }
163
164                 case 'Q': {
165                         const unsigned qualifiers = va_arg(ap, unsigned);
166                         print_type_qualifiers(qualifiers, QUAL_SEP_NONE);
167                         break;
168                 }
169
170                 case 'T': {
171                         const type_t* const type = va_arg(ap, const type_t*);
172                         const symbol_t*     sym  = NULL;
173                         if (extended) {
174                                 sym = va_arg(ap, const symbol_t*);
175                         }
176                         print_type_ext(type, sym, NULL);
177                         break;
178                 }
179
180                 case 't': {
181                         const token_t *const token = va_arg(ap, const token_t*);
182                         print_pp_token(stderr, token);
183                         break;
184                 }
185
186                 case 'K': {
187                         const token_t* const token = va_arg(ap, const token_t*);
188                         print_token(stderr, token);
189                         break;
190                 }
191
192                 case 'k': {
193                         if (extended) {
194                                 bool              first     = true;
195                                 va_list*          toks      = va_arg(ap, va_list*);
196                                 const char* const delimiter = va_arg(ap, const char*);
197                                 for (;;) {
198                                         const token_kind_t tok = (token_kind_t)va_arg(*toks, int);
199                                         if (tok == 0)
200                                                 break;
201                                         if (first) {
202                                                 first = false;
203                                         } else {
204                                                 fputs(delimiter, stderr);
205                                         }
206                                         print_token_kind(stderr, tok);
207                                 }
208                         } else {
209                                 const token_kind_t token = (token_kind_t)va_arg(ap, int);
210                                 print_token_kind(stderr, token);
211                         }
212                         break;
213                 }
214
215                 case 'N': {
216                         entity_t const *const ent = va_arg(ap, entity_t const*);
217                         if (extended && is_declaration(ent)) {
218                                 print_type_ext(ent->declaration.type, ent->base.symbol, NULL);
219                         } else {
220                                 char     const *const kind = get_entity_kind_name(ent->kind);
221                                 symbol_t const *const sym  = ent->base.symbol;
222                                 if (sym) {
223                                         fprintf(stderr, "%s %s", kind, sym->string);
224                                 } else {
225                                         fprintf(stderr, "anonymous %s", kind);
226                                 }
227                         }
228                         break;
229                 }
230
231                 case 'P': {
232                         const source_position_t *pos = va_arg(ap, const source_position_t *);
233                         print_source_position(stderr, pos);
234                         break;
235                 }
236
237                 default:
238                         panic("unknown format specifier");
239                 }
240         }
241         fputs(fmt, stderr); // Print rest.
242 }
243
244 void diagnosticf(const char *const fmt, ...)
245 {
246         va_list ap;
247         va_start(ap, fmt);
248         curr_pos = NULL;
249         diagnosticvf(fmt, ap);
250         va_end(ap);
251 }
252
253 static void diagnosticposvf(source_position_t const *const pos, char const *const kind, char const *const fmt, va_list ap)
254 {
255         FILE *const out = stderr;
256         fprintf(out, "%s:%u:", pos->input_name, pos->lineno);
257         if (show_column)
258                 fprintf(out, "%u:", (unsigned)pos->colno);
259         fprintf(out, " %s: ", kind);
260         curr_pos = pos;
261         diagnosticvf(fmt, ap);
262 }
263
264 static void errorvf(const source_position_t *pos,
265                     const char *const fmt, va_list ap)
266 {
267         ++error_count;
268         diagnosticposvf(pos, "error", fmt, ap);
269         fputc('\n', stderr);
270         if (is_warn_on(WARN_FATAL_ERRORS))
271                 exit(EXIT_FAILURE);
272 }
273
274 void errorf(const source_position_t *pos, const char *const fmt, ...)
275 {
276         va_list ap;
277         va_start(ap, fmt);
278         errorvf(pos, fmt, ap);
279         va_end(ap);
280 }
281
282 void warningf(warning_t const warn, source_position_t const* pos, char const *const fmt, ...)
283 {
284         va_list ap;
285         va_start(ap, fmt);
286         warning_switch_t const *const s = get_warn_switch(warn);
287         switch ((unsigned) s->state) {
288                         char const* kind;
289                 case WARN_STATE_ON:
290                         if (is_warn_on(WARN_ERROR)) {
291                 case WARN_STATE_ON | WARN_STATE_ERROR:
292                                 ++error_count;
293                                 kind = "error";
294                         } else {
295                 case WARN_STATE_ON | WARN_STATE_NO_ERROR:
296                                 ++warning_count;
297                                 kind = "warning";
298                         }
299                         diagnosticposvf(pos, kind, fmt, ap);
300                         if (diagnostics_show_option)
301                                 fprintf(stderr, " [-W%s]\n", s->name);
302                         break;
303
304                 default:
305                         break;
306         }
307         va_end(ap);
308 }
309
310 static void internal_errorvf(const source_position_t *pos,
311                     const char *const fmt, va_list ap)
312 {
313         diagnosticposvf(pos, "internal error", fmt, ap);
314         fputc('\n', stderr);
315 }
316
317 void internal_errorf(const source_position_t *pos, const char *const fmt, ...)
318 {
319         va_list ap;
320         va_start(ap, fmt);
321         internal_errorvf(pos, fmt, ap);
322         va_end(ap);
323         abort();
324 }