Merge TP_WIDE_STRING_LITERAL into TP_STRING_LITERAL.
[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(const char *const fmt, va_list ap)
59 {
60         for (const char* f = fmt; *f != '\0'; ++f) {
61                 if (*f == '%') {
62                         ++f;
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(*f, 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                 } else {
217                         fputc(*f, stderr);
218                 }
219         }
220 }
221
222 void diagnosticf(const char *const fmt, ...)
223 {
224         va_list ap;
225         va_start(ap, fmt);
226         ++diagnostic_count;
227         curr_pos = NULL;
228         diagnosticvf(fmt, ap);
229         va_end(ap);
230 }
231
232 static void diagnosticposvf(source_position_t const *const pos, char const *const kind, char const *const fmt, va_list ap)
233 {
234         FILE *const out = stderr;
235         fprintf(out, "%s:%u:", pos->input_name, pos->lineno);
236         if (show_column)
237                 fprintf(out, "%u:", (unsigned)pos->colno);
238         fprintf(out, " %s: ", kind);
239         curr_pos = pos;
240         diagnosticvf(fmt, ap);
241 }
242
243 static void errorvf(const source_position_t *pos,
244                     const char *const fmt, va_list ap)
245 {
246         curr_pos = pos;
247         ++error_count;
248         diagnosticposvf(pos, "error", fmt, ap);
249         fputc('\n', stderr);
250         if (is_warn_on(WARN_FATAL_ERRORS))
251                 exit(EXIT_FAILURE);
252 }
253
254 void errorf(const source_position_t *pos, const char *const fmt, ...)
255 {
256         va_list ap;
257         va_start(ap, fmt);
258         errorvf(pos, fmt, ap);
259         va_end(ap);
260 }
261
262 void warningf(warning_t const warn, source_position_t const* pos, char const *const fmt, ...)
263 {
264         va_list ap;
265         va_start(ap, fmt);
266         warning_switch_t const *const s = get_warn_switch(warn);
267         switch ((unsigned) s->state) {
268                         char const* kind;
269                 case WARN_STATE_ON:
270                         if (is_warn_on(WARN_ERROR)) {
271                 case WARN_STATE_ON | WARN_STATE_ERROR:
272                                 ++error_count;
273                                 kind = "error";
274                         } else {
275                 case WARN_STATE_ON | WARN_STATE_NO_ERROR:
276                                 ++warning_count;
277                                 kind = "warning";
278                         }
279                         diagnosticposvf(pos, kind, fmt, ap);
280                         if (diagnostics_show_option)
281                                 fprintf(stderr, " [-W%s]\n", s->name);
282                         break;
283
284                 default:
285                         break;
286         }
287         va_end(ap);
288 }
289
290 static void internal_errorvf(const source_position_t *pos,
291                     const char *const fmt, va_list ap)
292 {
293         diagnosticposvf(pos, "internal error", fmt, ap);
294         fputc('\n', stderr);
295 }
296
297 void internal_errorf(const source_position_t *pos, const char *const fmt, ...)
298 {
299         va_list ap;
300         va_start(ap, fmt);
301         curr_pos = pos;
302         internal_errorvf(pos, fmt, ap);
303         va_end(ap);
304         abort();
305 }