Use fprintf and %lc to output a wchar_t.
[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
38 static const source_position_t *curr_pos = NULL;
39
40 /**
41  * prints an additional source position
42  */
43 static void print_source_position(FILE *out, const source_position_t *pos) {
44         fprintf(out, "at line %u", pos->linenr);
45         if (curr_pos == NULL || curr_pos->input_name != pos->input_name)
46                 fprintf(out, " of \"%s\"", pos->input_name);
47 }
48
49 /**
50  * Issue a diagnostic message.
51  */
52 static void diagnosticvf(const char *const fmt, va_list ap)
53 {
54         for (const char* f = fmt; *f != '\0'; ++f) {
55                 if (*f == '%') {
56                         ++f;
57
58                         bool extended = false;
59                         if (*f == '#') {
60                                 extended = true;
61                                 ++f;
62                         }
63
64                         switch (*f) {
65                                 case '%':
66                                         fputc(*f, stderr);
67                                         break;
68
69                                 case 'C': {
70                                         const wint_t val = va_arg(ap, wint_t);
71                                         fprintf(stderr, "%lc", val);
72                                         break;
73                                 }
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 'u': {
94                                         const unsigned int val = va_arg(ap, unsigned int);
95                                         fprintf(stderr, "%u", val);
96                                         break;
97                                 }
98
99                                 case 'Y': {
100                                         const symbol_t *const symbol = va_arg(ap, const symbol_t*);
101                                         if (symbol == NULL)
102                                                 fputs("(null)", stderr);
103                                         else
104                                                 fputs(symbol->string, stderr);
105                                         break;
106                                 }
107
108                                 case 'E': {
109                                         const expression_t* const expr = va_arg(ap, const expression_t*);
110                                         print_expression(expr);
111                                         break;
112                                 }
113
114                                 case 'Q': {
115                                         const unsigned qualifiers = va_arg(ap, unsigned);
116                                         print_type_qualifiers(qualifiers);
117                                         break;
118                                 }
119
120                                 case 'T': {
121                                         const type_t* const type = va_arg(ap, const type_t*);
122                                         const symbol_t*     sym  = NULL;
123                                         if (extended) {
124                                                 sym = va_arg(ap, const symbol_t*);
125                                         }
126                                         print_type_ext(type, sym, NULL);
127                                         break;
128                                 }
129
130                                 case 't': {
131                                         const token_t *const token = va_arg(ap, const token_t*);
132                                         print_pp_token(stderr, token);
133                                         break;
134                                 }
135
136                                 case 'K': {
137                                         const token_t* const token = va_arg(ap, const token_t*);
138                                         print_token(stderr, token);
139                                         break;
140                                 }
141
142                                 case 'k': {
143                                         if (extended) {
144                                                 bool              first     = true;
145                                                 va_list*          toks      = va_arg(ap, va_list*);
146                                                 const char* const delimiter = va_arg(ap, const char*);
147                                                 for (;;) {
148                                                         const token_type_t tok = va_arg(*toks, token_type_t);
149                                                         if (tok == 0)
150                                                                 break;
151                                                         if (first) {
152                                                                 first = false;
153                                                         } else {
154                                                                 fputs(delimiter, stderr);
155                                                         }
156                                                         print_token_type(stderr, tok);
157                                                 }
158                                         } else {
159                                                 const token_type_t token = va_arg(ap, token_type_t);
160                                                 print_token_type(stderr, token);
161                                         }
162                                         break;
163                                 }
164
165                                 case 'P': {
166                                         const source_position_t *pos = va_arg(ap, const source_position_t *);
167                                         print_source_position(stderr, pos);
168                                         break;
169                                 }
170
171                                 default:
172                                         panic("unknown format specifier");
173                         }
174                 } else {
175                         fputc(*f, stderr);
176                 }
177         }
178 }
179
180 void diagnosticf(const char *const fmt, ...)
181 {
182         va_list ap;
183         va_start(ap, fmt);
184         ++diagnostic_count;
185         curr_pos = NULL;
186         diagnosticvf(fmt, ap);
187         va_end(ap);
188 }
189
190 static void errorvf(const source_position_t *pos,
191                     const char *const fmt, va_list ap)
192 {
193         fprintf(stderr, "%s:%u: error: ", pos->input_name, pos->linenr);
194         ++error_count;
195         curr_pos = pos;
196         diagnosticvf(fmt, ap);
197         fputc('\n', stderr);
198
199         if (warning.fatal_errors)
200                 exit(EXIT_FAILURE);
201 }
202
203 void errorf(const source_position_t *pos, const char *const fmt, ...)
204 {
205         va_list ap;
206         va_start(ap, fmt);
207         curr_pos = pos;
208         errorvf(pos, fmt, ap);
209         va_end(ap);
210 }
211
212 static void warningvf(const source_position_t *pos,
213                       const char *const fmt, va_list ap)
214 {
215         fprintf(stderr, "%s:%u: warning: ", pos->input_name, pos->linenr);
216         ++warning_count;
217         curr_pos = pos;
218         diagnosticvf(fmt, ap);
219         fputc('\n', stderr);
220 }
221
222 void warningf(const source_position_t *pos, const char *const fmt, ...)
223 {
224         va_list ap;
225         va_start(ap, fmt);
226         curr_pos = pos;
227         if (warning.s_are_errors) {
228                 errorvf(pos, fmt, ap);
229         } else {
230                 warningvf(pos, fmt, ap);
231         }
232         va_end(ap);
233 }
234
235 static void internal_errorvf(const source_position_t *pos,
236                     const char *const fmt, va_list ap)
237 {
238         fprintf(stderr, "%s:%u: internal error: ", pos->input_name, pos->linenr);
239         curr_pos = pos;
240         diagnosticvf(fmt, ap);
241         fputc('\n', stderr);
242 }
243
244 void internal_errorf(const source_position_t *pos, const char *const fmt, ...)
245 {
246         va_list ap;
247         va_start(ap, fmt);
248         curr_pos = pos;
249         internal_errorvf(pos, fmt, ap);
250         va_end(ap);
251         abort();
252 }