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