rework preprocessor token number
[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 'K': {
181                         const token_t* const token = va_arg(ap, const token_t*);
182                         print_token(stderr, token);
183                         break;
184                 }
185
186                 case 'k': {
187                         if (extended) {
188                                 bool              first     = true;
189                                 va_list*          toks      = va_arg(ap, va_list*);
190                                 const char* const delimiter = va_arg(ap, const char*);
191                                 for (;;) {
192                                         const token_kind_t tok = (token_kind_t)va_arg(*toks, int);
193                                         if (tok == 0)
194                                                 break;
195                                         if (first) {
196                                                 first = false;
197                                         } else {
198                                                 fputs(delimiter, stderr);
199                                         }
200                                         print_token_kind(stderr, tok);
201                                 }
202                         } else {
203                                 const token_kind_t token = (token_kind_t)va_arg(ap, int);
204                                 print_token_kind(stderr, token);
205                         }
206                         break;
207                 }
208
209                 case 'N': {
210                         entity_t const *const ent = va_arg(ap, entity_t const*);
211                         if (extended && is_declaration(ent)) {
212                                 print_type_ext(ent->declaration.type, ent->base.symbol, NULL);
213                         } else {
214                                 char     const *const kind = get_entity_kind_name(ent->kind);
215                                 symbol_t const *const sym  = ent->base.symbol;
216                                 if (sym) {
217                                         fprintf(stderr, "%s %s", kind, sym->string);
218                                 } else {
219                                         fprintf(stderr, "anonymous %s", kind);
220                                 }
221                         }
222                         break;
223                 }
224
225                 case 'P': {
226                         const source_position_t *pos = va_arg(ap, const source_position_t *);
227                         print_source_position(stderr, pos);
228                         break;
229                 }
230
231                 default:
232                         panic("unknown format specifier");
233                 }
234         }
235         fputs(fmt, stderr); // Print rest.
236 }
237
238 void diagnosticf(const char *const fmt, ...)
239 {
240         va_list ap;
241         va_start(ap, fmt);
242         curr_pos = NULL;
243         diagnosticvf(fmt, ap);
244         va_end(ap);
245 }
246
247 static void diagnosticposvf(source_position_t const *const pos, char const *const kind, char const *const fmt, va_list ap)
248 {
249         FILE *const out = stderr;
250         fprintf(out, "%s:%u:", pos->input_name, pos->lineno);
251         if (show_column)
252                 fprintf(out, "%u:", (unsigned)pos->colno);
253         fprintf(out, " %s: ", kind);
254         curr_pos = pos;
255         diagnosticvf(fmt, ap);
256 }
257
258 static void errorvf(const source_position_t *pos,
259                     const char *const fmt, va_list ap)
260 {
261         ++error_count;
262         diagnosticposvf(pos, "error", fmt, ap);
263         fputc('\n', stderr);
264         if (is_warn_on(WARN_FATAL_ERRORS))
265                 exit(EXIT_FAILURE);
266 }
267
268 void errorf(const source_position_t *pos, const char *const fmt, ...)
269 {
270         va_list ap;
271         va_start(ap, fmt);
272         errorvf(pos, fmt, ap);
273         va_end(ap);
274 }
275
276 void warningf(warning_t const warn, source_position_t const* pos, char const *const fmt, ...)
277 {
278         va_list ap;
279         va_start(ap, fmt);
280         warning_switch_t const *const s = get_warn_switch(warn);
281         switch ((unsigned) s->state) {
282                         char const* kind;
283                 case WARN_STATE_ON:
284                         if (is_warn_on(WARN_ERROR)) {
285                 case WARN_STATE_ON | WARN_STATE_ERROR:
286                                 ++error_count;
287                                 kind = "error";
288                         } else {
289                 case WARN_STATE_ON | WARN_STATE_NO_ERROR:
290                                 ++warning_count;
291                                 kind = "warning";
292                         }
293                         diagnosticposvf(pos, kind, fmt, ap);
294                         if (diagnostics_show_option)
295                                 fprintf(stderr, " [-W%s]\n", s->name);
296                         break;
297
298                 default:
299                         break;
300         }
301         va_end(ap);
302 }
303
304 static void internal_errorvf(const source_position_t *pos,
305                     const char *const fmt, va_list ap)
306 {
307         diagnosticposvf(pos, "internal error", fmt, ap);
308         fputc('\n', stderr);
309 }
310
311 void internal_errorf(const source_position_t *pos, const char *const fmt, ...)
312 {
313         va_list ap;
314         va_start(ap, fmt);
315         internal_errorvf(pos, fmt, ap);
316         va_end(ap);
317         abort();
318 }