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