typedef token_kind_t as int
[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 /**
54  * Issue a diagnostic message.
55  */
56 static void diagnosticvf(char const *fmt, va_list ap)
57 {
58         for (char const *f; (f = strchr(fmt, '%')); fmt = f) {
59                 fwrite(fmt, sizeof(*fmt), f - fmt, stderr); // Print till '%'.
60                 ++f; // Skip '%'.
61
62                 bool extended  = false;
63                 bool flag_zero = false;
64                 for (;; ++f) {
65                         switch (*f) {
66                         case '#': extended  = true; break;
67                         case '0': flag_zero = true; break;
68                         default:  goto done_flags;
69                         }
70                 }
71 done_flags:;
72
73                 int field_width = 0;
74                 if (*f == '*') {
75                         ++f;
76                         field_width = va_arg(ap, int);
77                 }
78
79                 switch (*f++) {
80                 case '%':
81                         fputc('%', stderr);
82                         break;
83
84                 case 'c': {
85                         const unsigned char val = (unsigned char) va_arg(ap, int);
86                         fputc(val, stderr);
87                         break;
88                 }
89
90                 case 'd': {
91                         const int val = va_arg(ap, int);
92                         fprintf(stderr, "%d", val);
93                         break;
94                 }
95
96                 case 's': {
97                         const char* const str = va_arg(ap, const char*);
98                         fputs(str, stderr);
99                         break;
100                 }
101
102                 case 'S': {
103                         const string_t *str = va_arg(ap, const string_t*);
104                         for (size_t i = 0; i < str->size; ++i) {
105                                 fputc(str->begin[i], stderr);
106                         }
107                         break;
108                 }
109
110                 case 'u': {
111                         const unsigned int val = va_arg(ap, unsigned int);
112                         fprintf(stderr, "%u", val);
113                         break;
114                 }
115
116                 case 'X': {
117                         unsigned int const val = va_arg(ap, unsigned int);
118                         char const  *const fmt = flag_zero ? "%0*X" : "%*X";
119                         fprintf(stderr, fmt, field_width, val);
120                         break;
121                 }
122
123                 case 'Y': {
124                         const symbol_t *const symbol = va_arg(ap, const symbol_t*);
125                         if (symbol == NULL)
126                                 fputs("(null)", stderr);
127                         else
128                                 fputs(symbol->string, stderr);
129                         break;
130                 }
131
132                 case 'E': {
133                         const expression_t* const expr = va_arg(ap, const expression_t*);
134                         print_expression(expr);
135                         break;
136                 }
137
138                 case 'Q': {
139                         const unsigned qualifiers = va_arg(ap, unsigned);
140                         print_type_qualifiers(qualifiers, QUAL_SEP_NONE);
141                         break;
142                 }
143
144                 case 'T': {
145                         const type_t* const type = va_arg(ap, const type_t*);
146                         const symbol_t*     sym  = NULL;
147                         if (extended) {
148                                 sym = va_arg(ap, const symbol_t*);
149                         }
150                         print_type_ext(type, sym, NULL);
151                         break;
152                 }
153
154                 case 't': {
155                         const token_t *const token = va_arg(ap, const token_t*);
156                         print_pp_token(stderr, token);
157                         break;
158                 }
159
160                 case 'K': {
161                         const token_t* const token = va_arg(ap, const token_t*);
162                         print_token(stderr, token);
163                         break;
164                 }
165
166                 case 'k': {
167                         if (extended) {
168                                 bool              first     = true;
169                                 va_list*          toks      = va_arg(ap, va_list*);
170                                 const char* const delimiter = va_arg(ap, const char*);
171                                 for (;;) {
172                                         const token_kind_t tok = (token_kind_t)va_arg(*toks, int);
173                                         if (tok == 0)
174                                                 break;
175                                         if (first) {
176                                                 first = false;
177                                         } else {
178                                                 fputs(delimiter, stderr);
179                                         }
180                                         print_token_kind(stderr, tok);
181                                 }
182                         } else {
183                                 const token_kind_t token = (token_kind_t)va_arg(ap, int);
184                                 print_token_kind(stderr, token);
185                         }
186                         break;
187                 }
188
189                 case 'N': {
190                         entity_t const *const ent = va_arg(ap, entity_t const*);
191                         if (extended && is_declaration(ent)) {
192                                 print_type_ext(ent->declaration.type, ent->base.symbol, NULL);
193                         } else {
194                                 char     const *const kind = get_entity_kind_name(ent->kind);
195                                 symbol_t const *const sym  = ent->base.symbol;
196                                 if (sym) {
197                                         fprintf(stderr, "%s %s", kind, sym->string);
198                                 } else {
199                                         fprintf(stderr, "anonymous %s", kind);
200                                 }
201                         }
202                         break;
203                 }
204
205                 case 'P': {
206                         const source_position_t *pos = va_arg(ap, const source_position_t *);
207                         print_source_position(stderr, pos);
208                         break;
209                 }
210
211                 default:
212                         panic("unknown format specifier");
213                 }
214         }
215         fputs(fmt, stderr); // Print rest.
216 }
217
218 void diagnosticf(const char *const fmt, ...)
219 {
220         va_list ap;
221         va_start(ap, fmt);
222         curr_pos = NULL;
223         diagnosticvf(fmt, ap);
224         va_end(ap);
225 }
226
227 static void diagnosticposvf(source_position_t const *const pos, char const *const kind, char const *const fmt, va_list ap)
228 {
229         FILE *const out = stderr;
230         fprintf(out, "%s:%u:", pos->input_name, pos->lineno);
231         if (show_column)
232                 fprintf(out, "%u:", (unsigned)pos->colno);
233         fprintf(out, " %s: ", kind);
234         curr_pos = pos;
235         diagnosticvf(fmt, ap);
236 }
237
238 static void errorvf(const source_position_t *pos,
239                     const char *const fmt, va_list ap)
240 {
241         ++error_count;
242         diagnosticposvf(pos, "error", fmt, ap);
243         fputc('\n', stderr);
244         if (is_warn_on(WARN_FATAL_ERRORS))
245                 exit(EXIT_FAILURE);
246 }
247
248 void errorf(const source_position_t *pos, const char *const fmt, ...)
249 {
250         va_list ap;
251         va_start(ap, fmt);
252         errorvf(pos, fmt, ap);
253         va_end(ap);
254 }
255
256 void warningf(warning_t const warn, source_position_t const* pos, char const *const fmt, ...)
257 {
258         va_list ap;
259         va_start(ap, fmt);
260         warning_switch_t const *const s = get_warn_switch(warn);
261         switch ((unsigned) s->state) {
262                         char const* kind;
263                 case WARN_STATE_ON:
264                         if (is_warn_on(WARN_ERROR)) {
265                 case WARN_STATE_ON | WARN_STATE_ERROR:
266                                 ++error_count;
267                                 kind = "error";
268                         } else {
269                 case WARN_STATE_ON | WARN_STATE_NO_ERROR:
270                                 ++warning_count;
271                                 kind = "warning";
272                         }
273                         diagnosticposvf(pos, kind, fmt, ap);
274                         if (diagnostics_show_option)
275                                 fprintf(stderr, " [-W%s]\n", s->name);
276                         break;
277
278                 default:
279                         break;
280         }
281         va_end(ap);
282 }
283
284 static void internal_errorvf(const source_position_t *pos,
285                     const char *const fmt, va_list ap)
286 {
287         diagnosticposvf(pos, "internal error", fmt, ap);
288         fputc('\n', stderr);
289 }
290
291 void internal_errorf(const source_position_t *pos, const char *const fmt, ...)
292 {
293         va_list ap;
294         va_start(ap, fmt);
295         internal_errorvf(pos, fmt, ap);
296         va_end(ap);
297         abort();
298 }