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