add %u specifier
[cparser] / diagnostic.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 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 "symbol_t.h"
26 #include "token_t.h"
27 #include "ast.h"
28 #include "type.h"
29 #include "warning.h"
30
31 /** Number of occurred diagnostics. */
32 unsigned diagnostic_count = 0;
33 /** Number of occurred errors. */
34 unsigned error_count      = 0;
35 /** Number of occurred warnings. */
36 unsigned warning_count    = 0;
37 /** true if warnings should be inhibited */
38 bool inhibit_all_warnings = false;
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         fprintf(out, "at line %u", pos->linenr);
47         if (curr_pos == NULL || curr_pos->input_name != pos->input_name)
48                 fprintf(out, " of \"%s\"", pos->input_name);
49 }
50
51 /**
52  * Issue a diagnostic message.
53  */
54 static void diagnosticvf(const char *const fmt, va_list ap)
55 {
56         for (const char* f = fmt; *f != '\0'; ++f) {
57                 if (*f == '%') {
58                         ++f;
59
60                         bool extended = false;
61                         if (*f == '#') {
62                                 extended = true;
63                                 ++f;
64                         }
65
66                         switch (*f) {
67                                 case '%':
68                                         fputc(*f, stderr);
69                                         break;
70
71                                 case 'C': {
72                                         const wint_t val = va_arg(ap, wint_t);
73                                         fputwc(val, stderr);
74                                         break;
75                                 }
76
77                                 case 'c': {
78                                         const unsigned char val = (unsigned char) va_arg(ap, int);
79                                         fputc(val, stderr);
80                                         break;
81                                 }
82
83                                 case 'd': {
84                                         const int val = va_arg(ap, int);
85                                         fprintf(stderr, "%d", val);
86                                         break;
87                                 }
88
89                                 case 's': {
90                                         const char* const str = va_arg(ap, const char*);
91                                         fputs(str, stderr);
92                                         break;
93                                 }
94
95                                 case 'u': {
96                                         const unsigned int val = va_arg(ap, unsigned int);
97                                         fprintf(stderr, "%u", val);
98                                         break;
99                                 }
100
101                                 case 'Y': {
102                                         const symbol_t *const symbol = va_arg(ap, const symbol_t*);
103                                         if (symbol == NULL)
104                                                 fputs("(null)", stderr);
105                                         else
106                                                 fputs(symbol->string, stderr);
107                                         break;
108                                 }
109
110                                 case 'E': {
111                                         const expression_t* const expr = va_arg(ap, const expression_t*);
112                                         print_expression(expr);
113                                         break;
114                                 }
115
116                                 case 'Q': {
117                                         const unsigned qualifiers = va_arg(ap, unsigned);
118                                         print_type_qualifiers(qualifiers);
119                                         break;
120                                 }
121
122                                 case 'T': {
123                                         const type_t* const type = va_arg(ap, const type_t*);
124                                         const symbol_t*     sym  = NULL;
125                                         if (extended) {
126                                                 sym = va_arg(ap, const symbol_t*);
127                                         }
128                                         print_type_ext(type, sym, NULL);
129                                         break;
130                                 }
131
132                                 case 'K': {
133                                         const token_t* const token = va_arg(ap, const token_t*);
134                                         print_token(stderr, token);
135                                         break;
136                                 }
137
138                                 case 'k': {
139                                         if (extended) {
140                                                 bool              first     = true;
141                                                 va_list*          toks      = va_arg(ap, va_list*);
142                                                 const char* const delimiter = va_arg(ap, const char*);
143                                                 for (;;) {
144                                                         const token_type_t tok = va_arg(*toks, token_type_t);
145                                                         if (tok == 0)
146                                                                 break;
147                                                         if (first) {
148                                                                 first = false;
149                                                         } else {
150                                                                 fputs(delimiter, stderr);
151                                                         }
152                                                         print_token_type(stderr, tok);
153                                                 }
154                                         } else {
155                                                 const token_type_t token = va_arg(ap, token_type_t);
156                                                 print_token_type(stderr, token);
157                                         }
158                                         break;
159                                 }
160
161                                 case 'P': {
162                                         const source_position_t *pos = va_arg(ap, const source_position_t *);
163                                         print_source_position(stderr, pos);
164                                         break;
165                                 }
166
167                                 default:
168                                         panic("unknown format specifier");
169                         }
170                 } else {
171                         fputc(*f, stderr);
172                 }
173         }
174 }
175
176 void diagnosticf(const char *const fmt, ...)
177 {
178         va_list ap;
179         va_start(ap, fmt);
180         ++diagnostic_count;
181         curr_pos = NULL;
182         diagnosticvf(fmt, ap);
183         va_end(ap);
184 }
185
186 static void errorvf(const source_position_t *pos,
187                     const char *const fmt, va_list ap)
188 {
189         fprintf(stderr, "%s:%u: error: ", pos->input_name, pos->linenr);
190         ++error_count;
191         curr_pos = pos;
192         diagnosticvf(fmt, ap);
193         fputc('\n', stderr);
194
195         if (warning.fatal_errors)
196                 exit(EXIT_FAILURE);
197 }
198
199 void errorf(const source_position_t *pos, const char *const fmt, ...)
200 {
201         va_list ap;
202         va_start(ap, fmt);
203         curr_pos = pos;
204         errorvf(pos, fmt, ap);
205         va_end(ap);
206 }
207
208 static void warningvf(const source_position_t *pos,
209                       const char *const fmt, va_list ap)
210 {
211         fprintf(stderr, "%s:%u: warning: ", pos->input_name, pos->linenr);
212         ++warning_count;
213         curr_pos = pos;
214         diagnosticvf(fmt, ap);
215         fputc('\n', stderr);
216 }
217
218 void warningf(const source_position_t *pos, const char *const fmt, ...)
219 {
220         if (inhibit_all_warnings)
221                 return;
222
223         va_list ap;
224         va_start(ap, fmt);
225         curr_pos = pos;
226         if (warning.s_are_errors) {
227                 errorvf(pos, fmt, ap);
228         } else {
229                 warningvf(pos, fmt, ap);
230         }
231         va_end(ap);
232 }
233
234 static void internal_errorvf(const source_position_t *pos,
235                     const char *const fmt, va_list ap)
236 {
237         fprintf(stderr, "%s:%u: internal error: ", pos->input_name, pos->linenr);
238         curr_pos = pos;
239         diagnosticvf(fmt, ap);
240         fputc('\n', stderr);
241 }
242
243 void internal_errorf(const source_position_t *pos, const char *const fmt, ...)
244 {
245         va_list ap;
246         va_start(ap, fmt);
247         curr_pos = pos;
248         internal_errorvf(pos, fmt, ap);
249         va_end(ap);
250         abort();
251 }